Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SecretiveShell opened this issue Dec 22, 2024 · 0 comments · Fixed by #120
Open

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

SecretiveShell opened this issue Dec 22, 2024 · 0 comments · Fixed by #120
Assignees
Labels
bug Something isn't working

Comments

@SecretiveShell
Copy link
Contributor

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.

@dsp-ant dsp-ant added the bug Something isn't working label Jan 3, 2025
@dsp-ant dsp-ant self-assigned this Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants