Skip to content

Commit 5bbf1c2

Browse files
committed
Adding support for returning collections from tools
This will mean we can return multiple AIContent objects from a tool, such as a mixed text/image set Contributes to modelcontextprotocol#68
1 parent 5171ccc commit 5bbf1c2

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal sealed class AIFunctionMcpServerTool : McpServerTool
2222
public static new AIFunctionMcpServerTool Create(
2323
Delegate method,
2424
string? name,
25-
string? description,
25+
string? description,
2626
IServiceProvider? services)
2727
{
2828
Throw.IfNull(method);
@@ -34,7 +34,7 @@ internal sealed class AIFunctionMcpServerTool : McpServerTool
3434
/// Creates an <see cref="McpServerTool"/> instance for a method, specified via a <see cref="Delegate"/> instance.
3535
/// </summary>
3636
public static new AIFunctionMcpServerTool Create(
37-
MethodInfo method,
37+
MethodInfo method,
3838
object? target,
3939
string? name,
4040
string? description,
@@ -195,57 +195,59 @@ public override async Task<CallToolResponse> InvokeAsync(
195195
};
196196
}
197197

198-
switch (result)
198+
return result switch
199199
{
200-
case null:
201-
return new()
202-
{
203-
Content = []
204-
};
205-
206-
case string text:
207-
return new()
208-
{
209-
Content = [new() { Text = text, Type = "text" }]
210-
};
211-
212-
case TextContent textContent:
213-
return new()
214-
{
215-
Content = [new() { Text = textContent.Text, Type = "text" }]
216-
};
217-
218-
case DataContent dataContent:
219-
return new()
220-
{
221-
Content = [new()
200+
null => new()
201+
{
202+
Content = []
203+
},
204+
string text => new()
205+
{
206+
Content = [new() { Text = text, Type = "text" }]
207+
},
208+
TextContent textContent => new()
209+
{
210+
Content = [new() { Text = textContent.Text, Type = "text" }]
211+
},
212+
DataContent dataContent => new()
213+
{
214+
Content = [new()
222215
{
223216
Data = dataContent.GetBase64Data(),
224217
MimeType = dataContent.MediaType,
225218
Type = dataContent.HasTopLevelMediaType("image") ? "image" : "resource",
226219
}]
227-
};
220+
},
221+
string[] texts => new()
222+
{
223+
Content = [.. texts.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })]
224+
},
228225

229-
case string[] texts:
230-
return new()
231-
{
232-
Content = texts
233-
.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })
234-
.ToList()
235-
};
226+
IEnumerable<AIContent> contentItems => new()
227+
{
228+
Content = [.. contentItems.Select(static item => item switch
229+
{
230+
TextContent textContent => new Content() { Type = "text", Text = textContent.Text },
231+
DataContent dataContent => new Content()
232+
{
233+
Data = dataContent.GetBase64Data(),
234+
MimeType = dataContent.MediaType,
235+
Type = dataContent.HasTopLevelMediaType("image") ? "image" : "resource",
236+
},
237+
_ => new Content() { Type = "text", Text = item.ToString() ?? string.Empty }
238+
})]
239+
},
236240

237241
// TODO https://github.com/modelcontextprotocol/csharp-sdk/issues/69:
238242
// Add specialization for annotations.
239-
240-
default:
241-
return new()
242-
{
243-
Content = [new()
243+
_ => new()
244+
{
245+
Content = [new()
244246
{
245247
Text = JsonSerializer.Serialize(result, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(object))),
246248
Type = "text"
247249
}]
248-
};
249-
}
250+
},
251+
};
250252
}
251253
}

0 commit comments

Comments
 (0)