@@ -266,7 +266,7 @@ public override async ValueTask<CallToolResult> InvokeAsync(
266266 object ? result ;
267267 result = await AIFunction . InvokeAsync ( arguments , cancellationToken ) . ConfigureAwait ( false ) ;
268268
269- JsonNode ? structuredContent = CreateStructuredResponse ( result ) ;
269+ JsonElement ? structuredContent = CreateStructuredResponse ( result ) ;
270270 return result switch
271271 {
272272 AIContent aiContent => new ( )
@@ -529,33 +529,37 @@ typeProperty.ValueKind is not JsonValueKind.String ||
529529 return outputSchema ;
530530 }
531531
532- private JsonNode ? CreateStructuredResponse ( object ? aiFunctionResult )
532+ private JsonElement ? CreateStructuredResponse ( object ? aiFunctionResult )
533533 {
534534 if ( ProtocolTool . OutputSchema is null )
535535 {
536536 // Only provide structured responses if the tool has an output schema defined.
537537 return null ;
538538 }
539539
540- JsonNode ? nodeResult = aiFunctionResult switch
540+ JsonElement ? elementResult = aiFunctionResult switch
541541 {
542- JsonNode node => node ,
543- JsonElement jsonElement => JsonSerializer . SerializeToNode ( jsonElement , McpJsonUtilities . JsonContext . Default . JsonElement ) ,
544- _ => JsonSerializer . SerializeToNode ( aiFunctionResult , AIFunction . JsonSerializerOptions . GetTypeInfo ( typeof ( object ) ) ) ,
542+ JsonElement jsonElement => jsonElement ,
543+ JsonNode node => JsonSerializer . SerializeToElement ( node , McpJsonUtilities . JsonContext . Default . JsonNode ) ,
544+ null => null ,
545+ _ => JsonSerializer . SerializeToElement ( aiFunctionResult , AIFunction . JsonSerializerOptions . GetTypeInfo ( typeof ( object ) ) ) ,
545546 } ;
546547
547548 if ( _structuredOutputRequiresWrapping )
548549 {
549- return new JsonObject
550+ JsonNode ? resultNode = elementResult is { } je
551+ ? JsonSerializer . SerializeToNode ( je , McpJsonUtilities . JsonContext . Default . JsonElement )
552+ : null ;
553+ return JsonSerializer . SerializeToElement ( new JsonObject
550554 {
551- [ "result" ] = nodeResult
552- } ;
555+ [ "result" ] = resultNode
556+ } , McpJsonUtilities . JsonContext . Default . JsonObject ) ;
553557 }
554558
555- return nodeResult ;
559+ return elementResult ;
556560 }
557561
558- private static CallToolResult ConvertAIContentEnumerableToCallToolResult ( IEnumerable < AIContent > contentItems , JsonNode ? structuredContent )
562+ private static CallToolResult ConvertAIContentEnumerableToCallToolResult ( IEnumerable < AIContent > contentItems , JsonElement ? structuredContent )
559563 {
560564 List < ContentBlock > contentList = [ ] ;
561565 bool allErrorContent = true ;
0 commit comments