π The fast, Pythonic way to build MCP servers and clients.
Carry domain and prefers_border onto the Prefab renderer resource (#5611)
Asem ahmedca81bbeaf643
diff --git a/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.py b/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.pyindex 111cd677e..dedd48615 100644--- a/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.py+++ b/fastmcp_slim/fastmcp/server/providers/prefab_synthesis.py@@ -126,19 +126,31 @@ def _build_resource_for_tool(tool: Tool) -> Resource | None:resource_csp = ResourceCSP(**merged) if any(merged.values()) else None- # Carry permissions from the tool's meta to the resource (same- # principle as CSP β belongs on the resource, not the tool).+ # Carry permissions, domain and prefersBorder from the tool's meta to the+ # resource (same principle as CSP β they belong on the resource, not the+ # tool).user_permissions = None+ domain = None+ prefers_border = Noneif tool.meta and isinstance(tool.meta.get("ui"), dict):- raw_perms = tool.meta["ui"].get("permissions")+ tool_ui = tool.meta["ui"]+ raw_perms = tool_ui.get("permissions")if isinstance(raw_perms, dict):from fastmcp.apps.config import ResourcePermissionsuser_permissions = ResourcePermissions(**raw_perms)+ raw_domain = tool_ui.get("domain")+ if isinstance(raw_domain, str):+ domain = raw_domain+ raw_border = tool_ui.get("prefersBorder")+ if isinstance(raw_border, bool):+ prefers_border = raw_borderresource_app = AppConfig(csp=resource_csp,permissions=user_permissions,+ domain=domain,+ prefers_border=prefers_border,)uri = f"ui://prefab/tool/{tool_hash}/renderer.html"@@ -227,7 +239,7 @@ async def synthesize_prefab_resource_by_uri(def rewrite_tool_meta_for_wire(tool: Tool) -> Tool:- """Return a model_copy with the per-tool URI and CSP stripped.+ """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@@ -243,6 +255,8 @@ def rewrite_tool_meta_for_wire(tool: Tool) -> Tool:new_ui["resourceUri"] = f"ui://prefab/tool/{tool_hash}/renderer.html"new_ui.pop("csp", None)new_ui.pop("permissions", None)+ new_ui.pop("domain", None)+ new_ui.pop("prefersBorder", None)new_meta = dict(tool.meta)new_meta["ui"] = new_uireturn tool.model_copy(update={"meta": new_meta})diff --git a/tests/server/providers/test_prefab_synthesis.py b/tests/server/providers/test_prefab_synthesis.pyindex 1a84ac0c4..addb8b640 100644--- a/tests/server/providers/test_prefab_synthesis.py+++ b/tests/server/providers/test_prefab_synthesis.py@@ -11,10 +11,14 @@ from __future__ import annotationsimport pytest-from fastmcp import FastMCP, FastMCPApp+from fastmcp import Client, FastMCP, FastMCPAppprefab_ui = pytest.importorskip("prefab_ui")-from fastmcp.apps.config import PrefabAppConfig, ResourceCSP # noqa: E402+from fastmcp.apps.config import ( # noqa: E402+ PrefabAppConfig,+ ResourceCSP,+ ResourcePermissions,+)class TestUserCSPReachesResource:@@ -92,6 +96,85 @@ class TestCSPStrippedFromToolMeta:assert "permissions" not in ui+class TestDisplayMetaReachesResource:+ """``domain`` and ``prefers_border`` are resource-level fields in the MCP+ Apps spec, so like CSP and permissions they move from the tool to the+ renderer resource."""++ async def test_domain_and_prefers_border_reach_resource(self):+ mcp = FastMCP("test")++ @mcp.tool(+ app=PrefabAppConfig(+ domain="https://widget.example.com",+ prefers_border=True,+ permissions=ResourcePermissions(camera={}),+ )+ )+ def show_widget() -> str:+ return "widget"++ resources = list(await mcp.list_resources())+ renderer = next(r for r in resources if "prefab/tool" in str(r.uri))+ assert renderer.meta is not None+ ui = renderer.meta["ui"]+ assert ui["domain"] == "https://widget.example.com"+ assert ui["prefersBorder"] is True+ assert ui["permissions"] == {"camera": {}}++ tool = next(t for t in await mcp.list_tools() if t.name == "show_widget")+ assert tool.meta is not None+ assert set(tool.meta["ui"]) == {"resourceUri"}++ async def test_prefers_border_false_is_preserved(self):+ mcp = FastMCP("test")++ @mcp.tool(app=PrefabAppConfig(prefers_border=False))+ def show_widget() -> str:+ return "widget"++ renderer = next(+ r for r in await mcp.list_resources() if "prefab/tool" in str(r.uri)+ )+ assert renderer.meta is not None+ assert renderer.meta["ui"]["prefersBorder"] is False++ async def test_unset_fields_stay_off_resource(self):+ mcp = FastMCP("test")++ @mcp.tool(app=True)+ def show_widget() -> str:+ return "widget"++ renderer = next(+ r for r in await mcp.list_resources() if "prefab/tool" in str(r.uri)+ )+ assert renderer.meta is not None+ assert "domain" not in renderer.meta["ui"]+ assert "prefersBorder" not in renderer.meta["ui"]++ async def test_read_resource_carries_domain_and_prefers_border(self):+ mcp = FastMCP("test")++ @mcp.tool(+ app=PrefabAppConfig(domain="https://w.example.com", prefers_border=True)+ )+ def show_widget() -> str:+ return "widget"++ async with Client(mcp) as client:+ tools = await client.list_tools()+ uri = next(t for t in tools if t.name == "show_widget").meta["ui"][+ "resourceUri"+ ]+ contents = await client.read_resource(uri)++ meta = contents[0].meta+ assert meta is not None+ assert meta["ui"]["domain"] == "https://w.example.com"+ assert meta["ui"]["prefersBorder"] is True++class TestPerToolURIs:"""Each prefab tool gets its own URI β distinct CSP per tool becomespossible because no two tools share a renderer resource."""@@ -155,8 +238,6 @@ class TestReadResource:"""The synthesized resources are actually fetchable via read_resource."""async def test_read_resource_returns_renderer_html(self):- from fastmcp import Client-mcp = FastMCP("test")@mcp.tool(app=True)