Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Register templated MCPMixin resources as resource templates (#5895)

Asem ahmed0c9f5f7a2bb6
diff --git a/fastmcp_slim/fastmcp/contrib/mcp_mixin/mcp_mixin.py b/fastmcp_slim/fastmcp/contrib/mcp_mixin/mcp_mixin.py
index 460e169c8..cc0d62818 100644
--- a/fastmcp_slim/fastmcp/contrib/mcp_mixin/mcp_mixin.py
+++ b/fastmcp_slim/fastmcp/contrib/mcp_mixin/mcp_mixin.py
@@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Any
from fastmcp.prompts.base import Prompt
from fastmcp.resources.base import Resource
+from fastmcp.resources.template import ResourceTemplate
from fastmcp.tools.base import Tool
from fastmcp.utilities.types import get_fn_name
@@ -229,6 +230,9 @@ class MCPMixin:
) -> None:
"""Registers all methods marked with @mcp_resource with the FastMCP server.
+ A method whose URI contains `{param}` placeholders is registered as a
+ resource template; any other method is registered as a static resource.
+
Args:
mcp_server: The FastMCP server instance to register resources with.
prefix: Optional prefix to prepend to resource names and URIs. If
@@ -253,8 +257,17 @@ class MCPMixin:
if enabled is False:
continue
- resource = Resource.from_function(fn=method, **registration_info)
- mcp_server.add_resource(resource)
+ uri = registration_info.pop("uri")
+ if "{" in uri and "}" in uri:
+ template = ResourceTemplate.from_function(
+ fn=method, uri_template=uri, **registration_info
+ )
+ mcp_server.add_template(template)
+ else:
+ resource = Resource.from_function(
+ fn=method, uri=uri, **registration_info
+ )
+ mcp_server.add_resource(resource)
def register_prompts(
self,
diff --git a/tests/contrib/test_mcp_mixin.py b/tests/contrib/test_mcp_mixin.py
index 972bf6795..c58791291 100644
--- a/tests/contrib/test_mcp_mixin.py
+++ b/tests/contrib/test_mcp_mixin.py
@@ -4,7 +4,7 @@ import inspect
import pytest
-from fastmcp import FastMCP
+from fastmcp import Client, FastMCP
from fastmcp.contrib.mcp_mixin import (
MCPMixin,
mcp_prompt,
@@ -600,3 +600,117 @@ class TestMCPMixinNewParams:
# list_prompts() filters by auth context; check internal provider directly
prompts = await mcp.local_provider.list_prompts()
assert any(p.name == "secure_prompt" for p in prompts)
+
+
+class TestMCPMixinResourceTemplates:
+ """A URI with `{param}` placeholders registers a resource template."""
+
+ @pytest.mark.parametrize(
+ "prefix, separator, expected_template, expected_name, expected_static, read_uri",
+ [
+ (
+ None,
+ _DEFAULT_SEPARATOR_RESOURCE,
+ "users://{user_id}/profile",
+ "profile",
+ "users://all",
+ "users://42/profile",
+ ),
+ (
+ "pref",
+ _DEFAULT_SEPARATOR_RESOURCE,
+ f"pref{_DEFAULT_SEPARATOR_RESOURCE}users://{{user_id}}/profile",
+ f"pref{_DEFAULT_SEPARATOR_RESOURCE}profile",
+ f"pref{_DEFAULT_SEPARATOR_RESOURCE}users://all",
+ f"pref{_DEFAULT_SEPARATOR_RESOURCE}users://42/profile",
+ ),
+ (
+ "pref",
+ "-",
+ "pref-users://{user_id}/profile",
+ "pref-profile",
+ "pref-users://all",
+ "pref-users://42/profile",
+ ),
+ ],
+ ids=["No prefix", "Default separator", "Custom separator"],
+ )
+ async def test_templated_uri_registers_template(
+ self,
+ prefix,
+ separator,
+ expected_template,
+ expected_name,
+ expected_static,
+ read_uri,
+ ):
+ mcp = FastMCP()
+
+ class MyMixin(MCPMixin):
+ @mcp_resource(uri="users://{user_id}/profile")
+ def profile(self, user_id: str) -> str:
+ return f"profile of {user_id}"
+
+ @mcp_resource(uri="users://all")
+ def everyone(self) -> str:
+ return "everyone"
+
+ MyMixin().register_resources(mcp, prefix=prefix, separator=separator)
+
+ async with Client(mcp) as client:
+ templates = await client.list_resource_templates()
+ assert [(t.uri_template, t.name) for t in templates] == [
+ (expected_template, expected_name)
+ ]
+
+ # The templated method is not also listed as a static resource.
+ resources = await client.list_resources()
+ assert [str(r.uri) for r in resources] == [expected_static]
+
+ result = await client.read_resource(read_uri)
+ assert len(result) == 1
+ assert result[0].text == "profile of 42"
+
+ async def test_template_enabled_false_skips_registration(self):
+ mcp = FastMCP()
+
+ class MyMixin(MCPMixin):
+ @mcp_resource(uri="test://{item}/hidden", enabled=False)
+ def hidden(self, item: str) -> str:
+ return item
+
+ @mcp_resource(uri="test://{item}/visible")
+ def visible(self, item: str) -> str:
+ return item
+
+ MyMixin().register_resources(mcp)
+
+ async with Client(mcp) as client:
+ templates = await client.list_resource_templates()
+ assert [t.uri_template for t in templates] == ["test://{item}/visible"]
+
+ async def test_template_params_forwarded(self):
+ mcp = FastMCP()
+
+ class MyMixin(MCPMixin):
+ @mcp_resource(
+ uri="test://{item}",
+ title="Item Title",
+ description="An item",
+ mime_type="application/json",
+ tags={"data"},
+ meta={"category": "data"},
+ )
+ def item(self, item: str) -> str:
+ return item
+
+ MyMixin().register_resources(mcp)
+
+ templates = await mcp.local_provider.list_resource_templates()
+ assert len(templates) == 1
+ template = templates[0]
+ assert template.title == "Item Title"
+ assert template.description == "An item"
+ assert template.mime_type == "application/json"
+ assert template.tags == {"data"}
+ assert template.meta == {"category": "data"}