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 <antagg@microsoft.com>
This commit is contained in:
Astaggrock 2025-05-09 04:15:58 +01:00 • committed by GitHub
commit 47de86ab91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -253,7 +253,8 @@ internal sealed class AIFunctionMcpServerTool : McpServerTool
{
AIContent aiContent => new()
{
Content = [aiContent.ToContent()]
Content = [aiContent.ToContent()],
IsError = aiContent is ErrorContent
},
null => new()
@ -276,10 +277,7 @@ internal sealed class AIFunctionMcpServerTool : McpServerTool
Content = [.. texts.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })]
},
IEnumerable<AIContent> contentItems => new()
{
Content = [.. contentItems.Select(static item => item.ToContent())]
},
IEnumerable<AIContent> contentItems => ConvertAIContentEnumerableToCallToolResponse(contentItems),
IEnumerable<Content> contents => new()
{
@ -299,4 +297,27 @@ internal sealed class AIFunctionMcpServerTool : McpServerTool
};
}
private static CallToolResponse ConvertAIContentEnumerableToCallToolResponse(IEnumerable<AIContent> contentItems)
{
List<Content> contentList = [];
bool allErrorContent = true;
bool hasAny = false;
foreach (var item in contentItems)
{
contentList.Add(item.ToContent());
hasAny = true;
if (allErrorContent && item is not ErrorContent)
{
allErrorContent = false;
}
}
return new()
{
Content = contentList,
IsError = allErrorContent && hasAny
};
}
}