Skip to content

[DRAFT] feat: #502 resource templates#604

Closed
kpavlov wants to merge 6 commits intomainfrom
kpavlov/502-resource-temlates
Closed

[DRAFT] feat: #502 resource templates#604
kpavlov wants to merge 6 commits intomainfrom
kpavlov/502-resource-temlates

Conversation

@kpavlov
Copy link
Copy Markdown
Contributor

@kpavlov kpavlov commented Mar 16, 2026

Motivation and Context

Implements MCP resource templates per the MCP 2025-11-25
specification
.

Closing #502. Requires #603

Clients can now:

  • Discover parameterized resource patterns via resources/templates/list
  • Read any URI that matches a registered RFC 6570 template via resources/read

Changes

kotlin-sdk-server

  • Server.addResourceTemplate(template, handler) — registers a ResourceTemplate with a two-parameter handler (ReadResourceRequest, Map<String, String>) -> ReadResourceResult, where the map contains the extracted URI variables.
  • Server.addResourceTemplate(uriTemplate, name, …, handler) — convenience overload.
  • Server.removeResourceTemplate(uriTemplate) — removes a registered template.
  • Server.resourceTemplates — snapshot of all registered templates.
  • handleReadResource now applies a three-tier lookup:
    1. Exact URI match against static resources (highest priority)
    2. Most-specific matching template (highest MatchResult.score from the RFC 6570 matcher)
    3. McpException(RESOURCE_NOT_FOUND) if nothing matches
  • handleListResourceTemplates now returns the actual registered templates instead of an empty list.
  • Template change notifications piggyback on resources/list_changed when that capability is enabled.

kotlin-sdk-core

  • RPCError.ErrorCode.RESOURCE_NOT_FOUND = -32002 — new SDK-defined error code for
    unknown resource URIs.
  • McpException.errorMessage — exposes the raw protocol message (without the
    "MCP error $code:" prefix) so Protocol can reconstruct the exact wire RPCError.
  • Protocol.onRequest — error handler now propagates McpException.code,
    errorMessage, and data to the wire instead of always emitting INTERNAL_ERROR.

Conformance / integration tests

  • conformance-baseline.yml: removes resources-templates-read from expected failures.
  • ConformanceResources.kt: replaces workaround static resource with a real
    addResourceTemplate registration.
  • AbstractResourceIntegrationTest.testInvalidResourceUri: updated to expect
    RESOURCE_NOT_FOUND (-32002) instead of INTERNAL_ERROR (-32603).

Breaking changes

handleReadResource now throws McpException(RESOURCE_NOT_FOUND) for unrecognised URIs instead of IllegalArgumentException. Clients receive error code -32002 on the wire instead of -32603. Any integration test asserting INTERNAL_ERROR for a missing resource must be updated (one such test is updated in this PR).

How Has This Been Tested?

  • unit/integration tests.
  • Existing integration and conformance tests pass with resources-templates-read removed from the baseline.

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

Additional context

kpavlov added 3 commits March 13, 2026 14:07
…ilities

Add `UriTemplate`, `UriTemplateMatcher`, and `MatchResult` classes to support [RFC-6570](https://www.rfc-editor.org/rfc/rfc6570.txt) URI template matching and expansion. Implemented unit tests for conformance with all expansion levels and path operators.
Introduce `ResourceTemplate` and `RegisteredResourceTemplate` implementation to register and manage parameterized resource templates. Added support for listing, reading, and removing resource templates. Updated error handling for unmatched URIs with a new `RESOURCE_NOT_FOUND` RPC error. Includes corresponding tests and API updates.
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 16, 2026

@kpavlov kpavlov added enhancement New feature or request api Public API changes labels Mar 16, 2026
kpavlov added 3 commits March 16, 2026 14:10
…lidation

Replaced `check(options.capabilities.* != null)` with `checkNotNull` for improved clarity and consistency in validating server capabilities.
…ssion

Add specific handling for `CancellationException` in `Protocol.kt` to ensure it is rethrown instead of being suppressed by the generic `Throwable` catch block.
@kpavlov kpavlov changed the title feat: #502 resource templates [DRAFT] feat: #502 resource templates Mar 16, 2026
@kpavlov kpavlov closed this Mar 19, 2026
@kpavlov kpavlov deleted the kpavlov/502-resource-temlates branch March 19, 2026 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Public API changes enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants