Skip to content

fix: handle malformed JSON-RPC errors in the client - #729

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/client-malformed-error-response
Dec 1, 2026
Merged

koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/client-malformed-error-response

Conversation

@yorzi

@yorzi yorzi commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

A JSON-RPC error that is not an object now raises the SDK's own exceptions instead of NoMethodError or TypeError.

Fixes #726

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?

  • Automated unit / integration tests

  • Tested manually

  • Tested in a real application

  • 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. 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-output and bundle exec rake conformance:test (baseline) pass locally.

Breaking Changes

None for servers that send an object. Code that rescued NoMethodError or TypeError around a malformed error now gets ServerError or RequestHandlerError.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change that 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 (none needed)

Additional context

#716 covers the server transports reading a client's reply and does not overlap.

## 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.
@koic
koic merged commit c5d8556 into modelcontextprotocol:main Dec 1, 2026
13 checks passed
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.

Client raises NoMethodError or TypeError when a JSON-RPC error is not an object

2 participants