Skip to content

Cannot Configure JSON Serialization for MCPServer #636

@sylvain-guillet

Description

@sylvain-guillet

Describe the bug
It seems is not possible to configure JsonSerialisation used by MCPServer

To Reproduce
Steps to reproduce the behavior:

  1. Try to serialize a response from tool containing double.infinity or Nan
  2. 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 };
});

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions