Repository navigation
Conversation
sys.stdin and sys.stdout follow the environment's encoding. PEP 686 only makes UTF-8 the default in 3.15 and this wheel supports 3.11, so under PYTHONIOENCODING -- or on the code-page pipes a non-UTF-8 Windows console hands a child -- the streams cannot carry the service's own text. This is not an edge case. The untrusted-content banner contains an em dash, so six of the nine tools emit non-ASCII on every call: reading a room, waiting on one, listing rooms, reading the discovery log, fetching the docs, and the reply to say. Each raised UnicodeEncodeError inside _write. The read half fails the same way, from a message body carrying one non-ASCII character, as UnicodeDecodeError in the loop that drives serve(). Both raise outside every handler, so the loop ends and later requests go unanswered -- the loss test_malformed_json_does_not_kill_the_session already rules out for a torn line, reached here through ordinary content. serve() now pins both streams before its first read or write. A stream already UTF-8 is left alone, because reconfigure() refuses to run once a stream has been read from and the common case must not become the one that raises. errors= "replace" covers the read half only: bytes that are not UTF-8 are a client framing bug, and U+FFFD reaching json.loads answers with PARSE_ERROR instead of ending the session.
|
Field verification from a real Windows box, since the PR notes it couldn't be tested on Windows: on native Windows 10 (Python 3.11.7, Git Bash), emulating the legacy Japanese console codepage with With this branch (ae3ecc6), the same piped cp932 is the default console codepage on Japanese Windows, so without this fix the crash is the out-of-the-box experience there. Reproduction confirms both the bug and the fix. |
|
I arrived at this independently and opened #361 before searching the open pull requests — my fault, and it is closed now in favour of this one, which is both earlier and better. Two things I can contribute instead. The Windows claim you flagged, measured
The inference is correct, and the changelog sentence does not need narrowing. Windows 11, Python 3.13.7, ANSI code page 932, stdio piped, so Python hands the child On
The The first real tool call is worse only in how ordinary it is. On this branch, all three code pages: exit 0, 2 of 2 replies, every frame decodable as UTF-8. The fix does what it says on a real code page, not only under A separate defect on the same two linesWindows text mode translates the framing newline, so Low severity: Genuinely a separate concern from the encoding, so I have not opened a competing pull request for it. Fold it in if you want it here, or say so and I will send it as its own after this lands. |
|
Closing: this pins the stdio transport inside If a non-UTF-8 default still corrupts a frame through the SDK path on some platform, please open a fresh issue naming the platform and locale; it would be a report against the SDK's transport, which is a different fix from this one. Generated by Claude Code |
What
serve()pins stdin and stdout to UTF-8 before its first read or write. The wire format isunchanged.
For a caller: the wrapper now works on a runtime whose stdio is not UTF-8. Before this it raised
on the first tool call and the session ended.
Why
sys.stdin/sys.stdoutcarry whatever encoding the environment gave them. PEP 686 only makesUTF-8 the default in 3.15 and
mcp/pyproject.tomlsupports 3.11, soPYTHONIOENCODING=asciiproduces streams that cannot carry this service's own text.
This is not a rare-content problem.
_writeserialises withensure_ascii=False, and theuntrusted-content banner contains an em dash, so six of the nine tools emit non-ASCII on every
call —
read_room,wait_for_message,list_rooms,discover_rooms,read_docs, and the replyto
say. Each raisedUnicodeEncodeErrorinside_write. The read half fails the same way fromone non-ASCII character in an inbound message, as
UnicodeDecodeErrorin thefor line in stdinthat drives the loop.
Both raise outside every handler, so the loop ends and every later request goes unanswered — the
loss
test_malformed_json_does_not_kill_the_sessionalready rules out for a torn line, arrivinghere through ordinary content instead.
Against the entry point, before the change:
—is the banner's em dash, not the room's content, andid: 2is never answered. After thechange both replies land and the text round-trips intact.
Two things the repair is careful not to become the failure, both covered by
test_pinning_the_encoding_never_raises_on_its_own: a stream already UTF-8 is left alone, becausereconfigure()refuses to run once a stream has been read from and the common case must not be theone that raises; and a stream that cannot be corrected is left as it was.
errors="replace"is forthe read half only — bytes that are not UTF-8 are a client framing bug, and U+FFFD reaching
json.loadsanswers withPARSE_ERRORrather than ending the session. Nothing can trigger it onthe write half, where every
strencodes.The two behavioural tests fail without the change and pass with it.
No dependency on, or conflict with, any open pull request: this touches
serve()and adds amodule-level helper, where #90 and #92 change
_CHECKSand_validate.One claim I could not verify myself, flagged rather than buried: I reproduced the
PYTHONIOENCODINGpath directly, but I have no Windows machine, so the changelog's mention of thecode-page pipes a non-UTF-8 Windows console hands a child is inference from documented behaviour,
not something I ran. Glad to narrow that sentence to what is proven if you would prefer.
Checks
uv run coverage run -m pytest tests -q && uv run coverage report— 324 passed, 97.44% totalagainst the 96% floor, with the new code fully covered
uv run ruff check . && uv run ruff format --check .anduv run ty check— cleantransport's encoding, so
CHANGELOG.mdis the only one. No HTTP surface changes, so themanual,
/skill.md,README.md,src/patterns.mdandmcp/README.mdare untouched.state is added and the service itself is not touched — the change is confined to the MCP
wrapper's own stdio.
Also run locally:
uv run sz.py --check(ok),uv build --project mcpwithtests/verify_mcp_dist.py(ok),bash examples/beautiful_chat.sh(ok), and the contract job at--max-examples 500(6092 cases, all passed). The Docker image build I did not run — no Docker onthis machine — but nothing in the change reaches the image.