Skip to content

Answer stdio lines that fail JSON-RPC validation with a Parse error - #3895

Closed
IngTian wants to merge 1 commit into
modelcontextprotocol:mainfrom
IngTian:fix/stdio-parse-error-reply
Closed

IngTian wants to merge 1 commit into
modelcontextprotocol:mainfrom
IngTian:fix/stdio-parse-error-reply

Conversation

@IngTian

@IngTian IngTian commented Nov 22, 2026

Copy link
Copy Markdown

What & why

A stdio server dropped any request line that failed JSON-RPC validation with no reply on the wire and nothing on stderr, so the client's call hung until its own timeout. Reported in #3894: a lone surrogate escape like \ud83d — which json.dumps emits for text cut mid-emoji — is rejected by pydantic-core even though the line is pure ASCII.

The transport now answers such a line with a JSON-RPC Parse error (-32700, per JSON-RPC 2.0):

  • the request id is echoed when it can be recovered from the raw line — stdlib json tolerates the escapes pydantic-core rejects, so an SDK client's pending call for that id resolves instead of hanging;
  • "id": null when the id cannot be recovered (e.g. the line is not JSON at all);
  • notification-shaped lines (an object without an id member — the same key-presence rule the streamable-HTTP entry uses) are never answered, per JSON-RPC 2.0 §4.1.

The failure is also logged, so it is visible on stderr.

Why at the transport layer

The dispatcher's on_stream_exception path can't recover the id (validation failed before the id was parsed), and the server never sets an observer, so a dispatcher-level default could only emit "id": null — which an SDK client can't correlate, leaving the hang this issue is about. Only the stdio reader still has the raw line. The SSE transport already answers its own parse failures (HTTP 400) and the streamable-HTTP entries answer theirs; stdio was the only transport that stayed silent.

Tests

  • test_stdio_server_answers_an_invalid_line_with_a_parse_error — parametrised over not-JSON, non-object, and lone-surrogate lines; asserts the reply (recovered id / null) and that serving continues.
  • test_stdio_server_does_not_answer_a_notification_shaped_line
  • test_stdio_server_drops_the_parse_error_reply_when_the_write_stream_is_closed
  • test_mcpserver_run_stdio_answers_a_malformed_line_and_keeps_serving — end-to-end through run("stdio") with the exact line from the issue.

Coverage for src/mcp/server/stdio.py stays 100%; strict-no-cover, ruff, and pyright (strict) pass.

Fixes #3894

Note

AI-assisted: this PR was prepared with the help of an AI coding agent; the change, reasoning, and tests were reviewed and are owned by the author.

A request line that fails validate_json (e.g. carrying a lone
surrogate escape, which pydantic-core rejects) was dropped with no
reply, so the client's call hung until its own timeout. Reply -32700
per JSON-RPC 2.0, echoing the request id recovered from the raw line
with stdlib json - it tolerates the escapes pydantic-core rejects -
or null when the id cannot be recovered. Notification-shaped lines
(an object without an id member, the same key-presence rule as the
streamable-HTTP entry) are never answered.

Fixes modelcontextprotocol#3894
@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 22, 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 22, 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