Skip to content

feat: add_resource_template to mcpserver and resource manager#2774

Open
mplemay wants to merge 5 commits into
modelcontextprotocol:mainfrom
mplemay:resource-templates
Open

feat: add_resource_template to mcpserver and resource manager#2774
mplemay wants to merge 5 commits into
modelcontextprotocol:mainfrom
mplemay:resource-templates

Conversation

@mplemay
Copy link
Copy Markdown
Contributor

@mplemay mplemay commented Jun 3, 2026

Summary

Enables registering pre-built ResourceTemplate instances at init or runtime, parallel to add_resource.

Adds ResourceManager.add_resource_template(), MCPServer.add_resource_template(), and an optional resource_templates constructor argument on both. add_template() now delegates to add_resource_template() after building the template from a function.

Motivation and Context

MCPServer already supported static resources via add_resource() and function-based templates via @server.resource(...), but there was no way to register a pre-built ResourceTemplate (for example one created with ResourceTemplate.from_function() elsewhere, or assembled manually). That blocked use cases where templates are defined outside the decorator flow or composed in library code.

This change mirrors the existing add_resource / resources=[...] pattern so custom templates can be seeded at construction or added later.

How Has This Been Tested?

  • tests/server/mcpserver/resources/test_resource_manager.py: init with resource_templates, add_resource_template, duplicate handling and warnings (aligned with resource tests)
  • tests/server/mcpserver/test_server.py: test_init_with_resource_templates and test_add_resource_template using in-memory Client(mcp) — list templates and read a templated URI end-to-end

All of the above pass locally via uv run --frozen pytest on the touched test files.

Breaking Changes

None. Existing @server.resource(...) and add_resource() behavior is unchanged.

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

Example usage:

from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.resources import ResourceTemplate

template = ResourceTemplate.from_function(
    fn=get_weather,
    uri_template="weather://{city}",
    name="weather",
)

mcp = MCPServer(resource_templates=[template])
# or: mcp.add_resource_template(template)

Duplicate templates are keyed by uri_template and use the same warn_on_duplicate_resources setting as concrete resources.

mplemay and others added 5 commits June 3, 2026 10:29
Enables registering pre-built ResourceTemplate instances at init or runtime, parallel to add_resource.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
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