Skip to content

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

Closed

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

Activity

  1. added
    bug
    on Sep 10, 2025
  2. self-assigned this
    on Sep 10, 2025
  3. pja-ant commented on Sep 10, 2025

    @pja-ant
    Contributor

    Thanks for reporting, I can reproduce this.

    From my investigation so far, it looks like the httpx parser is incorrectly treating \u2028 as a line separator, which goes against what the SSE spec mandates: https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream

    This is where SSE parsing happens, it uses aiter_lines to iterate the stream, which internally uses LineDecoder with a set of line separators that doesn't match what SSE specifies.

  4. pja-ant commented on Sep 10, 2025

    @pja-ant
    Contributor

    And yeah, the exception handling here is bad also for the reasons mentioned.

  5. pja-ant commented on Sep 10, 2025

    @pja-ant
    Contributor

    Reported issue to httpx-sse: florimondmanca/httpx-sse#34

  6. added
    ready for work
    P2
    on Oct 3, 2025
  7. pja-ant commented on Oct 8, 2025

    @pja-ant
    Contributor

    The fix in httpx-sse is now landed and released in 0.4.2. Update your dependencies to that version if you need this fix critically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2bugready for work

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions