Skip to content

feat(mcpserver): add add_resource_template() for programmatic template registration - #3348

Closed
pasmud wants to merge 1 commit into
modelcontextprotocol:mainfrom
pasmud:gaz/mcp-python-sdk
Closed

feat(mcpserver): add add_resource_template() for programmatic template registration#3348
pasmud wants to merge 1 commit into
modelcontextprotocol:mainfrom
pasmud:gaz/mcp-python-sdk

Conversation

@pasmud

@pasmud pasmud commented Aug 21, 2026

Copy link
Copy Markdown

Closes #3333

Summary

Adds add_resource_template() to both ResourceManager and MCPServer so a ResourceTemplate instance can be registered programmatically, without going through the @resource decorator.

server = MCPServer("...")

# Configure resources in separate modules
server.add_resource_template(
    ResourceTemplate.from_function(users.list_users, "resource://users/{id}")
)

ResourceManager.add_resource_template() mirrors add_resource() semantics: registering a template whose uri_template already exists logs a warning (respecting warn_on_duplicate_resources) and returns the existing template. add_template() now delegates to the new method so there's a single registration path.

Changes

  • src/mcp/server/mcpserver/resources/resource_manager.py: new add_resource_template(); add_template() delegates to it
  • src/mcp/server/mcpserver/server.py: new thin add_resource_template() on MCPServer
  • docs/servers/resources.md: document programmatic template registration
  • Tests: registration, duplicate handling (warn + return existing, opt-out), template resolution through get_resource, and listing via list_resource_templates()

Verification

  • ruff format / ruff check / pyright: clean
  • ./scripts/test: 5747 passed, 8 skipped, 1 xfailed; coverage 100%. (The single failure, test_permission_error, is a root-user environment artifact that also fails on origin/main here; it passes in CI.)
  • pre-commit: all hooks pass

Types of changes

  • New feature (non-breaking change which adds functionality)

…e registration

Add add_resource_template() to both ResourceManager and MCPServer so
ResourceTemplate instances can be registered directly, without the
@resource decorator. Duplicate uri_template registrations warn and
return the existing template, mirroring add_resource() semantics.
add_template() now delegates to the new method.
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Aug 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the contribution. This repository only keeps pull requests open when they're linked to an issue that a maintainer has assigned to the author — CONTRIBUTING.md explains why and how we work. This PR has been closed for now because you aren't currently assigned to #3333.

If a maintainer would like this change as a PR from you, they'll assign you to #3333 and this PR will reopen automatically — there's nothing more you need to do. (If you opened the issue, this PR already shows up on its timeline.)

There's no need to open a new PR — this one will be reopened. While it's closed, please push any updates as new commits rather than force-pushing, since GitHub can't reopen a PR whose branch has been rewritten.

Maintainers: reopening this PR, removing the missing-issue-link label, or adding bypass-issue-check bypasses the check.

@github-actions github-actions Bot closed this Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add add_resource_template() to MCPServer

1 participant