Skip to content

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
modelcontextprotocol:mainfrom
koic:keep_the_error_code_of_a_refusal_that_quotes_bytes_that_are_not_utf_8
Open

koic wants to merge 1 commit into
modelcontextprotocol:mainfrom
koic:keep_the_error_code_of_a_refusal_that_quotes_bytes_that_are_not_utf_8

Conversation

@koic

@koic koic commented Dec 1, 2026

Copy link
Copy Markdown
Member

Motivation and Context

The text of a RequestHandlerError often 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, 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.

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.

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Test update (non-breaking change which adds or updates tests)
  • Refactoring (non-breaking change which makes things better)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • CI/build script update (update CI workflow or build scripts)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • I have added automated tests for my change
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

… 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.

## 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.

## 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant