Normalize CallToolResult.StructuredContent to JsonElement? (#1357)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
This commit is contained in:
Copilot 2026-02-23 18:04:09 -05:00 • committed by GitHub
commit 0a0a575a3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -266,7 +266,7 @@ internal sealed partial class AIFunctionMcpServerTool : McpServerTool
object? result;
result = await AIFunction.InvokeAsync(arguments, cancellationToken).ConfigureAwait(false);
JsonNode? structuredContent = CreateStructuredResponse(result);
JsonElement? structuredContent = CreateStructuredResponse(result);
return result switch
{
AIContent aiContent => new()
@ -529,7 +529,7 @@ internal sealed partial class AIFunctionMcpServerTool : McpServerTool
return outputSchema;
}
private JsonNode? CreateStructuredResponse(object? aiFunctionResult)
private JsonElement? CreateStructuredResponse(object? aiFunctionResult)
{
if (ProtocolTool.OutputSchema is null)
{
@ -537,25 +537,29 @@ internal sealed partial class AIFunctionMcpServerTool : McpServerTool
return null;
}
JsonNode? nodeResult = aiFunctionResult switch
JsonElement? elementResult = aiFunctionResult switch
{
JsonNode node => node,
JsonElement jsonElement => JsonSerializer.SerializeToNode(jsonElement, McpJsonUtilities.JsonContext.Default.JsonElement),
_ => JsonSerializer.SerializeToNode(aiFunctionResult, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object))),
JsonElement jsonElement => jsonElement,
JsonNode node => JsonSerializer.SerializeToElement(node, McpJsonUtilities.JsonContext.Default.JsonNode),
null => null,
_ => JsonSerializer.SerializeToElement(aiFunctionResult, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object))),
};
if (_structuredOutputRequiresWrapping)
{
return new JsonObject
JsonNode? resultNode = elementResult is { } je
? JsonSerializer.SerializeToNode(je, McpJsonUtilities.JsonContext.Default.JsonElement)
: null;
return JsonSerializer.SerializeToElement(new JsonObject
{
["result"] = nodeResult
};
["result"] = resultNode
}, McpJsonUtilities.JsonContext.Default.JsonObject);
}
return nodeResult;
return elementResult;
}
private static CallToolResult ConvertAIContentEnumerableToCallToolResult(IEnumerable<AIContent> contentItems, JsonNode? structuredContent)
private static CallToolResult ConvertAIContentEnumerableToCallToolResult(IEnumerable<AIContent> contentItems, JsonElement? structuredContent)
{
List<ContentBlock> contentList = [];
bool allErrorContent = true;