Skip to content
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
};
}
}