Skip to content

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

Fix session visibility lookups through mounted servers (#5966)

HootRocka0c04889b18d
diff --git a/docs/servers/visibility.mdx b/docs/servers/visibility.mdx
index 8ff71867c..ea3c2820e 100644
--- a/docs/servers/visibility.mdx
+++ b/docs/servers/visibility.mdx
@@ -339,6 +339,8 @@ mcp.disable(tags={"premium"})
All sessions start with `premium_analysis` hidden. When a session calls `unlock_premium`, that session gains access to premium tools while other sessions remain unaffected. Calling `reset_features` returns the session to the global defaults.
+For a mounted server, these rules use that server's session state store and its local component names. Components it enables can be listed and called through the parent, including through a namespace. The parent's visibility rules still apply to the components exposed by the mount.
+
### How Session Rules Work
Session rules override global transforms. When listing components, FastMCP first applies global enable/disable rules, then applies session-specific rules on top. Rules within a session accumulate, and later rules override earlier ones for the same component.
diff --git a/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py b/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py
index d71675b5c..197aa6520 100644
--- a/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py
+++ b/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py
@@ -448,14 +448,22 @@ class FastMCPProvider(Provider):
exact version matching and range filtering. Uses get_tool to ensure
the nested server's transforms are applied.
"""
- raw_tool = await self.server.get_tool(name, version)
+ # Lookups must use the child's state store, just as its list and
+ # execution methods do, before the parent applies its own rules.
+ from fastmcp.server.context import Context
+
+ async with Context(fastmcp=self.server):
+ raw_tool = await self.server.get_tool(name, version)
if raw_tool is None:
return None
return FastMCPProviderTool.wrap(self.server, raw_tool)
async def get_app_tool(self, app_name: str, tool_name: str) -> Tool | None:
"""Delegate to nested server's get_app_tool, wrapping for middleware."""
- raw_tool = await self.server.get_app_tool(app_name, tool_name)
+ from fastmcp.server.context import Context
+
+ async with Context(fastmcp=self.server):
+ raw_tool = await self.server.get_app_tool(app_name, tool_name)
if raw_tool is None:
return None
wrapped = FastMCPProviderTool.wrap(self.server, raw_tool)
@@ -472,7 +480,10 @@ class FastMCPProvider(Provider):
resolves the same tool again rather than whatever its listed name
reaches.
"""
- raw_tool = await self.server.get_tool_by_hash(tool_hash, tool_name)
+ from fastmcp.server.context import Context
+
+ async with Context(fastmcp=self.server):
+ raw_tool = await self.server.get_tool_by_hash(tool_hash, tool_name)
if raw_tool is None:
return None
wrapped = FastMCPProviderTool.wrap(self.server, raw_tool)
@@ -502,7 +513,10 @@ class FastMCPProvider(Provider):
exact version matching and range filtering. Uses get_resource to ensure
the nested server's transforms are applied.
"""
- raw_resource = await self.server.get_resource(uri, version)
+ from fastmcp.server.context import Context
+
+ async with Context(fastmcp=self.server):
+ raw_resource = await self.server.get_resource(uri, version)
if raw_resource is None:
return None
return FastMCPProviderResource.wrap(self.server, raw_resource)
@@ -532,7 +546,10 @@ class FastMCPProvider(Provider):
exact version matching and range filtering. Uses get_resource_template
to ensure the nested server's transforms are applied.
"""
- raw_template = await self.server.get_resource_template(uri, version)
+ from fastmcp.server.context import Context
+
+ async with Context(fastmcp=self.server):
+ raw_template = await self.server.get_resource_template(uri, version)
if raw_template is None:
return None
return FastMCPProviderResourceTemplate.wrap(self.server, raw_template)
@@ -560,7 +577,10 @@ class FastMCPProvider(Provider):
exact version matching and range filtering. Uses get_prompt to ensure
the nested server's transforms are applied.
"""
- raw_prompt = await self.server.get_prompt(name, version)
+ from fastmcp.server.context import Context
+
+ async with Context(fastmcp=self.server):
+ raw_prompt = await self.server.get_prompt(name, version)
if raw_prompt is None:
return None
return FastMCPProviderPrompt.wrap(self.server, raw_prompt)
diff --git a/tests/server/mount/test_session_visibility.py b/tests/server/mount/test_session_visibility.py
new file mode 100644
index 000000000..c850eef49
--- /dev/null
+++ b/tests/server/mount/test_session_visibility.py
@@ -0,0 +1,170 @@
+"""Mounted session visibility must agree between listing and dispatch."""
+
+from typing import Literal
+
+import pytest
+from mcp_types import TextContent, TextResourceContents
+
+from fastmcp import Client, FastMCP, FastMCPApp
+from fastmcp.exceptions import ToolError
+from fastmcp.server.context import Context
+from fastmcp.server.providers.addressing import hashed_backend_name
+
+
[email protected]("mode", ["auto", "legacy"])
[email protected]("nested", [False, True])
[email protected]("component", ["tool", "resource", "template", "prompt"])
+async def test_child_session_enable_allows_dispatch(
+ mode: Literal["auto", "legacy"], nested: bool, component: str
+) -> None:
+ child = FastMCP("child")
+
+ @child.tool
+ async def unlock(ctx: Context) -> str:
+ await ctx.enable_components(tags={"premium"})
+ return "unlocked"
+
+ @child.tool(tags={"premium"})
+ def premium_tool() -> str:
+ return "tool ok"
+
+ @child.resource("data://premium", tags={"premium"})
+ def premium_resource() -> str:
+ return "resource ok"
+
+ @child.resource("data://premium/{item}", tags={"premium"})
+ def premium_template(item: str) -> str:
+ return f"template {item}"
+
+ @child.prompt(tags={"premium"})
+ def premium_prompt() -> str:
+ return "prompt ok"
+
+ child.disable(tags={"premium"})
+ parent = FastMCP("parent")
+ parent.mount(child, namespace="child")
+ namespace = "child"
+ if nested:
+ root = FastMCP("root")
+ root.mount(parent, namespace="parent")
+ parent = root
+ namespace = "parent_child"
+ uri_namespace = namespace.replace("_", "/")
+
+ async with Client(parent, mode=mode) as client:
+ await client.call_tool(f"{namespace}_unlock", {})
+ if component == "tool":
+ assert f"{namespace}_premium_tool" in {
+ tool.name for tool in await client.list_tools()
+ }
+ tool_result = await client.call_tool(f"{namespace}_premium_tool", {})
+ assert tool_result.data == "tool ok"
+ elif component == "resource":
+ uri = f"data://{uri_namespace}/premium"
+ assert uri in {
+ str(resource.uri) for resource in await client.list_resources()
+ }
+ resource_result = await client.read_resource(uri)
+ assert isinstance(resource_result[0], TextResourceContents)
+ assert resource_result[0].text == "resource ok"
+ elif component == "template":
+ assert f"data://{uri_namespace}/premium/{{item}}" in {
+ template.uri_template
+ for template in await client.list_resource_templates()
+ }
+ template_result = await client.read_resource(
+ f"data://{uri_namespace}/premium/one"
+ )
+ assert isinstance(template_result[0], TextResourceContents)
+ assert template_result[0].text == "template one"
+ else:
+ assert f"{namespace}_premium_prompt" in {
+ prompt.name for prompt in await client.list_prompts()
+ }
+ prompt_result = await client.get_prompt(f"{namespace}_premium_prompt")
+ assert isinstance(prompt_result.messages[0].content, TextContent)
+ assert prompt_result.messages[0].content.text == "prompt ok"
+
+
[email protected]("mode", ["auto", "legacy"])
+async def test_child_session_enable_allows_hashed_app_dispatch(
+ mode: Literal["auto", "legacy"],
+) -> None:
+ app = FastMCPApp("premium")
+
+ @app.tool()
+ def premium_tool() -> str:
+ return "app ok"
+
+ child = FastMCP("child", providers=[app])
+ child.disable(names={"premium_tool"})
+
+ @child.tool
+ async def unlock(ctx: Context) -> None:
+ await ctx.enable_components(names={"premium_tool"})
+
+ parent = FastMCP("parent")
+ parent.mount(child, namespace="child")
+ async with Client(parent, mode=mode) as client:
+ await client.call_tool("child_unlock", {})
+ result = await client.call_tool(
+ hashed_backend_name("premium", "premium_tool"), {}
+ )
+ assert result.data == "app ok"
+
+
[email protected]("mode", ["auto", "legacy"])
+async def test_child_rules_stay_isolated_and_reset(
+ mode: Literal["auto", "legacy"],
+) -> None:
+ child = FastMCP("child")
+
+ @child.tool(tags={"premium"})
+ def premium_tool() -> str:
+ return "child ok"
+
+ @child.tool
+ async def unlock(ctx: Context) -> None:
+ await ctx.enable_components(tags={"premium"})
+
+ @child.tool
+ async def reset(ctx: Context) -> None:
+ await ctx.reset_visibility()
+
+ child.disable(tags={"premium"})
+ sibling = FastMCP("sibling")
+
+ @sibling.tool(tags={"premium"})
+ def sibling_tool() -> str:
+ return "sibling ok"
+
+ sibling.disable(tags={"premium"})
+ parent = FastMCP("parent")
+ parent.mount(child, namespace="child")
+ parent.mount(sibling, namespace="sibling")
+
+ @parent.tool
+ async def parent_rule(ctx: Context) -> None:
+ await ctx.disable_components(names={"child_premium_tool"})
+
+ async with Client(parent, mode=mode) as first, Client(parent, mode=mode) as second:
+ await first.call_tool("child_unlock", {})
+ assert (await first.call_tool("child_premium_tool", {})).data == "child ok"
+ denied = [(first, "sibling_sibling_tool")]
+ # Modern in-memory clients share process identity; legacy clients
+ # have distinct sessions even when they run in the same process.
+ if mode == "legacy":
+ denied.append((second, "child_premium_tool"))
+ for client, name in denied:
+ with pytest.raises(ToolError, match="Unknown tool"):
+ await client.call_tool(name, {})
+ await first.call_tool("child_reset", {})
+ with pytest.raises(ToolError, match="Unknown tool"):
+ await first.call_tool("child_premium_tool", {})
+ await first.call_tool("child_unlock", {})
+ await first.call_tool("parent_rule", {})
+ assert "child_premium_tool" not in {
+ tool.name for tool in await first.list_tools()
+ }
+ with pytest.raises(ToolError, match="Unknown tool"):
+ await first.call_tool("child_premium_tool", {})