π The fast, Pythonic way to build MCP servers and clients.
Share Prefab renderer resources across equivalent tools (#6077)
Jeremiah Lowind8ee63fb0b15
diff --git a/docs/apps/architecture.mdx b/docs/apps/architecture.mdxindex 9e0ff1826..e4dc86f3c 100644--- a/docs/apps/architecture.mdx+++ b/docs/apps/architecture.mdx@@ -31,7 +31,7 @@ When you mark a tool with `app=True` or `@app.ui()`, FastMCP wires up the metada`app` on `@mcp.tool` accepts `True`, an `AppConfig`, or a dict. When you pass `True`, FastMCP explicitly marks the tool as a Prefab UI tool and stamps placeholder UI metadata so the provider can synthesize the correct renderer resource later. When you omit `app`, FastMCP only applies this automatically if the tool's return type is a Prefab type (`PrefabApp`, `Component`, or unions containing them).-The tool and renderer are linked through a `resourceUri` field in the metadata. Internally, registration uses the placeholder URI `ui://prefab/renderer.html`; when tools and resources are listed or read, FastMCP rewrites that placeholder to a per-tool URI like `ui://prefab/tool/<hash>/renderer.html` and synthesizes the matching renderer resource on demand.+The tool and renderer are linked through a `resourceUri` field in the metadata. Internally, registration uses the placeholder URI `ui://prefab/renderer.html`; when tools and resources are listed or read, FastMCP rewrites that placeholder to a shared URI like `ui://prefab/renderer/<hash>/renderer.html` and synthesizes the matching renderer resource on demand.### FastMCPApp registration@@ -111,7 +111,9 @@ The Prefab renderer is a self-contained JavaScript application that interprets t### Renderer resources-FastMCP exposes the renderer through per-tool resources such as `ui://prefab/tool/<hash>/renderer.html`, each with MIME type `text/html;profile=mcp-app`. The HTML is bundled inside the `prefab-ui` Python package; `get_renderer_html()` returns it as a string. The resources are synthesized on demand from each tool's UI metadata, so CSP and permissions can differ per tool even though they use the same Prefab renderer.+Tools with matching renderer configurations share one resource at `ui://prefab/renderer/<hash>/renderer.html`, with MIME type `text/html;profile=mcp-app`. The identity includes the installed Prefab version, delivery mode, and effective resource metadata. Different CSP, permissions, domains, or border preferences produce separate resources. Explicit resource URI overrides are left unchanged, and previously advertised per-tool URIs remain readable.++Listing tools and resources does not load the renderer HTML. FastMCP generates the page when a host reads the resource. The default delivery mode returns a small HTML page that loads version-pinned assets from a CDN; `PREFAB_BUNDLED_RENDERER=1` serves a self-contained page instead. Shared URIs let hosts recognize equivalent resources, whether or not they choose to cache them.The resource also carries CSP metadata (via `get_renderer_csp()`) declaring the CDN domains the renderer needs. Hosts use this to configure the iframe's Content Security Policy.diff --git a/fastmcp_slim/fastmcp/server/providers/addressing.py b/fastmcp_slim/fastmcp/server/providers/addressing.pyindex 5b8ef0a27..9cc879fb9 100644--- a/fastmcp_slim/fastmcp/server/providers/addressing.py+++ b/fastmcp_slim/fastmcp/server/providers/addressing.py@@ -9,9 +9,9 @@ app name + tool name. The hash serves two purposes:matches and applies the same transforms, visibility, and auth as a lookupby the tool's listed name.-2. **Per-tool Prefab renderer URIs.** Each prefab tool gets a unique renderer- resource at ``ui://prefab/tool/<hash>/renderer.html``. ``list_resources``- and ``read_resource`` synthesize these on demand from the tool's meta.+2. **Legacy Prefab renderer aliases.** Previously advertised per-tool URIs at+ ``ui://prefab/tool/<hash>/renderer.html`` remain readable. New renderer+ resources are shared by effective configuration in ``prefab_synthesis``.The hash is computed at registration time from ``(app_name, tool_name)`` βboth known at that moment β and stored in ``meta["fastmcp"]["tool_hash"]``.diff --git a/fastmcp_slim/fastmcp/server/providers/local_provider/decorators/tools.py b/fastmcp_slim/fastmcp/server/providers/local_provider/decorators/tools.pyindex 6a3689562..9779801b5 100644--- a/fastmcp_slim/fastmcp/server/providers/local_provider/decorators/tools.py+++ b/fastmcp_slim/fastmcp/server/providers/local_provider/decorators/tools.py@@ -67,9 +67,8 @@ def _stamp_prefab_marker(tool: Tool) -> None:Sets ``meta["ui"]["resourceUri"]`` to a placeholder URI. The serverrecognizes the placeholder at list_tools / list_resources / read_resource- time and synthesizes a per-tool resource on the fly with a hashed URI- derived from the tool's mount-point address. Nothing is stored β the- renderer HTML and CSP are generated on demand from the tool's own meta.+ time and synthesizes a shared renderer URI from its effective configuration.+ Resource metadata comes from the tool; HTML is loaded only when read."""from fastmcp.apps.config import AppConfig, app_config_to_meta_dictdiff --git a/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.py b/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.pyindex 30ebd05eb..66c5d4ae0 100644--- a/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.py+++ b/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.py@@ -1,20 +1,18 @@-"""On-demand Prefab renderer resource synthesis.--Tools marked as Prefab (via ``app=True``, ``PrefabAppConfig``, etc.) carry-a placeholder ``meta.ui.resourceUri`` and optionally a hash in-``meta.fastmcp.tool_hash``. This module synthesizes per-tool renderer-resources on demand at ``list_resources`` and ``read_resource`` time-without storing or materializing anything.--Each tool's resource URI is ``ui://prefab/tool/<hash>/renderer.html``-where the hash comes from the tool's own meta (set at registration from-the app name + tool name). CSP on the resource is the tool's-``meta.ui.csp`` merged with the renderer defaults across all four-``*_domains`` fields.+"""On-demand Prefab renderer resources shared by effective configuration.++Tools carry a placeholder UI resource URI. Listings rewrite it to a stable+renderer URI derived from the Prefab version, delivery mode, and effective+resource metadata. Matching tools share a resource; different CSP, permissions,+domains, and border preferences stay distinct. HTML is loaded only on read.+Legacy per-tool URIs remain readable independently of backend tool identity."""from __future__ import annotations+import hashlib+import json+import os+from functools import partialfrom typing import TYPE_CHECKING, Any, castfrom fastmcp.server.providers.addressing import (@@ -73,8 +71,9 @@ def _merge_domain_lists(def _build_resource_for_tool(tool: Tool) -> Resource | None:- """Synthesize a FunctionResource for a prefab tool. Returns None if prefab_ui isn't installed."""+ """Build a lazy renderer resource, identified by its effective configuration."""try:+ from prefab_ui import __version__from prefab_ui.renderer import get_renderer_csp, get_renderer_htmlexcept ImportError:return None@@ -87,12 +86,15 @@ def _build_resource_for_tool(tool: Tool) -> Resource | None:)from fastmcp.resources.function_resource import FunctionResource- tool_hash = _get_tool_hash(tool)- if tool_hash is None:- return None+ # Mirror Prefab's documented delivery-mode precedence without loading HTML.+ mode = os.environ.get("PREFAB_RENDERER_URL", "").rstrip("/")+ if not mode:+ bundled = bool(os.environ.get("PREFAB_BUNDLED_RENDERER"))+ dev_version = "dev" in __version__ or __version__ == "0.0.0"+ mode = "bundled" if bundled or dev_version else "cdn"# Merge user CSP with renderer defaults β all four domain fields.- defaults: dict[str, Any] = get_renderer_csp() or {}+ defaults: dict[str, Any] = get_renderer_csp(mode=mode) or {}user_csp: dict[str, Any] = {}if tool.meta and isinstance(tool.meta.get("ui"), dict):raw = tool.meta["ui"].get("csp")@@ -123,6 +125,10 @@ def _build_resource_for_tool(tool: Tool) -> Resource | None:_get(user_csp, "base_uri_domains", "baseUriDomains"),),}+ # CSP domain lists are sets of allowed origins; order has no meaning.+ merged = {+ key: sorted(set(value)) if value else None for key, value in merged.items()+ }resource_csp = ResourceCSP(**merged) if any(merged.values()) else None@@ -152,15 +158,21 @@ def _build_resource_for_tool(tool: Tool) -> Resource | None:domain=domain,prefers_border=prefers_border,)- uri = f"ui://prefab/tool/{tool_hash}/renderer.html"+ meta = {"ui": app_config_to_meta_dict(resource_app)}+ identity = json.dumps(+ {"version": __version__, "mode": mode, "mime_type": UI_MIME_TYPE, "meta": meta},+ sort_keys=True,+ separators=(",", ":"),+ )+ digest = hashlib.sha256(identity.encode()).hexdigest()+ uri = f"ui://prefab/renderer/{digest}/renderer.html"- # The page is built on read: listing would otherwise build one copy per tool.return FunctionResource(uri=cast(Any, uri),- name=f"Prefab Renderer ({tool.name})",- fn=get_renderer_html,+ name="Prefab Renderer",+ fn=partial(get_renderer_html, mode=mode),mime_type=UI_MIME_TYPE,- meta={"ui": app_config_to_meta_dict(resource_app)},+ meta=meta,)@@ -211,17 +223,42 @@ def _walk_prefab_tools(server: FastMCP) -> list[Tool]:return results+async def _effective_prefab_tools(server: FastMCP) -> list[Tool]:+ """Use provider and session transforms before deriving resource identity."""+ from fastmcp.server.providers.base import Provider+ from fastmcp.server.transforms.visibility import (+ apply_session_transforms,+ is_enabled,+ )+ from fastmcp.utilities.versions import VersionSpec++ tools = await apply_session_transforms(list(await Provider.list_tools(server)))+ result: list[Tool] = []+ for tool in tools:+ if not is_enabled(tool):+ continue+ ui = (tool.meta or {}).get("ui")+ if _is_prefab_tool(tool):+ result.append(tool)+ elif isinstance(ui, dict) and str(ui.get("resourceUri", "")).startswith(+ "ui://prefab/renderer/"+ ):+ # Mounted servers have already rewritten their listed metadata.+ # A normal lookup retains the resource fields needed for synthesis.+ original = await server.get_tool(tool.name, VersionSpec(eq=tool.version))+ if original is not None and _is_prefab_tool(original):+ result.append(original)+ return result++async def synthesize_prefab_resources(server: FastMCP) -> list[Resource]:- """Return fresh synthetic Prefab resources for all prefab tools. Pure."""+ """Return one lazy resource per effective renderer configuration."""resources: list[Resource] = []- seen_hashes: set[str] = set()- for tool in _walk_prefab_tools(server):- h = _get_tool_hash(tool)- if h is None or h in seen_hashes:- continue- seen_hashes.add(h)+ seen: set[str] = set()+ for tool in await _effective_prefab_tools(server):resource = _build_resource_for_tool(tool)- if resource is not None:+ if resource is not None and resource.key not in seen:+ seen.add(resource.key)resources.append(resource)return resources@@ -231,29 +268,33 @@ async def synthesize_prefab_resource_by_uri() -> Resource | None:"""Intercept a Prefab renderer URI and synthesize on demand."""digest = parse_hashed_resource_uri(uri)- if digest is None:+ if digest is None and not uri.startswith("ui://prefab/renderer/"):return None- for tool in _walk_prefab_tools(server):- if _get_tool_hash(tool) == digest:- return _build_resource_for_tool(tool)+ tools = (+ _walk_prefab_tools(server)+ if digest is not None+ else await _effective_prefab_tools(server)+ )+ for tool in tools:+ resource = _build_resource_for_tool(tool)+ if resource is not None:+ if digest is not None and _get_tool_hash(tool) == digest:+ return resource.model_copy(update={"uri": uri})+ if str(resource.uri) == uri:+ return resourcereturn Nonedef rewrite_tool_meta_for_wire(tool: Tool) -> Tool:- """Return a model_copy with the per-tool URI and resource-only fields stripped.-- Reads the hash from the tool's own meta. If no hash is found,- returns the tool unchanged. Produces a fresh copy β the original- Tool object is untouched.- """+ """Return a copy with the shared URI and resource-only fields stripped."""if not _is_prefab_tool(tool):return tool- tool_hash = _get_tool_hash(tool)- if tool_hash is None:+ resource = _build_resource_for_tool(tool)+ if resource is None:return toolassert tool.meta is not Nonenew_ui = dict(tool.meta["ui"])- new_ui["resourceUri"] = f"ui://prefab/tool/{tool_hash}/renderer.html"+ new_ui["resourceUri"] = str(resource.uri)new_ui.pop("csp", None)new_ui.pop("permissions", None)new_ui.pop("domain", None)diff --git a/fastmcp_slim/fastmcp/server/server.py b/fastmcp_slim/fastmcp/server/server.pyindex 13770c883..b8747ad51 100644--- a/fastmcp_slim/fastmcp/server/server.py+++ b/fastmcp_slim/fastmcp/server/server.py@@ -686,13 +686,10 @@ class FastMCP(super().add_provider(provider, namespace=namespace)def _rewrite_prefab_uris(self, tools: list[Tool]) -> list[Tool]:- """Replace placeholder Prefab URIs with per-tool hashed ones.+ """Replace placeholder Prefab URIs with shared renderer identities.- For each tool whose ``meta.ui.resourceUri`` is the placeholder,- reads the tool's stored hash from ``meta.fastmcp.tool_hash``- and rewrites the URI to the per-tool form. Also strips CSP from- tool meta (it belongs on the resource). Produces ``model_copy``- views β originals are untouched.+ Matching renderer configurations share a URI. Resource-only fields+ such as CSP are stripped from copies of tool metadata."""from fastmcp.server.providers.prefab_synthesis import (_is_prefab_tool,@@ -999,14 +996,16 @@ class FastMCP(resources = await apply_session_transforms(resources)resources = [r for r in resources if is_enabled(r)]- # Append synthetic Prefab renderer resources β one per- # prefab tool, hashed by mount address. These don't live on+ # Append synthetic Prefab resources shared by effective+ # renderer configuration. These don't live on# any provider's storage; they're computed on demand.from fastmcp.server.providers.prefab_synthesis import (synthesize_prefab_resources,)- resources.extend(await synthesize_prefab_resources(self))+ known_keys = {resource.key for resource in resources}+ synthetic = await synthesize_prefab_resources(self)+ resources.extend(r for r in synthetic if r.key not in known_keys)skip_auth, token = _get_auth_context()authorized: list[Resource] = []diff --git a/tests/server/providers/test_prefab_sharing.py b/tests/server/providers/test_prefab_sharing.pynew file mode 100644index 000000000..9ad0ce58c--- /dev/null+++ b/tests/server/providers/test_prefab_sharing.py@@ -0,0 +1,173 @@+"""Renderer identity follows its content configuration, independently of tools."""++import pytest+from mcp_types import TextResourceContents++from fastmcp import Client, FastMCP, FastMCPApp+from fastmcp.apps import AppConfig, PrefabAppConfig, ResourceCSP, ResourcePermissions+from fastmcp.server.providers.addressing import hashed_resource_uri+from fastmcp.server.transforms import ToolTransform+from fastmcp.tools.tool_transform import ToolTransformConfig++pytest.importorskip("prefab_ui")+++async def test_matching_tools_share_one_listed_resource():+ server = FastMCP("test")+ server.tool(name="first", app=True)(lambda: "first")+ server.tool(name="second", app=PrefabAppConfig())(lambda: "second")++ async with Client(server) as client:+ tools = await client.list_tools()+ uris = {t.meta["ui"]["resourceUri"] for t in tools}+ assert len(uris) == 1+ resources = await client.list_resources()+ assert {str(r.uri) for r in resources} == uris+ assert len(resources) == 1+++ "config",+ [+ PrefabAppConfig(csp=ResourceCSP(connect_domains=["https://api.example.com"])),+ PrefabAppConfig(csp=ResourceCSP(resource_domains=["https://cdn.example.com"])),+ PrefabAppConfig(csp=ResourceCSP(frame_domains=["https://frame.example.com"])),+ PrefabAppConfig(csp=ResourceCSP(base_uri_domains=["https://base.example.com"])),+ PrefabAppConfig(permissions=ResourcePermissions(camera={})),+ PrefabAppConfig(domain="https://app.example.com"),+ PrefabAppConfig(prefers_border=True),+ PrefabAppConfig(prefers_border=False),+ ],+)+async def test_different_resource_metadata_stays_distinct(config):+ server = FastMCP("test")+ server.tool(name="default", app=True)(lambda: "default")+ server.tool(name="configured", app=config)(lambda: "configured")+ async with Client(server) as client:+ tools = await client.list_tools()+ assert tools[0].meta["ui"]["resourceUri"] != tools[1].meta["ui"]["resourceUri"]+ resources = await client.list_resources()+ assert len(resources) == 2+ for resource in resources:+ content = (await client.read_resource(resource.uri))[0]+ assert content.meta["ui"] == resource.meta["ui"]+++async def test_csp_domain_order_does_not_change_identity():+ server = FastMCP("test")+ domains = ["https://a.example.com", "https://b.example.com"]+ for name, values in [("first", domains), ("second", domains[::-1])]:+ config = PrefabAppConfig(csp=ResourceCSP(frame_domains=values))+ server.tool(name=name, app=config)(lambda: "view")+ resources = await server.list_resources()+ assert len(resources) == 1+++async def test_legacy_per_tool_uri_preserves_read_content_and_metadata():+ app = FastMCPApp("dashboard")+ app.ui(name="show")(lambda: "show")+ server = FastMCP("test")+ server.add_provider(app, namespace="crm")+ legacy = hashed_resource_uri("dashboard", "show")+ async with Client(server) as client:+ tool = (await client.list_tools())[0]+ uri = tool.meta["ui"]["resourceUri"]+ assert uri != legacy+ canonical = (await client.read_resource(uri))[0]+ alias = (await client.read_resource(legacy))[0]+ assert isinstance(alias, TextResourceContents)+ assert str(alias.uri) == legacy+ assert alias.text == canonical.text+ assert alias.mime_type == canonical.mime_type+ assert alias.meta == canonical.meta+++async def test_nested_mounts_share_renderer_and_keep_backend_identity():+ child = FastMCP("child")+ for name in ["one", "two"]:+ app = FastMCPApp(name)+ app.ui(name="show")(lambda: "show")+ child.add_provider(app, namespace=name)+ parent = FastMCP("parent")+ parent.mount(child, namespace="nested")+ async with Client(parent) as client:+ tools = await client.list_tools()+ assert len({t.meta["fastmcp"]["tool_hash"] for t in tools}) == 2+ uris = {t.meta["ui"]["resourceUri"] for t in tools}+ assert len(uris) == 1+ for uri in uris:+ assert isinstance(+ (await client.read_resource(uri))[0], TextResourceContents+ )+++async def test_custom_resource_uri_is_untouched():+ server = FastMCP("test")+ server.tool(name="custom", app=AppConfig(resource_uri="ui://custom/view.html"))(+ lambda: "view"+ )+ async with Client(server) as client:+ tool = (await client.list_tools())[0]+ assert tool.meta["ui"]["resourceUri"] == "ui://custom/view.html"+ assert await client.list_resources() == []+++async def test_listing_does_not_load_html(monkeypatch):+ def unexpected_html(*args, **kwargs):+ pytest.fail("listing loaded renderer HTML")++ monkeypatch.setattr("prefab_ui.renderer.get_renderer_html", unexpected_html)+ server = FastMCP("test")+ server.tool(name="one", app=True)(lambda: "one")+ server.tool(name="two", app=True)(lambda: "two")+ async with Client(server) as client:+ assert len(await client.list_tools()) == 2+ assert len(await client.list_resources()) == 1+++async def test_renderer_mode_and_version_change_identity(monkeypatch):+ monkeypatch.delenv("PREFAB_RENDERER_URL", raising=False)+ monkeypatch.delenv("PREFAB_BUNDLED_RENDERER", raising=False)+ server = FastMCP("test")+ server.tool(name="view", app=True)(lambda: "view")+ cdn = str((await server.list_resources())[0].uri)+ monkeypatch.setenv("PREFAB_BUNDLED_RENDERER", "1")+ bundled = str((await server.list_resources())[0].uri)+ assert cdn != bundled+ monkeypatch.setattr("prefab_ui.__version__", "999.0.0")+ upgraded = str((await server.list_resources())[0].uri)+ assert upgraded != bundled+ monkeypatch.setenv("PREFAB_RENDERER_URL", "https://dev.example.com")+ external = str((await server.list_resources())[0].uri)+ assert external != upgraded++[email protected]("mounted", [False, True])+async def test_transformed_resource_configuration_is_readable(mounted):+ server = FastMCP("test")+ server.tool(name="view", app=True)(lambda: "view")+ server.add_transform(+ ToolTransform(+ {+ "view": ToolTransformConfig(+ meta={+ "ui": {+ "resourceUri": "ui://prefab/renderer.html",+ "domain": "https://isolated.example",+ }+ }+ )+ }+ )+ )+ if mounted:+ parent = FastMCP("parent")+ parent.mount(server, namespace="nested")+ server = parent+ async with Client(server) as client:+ tool = (await client.list_tools())[0]+ uri = tool.meta["ui"]["resourceUri"]+ resources = await client.list_resources()+ assert uri in {str(r.uri) for r in resources}+ content = (await client.read_resource(uri))[0]+ assert content.meta["ui"]["domain"] == "https://isolated.example"diff --git a/tests/server/providers/test_prefab_synthesis.py b/tests/server/providers/test_prefab_synthesis.pyindex 0332d9f3b..e2a7ce16d 100644--- a/tests/server/providers/test_prefab_synthesis.py+++ b/tests/server/providers/test_prefab_synthesis.py@@ -38,7 +38,7 @@ class TestUserCSPReachesResource:# Find the synthesized prefab resource for this tool.resources = list(await mcp.list_resources())- renderer = next((r for r in resources if "prefab/tool" in str(r.uri)), None)+ renderer = next((r for r in resources if "prefab/renderer" in str(r.uri)), None)assert renderer is not None, "no prefab resource was synthesized"assert renderer.meta is not None@@ -66,7 +66,7 @@ class TestUserCSPReachesResource:return "x"resources = list(await mcp.list_resources())- renderer = next(r for r in resources if "prefab/tool" in str(r.uri))+ renderer = next(r for r in resources if "prefab/renderer" in str(r.uri))assert renderer.meta is not Nonecsp = renderer.meta["ui"]["csp"]@@ -116,7 +116,7 @@ class TestDisplayMetaReachesResource:return "widget"resources = list(await mcp.list_resources())- renderer = next(r for r in resources if "prefab/tool" in str(r.uri))+ renderer = next(r for r in resources if "prefab/renderer" in str(r.uri))assert renderer.meta is not Noneui = renderer.meta["ui"]assert ui["domain"] == "https://widget.example.com"@@ -135,7 +135,7 @@ class TestDisplayMetaReachesResource:return "widget"renderer = next(- r for r in await mcp.list_resources() if "prefab/tool" in str(r.uri)+ r for r in await mcp.list_resources() if "prefab/renderer" in str(r.uri))assert renderer.meta is not Noneassert renderer.meta["ui"]["prefersBorder"] is False@@ -148,7 +148,7 @@ class TestDisplayMetaReachesResource:return "widget"renderer = next(- r for r in await mcp.list_resources() if "prefab/tool" in str(r.uri)+ r for r in await mcp.list_resources() if "prefab/renderer" in str(r.uri))assert renderer.meta is not Noneassert "domain" not in renderer.meta["ui"]@@ -176,11 +176,10 @@ class TestDisplayMetaReachesResource:assert meta["ui"]["prefersBorder"] is True-class TestPerToolURIs:- """Each prefab tool gets its own URI β distinct CSP per tool becomes- possible because no two tools share a renderer resource."""+class TestSharedRendererURIs:+ """Equivalent resource configurations share a renderer URI."""- async def test_two_tools_get_distinct_uris(self):+ async def test_two_tools_share_uri(self):mcp = FastMCP("test")@mcp.tool(app=True)@@ -198,15 +197,15 @@ class TestPerToolURIs:assert b.meta is not Noneuri_a = a.meta["ui"]["resourceUri"]uri_b = b.meta["ui"]["resourceUri"]- assert uri_a != uri_b- assert uri_a.startswith("ui://prefab/tool/")- assert uri_b.startswith("ui://prefab/tool/")+ assert uri_a == uri_b+ assert uri_a.startswith("ui://prefab/renderer/")+ assert uri_b.startswith("ui://prefab/renderer/")class TestFastMCPAppMounts:- """Tools inside FastMCPApps get URIs derived from the app's mount address."""+ """Tools inside FastMCPApps receive shared renderer resources."""- async def test_app_tool_uri_uses_address(self):+ async def test_app_tool_synthesizes_renderer(self):app = FastMCPApp("dashboard")@app.ui()@@ -217,7 +216,7 @@ class TestFastMCPAppMounts:mcp.add_provider(app)resources = list(await mcp.list_resources())- prefab = [r for r in resources if "prefab/tool" in str(r.uri)]+ prefab = [r for r in resources if "prefab/renderer" in str(r.uri)]assert len(prefab) == 1async def test_namespaced_mount_still_synthesizes_resource(self):@@ -231,7 +230,7 @@ class TestFastMCPAppMounts:mcp.add_provider(app, namespace="customers")resources = list(await mcp.list_resources())- prefab = [r for r in resources if "prefab/tool" in str(r.uri)]+ prefab = [r for r in resources if "prefab/renderer" in str(r.uri)]assert len(prefab) == 1@@ -262,7 +261,7 @@ class TestReadResource:"""Listing must not build the renderer page once per tool (#6038)."""calls = 0- def fake_renderer_html() -> str:+ def fake_renderer_html(mode: str | None = None) -> str:nonlocal callscalls += 1return "<!doctype html><html>renderer</html>"@@ -275,7 +274,7 @@ class TestReadResource:async with Client(mcp) as client:resources = await client.list_resources()- assert len(resources) == 3+ assert len(resources) == 1assert calls == 0contents = await client.read_resource(resources[0].uri)@@ -305,4 +304,4 @@ class TestNonPrefabToolsUntouched:return nameresources = list(await mcp.list_resources())- assert not any("prefab/tool" in str(r.uri) for r in resources)+ assert not any("prefab/renderer" in str(r.uri) for r in resources)diff --git a/tests/test_apps.py b/tests/test_apps.pyindex d3d884262..375db8e91 100644--- a/tests/test_apps.py+++ b/tests/test_apps.py@@ -712,7 +712,7 @@ class TestPrefabAppConfig:assert config.csp.frame_domains == ["https://example.com"]async def test_auto_synthesizes_renderer_resource(self):- """Each prefab tool gets a per-tool renderer resource on demand."""+ """Prefab tools synthesize shared renderer resources on demand."""from fastmcp.apps import PrefabAppConfigserver = FastMCP("test")@@ -722,11 +722,11 @@ class TestPrefabAppConfig:return "hello"resources = list(await server.list_resources())- prefab = [r for r in resources if "prefab/tool" in str(r.uri)]+ prefab = [r for r in resources if "prefab/renderer" in str(r.uri)]assert len(prefab) == 1async def test_equivalent_to_app_true(self):- """PrefabAppConfig() and app=True both synthesize a per-tool renderer."""+ """PrefabAppConfig() and app=True produce equivalent renderer identities."""from fastmcp.apps import PrefabAppConfigserver1 = FastMCP("test1")@@ -748,6 +748,7 @@ class TestPrefabAppConfig:assert tools2[0].meta is not Noneui2 = tools2[0].meta.get("ui", {})- # Both produce per-tool URIs in the prefab/tool/<hash>/ form.- assert ui1.get("resourceUri", "").startswith("ui://prefab/tool/")- assert ui2.get("resourceUri", "").startswith("ui://prefab/tool/")+ assert ui1["resourceUri"] == ui2["resourceUri"]+ # Equivalent configurations produce the same shared renderer URI.+ assert ui1.get("resourceUri", "").startswith("ui://prefab/renderer/")+ assert ui2.get("resourceUri", "").startswith("ui://prefab/renderer/")diff --git a/tests/test_apps_prefab.py b/tests/test_apps_prefab.pyindex f1c5ade0f..4e6bc5d34 100644--- a/tests/test_apps_prefab.py+++ b/tests/test_apps_prefab.py@@ -130,7 +130,7 @@ class TestAppTrue:return "hello"resources = list(await mcp.list_resources())- prefab_resources = [r for r in resources if "prefab/tool" in str(r.uri)]+ prefab_resources = [r for r in resources if "prefab/renderer" in str(r.uri)]assert len(prefab_resources) == 1assert "renderer.html" in str(prefab_resources[0].uri)@@ -142,7 +142,7 @@ class TestAppTrue:return "hello"resources = list(await mcp.list_resources())- renderer = next(r for r in resources if "prefab/tool" in str(r.uri))+ renderer = next(r for r in resources if "prefab/renderer" in str(r.uri))assert isinstance(renderer, FunctionResource)assert renderer.mime_type == UI_MIME_TYPE@@ -154,14 +154,13 @@ class TestAppTrue:return "hello"resources = list(await mcp.list_resources())- renderer = next(r for r in resources if "prefab/tool" in str(r.uri))+ renderer = next(r for r in resources if "prefab/renderer" in str(r.uri))assert renderer.meta is not Noneassert "ui" in renderer.metaassert "csp" in renderer.meta["ui"]- async def test_multiple_tools_get_dedicated_resources(self):- """Each prefab tool gets its own resource at a distinct hashed- URI β no shared singleton, so per-tool CSP becomes possible."""+ async def test_multiple_tools_share_renderer_resource(self):+ """Tools with matching resource configuration share one renderer."""mcp = FastMCP("test")@mcp.tool(app=True)@@ -173,8 +172,8 @@ class TestAppTrue:return "b"resources = list(await mcp.list_resources())- prefab_uris = {str(r.uri) for r in resources if "prefab/tool" in str(r.uri)}- assert len(prefab_uris) == 2+ prefab_uris = {str(r.uri) for r in resources if "prefab/renderer" in str(r.uri)}+ assert len(prefab_uris) == 1def test_explicit_app_config_not_overridden(self):mcp = FastMCP("test")@@ -427,8 +426,8 @@ class TestIntegration:tool = next(t for t in tools if t.name == "my_tool")meta = tool.meta or {}assert "ui" in meta- # URI is the per-tool hashed form, not the singleton.- assert meta["ui"]["resourceUri"].startswith("ui://prefab/tool/")+ # URI identifies the effective renderer configuration.+ assert meta["ui"]["resourceUri"].startswith("ui://prefab/renderer/")assert meta["ui"]["resourceUri"].endswith("/renderer.html")async def test_renderer_resource_readable(self):diff --git a/tests/test_fastmcp_app.py b/tests/test_fastmcp_app.pyindex 1cb3eb32d..ce762810d 100644--- a/tests/test_fastmcp_app.py+++ b/tests/test_fastmcp_app.py@@ -875,9 +875,8 @@ class TestComposition:names = {t.name for t in tools}assert names == {"dashboard", "save"}- async def test_ui_synthesizes_per_tool_renderer_resource(self):- """Each @app.ui() tool gets its own renderer resource synthesized- on demand from the server's address registry."""+ async def test_ui_synthesizes_renderer_resource(self):+ """An @app.ui() tool gets its renderer resource synthesized on demand."""app = FastMCPApp("test")@app.ui()@@ -888,7 +887,7 @@ class TestComposition:server.add_provider(app)resources = list(await server.list_resources())- prefab = [r for r in resources if "prefab/tool" in str(r.uri)]+ prefab = [r for r in resources if "prefab/renderer" in str(r.uri)]assert len(prefab) == 1