-
Notifications
You must be signed in to change notification settings - Fork 557
Description
Describe the bug
It seems is not possible to configure JsonSerialisation used by MCPServer
To Reproduce
Steps to reproduce the behavior:
- Try to serialize a response from tool containing double.infinity or Nan
- The console output will display this error : Server (MCP.Server 1.0.0.0), Client (Visual Studio Code 1.102.1) method 'tools/call' request handler failed.
System.ArgumentException: .NET number values such as positive and negative infinity cannot be written as valid JSON. To make it work when using 'JsonSerializer', consider specifying 'JsonNumberHandling.AllowNamedFloatingPointLiterals' (see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).
Expected behavior
I can configure json serialisation for the MCP server with something like that : builder.Services.AddMcpServer(opt=>opt.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals)
Logs
Server (MCP.Server 1.0.0.0), Client (Visual Studio Code 1.102.1) method 'tools/call' request handler failed.
System.ArgumentException: .NET number values such as positive and negative infinity cannot be written as valid JSON. To make it work when using 'JsonSerializer', consider specifying 'JsonNumberHandling.AllowNamedFloatingPointLiterals' (see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).
Additional context
I tried these :
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals;
});
builder.Services.Configure(options =>
{
options.SerializerOptions.NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals;
});
// Configure System.Text.Json defaults
builder.Services.PostConfigure(options =>
{
options.NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals;
});
builder.Services.AddSingleton(sp =>
{
Console.Error.WriteLine("✅ JsonOptions created with NumberHandling=" +
JsonNumberHandling.AllowNamedFloatingPointLiterals);
return new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals };
});