app=True and return a component tree or a PrefabApp:
When you install
fastmcp[apps], FastMCP pulls in prefab-ui with only a minimum version — no upper bound. For production, pin prefab-ui explicitly in your own dependencies (e.g. prefab-ui==0.15.0). See Versioning Policy for details.ui:// resource, sets the MCP Apps metadata on your tool, and converts PrefabApp returns to structuredContent in the tool result.
How It Works
The flow has three steps:- Tool call — the host calls your tool via MCP
- PrefabApp → structuredContent — FastMCP serializes your return value into the Prefab JSON envelope, including the view, initial state, and any app tools
- Renderer — the host loads the registered Prefab renderer resource and passes it the structured content. The renderer builds the UI.
CallTool can send a request through MCP back to your server. Its response can contain data or a new component tree; response actions decide how to apply it to the UI. App-provided tools let the agent invoke an interaction on the open app as well.
Returning UI
You can return either aPrefabApp or a bare component. Returning a component is a shorthand — FastMCP wraps it in a PrefabApp automatically.
PrefabApp when you need initial state, a page title, or reusable definitions. Return a bare component for simple, stateless views.
Patterns
Calling Back to the Server
CallTool is the MCP equivalent of Fetch. It sends a tool call through MCP, and the response is available as $result (Python: RESULT) in the on_success callback:
browse) defines the layout with a Slot. The second tool (search) returns a component tree that fills that slot. This is the core MCP Apps pattern: the initial tool sets up the shell, and subsequent tool calls swap content in and out.
search doesn’t need app=True — it’s a helper tool called from within the UI, not an entry point that a host would show directly. FastMCP still auto-wires it because its return type is PrefabApp.Dynamic Component Results with Slot
When aCallTool writes its result into state via SetState("key", RESULT) in on_success, a Slot watching that key renders whatever component tree arrives:
Slot shows its fallback children until a CallTool populates the state key. This is the same pattern as Dynamic Component Routes in the API Server guide — the server decides what to render, not just what data to return.
Error Handling
If a tool raises an exception, the MCP protocol surfaces it as an error. Useon_error to display it:
Tool Visibility
By default, all tools registered on your server are visible to the model — including helper tools meant only for UI interactions. When the model can see tools likesearch or delete_item, it may call them directly instead of letting the user interact through the UI.
Use AppConfig(visibility=["app"]) to mark tools as app-only. These tools remain callable via CallTool from the UI, but the host should exclude them when presenting tools to the model:
app=True so the model can invoke it. Helper tools use AppConfig(visibility=["app"]) so they’re accessible to CallTool but won’t appear in the model’s tool list.
Tool visibility is metadata that the host is responsible for enforcing. The server returns all tools from
tools/list regardless of visibility — the host filters based on the visibility field in the tool’s app metadata.Host Capabilities
Once your app is running, its MCP host connects it to the conversation. Use Messages for user-initiated follow-up requests and Model Context to share the current selection or other useful state. App-provided tools let the agent invoke operations on the open view, and Display Modes let the UI request more space. Availability depends on the host.API Server vs FastMCP
Both use the same components and state model. The difference is transport:
If you’re building an MCP server, use
CallTool. If you’re building a web app, use Fetch. SendMessage and UpdateContext are MCP-only — they communicate with the host’s conversation, which doesn’t exist in standalone mode. The component tree and client-side actions are identical either way.
Example App
Theexamples/hitchhikers-guide directory contains a complete working MCP server — a Hitchhiker’s Guide catalog with search, dialog-based entry creation, inline deletion, and error handling. The same directory also contains a FastAPI version of the same app for comparison.