Aligns Resources and Resource Templates with the pattern used by Tools and Prompts, and introduces auto-routing.#217
Conversation
…ed Resources - Introduce `MCP::Primitive` to encapsulate common logic (name, title, description, icons, meta) for all MCP primitives. - Refactor `MCP::Tool`, `MCP::Resource`, `MCP::ResourceTemplate`, and `MCP::Prompt` to inherit from `MCP::Primitive`, reducing code duplication. - Enable class-based definitions for `MCP::Resource` and `MCP::ResourceTemplate`, aligning them with the `Tool` pattern. - Update `MCP::Server` to support registering class-based resources and templates. - Add tests for `Primitive`, `Resource`, and `ResourceTemplate` covering the new inheritance structure. - Update `README.md` with examples of the new class-based Resource and Resource Template APIs. - Preserve backward compatibility for instance-based creation of Resources and Templates.
|
Thank you for this PR and for the detailed write-up. The problem you identified is real, and this PR was a helpful reference during the design discussion for issue #98. I'm closing this one in favor of #447, which implements the same feature: class-based definitions for The Thanks again for your contribution! |
Aligns Resources and Resource Templates with the pattern used by Tools and Prompts, and introduces auto-routing.
Motivation and Context
Using Tools in this SDK was straightforward, but when I tried adding Resources, it was confusing. The interface is totally different, the documentation is sparse, and there are barely any examples.
Unlike Tools, the lack of support for encapsulating resources into dedicated classes, combined with the need for manual registration, made it difficult to organize code efficiently.
I checked the issues and found I'm not the only one with this problem (see #98).
How Has This Been Tested?
Breaking Changes
No. Backward compatibility is fully preserved.
Types of changes
Checklist
Additional context
While researching the MCP documentation, I noticed a reference to -32002 for "Resource not found" errors. I initially began refactoring JsonRpcHandler to centralize exception handling to support this. However, I suspect the documentation might be outdated and that the standard JSON-RPC error -32602 (Invalid params) is actually the correct implementation.
Since I am not completely sure which code is correct, I reverted the error handling refactor to keep this PR focused. Could you confirm if -32602 is the standard? I can submit a separate PR if a broader change to error codes is required.