Skip to content

Commit 47de86a

Browse files
AstaggrockAndy Tagg
and
Andy Tagg
authored
Set IsError on CallToolResponse to true when result is ErrorContent (#394)
* Set IsError on CallToolResponse to true when result is ErrorContent * Updated the `IsError` determination for a list of AIContent to require that all items in `contentItems` are instances of `ErrorContent` and that the collection is not empty * Introduce ConvertAiContentEnumerableToCallToolResponse to transform an IEnumerable<AIContent> into a CallToolResponse * Move location of ConvertAIContentEnumerableToCallToolResponse helper function --------- Co-authored-by: Andy Tagg <[email protected]>
1 parent c750f09 commit 47de86a

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs

+26-5
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ public override async ValueTask<CallToolResponse> InvokeAsync(
253253
{
254254
AIContent aiContent => new()
255255
{
256-
Content = [aiContent.ToContent()]
256+
Content = [aiContent.ToContent()],
257+
IsError = aiContent is ErrorContent
257258
},
258259

259260
null => new()
@@ -276,10 +277,7 @@ public override async ValueTask<CallToolResponse> InvokeAsync(
276277
Content = [.. texts.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })]
277278
},
278279

279-
IEnumerable<AIContent> contentItems => new()
280-
{
281-
Content = [.. contentItems.Select(static item => item.ToContent())]
282-
},
280+
IEnumerable<AIContent> contentItems => ConvertAIContentEnumerableToCallToolResponse(contentItems),
283281

284282
IEnumerable<Content> contents => new()
285283
{
@@ -299,4 +297,27 @@ public override async ValueTask<CallToolResponse> InvokeAsync(
299297
};
300298
}
301299

300+
private static CallToolResponse ConvertAIContentEnumerableToCallToolResponse(IEnumerable<AIContent> contentItems)
301+
{
302+
List<Content> contentList = [];
303+
bool allErrorContent = true;
304+
bool hasAny = false;
305+
306+
foreach (var item in contentItems)
307+
{
308+
contentList.Add(item.ToContent());
309+
hasAny = true;
310+
311+
if (allErrorContent && item is not ErrorContent)
312+
{
313+
allErrorContent = false;
314+
}
315+
}
316+
317+
return new()
318+
{
319+
Content = contentList,
320+
IsError = allErrorContent && hasAny
321+
};
322+
}
302323
}

0 commit comments

Comments
 (0)