Skip to content

Validate Required Params for resources/read - #687

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
yuki3738:validate_required_resources_read_params
Nov 15, 2026
Merged

koic merged 1 commit into
modelcontextprotocol:mainfrom
yuki3738:validate_required_resources_read_params

Conversation

@yuki3738

Copy link
Copy Markdown
Contributor

Motivation and Context

The MCP schema requires a string uri in resources/read params, but the server dispatched the request without checking it, so the outcome depended on how the read was served:

  • A custom resources_read_handler received the params unchanged. A handler that echoes params[:uri] answered successfully with a null or non-string uri.
  • With a class-based resource template registered, a non-string uri such as 123 raised a TypeError, which surfaced as -32603 Internal error.
  • Absent, null, or array params surfaced as -32603 from the class-based router or from a handler that reads them.

These requests now return -32602 Invalid params before dispatch, matching the check #451 added for initialize and resources/subscribe. tools/call and prompts/get already return -32602 when name is missing. The check covers custom handlers and the class-based router, on both the initialize-based lifecycle and the 2026-07-28 _meta envelope lifecycle. Requests that already answered -32602 with Resource not found, such as a null URI on the class-based router, now report Invalid params.

URI syntax is not validated. That needs a separate change, weighing what to reject against the risk to existing clients.

How Has This Been Tested?

Added tests that absent or array params, and an absent, null, or non-string uri, return -32602 across the custom handler, class-based resource, and class-based template paths. Further tests assert that a custom handler never runs when uri is missing or is not a string, including on a 2026-07-28 request.

rake test passes (2694 runs, 0 failures).

Breaking Changes

Over JSON-RPC, only requests that violate the MCP schema change: they now receive -32602 Invalid params instead of a success, -32603, or Resource not found. Calling Server#handle from Ruby with string-keyed params such as { "uri" => "file:///a" } is now refused, as the class-based router and the resources/subscribe check already expect symbol keys. Server#handle_json symbolizes keys, so this key-type restriction does not affect requests sent as JSON.

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

The MCP schema requires a string `uri` in `resources/read` params, but the server dispatched the request
without checking it, so the outcome depended on how the read was served:

- A custom `resources_read_handler` received the params unchanged. A handler that echoes `params[:uri]`
  answered successfully with a `null` or non-string `uri`.
- With a class-based resource template registered, a non-string `uri` such as `123` raised a `TypeError`,
  which surfaced as `-32603` Internal error.
- Absent, `null`, or array `params` surfaced as `-32603` from the class-based router or from a handler that
  reads them.

These requests now return `-32602` Invalid params before dispatch, matching the check modelcontextprotocol#451 added for
`initialize` and `resources/subscribe`. `tools/call` and `prompts/get` already return `-32602` when `name` is
missing. The check covers custom handlers and the class-based router, on both the `initialize`-based lifecycle
and the 2026-07-28 `_meta` envelope lifecycle. Requests that already answered `-32602` with
`Resource not found`, such as a `null` URI on the class-based router, now report `Invalid params`.

URI syntax is not validated. That needs a separate change, weighing what to reject against the risk to
existing clients.

## How Has This Been Tested?

Added tests that absent or array `params`, and an absent, `null`, or non-string `uri`, return `-32602` across
the custom handler, class-based resource, and class-based template paths. Further tests assert that a custom
handler never runs when `uri` is missing or is not a string, including on a 2026-07-28 request.

## Breaking Changes

Over JSON-RPC, only requests that violate the MCP schema change: they now receive `-32602` Invalid params
instead of a success, `-32603`, or `Resource not found`. Calling `Server#handle` from Ruby with string-keyed
params such as `{ "uri" => "file:///a" }` is now refused, as the class-based router and the
`resources/subscribe` check already expect symbol keys. `Server#handle_json` symbolizes keys, so this key-type
restriction does not affect requests sent as JSON.
@koic
koic merged commit 96222fc into modelcontextprotocol:main Nov 15, 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.

2 participants