Skip to content

Commit 36bb4ba

Browse files
Siddhartha Senguptaclaude
authored andcommitted
fix(client): default encoding_error_handler to "replace"
StdioServerParameters.encoding_error_handler defaulted to "strict", which TextReceiveStream uses when decoding the server's stdout. A single invalid UTF-8 byte raised UnicodeDecodeError inside stdout_reader's receive loop; that error matches none of the handler's except clauses, so it escaped the task group and brought the whole transport down as an ExceptionGroup, losing any valid message that followed. The shutdown drain already consumes raw bytes, so only the mid-session path was affected. Defaulting to "replace" makes the malformed bytes U+FFFD, which then fails JSON-RPC validation, and _parse_line returns that error as a value on the read stream -- the established behaviour for an unparseable line. The session stays up and later messages still arrive. This matches the server side, hardened the same way in #2302. Fixes #2454 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 77001c9 commit 36bb4ba

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎src/mcp/client/stdio.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ class StdioServerParameters(BaseModel):
107107
encoding: str = "utf-8"
108108
"""Text encoding for messages to and from the server."""
109109

110-
encoding_error_handler: Literal["strict", "ignore", "replace"] = "strict"
111-
"""Encoding error handler; see https://docs.python.org/3/library/codecs.html#error-handlers."""
110+
encoding_error_handler: Literal["strict", "ignore", "replace"] = "replace"
111+
"""Encoding error handler; see https://docs.python.org/3/library/codecs.html#error-handlers.
112+
113+
Defaults to `"replace"` so that malformed output from the server becomes U+FFFD and the
114+
line surfaces as a parse error on the read stream, leaving the session able to carry on.
115+
"""
112116

113117

114118
@asynccontextmanager

‎tests/client/test_stdio.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,30 @@ async def test_invalid_json_from_the_server_surfaces_as_an_in_stream_exception(
323323
assert await _next_message(read_stream) == ping
324324

325325

326+
@pytest.mark.anyio
327+
async def test_invalid_utf8_from_the_server_surfaces_as_an_in_stream_exception(
328+
monkeypatch: pytest.MonkeyPatch,
329+
) -> None:
330+
"""Undecodable bytes mid-session are replaced rather than ending the session.
331+
332+
A server emitting a malformed line must not take the transport down with it; the line
333+
arrives as an Exception and the messages after it still come through.
334+
"""
335+
ping = JSONRPCRequest(jsonrpc="2.0", id=1, method="ping")
336+
process = FakeProcess(on_stdin_close=lambda: process.exit(0))
337+
338+
install_fake_process(monkeypatch, process)
339+
340+
with anyio.fail_after(5):
341+
async with stdio_client(FAKE_PARAMS) as (read_stream, _):
342+
await process.feed(b"\xff\xfe\n" + _line(ping))
343+
344+
error = await read_stream.receive()
345+
# The undecodable bytes become U+FFFD, which then fails JSON-RPC validation.
346+
assert isinstance(error, ValueError)
347+
assert await _next_message(read_stream) == ping
348+
349+
326350
@pytest.mark.anyio
327351
async def test_a_server_that_dies_before_responding_fails_initialize_with_connection_closed(
328352
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)