From fc571e2763f7a2b8e8a7884206e3e3965c5b80a0 Mon Sep 17 00:00:00 2001 From: PederHP Date: Mon, 1 Sep 2025 17:36:04 +0200 Subject: [PATCH 1/2] Fix structured output backwards compatibility for string literals --- .../Server/AIFunctionMcpServerTool.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs index 664ede5a..0d6cae3f 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs @@ -257,10 +257,13 @@ public override async ValueTask InvokeAsync( Content = [], StructuredContent = structuredContent, }, - + string text => new() { - Content = [new TextContentBlock { Text = text }], + // If there is structuredOutput we must stringify it, as the MCP specification requires Content to be for backwards compatibility + // but otherwise not differ + Content = [new TextContentBlock { Text = structuredContent == null ? text : + JsonSerializer.Serialize(result, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object)))}], StructuredContent = structuredContent, }, From b9468e2ddd9e3a685ca258e76e562a6544aa70f8 Mon Sep 17 00:00:00 2001 From: PederHP Date: Mon, 1 Sep 2025 21:18:30 +0200 Subject: [PATCH 2/2] Serialize structured content not result --- src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs index 0d6cae3f..cb88e637 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs @@ -263,7 +263,7 @@ public override async ValueTask InvokeAsync( // If there is structuredOutput we must stringify it, as the MCP specification requires Content to be for backwards compatibility // but otherwise not differ Content = [new TextContentBlock { Text = structuredContent == null ? text : - JsonSerializer.Serialize(result, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object)))}], + JsonSerializer.Serialize(structuredContent, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object)))}], StructuredContent = structuredContent, },