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 3 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
32 changes: 27 additions & 5 deletions src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,30 @@ options.OpenWorld is not null ||
return newOptions;
}

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

/// <summary>Gets the <see cref="AIFunction"/> wrapped by this tool.</summary>
internal AIFunction AIFunction { get; }

Expand Down Expand Up @@ -253,7 +277,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 +301,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 Down