Skip to content

Commit 70960e1

Browse files
authored
Remove special-casing of string enumerables in McpServerTool (#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 329f848 commit 70960e1

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
@@ -270,12 +270,6 @@ public override async ValueTask<CallToolResult> InvokeAsync(
270270
StructuredContent = structuredContent,
271271
},
272272

273-
IEnumerable<string> texts => new()
274-
{
275-
Content = [.. texts.Select(x => new TextContentBlock { Text = x ?? string.Empty })],
276-
StructuredContent = structuredContent,
277-
},
278-
279273
IEnumerable<AIContent> contentItems => ConvertAIContentEnumerableToCallToolResult(contentItems, structuredContent),
280274

281275
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)