diff --git a/src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs index 06e0eed0c..8d31f01b1 100644 --- a/src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs @@ -253,7 +253,8 @@ public override async ValueTask InvokeAsync( { AIContent aiContent => new() { - Content = [aiContent.ToContent()] + Content = [aiContent.ToContent()], + IsError = aiContent is ErrorContent }, null => new() @@ -276,10 +277,7 @@ public override async ValueTask InvokeAsync( Content = [.. texts.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })] }, - IEnumerable contentItems => new() - { - Content = [.. contentItems.Select(static item => item.ToContent())] - }, + IEnumerable contentItems => ConvertAIContentEnumerableToCallToolResponse(contentItems), IEnumerable contents => new() { @@ -299,4 +297,27 @@ public override async ValueTask InvokeAsync( }; } + private static CallToolResponse ConvertAIContentEnumerableToCallToolResponse(IEnumerable contentItems) + { + List 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 + }; + } } \ No newline at end of file