Serve a Synapse UI from a FastMCP server
A Synapse app has two halves. The browser half is @nimblebrain/synapse, covered in the
React quickstart. The server half is the MCP Apps standard, which
FastMCP 4 implements natively in fastmcp.apps: a tool names a
ui:// resource, and a host renders that resource with the tool’s result. This page
connects the two.
The result is one HTML file that every MCP Apps host renders (ChatGPT, Claude, and the NimbleBrain runtime), served by a FastMCP server that needs no Synapse package of its own.
-
Build the UI as one self-contained HTML file.
A host loads a
ui://resource as a single document and fetches nothing else for it, so scripts, styles and fonts go inside the file. Build the React app from the React quickstart with Vite andvite-plugin-singlefile, which inlines every chunk intoindex.html:Terminal window npm install @nimblebrain/synapse react react-domnpm install -D vite @vitejs/plugin-react vite-plugin-singlefileui/vite.config.ts import { defineConfig } from "vite";import react from "@vitejs/plugin-react";import { viteSingleFile } from "vite-plugin-singlefile";export default defineConfig({plugins: [react(), viteSingleFile()],build: { outDir: "dist", assetsInlineLimit: Infinity },});npm run buildwritesui/dist/index.html, the whole app in one file. The HTML holds no data: the component receives the tool result from the host at runtime. Add the Vite plugin (synapseVite()) to the same config to preview against your running server. -
Read the file once at start.
import hashlibfrom pathlib import PathUI_HTML = (Path(__file__).parent / "ui" / "dist" / "index.html").read_text("utf-8")UI_BUILD = hashlib.sha256(UI_HTML.encode("utf-8")).hexdigest()[:12]Ship the built file with the server: commit it, or copy it into the wheel with a build hook. A server that cannot find it should serve a short “UI is not built” page rather than fail to start, so its tools keep working.
-
Serve it as a
ui://resource.from fastmcp import FastMCPfrom fastmcp.apps import AppConfig, ResourceCSPmcp = FastMCP("weather")UI_URI = "ui://weather/forecast"UI_APP = AppConfig(prefers_border=True, csp=ResourceCSP())@mcp.resource(UI_URI, app=UI_APP)def forecast_ui() -> str:"""The forecast UI: static HTML that an MCP Apps host renders in a frame."""return UI_HTMLFastMCP serves a
ui://resource under the MCP Apps MIME,text/html;profile=mcp-app, and writesapp=to the resource’s_meta.ui.ResourceCSP()with no origins is the frame’s policy for a self-contained file: reach nothing. List an origin only for something the UI actually loads or calls:connect_domainsforfetch,resource_domainsfor images, fonts and scripts. A UI whose bundle inlines fonts asdata:URLs declaresresource_domains=["data:"], or a host’s default policy drops the fonts and the UI falls back to system faces.Leave
domainunset._meta.ui.domainis checked by each host against an origin of its own (Claude derives one from the connector URL and rejects any other value), so one value cannot satisfy every host, and omitting it gives each host its default sandbox origin. -
Bind tools to it with
AppConfig(resource_uri=...).@mcp.tool(app=AppConfig(resource_uri=UI_URI, visibility=["model", "app"]))async def get_forecast(city: str) -> dict:"""Get the forecast for a city."""return {"city": city, "high": 78, "low": 61, "summary": "Sunny"}FastMCP writes
_meta.ui.resourceUriand_meta.ui.visibilityon the tool. A host reads the resource, renders it, and pushes the tool’s result to it; the component reads that withuseToolResult().visibilitynames who may call the tool:"model"(the agent) and"app"(the component, overtools/call). A tool the UI calls but the agent should not see is["app"].A plain MCP client that renders no UI ignores the binding and reads the tool’s
structuredContent, so the same server works everywhere. -
Key the bound URI to the build.
A host may cache a
ui://resource by its URI, so a new build served under the old URI can render as the old UI. Put the build hash in the URI the tools bind to, and serve every build key from one template:UI_BOUND_URI = f"{UI_URI}/{UI_BUILD}"@mcp.resource(f"{UI_URI}/{{build}}", app=UI_APP)def forecast_ui_build(build: str) -> str:"""The forecast UI under a build key. Every key serves the current build."""del buildreturn UI_HTML@mcp.tool(app=AppConfig(resource_uri=UI_BOUND_URI, visibility=["model", "app"]))async def get_forecast(city: str) -> dict: ...A new build is then a new URI, and the host fetches it. Any key resolves to the current file, so a conversation that names an older build still renders. Keep the unkeyed
ui://weather/forecastlisted too: a host placement that names the resource (a NimbleBrain sidebar view, in a bundle manifest) stays valid across builds. -
Run the server. Start it as you normally do (stdio or HTTP) and call
get_forecastfrom a host that supports MCP Apps. To check what each host will see before you connect it, runsynapse check:Terminal window npx synapse check --target claude,chatgpt http://localhost:8000/mcp
Rendering in ChatGPT
Section titled “Rendering in ChatGPT”ChatGPT renders the same resource from ui.resourceUri, and reads a few keys of its own
beside it. The one most servers need is openai/widgetAccessible: True on every tool the
UI calls: ChatGPT defaults it to false, which leaves the UI read-only there.
CHATGPT_TOOL_META = {"openai/widgetAccessible": True}
@mcp.tool( app=AppConfig(resource_uri=UI_BOUND_URI, visibility=["model", "app"]), meta=CHATGPT_TOOL_META,)async def get_forecast(city: str) -> dict: ...ChatGPT compatibility covers each key, which host reads it, and how to ask a signed-out user to sign in from a single tool.
What just happened
Section titled “What just happened”- Vite built the UI,
@nimblebrain/synapseincluded, into one HTML file with nothing to fetch. - FastMCP served that file as a
ui://resource under the MCP Apps MIME, with the frame’s policy and border preference in_meta.ui. AppConfig(resource_uri=...)bound the tool to the resource, so a host renders the result in the UI, and the build-keyed URI keeps hosts from rendering a cached build.
A complete server built this way is
Todos for Agents: its server.py
serves the board UI with AppConfig and a build-keyed URI, and its ui/ builds the board
with Vite and vite-plugin-singlefile.
Related
Section titled “Related”- ChatGPT compatibility: ChatGPT’s own
_metakeys and per-tool sign-in. - Quickstart: React app: the browser half of the same app.
- Develop locally with the Vite plugin: preview the UI against your server.
- Cross-host architecture: how one component reaches each host’s bridge.