Skip to content

Commit 51c9b13

Browse files
committed
More cases in switch and tests
1 parent 6f1dd69 commit 51c9b13

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

src/ModelContextProtocol/AIContentExtensions.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,7 @@ internal static string GetBase64Data(this DataContent dataContent)
104104
#endif
105105
}
106106

107-
/// <summary>
108-
/// Converts different types of <see cref="AIContent"/> into a standardized <see cref="Content"/> object with specific properties based on the
109-
/// content type.
110-
/// </summary>
111-
/// <param name="content"></param>
112-
/// <returns>A <see cref="Content"/> object that encapsulates the relevant properties derived from the input content.</returns>
113-
public static Content ToContent(this AIContent content) =>
107+
internal static Content ToContent(this AIContent content) =>
114108
content switch
115109
{
116110
TextContent textContent => new()
@@ -123,9 +117,9 @@ public static Content ToContent(this AIContent content) =>
123117
Data = dataContent.GetBase64Data(),
124118
MimeType = dataContent.MediaType,
125119
Type =
126-
dataContent.HasTopLevelMediaType("image") ? "image" :
127-
dataContent.HasTopLevelMediaType("audio") ? "audio" :
128-
"resource",
120+
dataContent.HasTopLevelMediaType("image") ? "image" :
121+
dataContent.HasTopLevelMediaType("audio") ? "audio" :
122+
"resource",
129123
},
130124
_ => new()
131125
{

src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ public override async Task<CallToolResponse> InvokeAsync(
225225
{
226226
Content = [.. contents]
227227
},
228+
CallToolResponse callToolResponse => callToolResponse,
228229

229230
// TODO https://github.com/modelcontextprotocol/csharp-sdk/issues/69:
230231
// Add specialization for annotations.

tests/ModelContextProtocol.Tests/Server/McpServerToolReturnTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,31 @@ public async Task CanReturnCollectionOfMcpContent()
164164
Assert.Equal("image/png", result.Content[1].MimeType);
165165
Assert.Null(result.Content[1].Text);
166166
}
167+
168+
[Fact]
169+
public async Task CanReturnCallToolResponse()
170+
{
171+
CallToolResponse response = new()
172+
{
173+
Content = [new() { Text = "text", Type = "text" }, new() { Data = "1234", Type = "image" }]
174+
};
175+
176+
Mock<IMcpServer> mockServer = new();
177+
McpServerTool tool = McpServerTool.Create((IMcpServer server) =>
178+
{
179+
Assert.Same(mockServer.Object, server);
180+
return response;
181+
});
182+
var result = await tool.InvokeAsync(
183+
new RequestContext<CallToolRequestParams>(mockServer.Object, null),
184+
TestContext.Current.CancellationToken);
185+
186+
Assert.Same(response, result);
187+
188+
Assert.Equal(2, result.Content.Count);
189+
Assert.Equal("text", result.Content[0].Text);
190+
Assert.Equal("text", result.Content[0].Type);
191+
Assert.Equal("1234", result.Content[1].Data);
192+
Assert.Equal("image", result.Content[1].Type);
193+
}
167194
}

0 commit comments

Comments
 (0)