8000 Serializing JSON as return value from Tool corrupts string resulting in Deserialization failures on client · Issue #274 · modelcontextprotocol/csharp-sdk · GitHub
[go: up one dir, main page]

Skip to content
Serializing JSON as return value from Tool corrupts string resulting in Deserialization failures on client #274
Open
@brandonh-msft

Description

@brandonh-msft

Describe the bug

In my implementation I am returning a complex object from my tool, so I JsonSerializer.Serialize(obj) it and return string from my tool.

When I get the value back on the other side, numerous characters in the JSON are escaped (\u\\\....) and so attempting to deserialize the value in .Text blows up.

To Reproduce
Steps to reproduce the behavior:

  1. Return a string created from JSON-serializing an object from an McpServerTool implementation.
  2. Attempt to deserialize the value of .Text on the client.

Expected behavior
Strings shouldn't get munged by anything from return time to arrival on the client.

Logs
String value upon return from Tool:

{"completion":[{"Role":{"Label":"system"},"Items":[{"$type":"TextContent","Text":"You are an expert data search ass

String value on client side when attempting to deserialize:

{"text":"{\u0022completion\u0022:[{\u0022Role\u0022:{\u0022Label\u0022:\u0022system\u0022},\u0022Items\u0022:[{\u0022$type\u0022:\u0022TextContent\u0022,\u0022Text\u0022:\u0022You are an expert data search 

Additional context
I've tried taking the bytes of the JSON value and wrapping them up in DataContent but this fails as well. Similarly, I've tried returning not just a string but a TextContent object also to no avail, even if I set RawRepresentation on it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0