# MCP Apps
URL: /docs/tools/mcp-apps

Render MCP App UI resources inline in chat. Native renderer for the Model Context Protocol Apps spec — sandboxed iframes, JSON-RPC bridge, AI SDK integration.

> For AI agents: a documentation index is available at [llms.txt](/llms.txt). Use `.md` for canonical markdown pages; `.mdx` is kept as a backwards-compatible alias on supported URL paths.

[MCP Apps](https://apps.extensions.modelcontextprotocol.io/) lets a Model Context Protocol server ship a UI resource alongside a tool: a self-contained HTML widget that the chat host renders inline when the tool is called. assistant-ui ships a native renderer that mounts the widget in a sandboxed iframe via [`SafeContentFrame`](/safe-content-frame) and runs a JSON-RPC postMessage bridge so the widget can call tools, send messages, request a display mode, and read host context.

## Overview

When an MCP server attaches a `_meta.ui.resourceUri` (the [`text/html;profile=mcp-app`](https://apps.extensions.modelcontextprotocol.io/api/index.html) MIME) to a tool, AI SDK forwards that metadata through the message stream. assistant-ui's renderer picks it up off the `mcp` field on `ToolCallMessagePart`, fetches the resource through your backend route, and mounts it.

The renderer only acts on URIs that start with `ui://` (per the MCP Apps spec). Tools whose `resourceUri` uses any other scheme are treated as non-MCP-Apps tools and fall through to your regular tool UI.

The widget communicates back through a JSON-RPC bridge:

- **widget → host requests**: `ui/initialize`, `tools/call`, `resources/read`, `resources/list`, `openLink`, `sendMessage`, `requestDisplayMode`, `updateModelContext`
- **host → widget notifications**: tool input streaming, tool result, host context changes
- **widget → host notifications**: initialized, size changed, log, error, request teardown

Capability presence is determined at mount time by which handlers you provide. Unknown methods return JSON-RPC `-32601`; bad params return `-32602`.

## Quick start

The renderer talks to a backend route you expose. The MCP client lives server-side so credentials and transport stay out of the browser. The route receives `{ method, params }` POSTs and dispatches to your MCP client.

### Install the MCP client

Install the AI SDK MCP client package used by the route handler:

```bash
npm install @ai-sdk/mcp
```

### Client

Compose `McpAppRenderer({...})` into your `Tools` resource. Provide `host.url` pointing at your route. Any tool-call part carrying `mcp.app` metadata renders the MCP App widget automatically.

```
import {
  AuiConfig,
  AuiProvider,
  Tools,
  McpAppRenderer,
  McpAppsRemoteHost,
  useAui,
} from "@assistant-ui/react";

function MyAssistant({ children }) {
  const aui = useAui();
  const config = AuiConfig({
    tools: Tools({
      toolkit: myToolkit,
      mcpApp: McpAppRenderer({
        host: McpAppsRemoteHost({ url: "/api/mcp-apps" }),
        hostInfo: { name: "my-app", version: "1.0.0" },
        hostContext: { theme: "light" },
      }),
    }),
  });
  return (
    <AuiProvider extends={aui} config={config}>
      {children}
    </AuiProvider>
  );
}
```

`McpAppsRemoteHost` is the default host strategy. It POSTs `{ method, params }` to your route. A different strategy (e.g. a client-side MCP client) can be plugged in by writing a custom resource that returns the same `McpAppsHost` shape (`{ loadResource, callTool, readResource, listResources }`).

Changes to `headers` or a custom `fetch` function apply to subsequent host requests without reloading the widget. When the account or workspace identity changes, key the host resource explicitly so its UI resource reloads.

Install the resource helper:

```
pnpm add @assistant-ui/tap
```

```
import { withKey } from "@assistant-ui/tap";

const mcpApp = McpAppRenderer({
  host: withKey(
    workspaceId,
    McpAppsRemoteHost({
      url: "/api/mcp-apps",
      headers: () => getWorkspaceHeaders(workspaceId),
    }),
  ),
});
```

`openLink` is auto-wired to `window.open(url, "_blank", "noopener,noreferrer")`; by default, `sendMessage` adds the text to the thread composer, after any draft, for the user to review and send, using the MCP Apps `{ role, content }` params and joining each `text` block in order with a newline, or accepting the legacy `string`, `{ prompt }`, `{ text }`, and `{ message }` forms. The host advertises `message: { text: {} }` during `ui/initialize`; non-text blocks are dropped, and a request carrying no text resolves to `{ isError: true }`.

Pass `handlers` when the widget needs host-side UI or lifecycle callbacks. The capabilities are advertised during mount, so provide the handlers before the widget initializes:

```
McpAppRenderer({
  host: McpAppsRemoteHost({ url: "/api/mcp-apps" }),
  handlers: {
    requestDisplayMode: ({ mode }) => applyHostDisplayMode(mode),
    updateModelContext: applyHostModelContext,
    openLink: ({ url }) => window.open(url, "_blank", "noopener,noreferrer"),
  },
});
```

`requestDisplayMode` should update host chrome and return the mode that was actually honored. Echoing `{ mode }` without applying it tells the widget the change succeeded while the host stays `inline`. If changing modes moves the app between layout regions, follow the [state-preserving placement guidance](/docs/guides/part-grouping#render-tool-calls-as-flat-rows) to avoid reloading its iframe.

Caller handlers override the default `openLink` and `sendMessage` behavior and can provide `requestDisplayMode`, `updateModelContext`, and lifecycle hooks such as `onInitialized` and `onSizeChange`. `callTool`, `readResource`, and `listResources` remain bound to the configured `host`; use a custom host to change those data-plane operations.

### Per-app options

One renderer serves every MCP app in the thread, so `hostContext`, `handlers`, and the other display options are thread-wide defaults. Pass `forPart` when an app needs its own values. It runs for each MCP-app tool call, and every option it returns replaces the thread-wide one for that app alone.

`handlers` is the exception and merges per key, so a resolver that returns only `requestDisplayMode` keeps the thread-wide `onError` and `onInitialized` for that app. Its keys are capabilities negotiated with the widget rather than data, and replacing the whole bag to add one handler would withdraw the rest from that app with nothing to observe it by. Name a key to override it.

```
const [modes, setModes] = useState<Record<string, McpAppDisplayMode>>({});
const declaredModes = useRef(
  new Map<string, McpAppDisplayMode[] | undefined>(),
);
const grantedModes = useRef(new Map<string, McpAppDisplayMode>());

McpAppRenderer({
  host: McpAppsRemoteHost({ url: "/api/mcp-apps" }),
  hostInfo: { name: "my-app", version: "1.0.0" },
  hostContext: { theme },
  forPart: (part) => ({
    hostContext: {
      theme,
      displayMode: modes[part.toolCallId] ?? "inline",
      availableDisplayModes: ["inline", "fullscreen", "pip"],
    },
    handlers: {
      onInitialized: ({ appCapabilities }) => {
        declaredModes.current.set(
          part.toolCallId,
          appCapabilities?.availableDisplayModes,
        );
      },
      requestDisplayMode: ({ mode }) => {
        const declared = declaredModes.current.get(part.toolCallId);
        if (declared && !declared.includes(mode)) {
          return {
            mode: grantedModes.current.get(part.toolCallId) ?? "inline",
          };
        }
        grantedModes.current.set(part.toolCallId, mode);
        setModes((current) => ({ ...current, [part.toolCallId]: mode }));
        return { mode };
      },
    },
  }),
});
```

The resolver closes over the part, so `requestDisplayMode` knows which app asked and the granted mode reaches that app alone through `notifications/host_context/changed`. Returned values may be rebuilt on every render; a host context that is structurally unchanged never reaches the widget.

`onInitialized` receives what the app sent in `ui/initialize`, including the display modes it supports in `appCapabilities.availableDisplayModes`. The MCP Apps spec forbids switching an app into a mode missing from that list, so the example answers such a request with the mode it last granted. The declared and granted modes live in refs because an app can send its next request before the host re-renders.

`forPart` resolves before a part's first render, so a per-part value is always in place from the start. What a *later* change to one reaches depends on the option. `hostContext` is delivered again whenever it changes. Handlers are called through live references, but which handler keys exist is captured when the frame mounts, so return the same keys for a part on every render. `hostInfo` and the frame settings in `sandbox` (`className` and `style` aside) are also read once at mount, so changing them mid-life does nothing until the app's resource URI changes and the frame is rebuilt.

### Route handler

The route accepts `POST` requests with `{ method, params }` JSON bodies. Dispatch by method name and return the result as JSON. Example for Next.js App Router:

```
// app/api/mcp-apps/route.ts
import { createMCPClient } from "@ai-sdk/mcp";

const serverUrls = new Map([
  ["search", process.env.SEARCH_MCP_SERVER_URL!],
  ["calendar", process.env.CALENDAR_MCP_SERVER_URL!],
]);
const fallbackServerUrl = process.env.MCP_SERVER_URL!;
const clientPromises = new Map<string, ReturnType<typeof createMCPClient>>();

const getClient = (serverId?: string) => {
  const url = serverId ? serverUrls.get(serverId) : fallbackServerUrl;
  if (!url) throw new Error(`Unknown MCP server: ${serverId}`);
  let clientPromise = clientPromises.get(url);
  if (!clientPromise) {
    clientPromise = createMCPClient({
      transport: { type: "sse", url },
    }).catch((error) => {
      clientPromises.delete(url);
      throw error;
    });
    clientPromises.set(url, clientPromise);
  }
  return clientPromise;
};

const findListedResource = async (
  client: Awaited<ReturnType<typeof createMCPClient>>,
  uri: string,
) => {
  const seenCursors = new Set<string>();
  let cursor: string | undefined;
  try {
    while (true) {
      const result = await client.listResources(
        cursor ? { params: { cursor } } : undefined,
      );
      const match = result.resources.find((resource) => resource.uri === uri);
      if (match) return match;
      const nextCursor = result.nextCursor;
      if (!nextCursor || seenCursors.has(nextCursor)) return undefined;
      seenCursors.add(nextCursor);
      cursor = nextCursor;
    }
  } catch {
    return undefined;
  }
};

export async function POST(req: Request) {
  const { method, params } = await req.json();
  const client = await getClient(params?.serverId);

  switch (method) {
    case "mcp-apps/read-resource": {
      const { contents } = await client.readResource({ uri: params.uri });
      const c = contents.find((x: { uri: string }) => x.uri === params.uri);
      const readMeta = c?._meta?.ui;
      const listed = readMeta
        ? undefined
        : await findListedResource(client, params.uri);
      return Response.json({
        uri: params.uri,
        mimeType: "text/html;profile=mcp-app",
        html: c?.text ?? "",
        meta: readMeta ?? listed?._meta?.ui,
      });
    }
    case "tools/call": {
      const tools = await client.tools();
      const tool = tools[params.name];
      if (!tool?.execute) {
        return Response.json({ error: "Tool not callable" }, { status: 400 });
      }
      return Response.json(
        await tool.execute(params.arguments ?? {}, {
          toolCallId: `mcp-apps-bridge-${crypto.randomUUID()}`,
          messages: [],
        }),
      );
    }
    case "resources/read":
      return Response.json(await client.readResource({ uri: params.uri }));
    case "resources/list": {
      const { serverId: _, ...listParams } = params ?? {};
      return Response.json(await client.listResources({ params: listParams }));
    }
    default:
      return Response.json({ error: "Unsupported method" }, { status: 400 });
  }
}
```

The renderer POSTs four method names: `mcp-apps/read-resource`, `tools/call`, `resources/read`, `resources/list`. Reject anything else server-side and apply your own auth / rate limiting in the route.

Forward the resource's `_meta.ui` object as `meta`. Metadata on the `resources/read` content item overrides metadata on the matching `resources/list` entry, so fall back to the list entry only when the content item omits it. The renderer enforces its `csp` declarations before any widget code runs. `connectDomains`, `resourceDomains`, `frameDomains`, and `baseUriDomains` accept HTTP(S) origins (`connectDomains` also accepts WebSocket origins); malformed entries are ignored. When the resource omits a declaration, the corresponding capability stays restricted, including `connect-src 'none'` and `frame-src 'none'` defaults.

### Multiple MCP servers

When a tool part carries `mcp.app.serverId`, the renderer forwards it to the host operations as `params.serverId` so the route can select the MCP client that owns the resource or tool. The agent stack emits this routable identity in part metadata. For `@ag-ui/mcp-apps-middleware`, assistant-ui uses its configured `serverId`, falling back to `serverHash` when `serverId` is absent or empty. Match the route's map keys to whichever identity your setup emits: configure an explicit `serverId` on each middleware server, or key the map by the emitted hashes. Omitting `serverId` preserves the single-server behavior, as shown by the fallback client in the route example.

Per-name `setToolUI` registrations always win over the MCP fallback, so you can still customize specific tools.

## AI SDK integration

AI SDK tool UI parts carry tool-owned metadata in `toolMetadata`. `@assistant-ui/ai-sdk` forwards `toolMetadata.app` into `ToolCallMessagePart.mcp.app`, while retaining `callProviderMetadata.mcp.app` and result `_meta` as compatibility sources. With an MCP-Apps-capable MCP server, no extra wiring is required on the part shape.

The `serverId` routing key comes only from `callProviderMetadata.mcp.app`. A `serverId` supplied in tool metadata or result `_meta` is ignored.

The rich UI comes from the MCP server's metadata, not from the model, so the path is identical whichever provider drives the conversation. Running Claude only changes the `model:` in `streamText` (`anthropic("claude-sonnet-5")` via `@ai-sdk/anthropic`); the MCP server, `splitMCPAppTools`, and the renderer are unchanged. MCP Apps is an open standard in the MCP ecosystem (Claude is one of its hosts), so a standard MCP-Apps server renders without the bridge. The bridge below is only needed for servers that use OpenAI's `openai/outputTemplate` convention, again independent of which model you run.

On the chat route, use `splitMCPAppTools()` (from `@ai-sdk/mcp`) to keep app-only tools out of the model's view:

```
import { splitMCPAppTools } from "@ai-sdk/mcp";

const tools = await client.listTools();
const { modelVisible } = splitMCPAppTools(tools);

const result = streamText({
  abortSignal: req.signal,
  model: openai("gpt-6-luna"),
  tools: client.toolsFromDefinitions(modelVisible),
  // ...
});
```

### OpenAI Apps SDK servers

[OpenAI Apps SDK](https://developers.openai.com/apps-sdk) servers carry the same `ui://` template under a different convention: the pointer is `_meta["openai/outputTemplate"]` on the tool definition (not `_meta.ui.resourceUri`), and the resource is served as `text/html+skybridge` rather than `text/html;profile=mcp-app`. `@ai-sdk/mcp` does not recognize `openai/outputTemplate`, so it never populates `toolMetadata.app` and the renderer stays idle.

The renderer needs no change; you only have to surface the pointer. assistant-ui reads the canonical `result._meta.ui.resourceUri` off tool results (and still accepts the deprecated flat `result._meta["ui/resourceUri"]`), so the smallest bridge is to copy the template onto the result by tool name. Build the map once from the tool listing, then stamp it inside each tool's `execute`:

```
import type { Tool } from "ai";

// reuse the listTools() result from the AI SDK integration step above; no second round-trip
const templateByTool = new Map(
  tools.tools
    .filter((t) => typeof t._meta?.["openai/outputTemplate"] === "string")
    .map((t) => [t.name, t._meta["openai/outputTemplate"] as string]),
);

const withTemplateUri = (tool: Tool, name: string): Tool => {
  const uri = templateByTool.get(name);
  const exec = tool.execute;
  if (!uri || !exec) return tool;
  return {
    ...tool,
    execute: async (args, options) => {
      const result = (await exec(args, options)) as { _meta?: Record<string, unknown> };
      return {
        ...result,
        _meta: {
          ...result._meta,
          ui: { ...(result._meta?.["ui"] as Record<string, unknown>), resourceUri: uri },
        },
      };
    },
  } satisfies Tool;
};
```

Wrap the AI SDK tool objects before handing them to `streamText`:

```
const aiTools = await client.tools();
const wrappedTools = Object.fromEntries(
  Object.entries(aiTools).map(([name, t]) => [name, withTemplateUri(t, name)]),
);
// pass wrappedTools to streamText
```

Your `mcp-apps/read-resource` handler reads the `ui://` resource as in the route example above. Set the response `mimeType` to the `text/html;profile=mcp-app` literal that `McpAppResource` expects and keep the server's HTML in `html`; don't forward the raw `text/html+skybridge` value, which the type rejects.

`@ai-sdk/mcp` does not currently read `openai/outputTemplate`. If it includes that pointer in tool metadata, `@assistant-ui/ai-sdk` forwards it through `toolMetadata.app`, and the result metadata bridge is unnecessary.

## AG-UI integration

With `@assistant-ui/react-ag-ui`, the backend associates an MCP App with a tool call through an `ACTIVITY_SNAPSHOT`. Include the tool call ID, the app's `ui://` resource URI, and the MCP server identity when routing across multiple servers:

```
{
  "type": "ACTIVITY_SNAPSHOT",
  "activityType": "mcp-apps",
  "content": {
    "toolCallId": "call-1",
    "resourceUri": "ui://maps/result.html",
    "serverId": "maps",
    "result": {
      "content": [{ "type": "text", "text": "Map ready" }],
      "structuredContent": { "center": [37.77, -122.42] },
      "_meta": { "initialView": "street" },
      "isError": false
    }
  }
}
```

`serverId` is optional. If it is absent, the runtime accepts `serverHash` as a fallback. Older middleware may omit `toolCallId`, in which case the snapshot applies to the last resolved tool call; new integrations should include it so concurrent tool calls are correlated unambiguously. Emit the snapshot after the tool call's `TOOL_CALL_START`. A snapshot that names a tool call restored from prior history applies to that message directly, so re-emitting the activity after a `MESSAGES_SNAPSHOT` rehydrates its widget; a snapshot for a tool call ID the runtime has never seen is silently ignored.

Send the normal `TOOL_CALL_RESULT` with a concise, model-visible summary:

```
{
  "type": "TOOL_CALL_RESULT",
  "toolCallId": "call-1",
  "content": "Map ready"
}
```

The snapshot's `result` becomes the widget-visible `part.result`, while the `TOOL_CALL_RESULT.content` string is retained for the model as `part.modelContent`, a text content-part array (`[{ "type": "text", "text": "Map ready" }]`).

Alternatively, AG-UI servers can place MCP host fields directly on `TOOL_CALL_RESULT`:

```
{
  "type": "TOOL_CALL_RESULT",
  "toolCallId": "call-1",
  "content": "Map ready",
  "structuredContent": { "center": [37.77, -122.42] },
  "_meta": { "initialView": "street" },
  "isError": false
}
```

In this form, the runtime assembles a `CallToolResult`-shaped `part.result` from `content`, `structuredContent`, `_meta`, and `isError`. At least one of `structuredContent` or `_meta` must be present; when both are absent the enriched path does not activate and the result falls back to the plain `content` string. The `_meta` can also carry the app pointer itself (canonical `ui.resourceUri`, or the deprecated flat `"ui/resourceUri"`), in which case no `ACTIVITY_SNAPSHOT` is needed to activate the widget. A snapshot remains the only carrier for server identity (`serverId`), and when both provide a `resourceUri` the snapshot wins.

The visibility split follows the MCP Apps contract: `content` is model-visible; `structuredContent` and `_meta` are available only to the host and widget. When assistant-ui serializes history into a later AG-UI request, it sends the saved model-visible text and never includes `structuredContent` or `_meta`.

## Bridge protocol

The bridge implements the MCP UI JSON-RPC protocol over `window.postMessage`, filtered by both `event.source === frame.iframe.contentWindow` and `event.origin === frame.origin`, the cross-origin domain `SafeContentFrame` issues per render. Messages from any other origin or window are dropped silently.

### Widget → host requests

| Method               | Notes                                                                                                                            |
| -------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `ui/initialize`      | Returns `{ protocolVersion, host, hostContext, capabilities }`. Always supported.                                                |
| `tools/call`         | Routed to `host.url` with method `tools/call`. Optional `handlers.allowedTools` allowlist. Invalid `arguments` shape → `-32602`. |
| `resources/read`     | Routed to `host.url` with method `resources/read`.                                                                               |
| `resources/list`     | Routed to `host.url` with method `resources/list`.                                                                               |
| `openLink`           | Requires `handlers.openLink`. Rejects non-`http(s)` URLs with `-32602`.                                                          |
| `sendMessage`        | Requires `handlers.sendMessage`.                                                                                                 |
| `requestDisplayMode` | Requires `handlers.requestDisplayMode`. Modes: `inline`, `fullscreen`, `pip`.                                                    |
| `updateModelContext` | Requires `handlers.updateModelContext`.                                                                                          |

When a handler isn't provided, the bridge returns JSON-RPC `-32601` (method not found) and reports the matching capability as unavailable in the `ui/initialize` response.

### Host → widget notifications

- `notifications/tools/call/input`: sent whenever `part.args` (the streaming tool input) changes
- `notifications/tools/call/result`: sent when the tool result lands (including error envelopes)
- `notifications/host_context/changed`: sent when `hostContext` changes (e.g. user toggles theme)

### Widget → host notifications

The widget sends `notifications/initialized`, `notifications/size_changed`, `notifications/log`, `notifications/error`, and `notifications/request_teardown`. Handle them with `handlers.onInitialized`, `onSizeChange`, `onLog`, `onError`, and `onRequestTeardown` respectively. `onInitialized` receives the params of the app's `ui/initialize` request as `McpAppInitializeParams` (`appInfo`, `appCapabilities`, and `protocolVersion`); a malformed field is omitted, and `availableDisplayModes` keeps only the modes the bridge recognizes.

If the widget never sends `notifications/initialized` (broken or non-spec-compliant), the host flushes its queued notifications after a 5-second safety timeout so the iframe doesn't appear hung.

## Sandboxing

The iframe is built with [`SafeContentFrame`](/safe-content-frame), which serves each widget from its own hashed cross-origin domain so the host page is not reachable by `same-origin` references. The hash uses a random salt per render, or a salt derived from the content when `enableBrowserCaching` is set. The iframe always carries the `allow-same-origin allow-scripts` sandbox flags, and the `sandbox` array adds to them. Set both through the `sandbox` field on `McpAppRendererOptions`:

```
McpAppRenderer({
  // ...
  sandbox: {
    sandbox: ["allow-forms", "allow-popups"],
    enableBrowserCaching: true,
    className: "my-mcp-app",
  },
});
```

## Security notes

- Widgets run cross-origin in a sandboxed iframe. The bridge filters incoming messages by both source window and origin.
- Resource CSP metadata is enforced inside the widget document. Preserve `_meta.ui.csp` when adapting `resources/read` responses; omitted or malformed declarations use restrictive defaults.
- The host route is your auth boundary. Apply session checks, rate limiting, and per-tool allowlists there. The renderer trusts whatever the route returns.
- `openLink` rejects non-`http(s)` URLs at the bridge layer, but your `openLink` handler should still treat the URL as untrusted (e.g. always use `noopener,noreferrer`).
- Keep custom `host` and `handlers` references stable across renders (e.g. module-scope constants or `useMemo`); an unstable custom-host identity keeps the widget in `loadingFallback` while refetching on every parent re-render.