Skip to content

Commit 888be88

Browse files
author
Andy Tagg
committed
Introduce ConvertAiContentEnumerableToCallToolResponse to transform an IEnumerable<AIContent> into a CallToolResponse
1 parent 803c882 commit 888be88

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ options.OpenWorld is not null ||
193193
return newOptions;
194194
}
195195

196+
private static CallToolResponse ConvertAiContentEnumerableToCallToolResponse(IEnumerable<AIContent> contentItems)
197+
{
198+
List<Content> contentList = [];
199+
bool allErrorContent = true;
200+
bool hasAny = false;
201+
202+
foreach (var item in contentItems)
203+
{
204+
contentList.Add(item.ToContent());
205+
hasAny = true;
206+
207+
if (allErrorContent && item is not ErrorContent)
208+
{
209+
allErrorContent = false;
210+
}
211+
}
212+
213+
return new()
214+
{
215+
Content = contentList,
216+
IsError = allErrorContent && hasAny
217+
};
218+
}
219+
196220
/// <summary>Gets the <see cref="AIFunction"/> wrapped by this tool.</summary>
197221
internal AIFunction AIFunction { get; }
198222

@@ -277,11 +301,7 @@ public override async ValueTask<CallToolResponse> InvokeAsync(
277301
Content = [.. texts.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })]
278302
},
279303

280-
IEnumerable<AIContent> contentItems => new()
281-
{
282-
Content = [.. contentItems.Select(static item => item.ToContent())],
283-
IsError = contentItems.All(item => item is ErrorContent) && contentItems.Any()
284-
},
304+
IEnumerable<AIContent> contentItems => ConvertAiContentEnumerableToCallToolResponse(contentItems),
285305

286306
IEnumerable<Content> contents => new()
287307
{

0 commit comments

Comments
 (0)