Skip to content

fix(client/stdio): default encoding_error_handler to 'replace' for resilient UTF-8 decoding#2907

Open
sarvesh1327 wants to merge 1 commit into
modelcontextprotocol:mainfrom
sarvesh1327:fix/stdio-client-utf8-replace
Open

fix(client/stdio): default encoding_error_handler to 'replace' for resilient UTF-8 decoding#2907
sarvesh1327 wants to merge 1 commit into
modelcontextprotocol:mainfrom
sarvesh1327:fix/stdio-client-utf8-replace

Conversation

@sarvesh1327

Copy link
Copy Markdown

Summary

  • Changes the default encoding_error_handler in StdioServerParameters from "strict" to "replace".
  • Malformed UTF-8 bytes from a child server stdout previously crashed the client transport with UnicodeDecodeError.
  • With "replace", invalid bytes become U+FFFD and the line fails JSON-RPC validation, which is surfaced as an in-stream Exception that the session can handle.
  • This mirrors the server-side stdio hardening from PR fix: handle non-UTF-8 bytes in stdio server stdin #2302.

Test Plan

  • Reproduced the crash with a child process emitting \xff\xfe\n followed by valid JSON-RPC.
  • Verified the fix: first item is a ValidationError, second item is the valid SessionMessage.
  • Added regression test test_invalid_utf8_from_the_server_surfaces_as_an_in_stream_exception.
  • Full tests/client/test_stdio.py suite passes (34 passed, 1 skipped).

Notes

…silient UTF-8 decoding

Malformed UTF-8 bytes from a child server stdout previously crashed the
client transport because the default handler was 'strict'. Changing the
default to 'replace' mirrors the server-side fix from PR modelcontextprotocol#2302 and lets
the transport survive single bad lines while surfacing them as parse
errors for the session to handle.

Fixes modelcontextprotocol#2454
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stdio_client crashes on malformed UTF-8 from child stdout instead of surfacing parse error

1 participant