Skip to content

Commit 1472b0c

Browse files
Copilotstephentoub
andcommitted
Normalize CallToolResult.StructuredContent to JsonElement? and CreateMessageRequestParams.Metadata to JsonObject?
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 1381376 commit 1472b0c

3 files changed

Lines changed: 19 additions & 15 deletions

File tree

src/ModelContextProtocol.Core/Protocol/CallToolResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Diagnostics.CodeAnalysis;
2-
using System.Text.Json.Nodes;
2+
using System.Text.Json;
33
using System.Text.Json.Serialization;
44

55
namespace ModelContextProtocol.Protocol;
@@ -41,7 +41,7 @@ public sealed class CallToolResult : Result
4141
/// Gets or sets an optional JSON object representing the structured result of the tool call.
4242
/// </summary>
4343
[JsonPropertyName("structuredContent")]
44-
public JsonNode? StructuredContent { get; set; }
44+
public JsonElement? StructuredContent { get; set; }
4545

4646
/// <summary>
4747
/// Gets or sets a value that indicates whether the tool call was unsuccessful.

src/ModelContextProtocol.Core/Protocol/CreateMessageRequestParams.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Diagnostics.CodeAnalysis;
2-
using System.Text.Json;
2+
using System.Text.Json.Nodes;
33
using System.Text.Json.Serialization;
44

55
namespace ModelContextProtocol.Protocol;
@@ -68,7 +68,7 @@ public sealed class CreateMessageRequestParams : RequestParams
6868
/// </para>
6969
/// </remarks>
7070
[JsonPropertyName("metadata")]
71-
public JsonElement? Metadata { get; set; }
71+
public JsonObject? Metadata { get; set; }
7272

7373
/// <summary>
7474
/// Gets or sets the server's preferences for which model to select.

src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)