Skip to content

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

fix(resources): reverse hostless URIs through Namespace unchanged (#5230)

Serhii Zghama391b6515a826
diff --git a/docs/servers/composition.mdx b/docs/servers/composition.mdx
index 4a66e5fd1..5eb205aea 100644
--- a/docs/servers/composition.mdx
+++ b/docs/servers/composition.mdx
@@ -144,6 +144,8 @@ main.mount(calendar, namespace="calendar")
Namespacing uses [transforms](/servers/transforms/transforms) under the hood.
+Resource and template URIs without `://`, such as `urn:example:config`, remain unchanged and can be read using their listed identifiers. Namespaces do not isolate these URIs from collisions; equal identifiers use the normal [provider resolution order](#conflict-resolution).
+
## Dynamic Composition
Because `mount()` creates a live link, you can add components to a child server after mounting and they'll be immediately available through the parent:
diff --git a/docs/servers/transforms/namespace.mdx b/docs/servers/transforms/namespace.mdx
index fdb0d1c7f..fbe6e7184 100644
--- a/docs/servers/transforms/namespace.mdx
+++ b/docs/servers/transforms/namespace.mdx
@@ -11,7 +11,7 @@ import { VersionBadge } from '/snippets/version-badge.mdx'
The `Namespace` transform prefixes all component names, preventing conflicts when composing multiple servers.
-Tools and prompts receive an underscore-separated prefix. Resources and templates receive a path-segment prefix in their URIs.
+Tools and prompts receive an underscore-separated prefix. Resources and templates with `scheme://` URIs receive a path-segment prefix. URIs without `://`, such as `urn:example:config`, remain unchanged for both listing and reading. They can collide across namespaces; equal identifiers follow the normal [provider resolution order](/servers/composition#conflict-resolution).
| Component | Original | With `Namespace("api")` |
|-----------|----------|-------------------------|
diff --git a/fastmcp_slim/fastmcp/server/transforms/namespace.py b/fastmcp_slim/fastmcp/server/transforms/namespace.py
index f1a219ac7..5782999e2 100644
--- a/fastmcp_slim/fastmcp/server/transforms/namespace.py
+++ b/fastmcp_slim/fastmcp/server/transforms/namespace.py
@@ -88,7 +88,9 @@ class Namespace(Transform):
if path.startswith(prefix):
return f"{protocol}{path[len(prefix) :]}"
return None
- return None
+ # No protocol://path to namespace, so _transform_uri left this URI
+ # alone and it is listed unprefixed. Reverse it to itself to match.
+ return uri
# -------------------------------------------------------------------------
# Tools
diff --git a/tests/server/mount/test_resources.py b/tests/server/mount/test_resources.py
index 06e2ce548..ed0dc553e 100644
--- a/tests/server/mount/test_resources.py
+++ b/tests/server/mount/test_resources.py
@@ -71,6 +71,31 @@ class TestResourcesAndTemplates:
result = await main_app.read_resource("resource://sub/multi/abc")
assert result.contents[0].content == "abc"
+ async def test_mount_with_hostless_uris(self):
+ """URIs with no `scheme://` are listed unprefixed, so they must stay readable."""
+ main_app = FastMCP("MainApp")
+ sub_app = FastMCP("SubApp")
+
+ @sub_app.resource("urn:example:config")
+ def config() -> str:
+ return "settings"
+
+ @sub_app.resource("urn:example:item/{item_id}")
+ def item(item_id: str) -> str:
+ return f"item {item_id}"
+
+ main_app.mount(sub_app, "api")
+
+ resources = await main_app.list_resources()
+ assert any(str(r.uri) == "urn:example:config" for r in resources)
+ templates = await main_app.list_resource_templates()
+ assert any(t.uri_template == "urn:example:item/{item_id}" for t in templates)
+
+ result = await main_app.read_resource("urn:example:config")
+ assert result.contents[0].content == "settings"
+ result = await main_app.read_resource("urn:example:item/7")
+ assert result.contents[0].content == "item 7"
+
async def test_adding_resource_after_mounting(self):
"""Test adding a resource after mounting."""
main_app = FastMCP("MainApp")
diff --git a/tests/server/providers/test_transforming_provider.py b/tests/server/providers/test_transforming_provider.py
index ce7acd2a4..4d6aa96b8 100644
--- a/tests/server/providers/test_transforming_provider.py
+++ b/tests/server/providers/test_transforming_provider.py
@@ -194,6 +194,54 @@ class TestTransformReverseLookup:
assert resource is not None
assert str(resource.uri) == "resource://ns/data"
+ async def test_namespace_get_resource_with_hostless_uri(self):
+ """A URI with no protocol://path is listed unprefixed, so it must resolve as-is."""
+ server = FastMCP("Test")
+
+ @server.resource("urn:example:config")
+ def my_resource() -> str:
+ return "content"
+
+ provider = FastMCPProvider(server)
+ layer = Namespace("ns")
+
+ resources = await layer.list_resources(await provider.list_resources())
+ assert str(resources[0].uri) == "urn:example:config"
+
+ async def get_resource(uri: str, version=None):
+ return await provider._get_resource(uri, version)
+
+ resource = await layer.get_resource("urn:example:config", get_resource)
+
+ assert resource is not None
+ assert str(resource.uri) == "urn:example:config"
+
+ async def test_namespace_get_resource_template_with_hostless_uri(self):
+ """Same for templates, which share the URI reversal."""
+ server = FastMCP("Test")
+
+ @server.resource("urn:example:item/{item_id}")
+ def my_template(item_id: str) -> str:
+ return f"item {item_id}"
+
+ provider = FastMCPProvider(server)
+ layer = Namespace("ns")
+
+ templates = await layer.list_resource_templates(
+ await provider.list_resource_templates()
+ )
+ assert templates[0].uri_template == "urn:example:item/{item_id}"
+
+ async def get_template(uri: str, version=None):
+ return await provider._get_resource_template(uri, version)
+
+ template = await layer.get_resource_template(
+ "urn:example:item/{item_id}", get_template
+ )
+
+ assert template is not None
+ assert template.uri_template == "urn:example:item/{item_id}"
+
async def test_nonmatching_namespace_returns_none(self):
"""Test that lookups with wrong namespace return None."""
server = FastMCP("Test")