Fix ToJsonObject serialization failure with anonymous types and add support for custom JsonSerializerOptions (#1113)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
This commit is contained in:
Copilot 2026-01-20 11:21:11 +02:00 • committed by GitHub
commit 46d30444a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -165,6 +165,14 @@ public class McpClientToolTests : ClientServerTestBase
var metaJson = meta?.ToJsonString() ?? "{}";
return new TextContentBlock { Text = metaJson };
}
// Tool that accepts arbitrary JsonElement parameter to test anonymous type serialization
[McpServerTool]
public static TextContentBlock ArgumentEchoTool(string text, JsonElement coordinates)
{
var result = new { text, coordinates };
return new TextContentBlock { Text = JsonSerializer.Serialize(result) };
}
}
[Fact]
@ -818,4 +826,32 @@ public class McpClientToolTests : ClientServerTestBase
Assert.NotNull(receivedMetadata);
Assert.Equal("requestOnlyValue", receivedMetadata["requestOnlyKey"]?.GetValue<string>());
}
[Fact]
public async Task CallToolAsync_WithAnonymousTypeArguments_Works()
{
if (!JsonSerializer.IsReflectionEnabledByDefault)
{
return;
}
await using McpClient client = await CreateMcpClientForServer();
// Call with dictionary containing anonymous type values
var arguments = new Dictionary<string, object?>
{
["text"] = "test",
["coordinates"] = new { X = 1.0, Y = 2.0 } // Anonymous type
};
// This should not throw NotSupportedException
var result = await client.CallToolAsync("argument_echo_tool", arguments, cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(result);
Assert.NotEmpty(result.Content);
// Verify the anonymous type was serialized correctly
var textBlock = Assert.IsType<TextContentBlock>(result.Content[0]);
Assert.Contains("coordinates", textBlock.Text);
}
}