Support class-based Resource and ResourceTemplate definitions#447
Open
koic wants to merge 1 commit into
Open
Conversation
## 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
9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_handlerblock with manual URI routing.MCP::ResourceandMCP::ResourceTemplatenow support the same class-level DSL asMCP::ToolandMCP::Prompt(uri,resource_name,title,description,icons,mime_type,annotations,size,meta, anduri_template/resource_template_namefor templates), plusdefinefactory methods andMCP::Server#define_resource/#define_resource_template.The default
resources/readhandler now routes requests automatically: an exact URI match calls the class-based resource'scontentsmethod, and class-based resource templates match simple RFC 6570 level 1{var}expressions, passing extracted variables as keyword arguments.contentscan opt in to aserver_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?
server_context:opt-in, error responses, and backward compatibility of the no-op default.Breaking Changes
No. Instance-based registration,
resources_read_handlerprecedence, and the no-op[]response for setups without class-based entries are unchanged. ReassigningServer#resourcesnow rebuilds the internal URI index.Closes #98
Types of changes