← All Labs Lab 14 / 28 Two-way MCP App
Lab 14 · Tools & UI · Folded back

Two-way MCP App

A ui:// view calls app-only tools on its MCP server through the host without a model turn, while the model changes the same state from chat.

Folded back Needs: tool-calling Smoke: pnpm exec tsx examples/14-two-way-mcp-app/smoke.ts Run: pnpm tsx examples/14-two-way-mcp-app/server.ts --port 7710 Deck ↗ Source: examples/14-two-way-mcp-app

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_light through 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

Round trip: flip a switch, ask in chat, evening scene

  1. Flip a switch. The panel runs in an iframe and can only talk to the chat page, so it posts a JSON-RPC tools/call with an id. The page knows which MCP server the view came from (data-server="home") and sends POST /api/apps/tools/call to the agent. The agent calls RemoteMcpClient.callToolForApp, which refuses tools the server didn't list or didn't mark visible to apps, and sends a normal MCP tools/call to 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.
  2. Ask in chat. A normal model turn. The model sees show_lights and set_light, but not get_lights, because the habitat tool bridge skips app-only tools. The panel finds out on its next get_lights poll, which goes through the same path as step 1.
  3. Evening scene. The panel sends ui/message. The page sends its text to POST /api/chat as 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.

ToolVisible toPurpose
show_lightsmodel + appOpens the panel and returns the lights as structuredContent
set_lightmodel + app{room | "all", on?, brightness?}
get_lightsapp only (_meta.ui.visibility: ["app"])The panel polls it; the model never sees it

Run it with the chat example

sh
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

sh
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

Coding-Agent Prompt · Ready to Paste
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`.
Works in Pi, Claude Code, Codex & Antigravity Offline mock first → --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.visibility decides who sees a tool. callToolForApp refuses 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-result after ui/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-server when it renders the view.
  • Older MCP-UI views send { type, messageId } messages and expect ui-message-received / ui-message-response acknowledgements.
  • The lab had no check of its own. Its smoke: used to be pnpm run demo:mcp-skills, which runs _chat/e2e.ts and never starts this server. smoke.ts now covers both paths offline. The offline _chat demo model has no lights rule, so the smoke scripts its own faux model through startAgent({ model: "faux-home:lights" }) after registering the provider on getPiModels().

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.