python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

Client side JSON decode error when server does not use UTF-8

Open SecretiveShell opened this issue 2 months ago • 0 comments

Describe the bug If the server sends a JSON RPC message that contains invalid characters it is possible to crash the client STDOUT reader.

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 113: invalid start byte

There is no error handling in place for this case, and the default policy throws a traceback which can crash the client application completely.

To Reproduce

I wrote an example client/server that can reproduce the error 100% of the time. Please see https://github.com/SecretiveShell/MCP-SDK-JSON-DECODE-ERROR

The important part is:

    return [
        types.TextContent(
            type="text",
            text=b"This is a test with a problematic character: \x92".decode("windows-1252"),
        )
    ]

Expected behavior Ideally it should be possible to pass through a value to describe how to handle the error. The TextRecieveStream line that throws the error has a parameter for this already which should be exposed to the user.

    :param errors: handling scheme for decoding errors (defaults to ``strict``; see the
        `codecs module documentation`_ for a comprehensive list of options)

Additional context

I tested a small modification to the code that seems to have mitigated the issue for my application:

            async with read_stream_writer:
                buffer = ""
                async for chunk in TextReceiveStream(process.stdout, errors="ignore"):
                    lines = (buffer + chunk).split("\n")
                    buffer = lines.pop()

Since the erroneous characters are generally punctuation I noticed no impact on performance from just ignoring them.

SecretiveShell avatar Dec 22 '24 20:12 SecretiveShell