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
2 changes: 1 addition & 1 deletion src/mcp/client/stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class StdioServerParameters(BaseModel):
encoding: str = "utf-8"
"""Text encoding for messages to and from the server."""

encoding_error_handler: Literal["strict", "ignore", "replace"] = "strict"
encoding_error_handler: Literal["strict", "ignore", "replace"] = "replace"
"""Encoding error handler; see https://docs.python.org/3/library/codecs.html#error-handlers."""


Expand Down
24 changes: 24 additions & 0 deletions tests/client/test_stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,30 @@ async def test_invalid_json_from_the_server_surfaces_as_an_in_stream_exception(
assert await _next_message(read_stream) == ping


@pytest.mark.anyio
async def test_invalid_utf8_from_the_server_surfaces_as_an_in_stream_exception(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A line containing invalid UTF-8 is replaced and delivered as an Exception on the read stream.

The transport stays alive so subsequent valid messages still come through.
Regression for #2454.
"""
ping = JSONRPCRequest(jsonrpc="2.0", id=1, method="ping")
process = FakeProcess(on_stdin_close=lambda: process.exit(0))

install_fake_process(monkeypatch, process)

with anyio.fail_after(5):
async with stdio_client(FAKE_PARAMS) as (read_stream, _):
await process.feed(b"\xff\xfe\n" + _line(ping))

error = await read_stream.receive()
# The transport surfaces parse failures as the underlying validation error.
assert isinstance(error, ValueError)
assert await _next_message(read_stream) == ping


@pytest.mark.anyio
async def test_a_server_that_dies_before_responding_fails_initialize_with_connection_closed(
monkeypatch: pytest.MonkeyPatch,
Expand Down
Loading