Skip to content

Commit 2c07b8f

Browse files
Copilotstephentoub
andcommitted
Normalize CallToolResult.StructuredContent from JsonNode? to JsonElement? to match ToolResultContentBlock.StructuredContent
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 68f8ddd commit 2c07b8f

6 files changed

Lines changed: 15 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/Server/AIFunctionMcpServerTool.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public override async ValueTask<CallToolResult> InvokeAsync(
267267
object? result;
268268
result = await AIFunction.InvokeAsync(arguments, cancellationToken).ConfigureAwait(false);
269269

270-
JsonNode? structuredContent = CreateStructuredResponse(result);
270+
JsonElement? structuredContent = CreateStructuredResponse(result);
271271
return result switch
272272
{
273273
AIContent aiContent => new()
@@ -530,7 +530,7 @@ typeProperty.ValueKind is not JsonValueKind.String ||
530530
return outputSchema;
531531
}
532532

533-
private JsonNode? CreateStructuredResponse(object? aiFunctionResult)
533+
private JsonElement? CreateStructuredResponse(object? aiFunctionResult)
534534
{
535535
if (ProtocolTool.OutputSchema is null)
536536
{
@@ -547,16 +547,16 @@ typeProperty.ValueKind is not JsonValueKind.String ||
547547

548548
if (_structuredOutputRequiresWrapping)
549549
{
550-
return new JsonObject
550+
nodeResult = new JsonObject
551551
{
552552
["result"] = nodeResult
553553
};
554554
}
555555

556-
return nodeResult;
556+
return nodeResult?.Deserialize(McpJsonUtilities.JsonContext.Default.JsonElement);
557557
}
558558

559-
private static CallToolResult ConvertAIContentEnumerableToCallToolResult(IEnumerable<AIContent> contentItems, JsonNode? structuredContent)
559+
private static CallToolResult ConvertAIContentEnumerableToCallToolResult(IEnumerable<AIContent> contentItems, JsonElement? structuredContent)
560560
{
561561
List<ContentBlock> contentList = [];
562562
bool allErrorContent = true;

tests/ModelContextProtocol.Tests/Client/McpClientToolTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static CallToolResult StructuredContentTool() =>
122122
new()
123123
{
124124
Content = [new TextContentBlock { Text = "Regular content" }],
125-
StructuredContent = JsonNode.Parse("{\"key\":\"value\"}")
125+
StructuredContent = JsonElement.Parse("{\"key\":\"value\"}")
126126
};
127127

128128
// Tool that returns CallToolResult with Meta

tests/ModelContextProtocol.Tests/Protocol/CallToolResultTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static void CallToolResult_SerializationRoundTrip_PreservesAllProperties(
1212
var original = new CallToolResult
1313
{
1414
Content = [new TextContentBlock { Text = "Result text" }],
15-
StructuredContent = JsonNode.Parse("""{"temperature":72}"""),
15+
StructuredContent = JsonElement.Parse("""{"temperature":72}"""),
1616
IsError = false,
1717
Task = new McpTask
1818
{
@@ -32,7 +32,7 @@ public static void CallToolResult_SerializationRoundTrip_PreservesAllProperties(
3232
var textBlock = Assert.IsType<TextContentBlock>(deserialized.Content[0]);
3333
Assert.Equal("Result text", textBlock.Text);
3434
Assert.NotNull(deserialized.StructuredContent);
35-
Assert.Equal(72, deserialized.StructuredContent["temperature"]!.GetValue<int>());
35+
Assert.Equal(72, deserialized.StructuredContent.Value.GetProperty("temperature").GetInt32());
3636
Assert.False(deserialized.IsError);
3737
Assert.NotNull(deserialized.Task);
3838
Assert.Equal("task-1", deserialized.Task.TaskId);

tests/ModelContextProtocol.Tests/Protocol/UnknownPropertiesTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public void CallToolResult_WithStructuredContentAtCorrectLevel_PreservesProperty
252252
// Assert
253253
Assert.NotNull(deserialized);
254254
Assert.NotNull(deserialized.StructuredContent);
255-
Assert.Equal("correctly placed here", deserialized.StructuredContent["result"]?.ToString());
256-
Assert.Equal(42, (int?)deserialized.StructuredContent["value"]);
255+
Assert.Equal("correctly placed here", deserialized.StructuredContent.Value.GetProperty("result").GetString());
256+
Assert.Equal(42, deserialized.StructuredContent.Value.GetProperty("value").GetInt32());
257257
}
258258
}

tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,11 +654,11 @@ public object InstanceMethod()
654654
}
655655
}
656656

657-
private static void AssertMatchesJsonSchema(JsonElement schemaDoc, JsonNode? value)
657+
private static void AssertMatchesJsonSchema(JsonElement schemaDoc, JsonElement? value)
658658
{
659659
JsonSchema schema = JsonSerializer.Deserialize(schemaDoc, JsonContext2.Default.JsonSchema)!;
660660
EvaluationOptions options = new() { OutputFormat = OutputFormat.List };
661-
EvaluationResults results = schema.Evaluate(JsonSerializer.SerializeToElement(value, JsonContext2.Default.JsonNode), options);
661+
EvaluationResults results = schema.Evaluate(value ?? default, options);
662662
if (!results.IsValid)
663663
{
664664
IEnumerable<string> errors = (results.Details ?? [])
@@ -670,7 +670,7 @@ Instance JSON document does not match the specified schema.
670670
Schema:
671671
{JsonSerializer.Serialize(schema)}
672672
Instance:
673-
{value?.ToJsonString() ?? "null"}
673+
{value?.ToString() ?? "null"}
674674
Errors:
675675
{string.Join(Environment.NewLine, errors)}
676676
""");

0 commit comments

Comments
 (0)