app=True to a tool and return Prefab components. The host renders an interactive UI instead of text. This works for everything from static charts to reactive dashboards with client-side state — no server round-trips needed.
For apps that need server interaction (forms, search, CRUD), see FastMCPApp which adds managed tool binding on top of Prefab UI. For LLM-generated UIs, see Generative UI.
Getting Started
Here’s a tool that returns a bar chart:app=True flag tells FastMCP this tool returns a UI. When a host calls the tool, the user sees an interactive chart instead of a JSON blob. The Patterns page has more examples.
Layout and Components
Prefab uses Python’swith statement to express nesting. Containers like Column, Row, and Grid collect their children automatically:
ForEach (covered below).
The full component library — layout containers, data display, charts, forms, overlays — is documented in the Prefab component reference.
State and Reactivity
Display tools can be interactive without calling the server. The key is state — a client-side key-value store that lives in the browser. Components read from state, actions mutate it, and the UI re-renders automatically.Declaring State
Pass astate dict to PrefabApp to declare initial state, then use Rx("key") to create reactive references:
state dict on PrefabApp declares the keys and their starting values. Rx("dark_mode") creates a reactive reference that compiles to {{ dark_mode }} in the wire protocol.
Interactive components with a name prop automatically bind to state. The Switch(name="dark_mode") syncs its on/off value to the dark_mode state key on every toggle — no event wiring needed.
If(Rx("dark_mode")) shows its children only when the state key is truthy. When the switch flips, the condition re-evaluates instantly in the browser.
Reactive References with Rx
TheRx class is how you reference state in component props:
Rx("tip_pct") / 100 * Rx("bill") builds a compound expression — it doesn’t do the math in Python. The renderer evaluates it live as the sliders move. The .currency() pipe formats the result as currency.
Pipes
Rx objects support formatting pipes that transform values at render time:currency, percent, number, compact, round, and abs. String pipes include upper, lower, and truncate. See the Prefab expression docs for the full list.
Conditionals
The.then() method creates ternary expressions:
Dynamic Iteration with ForEach
Pythonfor loops generate static content at build time. When you need to iterate over state that can change — a list that grows, items that get filtered — use ForEach:
ForEach("members") iterates over the members state key. The as member gives you an Rx proxy scoped to each item, so member.name resolves to {{ $item.name }} in the wire protocol. If the members state changes (e.g., through an action), the list re-renders automatically.
Conditional Rendering
If, Elif, and Else control what’s visible based on state:
Giving the LLM Context
By default, Prefab sends"[Rendered Prefab UI]" as the text content for the LLM. If the model needs to reason about the data, wrap your return in a ToolResult with a meaningful summary:
Advanced
Customizing CSP
Customizing CSP
app=True auto-wires the Prefab renderer with default CSP settings. If your app loads external resources — embedding iframes, fetching from APIs, loading scripts — use PrefabAppConfig to add the required domains:PrefabAppConfig() with no arguments is equivalent to app=True. It auto-sets the renderer URI and merges the renderer’s CSP with any additional domains you provide.Type inference
Type inference
If your return type annotation is a Prefab type — Explicit
PrefabApp, Component, or unions containing them — FastMCP enables app rendering automatically, even without app=True:app=True is recommended for clarity.Mixing with custom HTML
Mixing with custom HTML
Prefab tools and custom HTML tools coexist on the same server:
Next Steps
- FastMCPApp — Managed tool binding for apps with heavy server interaction
- Patterns — Charts, tables, dashboards, and other common examples
- Development — Preview app tools locally with
fastmcp dev apps - Prefab UI Docs — Full component reference, advanced state patterns, and more

