Skip to content

Fix CreateMessageRule.Role to be of type Role #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Client/McpClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ internal static CreateMessageResult ToCreateMessageResult(this ChatResponse chat
{
Content = content,
Model = chatResponse.ModelId ?? "unknown",
Role = lastMessage?.Role == ChatRole.User ? "user" : "assistant",
Role = lastMessage?.Role == ChatRole.User ? Role.User : Role.Assistant,
StopReason = chatResponse.FinishReason == ChatFinishReason.Length ? "maxTokens" : "endTurn",
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ public class CreateMessageResult
/// Gets or sets the role of the user who generated the message.
/// </summary>
[JsonPropertyName("role")]
public required string Role { get; init; }
public required Role Role { get; init; }
}
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Server/McpServerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static async Task<ChatResponse> RequestSamplingAsync(
ModelPreferences = modelPreferences,
}, cancellationToken).ConfigureAwait(false);

return new(new ChatMessage(new(result.Role), [result.Content.ToAIContent()]))
return new(new ChatMessage(result.Role is Role.User ? ChatRole.User : ChatRole.Assistant, [result.Content.ToAIContent()]))
{
ModelId = result.Model,
FinishReason = result.StopReason switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public async Task Sampling_Sse_TestServer()
return new CreateMessageResult
{
Model = "test-model",
Role = "assistant",
Role = Role.Assistant,
Content = new Content
{
Type = "text",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private async Task Sampling(JsonRpcRequest request, CancellationToken cancellati
await WriteMessageAsync(new JsonRpcResponse
{
Id = request.Id,
Result = JsonSerializer.SerializeToNode(new CreateMessageResult { Content = new(), Model = "model", Role = "role" }),
Result = JsonSerializer.SerializeToNode(new CreateMessageResult { Content = new(), Model = "model", Role = Role.Assistant }),
}, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public async Task CreateSamplingHandler_ShouldHandleTextMessages(float? temperat
Assert.NotNull(result);
Assert.Equal("Hello, World!", result.Content.Text);
Assert.Equal("test-model", result.Model);
Assert.Equal("assistant", result.Role);
Assert.Equal(Role.Assistant, result.Role);
Assert.Equal("endTurn", result.StopReason);
}

Expand Down Expand Up @@ -139,7 +139,7 @@ public async Task CreateSamplingHandler_ShouldHandleImageMessages()
Assert.NotNull(result);
Assert.Equal(expectedData, result.Content.Data);
Assert.Equal("test-model", result.Model);
Assert.Equal("assistant", result.Role);
Assert.Equal(Role.Assistant, result.Role);
Assert.Equal("endTurn", result.StopReason);
}

Expand Down Expand Up @@ -201,7 +201,7 @@ public async Task CreateSamplingHandler_ShouldHandleResourceMessages()
// Assert
Assert.NotNull(result);
Assert.Equal("test-model", result.Model);
Assert.Equal(ChatRole.Assistant.ToString(), result.Role);
Assert.Equal(Role.Assistant, result.Role);
Assert.Equal("endTurn", result.StopReason);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task CreateAsync_WithCapabilitiesOptions(Type transportType)
new CreateMessageResult {
Content = new Content { Text = "result" },
Model = "test-model",
Role = "test-role",
Role = Role.User,
StopReason = "endTurn"
}),
},
Expand Down
2 changes: 1 addition & 1 deletion tests/ModelContextProtocol.Tests/ClientIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public async Task Sampling_Stdio(string clientId)
return Task.FromResult(new CreateMessageResult
{
Model = "test-model",
Role = "assistant",
Role = Role.Assistant,
Content = new Content
{
Type = "text",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task Sampling_Sse_EverythingServer()
return Task.FromResult(new CreateMessageResult
{
Model = "test-model",
Role = "assistant",
Role = Role.Assistant,
Content = new Content
{
Type = "text",
Expand Down
2 changes: 1 addition & 1 deletion tests/ModelContextProtocol.Tests/Server/McpServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, Cancellati
{
Content = new() { Text = "The Eiffel Tower.", Type = "text" },
Model = "amazingmodel",
Role = "assistant",
Role = Role.Assistant,
StopReason = "endTurn",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private async Task Sampling(JsonRpcRequest request, CancellationToken cancellati
await WriteMessageAsync(new JsonRpcResponse
{
Id = request.Id,
Result = JsonSerializer.SerializeToNode(new CreateMessageResult { Content = new(), Model = "model", Role = "role" }),
Result = JsonSerializer.SerializeToNode(new CreateMessageResult { Content = new(), Model = "model", Role = Role.User }),
}, cancellationToken);
}

Expand Down