π The fast, Pythonic way to build MCP servers and clients.
Build the Prefab renderer page on read, not on resources/list (#6039)
Adam Swensonc3cca93f775d
diff --git a/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.py b/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.pyindex dedd48615..30ebd05eb 100644--- a/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.py+++ b/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.py@@ -73,7 +73,7 @@ def _merge_domain_lists(def _build_resource_for_tool(tool: Tool) -> Resource | None:- """Synthesize a TextResource for a prefab tool. Returns None if prefab_ui isn't installed."""+ """Synthesize a FunctionResource for a prefab tool. Returns None if prefab_ui isn't installed."""try:from prefab_ui.renderer import get_renderer_csp, get_renderer_htmlexcept ImportError:@@ -85,7 +85,7 @@ def _build_resource_for_tool(tool: Tool) -> Resource | None:ResourceCSP,app_config_to_meta_dict,)- from fastmcp.resources.types import TextResource+ from fastmcp.resources.function_resource import FunctionResourcetool_hash = _get_tool_hash(tool)if tool_hash is None:@@ -154,10 +154,11 @@ def _build_resource_for_tool(tool: Tool) -> Resource | None:)uri = f"ui://prefab/tool/{tool_hash}/renderer.html"- return TextResource(+ # 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})",- text=get_renderer_html(),+ fn=get_renderer_html,mime_type=UI_MIME_TYPE,meta={"ui": app_config_to_meta_dict(resource_app)},)diff --git a/tests/server/providers/test_prefab_synthesis.py b/tests/server/providers/test_prefab_synthesis.pyindex addb8b640..0332d9f3b 100644--- a/tests/server/providers/test_prefab_synthesis.py+++ b/tests/server/providers/test_prefab_synthesis.py@@ -10,6 +10,7 @@ exercise the synthesis path directly through the public server API.from __future__ import annotationsimport pytest+from mcp_types import TextResourceContentsfrom fastmcp import Client, FastMCP, FastMCPApp@@ -255,6 +256,34 @@ class TestReadResource:text = contents[0].text if hasattr(contents[0], "text") else ""assert "<html" in text.lower() or "<!doctype" in text.lower()+ async def test_renderer_html_is_built_on_read_not_list(+ self, monkeypatch: pytest.MonkeyPatch+ ):+ """Listing must not build the renderer page once per tool (#6038)."""+ calls = 0++ def fake_renderer_html() -> str:+ nonlocal calls+ calls += 1+ return "<!doctype html><html>renderer</html>"++ monkeypatch.setattr("prefab_ui.renderer.get_renderer_html", fake_renderer_html)++ mcp = FastMCP("test")+ for i in range(3):+ mcp.tool(name=f"view_{i}", app=True)(lambda: "view")++ async with Client(mcp) as client:+ resources = await client.list_resources()+ assert len(resources) == 3+ assert calls == 0++ contents = await client.read_resource(resources[0].uri)++ assert calls == 1+ assert isinstance(contents[0], TextResourceContents)+ assert contents[0].text == "<!doctype html><html>renderer</html>"+class TestNonPrefabToolsUntouched:async def test_plain_tool_has_no_ui_meta(self):diff --git a/tests/test_apps_prefab.py b/tests/test_apps_prefab.pyindex f864ab785..f1c5ade0f 100644--- a/tests/test_apps_prefab.py+++ b/tests/test_apps_prefab.py@@ -15,7 +15,7 @@ from prefab_ui.components.base import Componentfrom fastmcp import Client, FastMCPfrom fastmcp.apps import UI_MIME_TYPE, AppConfig-from fastmcp.resources.types import TextResource+from fastmcp.resources.function_resource import FunctionResourcefrom fastmcp.server.providers.local_provider.decorators.tools import (PREFAB_RENDERER_URI,)@@ -143,7 +143,7 @@ class TestAppTrue:resources = list(await mcp.list_resources())renderer = next(r for r in resources if "prefab/tool" in str(r.uri))- assert isinstance(renderer, TextResource)+ assert isinstance(renderer, FunctionResource)assert renderer.mime_type == UI_MIME_TYPEasync def test_renderer_resource_has_csp(self):