[v1.x] fix: handle non-UTF-8 bytes in stdio server stdin by maxisbey · Pull Request #2303 · modelcontextprotocol/python-sdk

@maxisbey

TextIOWrapper defaults to errors='strict', which raises
UnicodeDecodeError when stdin contains bytes that are not valid UTF-8.
This exception occurs during 'async for line in stdin' iteration, which
is outside the JSON-parsing try/except block, so it propagates through
the task group and terminates the server process.

With errors='replace', invalid bytes are converted to U+FFFD. The
resulting line then fails JSON validation, which is already caught and
sent into the read stream as an exception for the session layer to
handle - the same path as any other malformed input.