Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions apps/claude/skills/mobile-dev/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ performance beside the device), before building when the user wants to watch. It
result gives a private `127.0.0.1` link, as `browserPanel.url` in its structured
result: open it in your in-app browser in the foreground, and reuse a tab that already
shows Mobile Dev. Opening a page can leave the Browser pane hidden behind a card in the
chat, so in the Claude desktop app's Code tab then call `mcp__ccd_view__show_pane` with
`pane: "preview"` (load it with ToolSearch first if it is deferred), so the panel shows
beside the chat without the user clicking Open. In T3 Code, open it with the
chat, even when the browser tool reports it opened, so in the Claude desktop app's Code
tab then call `mcp__ccd_view__show_pane` with `pane: "preview"` (load it with
ToolSearch first if it is deferred), so the panel shows beside the chat without the
user clicking Open. If `show_pane` reports that the session is not open in a window,
the pane stays closed: tell the user the panel is in the Browser pane, and on your next
turn that uses Mobile Dev, check `mcp__Claude_Browser__tabs_context` and call
`show_pane` again while it reports the pane hidden. In T3 Code, open it with the
`t3-code` server's `preview_open` (load it with ToolSearch first if it is deferred). T3
shows a page an agent opened as a floating preview that the user can dock beside the
chat, so a result with `visible: false` still counts as shown: do not open it again,
Expand Down
20 changes: 17 additions & 3 deletions packages/core/src/browser-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ export const BROWSER_SESSION_IDLE_MS = 5 * 60_000;
// Screenshots the user adds to chat travel as base64 images.
export const BROWSER_CONTEXT_MAX_BYTES = 16 * 1024 * 1024;
const NO_CONTEXT = "No Mobile Dev browser panel has shared context. Open Mobile Dev, or ask the user to select a device in the panel.";
// Hosts can hide their browser pane while a tab stays open, so every reply that relies on the panel checks it is in view.
const SHOW_PANE = "A browser tool that reports the page opened, or a tab that is still open, can sit in a hidden pane, where the user sees only a card in the chat. When your host has a tool that brings its browser or preview pane into view, such as a show_pane tool for its preview pane, load it if it is deferred and call it now, unless your host reports that pane already in view. If it says the user is not viewing this chat, the pane stays closed: say the panel is in the browser pane, and show the pane again the next time you use Mobile Dev's tools.";
const REOPEN = "Mobile Dev's panel is already open in a browser tab.";
export const CONTEXT_PANE = "If your host's browser or preview pane is hidden, show it now, such as with a show_pane tool for its preview pane, so the user sees the panel.";

type PanelContent = TextContent | ImageContent;
type BrowserSession = { transport: StreamableHTTPServerTransport; touched: number; requests: number; context?: { content: PanelContent[]; updated: number } };
type PanelPage = { url(tool: string, target: PanelTarget): string; context(): PanelContent[]; close(): Promise<void> };
type PanelPage = { url(tool: string, target: PanelTarget): string; context(): PanelContent[]; connected(): boolean; close(): Promise<void> };

// What a panel would otherwise give an MCP Apps host's model context. Hosts mark
// audience and other annotations; the agent reads only text and images.
Expand Down Expand Up @@ -52,7 +56,7 @@ export function browserPanel({ template = PAGE, always = false }: BrowserPanelOp
page ??= startPanelPage(connections, host, template);
const opened = await page;
const url = opened.url(tool, target);
const instructions = `Mobile Dev's panel is a page to show beside the chat, not in it, and it is not open yet. Show it to the user now: open ${url} in your in-app browser or side panel, in the foreground, so the page is visible without another click. This page is how Mobile Dev shows its panel, so opening it is part of opening the panel. Use your host's own browser or preview tool, and load it first if it is deferred. Never open the page with a computer-use tool that drives the screen. Opening a page can leave the browser pane hidden: when your host has a tool that brings its browser or preview pane into view, such as a show_pane tool for its preview pane, load it if needed and call it right after. A host can also show the page as a floating preview and report it as not visible. That still counts as shown, so do not open it again. Leave the tab open after you reply. Reuse a tab that already shows Mobile Dev. Open no other address for it, such as the backend's baseUrl. If you cannot open pages, give the user the link. The page shares the user's devices and what they add to chat through mobile_read_panel_context; read it before device work.`;
const instructions = openingInstructions(url, opened.connected());
return { url, instructions };
});
// A browser has no model context of its own, so the agent asks the server for it.
Expand All @@ -63,7 +67,9 @@ export function browserPanel({ template = PAGE, always = false }: BrowserPanelOp
}, async () => {
const opened = await page?.catch(() => undefined);
const content = opened?.context() ?? [];
return { content: content.length ? content : [{ type: "text", text: NO_CONTEXT }] };
if (content.length === 0) return { content: [{ type: "text", text: NO_CONTEXT }] };
// Agents read context before device work, so a pane hidden since the panel opened is shown then.
return { content: [...content, { type: "text", text: CONTEXT_PANE }] };
});
return async () => {
const opened = await page?.catch(() => undefined);
Expand All @@ -72,6 +78,13 @@ export function browserPanel({ template = PAGE, always = false }: BrowserPanelOp
};
}

function openingInstructions(url: string, connected: boolean) {
const open = connected
? `${REOPEN} Show ${url} in the tab that already shows Mobile Dev, in the foreground, rather than opening another tab.`
: `Mobile Dev's panel is a page to show beside the chat, not in it, and it is not open yet. Show it to the user now: open ${url} in your in-app browser or side panel, in the foreground, so the page is visible without another click. This page is how Mobile Dev shows its panel, so opening it is part of opening the panel.`;
return `${open} Use your host's own browser or preview tool, and load it first if it is deferred. Never open the page with a computer-use tool that drives the screen. ${SHOW_PANE} A host can also show the page as a floating preview and report it as not visible. That still counts as shown, so do not open it again. Leave the tab open after you reply. Reuse a tab that already shows Mobile Dev. Open no other address for it, such as the backend's baseUrl. If you cannot open pages, give the user the link. The page shares the user's devices and what they add to chat through mobile_read_panel_context; read it before device work.`;
}

export async function startPanelPage(connections: SharedTransport, host: TelemetryHost, page: URL = PAGE): Promise<PanelPage> {
const template = await readFile(page, "utf8");
const html = template.replace("<html", `<html data-telemetry-host="${host}"`);
Expand Down Expand Up @@ -189,6 +202,7 @@ export async function startPanelPage(connections: SharedTransport, host: Telemet
}
return `${origins[0]}${base}?${query}`;
},
connected: () => sessions.size > 0,
context() {
// The most recently updated tab comes first; each open tab keeps its own.
const shared = Array.from(sessions.values()).flatMap(session => session.context ? [session.context] : []);
Expand Down
15 changes: 11 additions & 4 deletions packages/core/test/browser-panel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { EXTENSION_ID, RESOURCE_MIME_TYPE } from "@modelcontextprotocol/ext-apps
import { Baguette } from "../src/baguette.ts";
import { createPanel } from "../src/panel-tools.ts";
import { PANEL_PAGE_META } from "@mobile-dev/shared/panel-target.ts";
import { BROWSER_CONTEXT_MAX_BYTES, BROWSER_SESSION_IDLE_MS, browserPanel, startPanelPage } from "../src/browser-panel.ts";
import { BROWSER_CONTEXT_MAX_BYTES, BROWSER_SESSION_IDLE_MS, CONTEXT_PANE, browserPanel, startPanelPage } from "../src/browser-panel.ts";
import { SharedTransport } from "../src/shared-transport.ts";
import { createTestPlugin, fakeBaguette, fakeSimulatorInput } from "../../test-support/fixtures.ts";

Expand Down Expand Up @@ -319,13 +319,20 @@ test("a browser tab's model context reaches the agent and leaves with the tab",
const texts = (opened.content as Array<{ text: string }>).map(item => item.text);
assert.match(texts[1], /in the foreground/);
assert.match(texts[1], /mobile_read_panel_context/);
assert.match(texts[1], /not open yet/);
assert.match(texts[1], /show_pane/);
const link = new URL(texts[1].match(/http:\/\/127\.0\.0\.1:\d+\/[\w-]+\/\?\S+/)![0]);
const endpoint = new URL("context", link);

const tabs = [new StreamableHTTPClientTransport(new URL("mcp", link)), new StreamableHTTPClientTransport(new URL("mcp", link))];
const clients = tabs.map((_, index) => new Client({ name: `tab-${index}`, version: "1" }));
for (const [index, client] of clients.entries()) await client.connect(tabs[index]);
const [first, second] = tabs.map(tab => tab.sessionId!);
// With a tab connected, opening again reuses it and still asks for its pane in view.
const reopened = ((await host.callTool({ name: "mobile_open_simulator", arguments: {} })).content as Array<{ text: string }>)[1].text;
assert.match(reopened, /already open in a browser tab/);
assert.doesNotMatch(reopened, /not open yet/);
assert.match(reopened, /show_pane/);

assert.equal((await putContext(endpoint, undefined, "{}")).status, 404);
assert.equal((await putContext(endpoint, "missing", "{}")).status, 404);
Expand All @@ -338,20 +345,20 @@ test("a browser tab's model context reaches the agent and leaves with the tab",
const device = "Active iOS simulator: iPhone 17. Device ID: 11111111-1111-1111-1111-111111111111.";
const stored = await putContext(endpoint, first, JSON.stringify({ content: [{ type: "text", text: device, annotations: { audience: ["assistant"] } }] }));
assert.equal(stored.status, 204);
assert.deepEqual(await read(), [{ type: "text", text: "Mobile Dev browser panel." }, { type: "text", text: device }]);
assert.deepEqual(await read(), [{ type: "text", text: "Mobile Dev browser panel." }, { type: "text", text: device }, { type: "text", text: CONTEXT_PANE }]);

await new Promise(resolve => setTimeout(resolve, 5));
const screenshot = { type: "image", data: "aGVsbG8=", mimeType: "image/png" };
assert.equal((await putContext(endpoint, second, JSON.stringify({ content: [screenshot] }))).status, 204);
const both = await read();
assert.equal(both.length, 4);
assert.equal(both.length, 5);
assert.match(both[0].text!, /tab 1 of 2/);
assert.equal(both[1].data, screenshot.data);
assert.equal(both[3].text, device);

// Closing a tab removes what it shared; an empty update clears without closing.
await tabs[1].terminateSession();
assert.deepEqual((await read()).map(item => item.text), ["Mobile Dev browser panel.", device]);
assert.deepEqual((await read()).map(item => item.text), ["Mobile Dev browser panel.", device, CONTEXT_PANE]);
assert.equal((await putContext(endpoint, first, JSON.stringify({ content: [] }))).status, 204);
assert.match((await read())[0].text!, /No Mobile Dev browser panel/);
await tabs[0].terminateSession();
Expand Down
Loading