Skip to content

Commit 4dc7ef9

Browse files
stephentoubouvreboite
authored andcommitted
Remove special-casing of string enumerables in McpServerTool (modelcontextprotocol#699)
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.
1 parent 9af2ed8 commit 4dc7ef9

File tree

3 files changed

+3
-11
lines changed

3 files changed

+3
-11
lines changed

src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,6 @@ public override async ValueTask<CallToolResult> InvokeAsync(
278278
StructuredContent = structuredContent,
279279
},
280280

281-
IEnumerable<string> texts => new()
282-
{
283-
Content = [.. texts.Select(x => new TextContentBlock { Text = x ?? string.Empty })],
284-
StructuredContent = structuredContent,
285-
},
286-
287281
IEnumerable<AIContent> contentItems => ConvertAIContentEnumerableToCallToolResult(contentItems, structuredContent),
288282

289283
IEnumerable<ContentBlock> contents => new()

tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ public async Task Can_Call_Registered_Tool_With_Array_Result()
254254

255255
Assert.NotNull(result.Content);
256256
Assert.NotEmpty(result.Content);
257-
Assert.Equal("hello Peter", (result.Content[0] as TextContentBlock)?.Text);
258-
Assert.Equal("hello2 Peter", (result.Content[1] as TextContentBlock)?.Text);
257+
Assert.Equal("""["hello Peter","hello2 Peter"]""", (result.Content[0] as TextContentBlock)?.Text);
259258

260259
result = await client.CallToolAsync(
261260
"SecondCustomTool",

tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,8 @@ public async Task CanReturnCollectionOfStrings()
356356
var result = await tool.InvokeAsync(
357357
new RequestContext<CallToolRequestParams>(mockServer.Object),
358358
TestContext.Current.CancellationToken);
359-
Assert.Equal(2, result.Content.Count);
360-
Assert.Equal("42", Assert.IsType<TextContentBlock>(result.Content[0]).Text);
361-
Assert.Equal("43", Assert.IsType<TextContentBlock>(result.Content[1]).Text);
359+
Assert.Single(result.Content);
360+
Assert.Equal("""["42","43"]""", Assert.IsType<TextContentBlock>(result.Content[0]).Text);
362361
}
363362

364363
[Fact]

0 commit comments

Comments
 (0)