Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/mcp/client/stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,15 @@ class StdioServerParameters(BaseModel):
Defaults to utf-8.
"""

encoding_error_handler: Literal["strict", "ignore", "replace"] = "strict"
encoding_error_handler: Literal["strict", "ignore", "replace"] = "replace"
"""
The text encoding error handler.

Defaults to "replace" so that malformed bytes from the child process are
substituted with U+FFFD rather than crashing the transport. The invalid
line will then fail JSON validation and be surfaced as an in-stream
exception, keeping the transport alive for subsequent valid messages.

See https://docs.python.org/3/library/codecs.html#codec-base-classes for
explanations of possible values.
"""
Expand Down Expand Up @@ -151,8 +156,8 @@ async def stdout_reader():
for line in lines:
try:
message = types.jsonrpc_message_adapter.validate_json(line, by_name=False)
except Exception as exc: # pragma: no cover
logger.exception("Failed to parse JSONRPC message from server")
except Exception as exc:
logger.warning("Failed to parse JSONRPC message from server: %s", exc)
await read_stream_writer.send(exc)
continue

Expand Down
47 changes: 47 additions & 0 deletions tests/client/test_stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,53 @@ async def test_stdio_client_nonexistent_command():
assert exc_info.value.errno == errno.ENOENT


@pytest.mark.anyio
async def test_stdio_client_invalid_utf8_resilience():
"""Malformed UTF-8 from child stdout must not crash the transport.

With encoding_error_handler="replace" (now the default), invalid bytes
are replaced with U+FFFD. The resulting line fails JSON validation and
is surfaced as an in-stream Exception. Subsequent valid messages must
still be delivered.
"""
# Child script writes one malformed line, then one valid JSON-RPC line
child_script = textwrap.dedent(
"""
import sys
# Write invalid UTF-8 bytes followed by newline
sys.stdout.buffer.write(b"\\xff\\xfe\\n")
sys.stdout.buffer.flush()
# Write a valid JSON-RPC message
sys.stdout.buffer.write(b'{"jsonrpc":"2.0","id":1,"method":"ping"}\\n')
sys.stdout.buffer.flush()
# Exit cleanly
"""
)

server_params = StdioServerParameters(
command=sys.executable,
args=["-c", child_script],
)

async with stdio_client(server_params) as (read_stream, write_stream):
received: list[SessionMessage | Exception] = []
async with read_stream:
async for item in read_stream:
received.append(item)
if len(received) == 2:
break

# First item: the malformed line should surface as a parse exception
assert isinstance(received[0], Exception), (
f"Expected Exception for malformed UTF-8 line, got {type(received[0])}"
)
# Second item: the valid JSON-RPC message should come through
assert isinstance(received[1], SessionMessage), (
f"Expected SessionMessage for valid line, got {type(received[1])}"
)
assert received[1].message == JSONRPCRequest(jsonrpc="2.0", id=1, method="ping")


@pytest.mark.anyio
async def test_stdio_client_universal_cleanup():
"""Test that stdio_client completes cleanup within reasonable time
Expand Down
Loading