Skip to content

Fix client methods silently swallowing JSON-RPC error responses#281

Merged
koic merged 1 commit intomodelcontextprotocol:mainfrom
koic:client_raise_on_error_response
Mar 31, 2026
Merged

Fix client methods silently swallowing JSON-RPC error responses#281
koic merged 1 commit intomodelcontextprotocol:mainfrom
koic:client_raise_on_error_response

Conversation

@koic
Copy link
Copy Markdown
Member

@koic koic commented Mar 31, 2026

Motivation and Context

Client methods (tools, resources, resource_templates, prompts, read_resource, get_prompt, call_tool) silently swallowed JSON-RPC error responses by falling back to empty defaults. This did not conform to JSON-RPC 2.0, which clearly distinguishes error responses from success responses. Both the Python SDK (MCPError) and TypeScript SDK (ProtocolError) raise exceptions on error responses. The Ruby SDK was the only one silently discarding them.

Introduces a common request method that handles JSON-RPC request construction and error checking in one place, replacing the duplicated transport.send_request calls across all client methods.

How Has This Been Tested?

Added tests for each affected method verifying that ServerError is raised with the correct code, message, and data attributes.

Breaking Changes

The previous behavior of silently returning empty defaults on server errors did not conform to JSON-RPC 2.0. Error responses should be surfaced to callers, not swallowed. Callers that relied on the silent fallback behavior will need to rescue MCP::Client::ServerError.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

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

## Motivation and Context

Client methods (`tools`, `resources`, `resource_templates`, `prompts`, `read_resource`, `get_prompt`, `call_tool`)
silently swallowed JSON-RPC error responses by falling back to empty defaults. This did not conform to JSON-RPC 2.0,
which clearly distinguishes error responses from success responses.
Both the Python SDK (`MCPError`) and TypeScript SDK (`ProtocolError`) raise exceptions on error responses.
The Ruby SDK was the only one silently discarding them.

Introduces a common `request` method that handles JSON-RPC request construction and error checking in one place,
replacing the duplicated `transport.send_request` calls across all client methods.

## How Has This Been Tested?

Added tests for each affected method verifying that `ServerError` is raised with the correct `code`, `message`,
and `data` attributes.

## Breaking Changes

The previous behavior of silently returning empty defaults on server errors did not conform to JSON-RPC 2.0.
Error responses should be surfaced to callers, not swallowed.
Callers that relied on the silent fallback behavior will need to rescue `MCP::Client::ServerError`.
@koic koic force-pushed the client_raise_on_error_response branch from 5202bab to 590feb1 Compare March 31, 2026 03:51
@koic koic merged commit 11f2e05 into modelcontextprotocol:main Mar 31, 2026
11 checks passed
@koic koic deleted the client_raise_on_error_response branch March 31, 2026 06:14
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.

2 participants