Skip to content

fix: validate object results before reading client responses - #738

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/client-non-object-result-validation
Dec 1, 2026
Merged

koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/client-non-object-result-validation

Conversation

@yorzi

@yorzi yorzi commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

The client raises MCP::Client::ValidationError for a result that is not an object, instead of leaking Ruby errors or returning it.

Fixes #737

Motivation and Context

list_tools, list_resources, list_resource_templates, list_prompts, read_resource, get_prompt and complete index the result without checking it. A non-object one raises TypeError or NoMethodError, comes back from get_prompt, or reads as an empty page.

The seven methods now read it through one private helper, which raises the error discover and ping already raise. On a modern connection MCP::Client::HTTP reads the x-mcp-header declarations out of a tools/list body before that, so it now learns nothing from a non-object result and lets the client refuse it. Error responses and input_required results are handled before the check, as before.

How Has This Been Tested?

  • Automated unit / integration tests

  • Tested manually

  • Tested in a real application

  • test/mcp/client_test.rb: each method refuses array, string, number and boolean results, and a response that is not a Hash. Missing and null results keep the empty defaults.

  • test/mcp/client/http_test.rb: list_tools refuses the same results on a modern connection.

All nine tests fail on main. Reverting any one reader, the HTTP guard, or any part of the helper fails a test. bundle exec rake test (2,824 tests, 10,313 assertions), bundle exec rake rubocop, bundle exec yard --no-output and bundle exec rake conformance:test (baseline) pass locally on Ruby 4.0.9.

Breaking Changes

None for a server that sends object results. get_prompt returns {} instead of nil for a null result, as it already does for a missing one.

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 (none needed)

Raise the client validation error for malformed result payloads before
indexing them. Keep the empty defaults used for missing results. The
HTTP transport no longer indexes a non-object tools/list result while
learning x-mcp-header declarations, so the client can refuse it.

Fixes modelcontextprotocol#737.
@koic
koic merged commit e81639f into modelcontextprotocol:main Dec 1, 2026
20 of 23 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 helpers accept or misread a non-object result

2 participants