Skip to content

JSON Parsing Fails & Client Gets Stuck in Waiting When Tool Returns Results with Special Unicode Characters #1356

@Hugh488

Description

@Hugh488

Initial Checks

Description

During my usage, I noticed that if the tool's returned results contain special Unicode characters, it will cause JSON parsing to abort. What's more critical is that when a JSON parsing error occurs on the client side, the parsing function sends the exception directly to the tool-invoking function without wrapping the exception in the format agreed upon by both parties. This prevents the tool-invoking function from properly capturing the exception, leading it to get stuck in a state of continuous waiting.

In mcp.client.sse:

case "message":
    try:
        message = types.JSONRPCMessage.model_validate_json(  # noqa: E501
            sse.data
        )
        logger.debug(f"Received server message: {message}")
    except Exception as exc:
        logger.exception("Error parsing server message")
        await read_stream_writer.send(exc)
        continue

    session_message = SessionMessage(message)
    await read_stream_writer.send(session_message)

Should the exc be encapsulated before executing await read_stream_writer.send(exc)?
Currently, it seems that when an exception is sent, the message cannot be received at line 272 in mcp.shared.session, getting stuck in a wait state.

Example Code

When there is `\u2028` in the tool's results.

Python & MCP Python SDK

python3.13.7
MCP Python SDK:1.13.1

Metadata

Metadata

Assignees

Labels

P2Moderate issues affecting some users, edge cases, potentially valuable featurebugSomething isn't workingready for workEnough information for someone to start working on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions