🌐 US-Proxy
class="astro-vdxcvpsm">Skip to content

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.

  1. 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 and vite-plugin-singlefile, which inlines every chunk into index.html:

    Terminal window
    npm install @nimblebrain/synapse react react-dom
    npm install -D vite @vitejs/plugin-react vite-plugin-singlefile
    ui/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 build writes ui/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.

  2. Read the file once at start.

    import hashlib
    from pathlib import Path
    UI_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.

  3. Serve it as a ui:// resource.

    from fastmcp import FastMCP
    from fastmcp.apps import AppConfig, ResourceCSP
    mcp = 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_HTML

    FastMCP serves a ui:// resource under the MCP Apps MIME, text/html;profile=mcp-app, and writes app= 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_domains for fetch, resource_domains for images, fonts and scripts. A UI whose bundle inlines fonts as data: URLs declares resource_domains=["data:"], or a host’s default policy drops the fonts and the UI falls back to system faces.

    Leave domain unset. _meta.ui.domain is 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.

  4. 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.resourceUri and _meta.ui.visibility on the tool. A host reads the resource, renders it, and pushes the tool’s result to it; the component reads that with useToolResult(). visibility names who may call the tool: "model" (the agent) and "app" (the component, over tools/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.

  5. 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 build
    return 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/forecast listed too: a host placement that names the resource (a NimbleBrain sidebar view, in a bundle manifest) stays valid across builds.

  6. Run the server. Start it as you normally do (stdio or HTTP) and call get_forecast from a host that supports MCP Apps. To check what each host will see before you connect it, run synapse check:

    Terminal window
    npx synapse check --target claude,chatgpt http://localhost:8000/mcp

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.

  • Vite built the UI, @nimblebrain/synapse included, 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.