Skip to content

Support class-based Resource and ResourceTemplate definitions#447

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

Support class-based Resource and ResourceTemplate definitions#447
koic wants to merge 1 commit into
modelcontextprotocol:mainfrom
koic:support_class_based_resource_definitions

Conversation

@koic

@koic koic commented Jul 11, 2026

Copy link
Copy Markdown
Member

Motivation and Context

Resolve the design difference raised in issue #98: Tools and Prompts support class-based definitions while Resources and Resource Templates were plain data objects requiring a resources_read_handler block with manual URI routing.

MCP::Resource and MCP::ResourceTemplate now support the same class-level DSL as MCP::Tool and MCP::Prompt (uri, resource_name, title, description, icons, mime_type, annotations, size, meta, and uri_template / resource_template_name for templates), plus define factory methods and MCP::Server#define_resource / #define_resource_template.

The default resources/read handler now routes requests automatically: an exact URI match calls the class-based resource's contents method, and class-based resource templates match simple RFC 6570 level 1 {var} expressions, passing extracted variables as keyword arguments. contents can opt in to a server_context: keyword argument. When class-based entries are registered and no match is found, the server responds with -32602 carrying the URI in error data per SEP-2164.

How Has This Been Tested?

  • Added regression tests covering the class-level DSL, URI template matching, auto-routing, server_context: opt-in, error responses, and backward compatibility of the no-op default.
  • Verified end-to-end with a smoke script exercising exact-match reads, template-match reads, unknown URIs, and legacy instance-only setups.

Breaking Changes

No. Instance-based registration, resources_read_handler precedence, and the no-op [] response for setups without class-based entries are unchanged. Reassigning Server#resources now rebuilds the internal URI index.

Closes #98

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

## Motivation and Context

Resolve the design difference raised in issue modelcontextprotocol#98: Tools and Prompts support class-based definitions
while Resources and Resource Templates were plain data objects requiring a `resources_read_handler` block
with manual URI routing.

`MCP::Resource` and `MCP::ResourceTemplate` now support the same class-level DSL as `MCP::Tool`
and `MCP::Prompt` (`uri`, `resource_name`, `title`, `description`, `icons`, `mime_type`, `annotations`,
`size`, `meta`, and `uri_template` / `resource_template_name` for templates), plus `define` factory methods
and `MCP::Server#define_resource` / `#define_resource_template`.

The default `resources/read` handler now routes requests automatically: an exact URI match calls
the class-based resource's `contents` method, and class-based resource templates match
simple RFC 6570 level 1 `{var}` expressions, passing extracted variables as keyword arguments.
`contents` can opt in to a `server_context:` keyword argument. When class-based entries are registered
and no match is found, the server responds with -32602 carrying the URI in error data per SEP-2164.

## How Has This Been Tested?

- Added regression tests covering the class-level DSL, URI template matching, auto-routing,
  `server_context:` opt-in, error responses, and backward compatibility of the no-op default.
- Verified end-to-end with a smoke script exercising exact-match reads, template-match reads,
  unknown URIs, and legacy instance-only setups.

## Breaking Changes

No. Instance-based registration, `resources_read_handler` precedence, and the no-op `[]` response for
setups without class-based entries are unchanged. Reassigning `Server#resources` now rebuilds
the internal URI index.

Closes modelcontextprotocol#98
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.

Design differences between Tools/Prompts vs Resources

1 participant