Skip to content

Commit 90476da

Browse files
tarekghTarek Mahmoud Sayed
andauthored
Fix serialization of annotated content blocks in tool results (#2039)
Co-authored-by: Tarek Mahmoud Sayed <tarekms@ntdev.microsoft.com>
1 parent 64cfd66 commit 90476da

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎src/ModelContextProtocol.Core/Protocol/ContentBlock.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ public sealed class Converter : JsonConverter<ContentBlock>
8989

9090
private static JsonTypeInfo<T> GetNestedTypeInfo<T>(JsonSerializerOptions options)
9191
{
92+
// Options bound to the SDK context already resolve the nested types, and System.Text.Json
93+
// doesn't allow chaining a resolver into options that are already bound to it.
94+
if (ReferenceEquals(options.TypeInfoResolver, McpJsonUtilities.JsonContext.Default))
95+
{
96+
return options.GetTypeInfo<T>();
97+
}
98+
9299
// JsonSerializer freezes options, but direct converter callers may still change them.
93100
JsonSerializerOptions nestedOptions = options.IsReadOnly
94101
? s_nestedOptions.GetValue(options, CreateNestedOptions)

‎tests/ModelContextProtocol.Tests/Server/McpServerTests.cs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,61 @@ await transport.SendMessageAsync(
14151415
await runTask;
14161416
}
14171417

1418+
[Fact]
1419+
public async Task Call_Tool_Result_With_Annotated_Content_Serializes()
1420+
{
1421+
await using var transport = new TestServerTransport();
1422+
var options = CreateOptions(new ServerCapabilities { Tools = new() });
1423+
options.Handlers.CallToolHandler = (request, ct) => new ValueTask<CallToolResult>(new CallToolResult
1424+
{
1425+
Content =
1426+
[
1427+
new TextContentBlock { Text = "x", Annotations = new Annotations { Audience = [Role.User], Priority = 0.5f } },
1428+
new ResourceLinkBlock
1429+
{
1430+
Uri = "https://example.com/x",
1431+
Name = "x",
1432+
Icons = [new Icon { Source = "https://example.com/icon.png" }],
1433+
Annotations = new Annotations { Audience = [Role.Assistant] },
1434+
},
1435+
],
1436+
});
1437+
options.Handlers.ListToolsHandler = (request, ct) => throw new NotImplementedException();
1438+
1439+
await using var server = McpServer.Create(transport, options, LoggerFactory);
1440+
var runTask = server.RunAsync(TestContext.Current.CancellationToken);
1441+
1442+
var reply = new TaskCompletionSource<JsonRpcMessageWithId>(TaskCreationOptions.RunContinuationsAsynchronously);
1443+
transport.OnMessageSent = message =>
1444+
{
1445+
if (message is JsonRpcResponse or JsonRpcError)
1446+
{
1447+
reply.TrySetResult((JsonRpcMessageWithId)message);
1448+
}
1449+
};
1450+
1451+
await transport.SendMessageAsync(
1452+
new JsonRpcRequest
1453+
{
1454+
Method = RequestMethods.ToolsCall,
1455+
Id = new RequestId(1),
1456+
Params = JsonNode.Parse("""{"name":"annotated"}"""),
1457+
},
1458+
TestContext.Current.CancellationToken);
1459+
1460+
var response = Assert.IsType<JsonRpcResponse>(await reply.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken));
1461+
var content = response.Result?["content"]?.AsArray();
1462+
Assert.NotNull(content);
1463+
Assert.Equal(2, content.Count);
1464+
Assert.Equal("user", content[0]?["annotations"]?["audience"]?[0]?.GetValue<string>());
1465+
Assert.Equal(0.5, content[0]?["annotations"]?["priority"]?.GetValue<double>());
1466+
Assert.Equal("assistant", content[1]?["annotations"]?["audience"]?[0]?.GetValue<string>());
1467+
Assert.Equal("https://example.com/icon.png", content[1]?["icons"]?[0]?["src"]?.GetValue<string>());
1468+
1469+
await transport.DisposeAsync();
1470+
await runTask;
1471+
}
1472+
14181473
private async Task Can_Handle_Requests(ServerCapabilities? serverCapabilities, string method, Action<McpServerOptions>? configureOptions, Action<McpServer, JsonNode?> assertResult)
14191474
{
14201475
await using var transport = new TestServerTransport();

0 commit comments

Comments
 (0)