Skip to content

Refuse a resource registered without a member the spec requires - #711

Open
koic wants to merge 1 commit into
modelcontextprotocol:mainfrom
koic:refuse_a_resource_registered_without_a_member_the_spec_requires
Open

koic wants to merge 1 commit into
modelcontextprotocol:mainfrom
koic:refuse_a_resource_registered_without_a_member_the_spec_requires

Conversation

@koic

@koic koic commented Nov 22, 2026

Copy link
Copy Markdown
Member

Motivation and Context

MCP::Resource#to_h and MCP::ResourceTemplate#to_h leave out the members that were not given. A resource registered without a uri or a name (a class-based resource that sets no uri, an anonymous class that sets no resource_name, Resource.new(uri: nil, ...)) was therefore accepted and then listed without that member, which the spec requires of every Resource, and a template without uriTemplate or name likewise. The server learned nothing of it, and a client that validates resources/list rejects the whole result for the one entry.

MCP::Server now refuses such a definition where it is registered: in the constructor, resources=, resource_templates=, define_resource and define_resource_template. The ArgumentError names the missing member and what the definition does have. resource_templates= becomes a method of its own for this; it was an attribute writer.

A resource a resources_list_handler returns is not checked, since it is produced per request, and neither is a resource that is never registered or one appended to the Array resources returns, which was never a way to register one (it is not indexed for resources/read either).

How Has This Been Tested?

test/mcp/server_resource_registration_test.rb; on the previous library the incomplete definitions are accepted.

Breaking Changes

A server that registers a resource without a uri or a name, or a resource template without a uri_template or a name, now raises ArgumentError when it does, where it used to start and list the entry without the member. The check serializes each definition, so one whose to_h raises now does so when it is registered, and resource_templates= needs something it can iterate.

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

`MCP::Resource#to_h` and `MCP::ResourceTemplate#to_h` leave out the members that were not given. A resource
registered without a `uri` or a `name` (a class-based resource that sets no `uri`, an anonymous class that sets no
`resource_name`, `Resource.new(uri: nil, ...)`) was therefore accepted and then listed without that member,
which the spec requires of every `Resource`, and a template without `uriTemplate` or `name` likewise.
The server learned nothing of it, and a client that validates `resources/list` rejects the whole result
for the one entry.

`MCP::Server` now refuses such a definition where it is registered: in the constructor, `resources=`,
`resource_templates=`, `define_resource` and `define_resource_template`. The `ArgumentError` names
the missing member and what the definition does have. `resource_templates=` becomes a method of its own for this;
it was an attribute writer.

A resource a `resources_list_handler` returns is not checked, since it is produced per request, and neither is
a resource that is never registered or one appended to the Array `resources` returns, which was never a way to
register one (it is not indexed for `resources/read` either).

## How Has This Been Tested?

`test/mcp/server_resource_registration_test.rb`; on the previous library the incomplete definitions are accepted.

## Breaking Changes

A server that registers a resource without a `uri` or a `name`, or a resource template without a `uri_template` or
a `name`, now raises `ArgumentError` when it does, where it used to start and list the entry without the member.
The check serializes each definition, so one whose `to_h` raises now does so when it is registered, and
`resource_templates=` needs something it can iterate.

This branch has not been deployed

No deployments
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.

1 participant