|
| 1 | +# JSON Serialization Configuration for MCP Server |
| 2 | + |
| 3 | +This document demonstrates how to configure JSON serialization options for your MCP server to handle special cases like `double.Infinity` or `NaN` values. |
| 4 | + |
| 5 | +## Problem |
| 6 | + |
| 7 | +By default, JSON serialization in .NET doesn't support special floating-point values like positive/negative infinity and NaN. When a tool returns such values, you would get an error: |
| 8 | + |
| 9 | +``` |
| 10 | +System.ArgumentException: .NET number values such as positive and negative infinity cannot be written as valid JSON. |
| 11 | +To make it work when using 'JsonSerializer', consider specifying 'JsonNumberHandling.AllowNamedFloatingPointLiterals' |
| 12 | +``` |
| 13 | + |
| 14 | +## Solution |
| 15 | + |
| 16 | +Configure server-wide JSON serialization options when setting up your MCP server: |
| 17 | + |
| 18 | +```csharp |
| 19 | +using Microsoft.Extensions.DependencyInjection; |
| 20 | +using ModelContextProtocol.Server; |
| 21 | +using System.Text.Json; |
| 22 | +using System.Text.Json.Serialization; |
| 23 | + |
| 24 | +var builder = WebApplication.CreateBuilder(args); |
| 25 | + |
| 26 | +// Configure server-wide JSON serialization options |
| 27 | +builder.Services.AddMcpServer(options => |
| 28 | +{ |
| 29 | + options.JsonSerializerOptions = new JsonSerializerOptions(McpJsonUtilities.DefaultOptions) |
| 30 | + { |
| 31 | + NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals |
| 32 | + }; |
| 33 | +}) |
| 34 | +.WithTools<MyTools>(); |
| 35 | + |
| 36 | +var app = builder.Build(); |
| 37 | +app.Run(); |
| 38 | + |
| 39 | +[McpServerToolType] |
| 40 | +public class MyTools |
| 41 | +{ |
| 42 | + [McpServerTool] |
| 43 | + public static double[] GetSpecialNumbers() |
| 44 | + { |
| 45 | + // These values will now serialize correctly as "Infinity", "-Infinity", and "NaN" |
| 46 | + return new[] { double.PositiveInfinity, double.NegativeInfinity, double.NaN }; |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## How It Works |
| 52 | + |
| 53 | +1. The `JsonSerializerOptions` property on `McpServerOptions` provides server-wide default serialization settings |
| 54 | +2. All tools, prompts, and resources registered via `WithTools*`, `WithPrompts*`, and `WithResources*` will use these options by default |
| 55 | +3. Individual registrations can still override with their own specific options if needed |
| 56 | + |
| 57 | +## Override for Specific Tools |
| 58 | + |
| 59 | +If you need different serialization options for specific tools, you can still provide them explicitly: |
| 60 | + |
| 61 | +```csharp |
| 62 | +var customOptions = new JsonSerializerOptions |
| 63 | +{ |
| 64 | + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower |
| 65 | +}; |
| 66 | + |
| 67 | +builder.Services.AddMcpServer(options => |
| 68 | +{ |
| 69 | + options.JsonSerializerOptions = McpJsonUtilities.DefaultOptions; // Default for most tools |
| 70 | +}) |
| 71 | +.WithTools<MyTools>() // Uses server-wide options |
| 72 | +.WithTools<SpecialTools>(customOptions); // Uses custom options |
| 73 | +``` |
| 74 | + |
| 75 | +## Additional Configuration Options |
| 76 | + |
| 77 | +You can configure other JSON serialization settings as needed: |
| 78 | + |
| 79 | +```csharp |
| 80 | +options.JsonSerializerOptions = new JsonSerializerOptions(McpJsonUtilities.DefaultOptions) |
| 81 | +{ |
| 82 | + NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals, |
| 83 | + PropertyNameCaseInsensitive = true, |
| 84 | + AllowTrailingCommas = true, |
| 85 | + WriteIndented = true // For debugging |
| 86 | +}; |
| 87 | +``` |
0 commit comments