Skip to content

fix(stdio): handle lone surrogates and reply with parse error on invalid JSON - #3905

Closed
chengwudi1 wants to merge 1 commit into
modelcontextprotocol:mainfrom
chengwudi1:fix/stdio-server-json-parse-error
Closed

chengwudi1 wants to merge 1 commit into
modelcontextprotocol:mainfrom
chengwudi1:fix/stdio-server-json-parse-error

Conversation

@chengwudi1

Copy link
Copy Markdown

Summary

Fixes #3894.

A stdio server previously dropped any input line that failed JSON validation, leaving the client hanging indefinitely until timeout. In addition, lone surrogates in JSON strings (such as unpaired \ud83d) were rejected by Pydantic's JSON parser (pydantic_core / serde_json) with an unhandled exception that was also dropped silently.

Root Cause

  1. types.jsonrpc_message_adapter.validate_json(line) strictly rejects lone UTF-16 surrogates because RFC 8259 requires UTF-8 and serde_json enforces standard Unicode code points.
  2. In src/mcp/server/stdio.py, any exception caught when validating a line resulted in:
    • Silently continuing the loop without sending an error response over write_stream.
    • The client waiting forever for a response to its JSON-RPC request.

Solution

  1. Sanitize lone surrogates: In src/mcp/server/stdio.py, normalize lone/unpaired UTF-16 surrogates to U+FFFD (\ufffd) while preserving valid surrogate pairs before validation.
  2. Reply with JSON-RPC Parse Error (-32700): When a line fails JSON parsing, reply with a JSON-RPC 2.0 -32700 Parse error on write_stream. Extract the id from the line if present (per JSON-RPC 2.0 specification: if the id can be safely extracted, use it; otherwise use null).
  3. Preserve backwards compatibility: Continue forwarding the exception to read_stream_writer.

Testing

  • Added comprehensive unit test in tests/server/test_stdio.py:
    • Validates lone surrogate line sanitization and delivery.
    • Validates that valid surrogate pairs remain untouched.
    • Validates that invalid JSON with id emits -32700 with the matching id.
    • Validates that malformed JSON without id emits -32700 with id: null.
  • Verified all 18 stdio server tests pass: pytest tests/server/test_stdio.py.
  • Verified formatting and linting pass: ruff check and ruff format.

…lid JSON

Fixes modelcontextprotocol#3894.

A stdio server previously dropped any input line that failed JSON validation,
leaving the client hanging indefinitely until timeout. In addition, lone
surrogates in JSON strings were rejected by Pydantic's JSON parser.

This fix:
1. Sanitizes lone UTF-16 surrogates to U+FFFD before validation while preserving valid surrogate pairs.
2. Replies with a JSON-RPC Parse Error (-32700) on invalid JSON lines instead of silently dropping them, attempting to preserve the request id when extractable.
3. Continues to deliver the parse exception to the reader stream for backwards compatibility.
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #3894.

If a maintainer assigns you to #3894, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take.

You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way.

CONTRIBUTING.md has the full reasoning, but in short:

  • We're a small team with very little capacity to review community PRs right now.
  • Many recent PRs are AI-generated with little human review, and reviewing one carefully still costs a maintainer as much time as it ever did. A well-described issue is usually more useful to us than the code.

Maintainers: reopen, remove missing-issue-link, or add bypass-issue-check to override.

@github-actions github-actions Bot closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stdio server sends no reply to a request line that fails JSON validation (e.g. a lone surrogate escape "\ud83d"), so the call hangs

1 participant