Skip to content

CallToolResult deserialization fails when content block contains unexpected properties #955

@jeffhandley

Description

@jeffhandley

Describe the bug
When a tool call result contains unexpected properties in the content, the deserializer attempts to process the data. When that property is an array or object, it puts the converter into a bad state, leading to an exception.

To Reproduce

using ModelContextProtocol;
using ModelContextProtocol.Protocol;
using System.Text.Json;

const string malformedCallToolResultJson = """
{
    "content": [{
        "type": "text",
        "text": "[\n  {\n    \"Data\": \"1234567890\"\n  }\n]",
        "structuredContent": [ { "Data": "1234567890" } ]
    }]
}
""";

// Try to deserialize the unexpected CallToolResult using McpJsonUtilities.DefaultOptions
// This currently throws because the ContentBlock.Converter doesn't properly skip unknown properties
// that contain complex structures like arrays or objects
try
{
    JsonSerializer.Deserialize<CallToolResult>(malformedCallToolResultJson, McpJsonUtilities.DefaultOptions);
}
catch (JsonException ex)
{
    Console.WriteLine("Caught JsonException as expected:");
    Console.WriteLine(ex.Message);
}

This currently throws:

Caught JsonException as expected:
The converter 'ModelContextProtocol.Protocol.ContentBlock+Converter' read too much or not enough. Path: $.content[0] | LineNumber: 6 | BytePositionInLine: 1.

Expected behavior
Deserialization should succeed without an exception being thrown.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions