From fd8df5ceb4e4b7f3b7988b3c8206979fb7cb6c5b Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 12 Aug 2025 10:19:54 -0400 Subject: [PATCH] Remove special-casing of string enumerables in McpServerTool We special-case string enumerables, translating them to an array of text content blocks, but other enumerables just get serialized, and there's a reasonable expectation that returning a string[] would produce a JSON array of strings. Just delete the special-casing. --- .../Server/AIFunctionMcpServerTool.cs | 6 ------ .../Configuration/McpServerBuilderExtensionsToolsTests.cs | 3 +-- .../ModelContextProtocol.Tests/Server/McpServerToolTests.cs | 5 ++--- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs index afd3912b6..664ede5ab 100644 --- a/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs @@ -270,12 +270,6 @@ public override async ValueTask InvokeAsync( StructuredContent = structuredContent, }, - IEnumerable texts => new() - { - Content = [.. texts.Select(x => new TextContentBlock { Text = x ?? string.Empty })], - StructuredContent = structuredContent, - }, - IEnumerable contentItems => ConvertAIContentEnumerableToCallToolResult(contentItems, structuredContent), IEnumerable contents => new() diff --git a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs index d2080e1fc..35f833d50 100644 --- a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs +++ b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs @@ -254,8 +254,7 @@ public async Task Can_Call_Registered_Tool_With_Array_Result() Assert.NotNull(result.Content); Assert.NotEmpty(result.Content); - Assert.Equal("hello Peter", (result.Content[0] as TextContentBlock)?.Text); - Assert.Equal("hello2 Peter", (result.Content[1] as TextContentBlock)?.Text); + Assert.Equal("""["hello Peter","hello2 Peter"]""", (result.Content[0] as TextContentBlock)?.Text); result = await client.CallToolAsync( "SecondCustomTool", diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs index bd0ca5ef9..f961eef34 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs @@ -356,9 +356,8 @@ public async Task CanReturnCollectionOfStrings() var result = await tool.InvokeAsync( new RequestContext(mockServer.Object), TestContext.Current.CancellationToken); - Assert.Equal(2, result.Content.Count); - Assert.Equal("42", Assert.IsType(result.Content[0]).Text); - Assert.Equal("43", Assert.IsType(result.Content[1]).Text); + Assert.Single(result.Content); + Assert.Equal("""["42","43"]""", Assert.IsType(result.Content[0]).Text); } [Fact]