Skip to content

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

Closed

Description

@SecretiveShell

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.

Activity

  1. self-assigned this
    on Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions