Repository navigation
Conversation
## Motivation and Context The Streamable HTTP transport echoes a client-supplied value into some of its error bodies: the `requested` member of the `-32022` Unsupported protocol version error, the `MCP-Protocol-Version`, `Mcp-Method` and `Mcp-Name` mismatch messages of `-32020`, and the legacy unsupported-version message. A header value arrives as ASCII-8BIT from a Rack server or tagged UTF-8 from a test request, and a body value comes out of the JSON parser, which passes bytes that are not valid UTF-8 through. Such a value makes `to_json` raise while the 400 is being built, so the rescue answers 500 and calls the exception reporter for a request the transport meant to refuse with 400 and a specific code, and any client can repeat that per request. With json 3.1 the same happens to an ASCII-8BIT value holding any byte above 0x7F, valid UTF-8 included, so a Rack server answered 500 to a mismatching non-ASCII `Mcp-Name` as well. The TypeScript server echoes the same values, but a JavaScript string cannot hold invalid UTF-8, so its response never fails. The echoed values now go through `printable_value`, which tags the value UTF-8 and replaces bytes that are not valid UTF-8 with U+FFFD, so the 400 and its code reach the client. The `requested` member stays, as the schema has it. Fixes modelcontextprotocol#663. ## How Has This Been Tested? Four new tests in `test/mcp/server/transports/streamable_http_transport_test.rb`: a requested version, an `Mcp-Method` and a base64 `Mcp-Name` header, and a body envelope version, each holding an invalid byte, answer 400 with their code and the scrubbed value, and a non-ASCII `Mcp-Method` given as bytes is echoed in its 400; the exception reporter stays silent in each. Against the previous library the first three fail with a 500, and the fourth does so with json 3.1. ## Breaking Changes None. A valid value is echoed as it came; a value that could not be serialized before is echoed with U+FFFD in place of its invalid bytes.
5 of 9 tasks
atesgoral
approved these changes
Nov 6, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
The Streamable HTTP transport echoes a client-supplied value into some of its error bodies: the
requestedmember of the-32022Unsupported protocol version error, theMCP-Protocol-Version,Mcp-MethodandMcp-Namemismatch messages of-32020, and the legacy unsupported-version message. A header value arrives as ASCII-8BIT from a Rack server or tagged UTF-8 from a test request, and a body value comes out of the JSON parser, which passes bytes that are not valid UTF-8 through. Such a value makesto_jsonraise while the 400 is being built, so the rescue answers 500 and calls the exception reporter for a request the transport meant to refuse with 400 and a specific code, and any client can repeat that per request. With json 3.1 the same happens to an ASCII-8BIT value holding any byte above 0x7F, valid UTF-8 included, so a Rack server answered 500 to a mismatching non-ASCIIMcp-Nameas well. The TypeScript server echoes the same values, but a JavaScript string cannot hold invalid UTF-8, so its response never fails.The echoed values now go through
printable_value, which tags the value UTF-8 and replaces bytes that are not valid UTF-8 with U+FFFD, so the 400 and its code reach the client. Therequestedmember stays, as the schema has it.Fixes #663.
How Has This Been Tested?
Four new tests in
test/mcp/server/transports/streamable_http_transport_test.rb: a requested version, anMcp-Methodand a base64Mcp-Nameheader, and a body envelope version, each holding an invalid byte, answer 400 with their code and the scrubbed value, and a non-ASCIIMcp-Methodgiven as bytes is echoed in its 400; the exception reporter stays silent in each. Against the previous library the first three fail with a 500, and the fourth does so with json 3.1.Breaking Changes
None. A valid value is echoed as it came; a value that could not be serialized before is echoed with U+FFFD in place of its invalid bytes.
Types of changes
Checklist