Repository navigation
fix: handle malformed JSON-RPC errors in the client - #729
Merged
koic merged 1 commit intoDec 1, 2026
Merged
Conversation
## Motivation and Context `MCP::Client#request` and the `connect_legacy` and `probe_discover` handshakes of `Client::HTTP` and `Client::Stdio` read `error["message"]` without checking that `error` is an object. A `null`, boolean, array or number raises a Ruby exception out of the client, and a malformed answer to `server/discover` stops `connect(mode: :auto)` from falling back to `initialize`. Each of the five reads now checks for an object first. Requests raise `ServerError` (with no code or data), and handshakes raise `RequestHandlerError`, which the automatic mode already handles. Errors that are objects are unchanged. ## How Has This Been Tested? - `test/mcp/client_test.rb`: `ping` raises `ServerError` for `null`, `false`, an array, a number and a string. - `test/mcp/client/http_test.rb` and `test/mcp/client/stdio_test.rb`: the legacy and the modern handshake raise `RequestHandlerError`, and automatic mode falls back to the legacy handshake. The five new tests fail on `main`. ## Breaking Changes None for servers that send an object. Code that rescued `NoMethodError` or `TypeError` around a malformed error now gets `ServerError` or `RequestHandlerError`. Fixes modelcontextprotocol#726.
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.
A JSON-RPC
errorthat is not an object now raises the SDK's own exceptions instead ofNoMethodErrororTypeError.Fixes #726
Motivation and Context
MCP::Client#requestand theconnect_legacyandprobe_discoverhandshakes ofClient::HTTPandClient::Stdioreaderror["message"]without checking thaterroris an object. Anull, boolean, array or number raises a Ruby exception out of the client, and a malformed answer toserver/discoverstopsconnect(mode: :auto)from falling back toinitialize.Each of the five reads now checks for an object first. Requests raise
ServerError(with no code or data), and handshakes raiseRequestHandlerError, which the automatic mode already handles. Errors that are objects are unchanged.How Has This Been Tested?
Automated unit / integration tests
Tested manually
Tested in a real application
test/mcp/client_test.rb:pingraisesServerErrorfornull,false, an array, a number and a string.test/mcp/client/http_test.rbandtest/mcp/client/stdio_test.rb: the legacy and the modern handshake raiseRequestHandlerError, and automatic mode falls back to the legacy handshake.The five new tests fail on
main.bundle exec rake test(2,813 tests, 10,231 assertions on Ruby 4.0.9; also run on 2.7),bundle exec rake rubocop,bundle exec yard --no-outputandbundle exec rake conformance:test(baseline) pass locally.Breaking Changes
None for servers that send an object. Code that rescued
NoMethodErrororTypeErroraround a malformed error now getsServerErrororRequestHandlerError.Types of changes
Checklist
Additional context
#716 covers the server transports reading a client's reply and does not overlap.