Repository navigation
Conversation
model_dump_json rejects lone surrogates (common from os.fsdecode on non-UTF-8 paths) after every earlier stage has already accepted the value. That exception used to end stdio stdout_writer / stdin_writer (and could break SSE / streamable HTTP event framing), tearing down the session. Serialize through a shared helper that substitutes Internal server error for responses/errors with an id, drops or locally fails requests and notifications, and keeps the writer loop alive. Fixes modelcontextprotocol#3707
|
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 #3707. If a maintainer assigns you to #3707, 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:
Maintainers: reopen, remove |
Fixes #3707
Motivation and Context
A Python
strwith a lone surrogate (e.g. fromos.fsdecode()on a non-UTF-8 path) passes validation andmodel_dump(mode="json"), then fails only inmodel_dump_json()inside the transport writer. That exception was uncaught in stdiostdout_writer/stdin_writer(and could break SSE / streamable HTTP framing), so one bad message tore down the session.Escaping as
\udcff(JSJSON.stringify) is not a good wire fix here: pydantic’s JSON parser rejects lone-surrogate escapes on the receiving side. Containing the failure matches how other bad serializations already becomeInternal server errorhigher in the stack.Approach
mcp.shared.wire.serialize_outbound_message: trymodel_dump_json; on failure, substituteInternal server errorfor responses/errors with an id, otherwise returnNone.Internal errorand keep looping.How Has This Been Tested?
tests/shared/test_wire.py— helper behavior (response → Internal error, request/notification →None, healthy path unchanged).tests/server/test_stdio.py::test_stdio_server_surrogate_in_response_does_not_kill_writertests/client/test_stdio.py::test_surrogate_in_outbound_request_does_not_tear_down_writertests/server/test_stdio.py,tests/client/test_stdio.py,tests/server/test_sse_security.py,tests/client/test_streamable_http.py; pyright + ruff on touched files.Breaking Changes
None. Sessions that previously died on a surrogate now keep serving; the bad message becomes an error / drop for that request only.
Types of changes
Checklist
help wanted, or I'm a maintainer)Additional context
Per CONTRIBUTING, outside PRs need assignment or
help wanted— happy to adjust if maintainers prefer a different containment shape. Approach is described on the issue.AI-assisted drafting; I verified the repro, that
\udcffescaping is rejected by pydantic’s parser, and the regression tests.