# MCP Apps

OpenProgram hosts the MCP Apps extension (`io.modelcontextprotocol/ui`, SEP-1865): a tool whose `_meta.ui.resourceUri` names a `ui://` resource gets that HTML view drawn in its tool card in Web chat. The client side is in [MCP client integration](https://openprogram.io/docs/reference/design/integrations/extensions/mcp-integration.html); the user page is [MCP Apps](https://openprogram.io/docs/capabilities/mcp-apps.html).

## From tool call to view

1. **Initialize** client advertises the extension
2. **Tool call** model calls a tool with a view
3. **resources/read** adapter fetches the view while the call runs
4. **Tool block** `mcp_app` as progress, then on the result
5. **Tool card** two sandboxed frames
6. **Call back** tools, resources, messages, model context

| Step | Where and how |
| --- | --- |
| Capability | `AppsClientSession` (`extensions/mcp/client.py`) adds `capabilities.extensions["io.modelcontextprotocol/ui"] = {"mimeTypes": ["text/html;profile=mcp-app"]}` to `initialize`. |
| Tool list | Tools whose `_meta.ui.visibility` lacks `"model"` are not registered as Agent tools. `ui://` resources are left out of `list_resources` and stay readable by URI. |
| Fetch | When a tool with a view is called, the adapter (`extensions/mcp/adapter.py`) reads the resource with `app_view` (`extensions/mcp/apps.py`) beside the call. If the view arrives first it goes out as a progress update, `{server, tool, resource_uri, html, csp, prefers_border}` on `details["mcp_app"]`; at the end `with_result` adds `tool_result`. HTML is capped at 2 MiB and the raw result at 512 KiB; a failed read leaves the plain result. |
| Transport | While the call runs, `event_parsing` turns the progress update into a `tool_view` stream event, and `stream_tap` puts the view on the running tool block. At the end it rides the `tool_execution_end` event onto the live block and the call the turn tallies (`turn_tally`), so the reply's final write (`turn_runtime/persistence.py`) keeps it on the stored block; the transcript projection copies it onto the drawn block (one optional field, `Block.mcp_app`). A reload draws the view again. |
| Card | `McpAppView` replaces the step body; the card opens by default and folds like any step. |
| Bridge | `apps/web/lib/chat/mcp-apps/bridge.ts` speaks the spec's JSON-RPC: `ui/initialize`, then the tool input at once and the result (or `tool-cancelled`) when the call ends, then theme and width changes as `host-context-changed`. |
| Call back | A view's `tools/call` goes to `POST /api/mcp/apps/call` and `call_from_app`; the answer returns to the view as the JSON-RPC result. |
| Resources | `resources/read` goes to `POST /api/mcp/apps/resource` and `read_for_app`, always on the view's own server. |
| Messages | `ui/message` text goes through the conversation's own send path (`sendFromTranscript`): sent now when idle, queued like the composer while a turn runs. |
| Model context | `ui/update-model-context` goes to `POST /api/mcp/apps/context`. `update_model_context` stores the latest text per view on the session (`runtime/conversation/view_context.py`); the next turn takes it once as the `view_context` attachment of its turn context. |

## Decisions

| Decision | Choice | Basis |
| --- | --- | --- |
| Where the view sits | Inside the tool card, sized to content, fallback 150 px, maximum 800 px | Antigravity draws an inline card with the same bounds; a view belongs to one call. |
| Advertising support | The spec's client extension entry | Codex advertises the same entry and reads `_meta.ui.resourceUri`; servers send views only to hosts that declare it. |
| Network policy | Deny everything; add only the origins the resource declares in `_meta.ui.csp` | ZCode builds its policy from the declared origins (`csp.ts`); Antigravity's default is deny. |
| What a view may call | Tools of its own server marked `"app"` in `visibility`, 4 at a time | ZCode limits calls to the owning server's app-visible tools; Antigravity scopes them to the server and allows 4 concurrent. |
| Approval | The conversation's existing permission decision. A call that needs asking opens a live wait on its own execution in the conversation's session, so the conversation shows the same approval card a turn shows and answers it through the same command; Auto mode asks too. | ZCode sends app calls through its existing permission chain. Auto's reviewer judges a call against the turn that made it, and a view's call has no turn. |
| Showing while running | Send the view and the tool input as soon as the view is read; the result when it lands | The spec sends `tool-input` before `tool-result`; Antigravity mounts the view at the call. |
| Messages | The ordinary send path, text blocks only, 32 KiB; one waiting message per view | ZCode turns `ui/message` into a composer follow-up (`pluginUiFollowUp.ts`). |
| Model context | Latest text per tool call, redacted, 16 KiB, handed to the next turn once | ZCode keeps one context per tool call with the same 16 KiB cap (`pluginUiModelContext.ts`). |
| Display modes | Inline only | ZCode's fullscreen is its own side pane, which would need a new layout here; Antigravity draws inline. |
| `ui://` in resource lists | Hidden, still readable | Claude Code skips `ui://` resources in its lists and reads them by URI. |
| Terminal UI | Nothing; the text result is shown | Codex's TUI and the Antigravity CLI render nothing either. |
| Where the HTML loads | A server route, `/mcp-app-frame`, that relays to an inner `srcdoc` frame | A `srcdoc` frame in the App page would inherit the App's script policy; the route sets the view's own policy as a header, like `/visual-frame`, and follows the spec's sandbox-proxy messages. |

## Security model

### Two frames

| Frame | Settings |
| --- | --- |
| Proxy (`/mcp-app-frame`) | `sandbox="allow-scripts"`, `referrerpolicy="no-referrer"`. No `allow-same-origin`, so it runs in an opaque origin though served by the App. Response headers: the view's CSP (with its own `sandbox allow-scripts`), `Permissions-Policy` denying camera, microphone, geolocation, display capture, payment, USB and clipboard, `nosniff`, `no-referrer`, `no-store`. The opaque-origin request carries no owner cookie, so owner auth lets this one GET through (`owner_auth.MCP_APP_FRAME_PATH`) without the App shell's headers: the page holds no data and the view arrives by `postMessage`. |
| View (inner `srcdoc`) | `sandbox="allow-scripts"`, created once on the first `sandbox-resource-ready`; inherits the proxy's CSP. No forms, pop-ups, top navigation, downloads or modals. If it navigates itself, the proxy removes it. |

### Content Security Policy

```
sandbox allow-scripts; default-src 'none';
script-src 'unsafe-inline' blob: data: <resourceDomains>;
style-src 'unsafe-inline' <resourceDomains>;
img-src / media-src data: blob: <resourceDomains>; font-src data: <resourceDomains>;
connect-src <connectDomains> | 'none'; frame-src <frameDomains> | 'none';
base-uri <baseUriDomains> | 'none'; worker-src blob:;
object-src 'none'; form-action 'none'; frame-ancestors 'self'
```

- Declared origins must be `https` or `wss` with an optional `*.` prefix and port; `http` and `ws` only for `localhost`, `127.0.0.1` and `[::1]`. Anything else is dropped.
- `'self'` never appears, so a view cannot reach the App's API. `frame-src 'none'` also blocks the view from navigating its own frame to the App.

### Message validation

| Check | Rule |
| --- | --- |
| Sender | The card accepts a message only when `event.source` is its own proxy window; the proxy relays only between its parent and its inner frame. |
| Shape | JSON-RPC 2.0 objects only; anything else is ignored. |
| Order | The resource is sent once; requests before `ui/initialize` get `-32002`. |
| `tools/call` | Name must be a string and arguments an object (`-32602`); a fifth concurrent call is refused. The view names only the tool; the card adds the server from the payload. |
| `resources/read` | The URI must be a non-empty string (`-32602`); the server comes from the payload, and a failed read is `-32603`. |
| Messages and context | A message needs role `user` and text (`-32602`); while the view's last message still waits, the next gets `isError`. Context needs an array or object (`-32602`). |
| Other requests | `ui/open-link` accepts `http` and `https` and opens with `noopener`; `ui/request-display-mode` always answers `inline`; size changes are clamped; every other method gets `-32601`. |

### Call-back checks

`call_from_app` refuses, in order:

1. an unknown conversation (404 at the route)
2. a server that is not connected
3. a tool not on that server
4. a tool without `"app"` visibility
5. a `deny` verdict
6. an `ask` or `auto` verdict the user declines, or leaves for 5 minutes

The verdict comes from `permission_decision` with the conversation's mode and merged rules, the same function model calls use; a session or always answer is saved as a rule the same way. A refusal returns an `isError` result naming the reason. `read_for_app` reads only from the view's server and refuses an empty or over-long URI and contents over 2 MiB.

### Verified in Chrome

`tests/support/mcp_apps/hostile.html`, served by the fixture server with `--hostile`, tries each escape. In Chrome all fail:

- `localStorage`, cookies, `top.document` and `parent.document` raise `SecurityError`; top navigation is refused.
- `fetch` of another site and of `/api/mcp/servers` is blocked by `connect-src`.
- Calls to `filesystem__read_file` and `read_file` are refused as not on the server; reads of `file:///etc/hosts` and `filesystem://etc/hosts` fail on the view's server; a direct `top.postMessage` gets no answer.

## Tests

| File | Covers |
| --- | --- |
| `tests/unit/programs/mcp/test_mcp_apps.py` | URI and visibility parsing, origin filtering, CSP, frame route headers, payload and caps, event and block transport, the view during the call, call-back gating, ask and auto outcomes, reads limited to the server, bounded redacted context taken once by the next turn |
| `tests/integration/programs/test_mcp_apps.py` | Against `tests/support/mcp_apps/counter_server.py`: capability negotiation, app-only tools unregistered, view payload, `ui://` hidden but readable, a call and a read back, the view before a slow result, a real approval card answered through the wait command |
| `tests/component/agent/turns/test_mcp_app_turn.py` | A real turn that loads the deferred tool with `tool_search` and calls it inside a task step: the view on the live result, on the stored block beside the task stamps, and on the block a reload draws |
| `apps/web/tests/chat/mcp-apps/bridge.test.mjs` | Handshake, call gating, malformed messages, concurrency, links, resources, messages and the queue, model context, result after a running start, unsupported methods, height clamping, context changes |

## Implementation status

| Item | State |
| --- | --- |
| Inline views while running, tool input and result, theme, sizing, links, call back, resources, messages, model context, approval | Implemented in Web and desktop chat |
| Fullscreen and picture-in-picture | Not offered; the host always answers `inline` (see Decisions) |
| Partial tool input | The view gets the whole input once the call starts, not argument deltas while the model writes it |
| View permissions | Camera, microphone and the like stay denied; `_meta.ui.permissions` is ignored |
| Terminal UI | Shows nothing |

[MCP client integration](https://openprogram.io/docs/reference/design/integrations/extensions/mcp-integration.html)
