Skip to content

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

Keep disabled resources from being served by matching templates (#5876)

nate nowack49858c3239ef
diff --git a/fastmcp_slim/fastmcp/server/server.py b/fastmcp_slim/fastmcp/server/server.py
index 3cb0ff2d8..5cd90910d 100644
--- a/fastmcp_slim/fastmcp/server/server.py
+++ b/fastmcp_slim/fastmcp/server/server.py
@@ -1064,17 +1064,28 @@ class FastMCP(
Returns:
The resource if found and enabled, None otherwise.
"""
+ resource, _ = await self._lookup_resource(uri, version)
+ return resource
+
+ async def _lookup_resource(
+ self, uri: str, version: VersionSpec | None = None
+ ) -> tuple[Resource | None, bool]:
+ """Resolve a resource as `get_resource` does.
+
+ Also reports whether a concrete resource exists at `uri` at all, so
+ reads can tell a hidden resource apart from a missing one.
+ """
resource = await super().get_resource(uri, version)
if resource is None:
- return None
+ return None, False
# Apply session transforms to single item
resources = await apply_session_transforms([resource])
if resources and is_enabled(resources[0]):
- return resources[0]
+ return resources[0], True
if version is not None:
- return None
+ return None, True
all_resources = [r for r in await super().list_resources() if str(r.uri) == uri]
all_resources = list(await apply_session_transforms(all_resources))
@@ -1093,8 +1104,8 @@ class FastMCP(
authorized.append(r)
if not authorized:
- return None
- return max(authorized, key=version_sort_key)
+ return None, True
+ return max(authorized, key=version_sort_key), True
async def list_resource_templates(
self, *, run_middleware: bool = True
@@ -1613,7 +1624,7 @@ class FastMCP(
return await synthesized._read()
# Try concrete resources first (transforms + auth via _get_resource)
- resource = await self.get_resource(uri, version=version)
+ resource, exists = await self._lookup_resource(uri, version=version)
if resource is not None:
span.set_attributes(resource.get_span_attributes())
try:
@@ -1648,6 +1659,12 @@ class FastMCP(
f"Error reading resource {uri!r}: {e}"
) from e
+ # A hidden concrete resource shadows templates for its URI.
+ # Otherwise a matching template, such as a proxy's, would
+ # serve the resource that was hidden.
+ if exists:
+ raise NotFoundError(f"Unknown resource: {uri!r}")
+
# Try templates (transforms + auth via get_resource_template)
template = await self.get_resource_template(uri, version=version)
if template is None:
diff --git a/tests/server/providers/test_local_provider_resources.py b/tests/server/providers/test_local_provider_resources.py
index 0853f4cda..d3e58cf8d 100644
--- a/tests/server/providers/test_local_provider_resources.py
+++ b/tests/server/providers/test_local_provider_resources.py
@@ -13,13 +13,14 @@ from mcp_types import TextResourceContents
from pydantic import AnyUrl
from fastmcp import Client, Context, FastMCP
-from fastmcp.exceptions import NotFoundError
+from fastmcp.exceptions import FastMCPError, NotFoundError
from fastmcp.resources import (
Resource,
ResourceContent,
ResourceResult,
ResourceTemplate,
)
+from fastmcp.server import create_proxy
class TestResourceContext:
@@ -834,6 +835,63 @@ class TestResourceEnabled:
with pytest.raises(NotFoundError, match="Unknown resource"):
await mcp.read_resource("resource://data")
+ async def test_disabled_resource_is_not_served_by_matching_template(self):
+ mcp = FastMCP()
+
+ @mcp.resource("resource://{name}")
+ def by_name(name: str) -> str:
+ return f"template {name}"
+
+ @mcp.resource("resource://data")
+ def sample_resource() -> str:
+ return "Hello, world!"
+
+ mcp.disable(names={"resource://data"}, components={"resource"})
+
+ with pytest.raises(NotFoundError, match="Unknown resource"):
+ await mcp.read_resource("resource://data")
+ result = await mcp.read_resource("resource://other")
+ assert result.contents[0].content == "template other"
+
+ async def test_disabled_resource_is_not_served_through_proxy_template(self):
+ backend = FastMCP("backend")
+
+ @backend.resource("resource://{name}")
+ def by_name(name: str) -> str:
+ return f"template {name}"
+
+ @backend.resource("resource://data")
+ def sample_resource() -> str:
+ return "Hello, world!"
+
+ gateway = FastMCP("gateway")
+ gateway.mount(create_proxy(backend))
+ gateway.disable(names={"resource://data"}, components={"resource"})
+
+ with pytest.raises(NotFoundError, match="Unknown resource"):
+ await gateway.read_resource("resource://data")
+
+ async def test_disabled_resource_in_mounted_server_is_not_served_by_template(
+ self,
+ ):
+ child = FastMCP("child")
+
+ @child.resource("resource://{name}")
+ def by_name(name: str) -> str:
+ return f"template {name}"
+
+ @child.resource("resource://data")
+ def sample_resource() -> str:
+ return "Hello, world!"
+
+ child.disable(names={"resource://data"}, components={"resource"})
+ parent = FastMCP("parent")
+ parent.mount(child)
+
+ # The parent surfaces the child's NotFoundError as a ResourceError
+ with pytest.raises(FastMCPError, match="Unknown resource"):
+ await parent.read_resource("resource://data")
+
class TestResourceTemplatesTags:
def create_server(self, include_tags=None, exclude_tags=None):