modelcontextprotocol / modelcontextprotocol/csharp-sdk

Cannot Configure JSON Serialization for MCPServer

Open
#636 13 comments 5 reactions 2 assignees View on GitHub

@eiriktsarpalis is already working on this.

Since Aug 1, 2025.

enhancement needs confirmation
Dominant language
C#
Stars
4.5k
Forks
814
Avg merge
9d 19h
Merged PRs (30d)
4

Description

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 };
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.