A small MCP server whose ui://home-lights/panel view is a light panel you can use from the chat in either direction:
- Agent → panel. Ask "turn off the kitchen and dim the living room to 25%". The model calls
set_light, and the panel shows the change within 2 seconds, flagging "Changed outside this panel". - Panel → server. Flip a switch or drag a slider. The view calls
set_lightthrough the host (tools/call). No model turn runs, and nothing is added to the transcript. - Panel → agent. "Ask the agent: evening scene" sends a
ui/message, which becomes your next chat message.
How the round trip works
- Flip a switch. The panel runs in an iframe and can only talk to the chat page, so it posts a JSON-RPC
tools/callwith an id. The page knows which MCP server the view came from (data-server="home") and sendsPOST /api/apps/tools/callto the agent. The agent callsRemoteMcpClient.callToolForApp, which refuses tools the server didn't list or didn't mark visible to apps, and sends a normal MCPtools/callto this server. The result comes back the same way, and the page posts it only to the iframe that asked, under the same id. The model is never called. - Ask in chat. A normal model turn. The model sees
show_lightsandset_light, but notget_lights, because the habitat tool bridge skips app-only tools. The panel finds out on its nextget_lightspoll, which goes through the same path as step 1. - Evening scene. The panel sends
ui/message. The page sends its text toPOST /api/chatas your next message, so the model does the work.
The call goes through the agent rather than from the browser straight to the MCP server. That keeps the MCP connection and its credentials on the server side, where the visibility check runs.
| Tool | Visible to | Purpose |
|---|---|---|
show_lights | model + app | Opens the panel and returns the lights as structuredContent |
set_light | model + app | {room | "all", on?, brightness?} |
get_lights | app only (_meta.ui.visibility: ["app"]) | The panel polls it; the model never sees it |
Run it with the chat example
pnpm tsx examples/14-two-way-mcp-app/server.ts --port 7710
pnpm tsx examples/_chat/agent.ts --port 7702 \
--model mycel:z-ai/glm-5.3-flash --connect home=http://127.0.0.1:7710/mcp
pnpm tsx examples/_chat/chat-ui.ts --port 7700 --agent http://127.0.0.1:7702
Then open http://localhost:7700 and say "Show me the lights in the house". mycel: models need MYCEL_URL and MYCEL_API_KEY; any provider:model that @umwelten/pi resolves works.
State lives in memory; restart the server to reset the lights.
Smoke
pnpm exec tsx examples/14-two-way-mcp-app/smoke.ts
Runs offline with no API keys. It starts this server, the _chat agent and chat page on free ports, connects the server as home, and drives both directions. The view path goes through the host's POST /api/apps/tools/call. The model path is a real chat turn with a scripted pi-ai faux model that calls set_light {room: "kitchen", on: false}. It prints one PASS/FAIL line per check (tool visibility, the model's tool set, view get/set without a transcript entry, "all" off, refusals, the round trip, the ui:// split, and the panel's protocol strings) and exits 1 if any check fails.
The last part of the tools-ui deck (published, source) walks through this example.
Prompt
Build a lab under `examples/14-two-way-mcp-app/`: an MCP server for home lights whose `ui://home-lights/panel` view works in both directions inside the chat client in `examples/_chat/`.
Read first: `packages/protocols/src/mcp/client/remote.ts` (`callToolForApp`, `mcpToolVisibility`), `packages/habitat/src/durable/extensions.ts` (`bridgeMcpClientToExtension` skips tools not visible to the model), `examples/_chat/agent.ts` (`POST /api/apps/tools/call`) and `examples/_chat/ui/index.html` (the MCP Apps host: ui/initialize, tools/call, ui/message, ui/open-link).
Requirements:
1. `server.ts` exports `startHomeServer({ port })` and runs standalone with `--port` (default 7710). Five rooms with on/off and brightness, kept in memory.
2. Tools: `show_lights` (model + app) returns the panel view (mime `text/html;profile=mcp-app`) plus `structuredContent.lights`; `set_light` (model + app) takes `{ room | "all", on?, brightness? }`; `get_lights` is app-only (`_meta.ui.visibility: ["app"]`) and must never be offered to the model.
3. The panel speaks MCP Apps JSON-RPC over postMessage: ui/initialize, tools/call for switches, sliders and "All off" (no model turn), a polled get_lights every 2 s that flags rooms "Changed outside this panel", and an "Ask the agent: evening scene" button that sends ui/message so it becomes the next chat message.
4. README with a round-trip diagram (`round-trip.svg`) of the three flows: switch, ask in chat, evening scene.
Verify in a browser with the chat client on a real model (`--model mycel:z-ai/glm-5.3-flash`): the model opens the panel, a switch changes a light without adding to the transcript, and a chat request shows up on the panel. Strict TypeScript, no `any`.
--live opt-in
What we learned
Checked live in the chat client on mycel:z-ai/glm-5.3-flash: the model chose show_lights itself, a switch turned the bedroom on through set_light without adding to the transcript, and "turn off the kitchen and dim the living room to 25%" became two set_light calls that the panel showed within 2 seconds.
Answers
- View tool calls never touch the model. They go panel → chat page (postMessage) → agent (
POST /api/apps/tools/call) →RemoteMcpClient.callToolForApp→ MCP server, and back under the same JSON-RPC id. Routing through the agent keeps the MCP connection and its credentials server-side, where visibility is enforced. _meta.ui.visibilitydecides who sees a tool.callToolForApprefuses tools the server didn't list or didn't mark visible to apps; the habitat bridge skips app-only tools when registering tools for the model. A habitat test fails without that skip.
Gotchas
- Views reload when the transcript re-renders. The host re-sends the view's latest result as
ui/notifications/tool-resultafterui/notifications/initialized, or a reloaded panel shows stale state until its next poll. - The host has to know which server a view came from. The chat page tags each iframe with
data-serverwhen it renders the view. - Older MCP-UI views send
{ type, messageId }messages and expectui-message-received/ui-message-responseacknowledgements. - The lab had no check of its own. Its
smoke:used to bepnpm run demo:mcp-skills, which runs_chat/e2e.tsand never starts this server.smoke.tsnow covers both paths offline. The offline_chatdemo model has no lights rule, so the smoke scripts its own faux model throughstartAgent({ model: "faux-home:lights" })after registering the provider ongetPiModels().
Not done
- The panel learns about the agent's changes by polling every 2 s. A server push is the next step.
- "All off" and the evening scene button were not clicked in the live run.