Repository navigation
Keep the error code of a refusal that quotes bytes that are not valid UTF-8 - #734
Open
koic wants to merge 1 commit into
Conversation
koic
force-pushed
the
keep_the_error_code_of_a_refusal_that_quotes_bytes_that_are_not_utf_8
branch
2 times, most recently
from
December 2, 2026 00:47
65f0a23 to
274fb3c
Compare
… UTF-8 ## Motivation and Context The text of a `RequestHandlerError` often quotes what the client sent: "Tool not found: <name>", "Invalid log level <level>". The JSON parser passes bytes that are not valid UTF-8 through, and generating the response from such a text raises, so `JsonRpcHandler` fell back to its replacement for a response it cannot generate. A `tools/call` naming a tool with such bytes on a legacy session was answered `-32603` Internal error where an unknown tool gets `-32602` Invalid params, and the generation error went to the exception reporter as if the server had failed. `JsonRpcHandler` now replaces those bytes with U+FFFD in the `message` and `data` of a request handler error, as the Streamable HTTP transport does for the values its own refusals quote, so the error keeps its code and its text. The texts inside a structured `data` are treated the same way, keys included, such as the `uri` that `ResourceNotFoundError` carries. A `data` that holds itself is left as it is, and so is whatever lies deeper than `JSON.generate` writes, so a `data` that cannot be written still fails in the generator at once, as it did. ## How Has This Been Tested? `test/json_rpc_handler_test.rb`. Against the previous library the tests of bytes that are not valid UTF-8, in the message and in `data`, get `-32603` and a `JSON::GeneratorError` report, and the tests of a text in another encoding get that text back untranscoded. Without the check for a `data` that holds itself, the test of such a `data` ends in `SystemStackError`, and with a bound on the depth alone, or a check along one path at a time, it does not end within its time limit. ## Breaking Changes None. A valid UTF-8 text comes through unchanged, and a text valid in another encoding is transcoded to UTF-8 where Ruby has a converter for it.
koic
force-pushed
the
keep_the_error_code_of_a_refusal_that_quotes_bytes_that_are_not_utf_8
branch
from
December 2, 2026 00:51
274fb3c to
b663d52
Compare
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 text of a
RequestHandlerErroroften quotes what the client sent: "Tool not found: ", "Invalid log level ". The JSON parser passes bytes that are not valid UTF-8 through, and generating the response from such a text raises, soJsonRpcHandlerfell back to its replacement for a response it cannot generate. Atools/callnaming a tool with such bytes on a legacy session was answered-32603Internal error where an unknown tool gets-32602Invalid params, and the generation error went to the exception reporter as if the server had failed.JsonRpcHandlernow replaces those bytes with U+FFFD in themessageanddataof a request handler error, as the Streamable HTTP transport does for the values its own refusals quote, so the error keeps its code and its text. The texts inside a structureddataare treated the same way, keys included, such as theurithatResourceNotFoundErrorcarries.How Has This Been Tested?
test/json_rpc_handler_test.rb. Against the previous library the tests of bytes that are not valid UTF-8, in the message and indata, get-32603and aJSON::GeneratorErrorreport, and the tests of a text in another encoding get that text back untranscoded.Breaking Changes
None. A valid UTF-8 text comes through unchanged, and a text valid in another encoding is transcoded to UTF-8 where Ruby has a converter for it.
Types of changes
Checklist