From 61cb96a349f1672d74e7327d36684b1d7315a1b7 Mon Sep 17 00:00:00 2001 From: GeorgiPopovIT Date: Thu, 29 Jan 2026 12:57:49 +0200 Subject: [PATCH] Fix: handle JsonSerializerOptions without TypeInfoResolver Added a helper method which ensures that custom options are properly configured with required TypeInfoResolvers. Fixes #1150 --- .../Server/AIFunctionMcpServerTool.cs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs index f72e5a483..04ef11cb8 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs @@ -76,7 +76,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions( Name = options?.Name ?? method.GetCustomAttribute()?.Name ?? DeriveName(method), Description = options?.Description, MarshalResult = static (result, _, cancellationToken) => new ValueTask(result), - SerializerOptions = options?.SerializerOptions ?? McpJsonUtilities.DefaultOptions, + SerializerOptions = GetSerializerOptions(options?.SerializerOptions), JsonSchemaCreateOptions = options?.SchemaCreateOptions, ConfigureParameterBinding = pi => { @@ -580,4 +580,22 @@ private static CallToolResult ConvertAIContentEnumerableToCallToolResult(IEnumer IsError = allErrorContent && hasAny }; } + + private static JsonSerializerOptions GetSerializerOptions(JsonSerializerOptions? customOptions) + { + if (customOptions is null) + { + return McpJsonUtilities.DefaultOptions; + } + + if (customOptions.TypeInfoResolver is not null) + { + return customOptions; + } + + customOptions.TypeInfoResolverChain.Add(McpJsonUtilities.JsonContext.Default); + customOptions.TypeInfoResolverChain.Add(AIJsonUtilities.DefaultOptions.TypeInfoResolver!); + + return customOptions; + } } \ No newline at end of file