Skip to content

Set IsError on CallToolResponse to true when result is ErrorContent #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ public override async ValueTask<CallToolResponse> InvokeAsync(
{
AIContent aiContent => new()
{
Content = [aiContent.ToContent()]
Content = [aiContent.ToContent()],
IsError = aiContent is ErrorContent
},

null => new()
Expand All @@ -276,10 +277,7 @@ public override async ValueTask<CallToolResponse> InvokeAsync(
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()
{
Expand All @@ -299,4 +297,27 @@ public override async ValueTask<CallToolResponse> InvokeAsync(
};
}

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
};
}
}