diff --git a/apps/server/src/mcp/McpHttpServer.ts b/apps/server/src/mcp/McpHttpServer.ts index 5c6c4fece44b..d97350adc715 100644 --- a/apps/server/src/mcp/McpHttpServer.ts +++ b/apps/server/src/mcp/McpHttpServer.ts @@ -502,12 +502,17 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot readonly data: string; readonly width: number; readonly height: number; - }; + } | null; }; const { screenshot, ...page } = snapshot; - const png = new Uint8Array(Buffer.from(screenshot.data, "base64")); + const png = + screenshot === null + ? undefined + : new Uint8Array(Buffer.from(screenshot.data, "base64")); const screenshotPath = - payload?.save === true ? yield* saveScreenshot(snapshot.url, png) : undefined; + payload?.save === true && png !== undefined + ? yield* saveScreenshot(snapshot.url, png) + : undefined; // Images stay out of tool history unless asked for: providers replay them on every // later request, and some reject inline images outright. const includeImage = payload?.includeImage === true; @@ -524,11 +529,14 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot } const metadata = { ...page, - screenshot: { - mimeType: screenshot.mimeType, - width: screenshot.width, - height: screenshot.height, - }, + screenshot: + screenshot === null + ? null + : { + mimeType: screenshot.mimeType, + width: screenshot.width, + height: screenshot.height, + }, ...(screenshotPath === undefined ? {} : { screenshotPath }), }; const bounded = boundSnapshotMetadata(metadata); @@ -554,7 +562,7 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot text: `Snapshot text was bounded. Omitted: ${bounded.omitted.join("; ")}.`, }, ]), - ...(includeImage + ...(includeImage && png !== undefined && screenshot !== null ? [{ type: "image" as const, data: png, mimeType: screenshot.mimeType }] : []), ], diff --git a/apps/server/src/mcp/toolkits/preview/handlers.ts b/apps/server/src/mcp/toolkits/preview/handlers.ts index 37b4e0470783..aea0115a05fd 100644 --- a/apps/server/src/mcp/toolkits/preview/handlers.ts +++ b/apps/server/src/mcp/toolkits/preview/handlers.ts @@ -202,8 +202,13 @@ const handlers = { invokeTargeted("setColorScheme", input), ), preview_snapshot: McpToolAccess.readsAsCaller((input) => { - const { includeImage: _includeImage, save: _save, ...operationInput } = input ?? {}; - return invokeTargeted("snapshot", operationInput); + const { includeImage, save, ...operationInput } = input ?? {}; + // The screenshot bytes are only consumed for the inline image or the saved + // file; skip the capture and encode entirely when neither is requested. + return invokeTargeted("snapshot", { + ...operationInput, + includeScreenshot: includeImage === true || save === true, + }); }), preview_click: McpToolAccess.actsAsCaller((input) => invokeTargeted("click", input, input.timeoutMs), diff --git a/apps/server/src/mcp/toolkits/preview/tools.ts b/apps/server/src/mcp/toolkits/preview/tools.ts index 88a260ed6d3c..f442745ed12e 100644 --- a/apps/server/src/mcp/toolkits/preview/tools.ts +++ b/apps/server/src/mcp/toolkits/preview/tools.ts @@ -139,7 +139,7 @@ const PreviewSetAppearanceTool = safeBrowserTool( export const PreviewSnapshotTool = safeBrowserTool( Tool.make("preview_snapshot", { description: - "Inspect a page before interacting. Pass tabId to inspect a specific tab, including one the user opened (see preview_status tabs), even while they control it; omit it to use this agent session's current tab, or the tab the user is viewing when you have none. Returns page state, semantic elements, diagnostics, action history, and screenshot dimensions. Server snapshots include an accessibilityTree with refs; pass locator=aria-ref= to target one exact element, including inside frames. Refresh refs after navigation, another snapshot, or human takeover. The text is capped near 20 KB and lists what it omitted; use preview_evaluate to read more. Set includeImage=true only when you need to see the page; the image stays in tool history. Set save=true to write the PNG to disk and get back only the url and screenshotPath. Embed that path in your reply as ![alt](screenshotPath) so the user sees it. This is the only way to show the user a screenshot; the image in the tool result is not saved anywhere.", + "Inspect a page before interacting. Pass tabId to inspect a specific tab, including one the user opened (see preview_status tabs), even while they control it; omit it to use this agent session's current tab, or the tab the user is viewing when you have none. Returns page state, semantic elements, diagnostics, action history, and screenshot dimensions when a screenshot is captured. Server snapshots include an accessibilityTree with refs; pass locator=aria-ref= to target one exact element, including inside frames. Refresh refs after navigation, another snapshot, or human takeover. The text is capped near 20 KB and lists what it omitted; use preview_evaluate to read more. Set includeImage=true only when you need to see the page; the image stays in tool history. Set save=true to write the PNG to disk and get back only the url and screenshotPath. Embed that path in your reply as ![alt](screenshotPath) so the user sees it. This is the only way to show the user a screenshot; the image in the tool result is not saved anywhere.", parameters: Schema.Struct({ ...PreviewAutomationTabTargetInput.fields, includeImage: Schema.optional( diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts index 5b386e5f0797..6ca1a4b706f2 100644 --- a/apps/server/src/preview/ServerBrowser.ts +++ b/apps/server/src/preview/ServerBrowser.ts @@ -29,6 +29,7 @@ import { type PreviewAutomationScrollInput, type PreviewAutomationSelectInput, type PreviewAutomationSetColorSchemeInput, + type PreviewAutomationSnapshotInput, type PreviewAutomationStatus, type PreviewAutomationTypeInput, type PreviewAutomationUploadInput, @@ -1968,10 +1969,12 @@ const make = Effect.gen(function* () { return { tabId: tab.tabId, colorScheme }; } case "snapshot": { + const snapshotInput = input as PreviewAutomationSnapshotInput; return withScreencastsPaused(tab, () => ServerBrowserPage.snapshot({ ...tab, renderScale: tab.desktop ? "display" : RENDER_SCALE, + includeScreenshot: snapshotInput.includeScreenshot !== false, }), ); } diff --git a/apps/server/src/preview/ServerBrowserPage.test.ts b/apps/server/src/preview/ServerBrowserPage.test.ts index 2491e6ac9a5f..c67901e7e0a2 100644 --- a/apps/server/src/preview/ServerBrowserPage.test.ts +++ b/apps/server/src/preview/ServerBrowserPage.test.ts @@ -245,6 +245,45 @@ describe("server browser element refs", () => { await ServerBrowserPage.click(page, { selector: 'button[data-example=" >> aria-ref=e1"]' }); expect(await page.locator("button").textContent()).toBe("saved"); }); + + it("skips the screenshot capture when includeScreenshot is false", async () => { + await page.setContent(""); + let captures = 0; + const countingCdp = new Proxy(cdp, { + get(target, property) { + const value = Reflect.get(target, property, target); + if (property === "send") { + return (method: string, ...args: Array) => { + if (method === "Page.captureScreenshot") captures += 1; + return (target.send as (...sendArgs: Array) => unknown).apply(target, [ + method, + ...args, + ]); + }; + } + return typeof value === "function" ? value.bind(target) : value; + }, + }); + const base = { + page, + cdp: countingCdp, + renderScale: 1 as const, + consoleEntries: [], + networkEntries: [], + actionTimeline: [], + }; + const withScreenshot = await ServerBrowserPage.snapshot(base); + expect(withScreenshot.screenshot).not.toBeNull(); + expect(captures).toBe(1); + + const withoutScreenshot = await ServerBrowserPage.snapshot({ + ...base, + includeScreenshot: false, + }); + expect(withoutScreenshot.screenshot).toBeNull(); + expect(captures).toBe(1); + expect(String(withoutScreenshot.accessibilityTree)).toContain("hi"); + }); }); describe("server browser drag", () => { diff --git a/apps/server/src/preview/ServerBrowserPage.ts b/apps/server/src/preview/ServerBrowserPage.ts index 02089cca3205..5e00f42e2f2e 100644 --- a/apps/server/src/preview/ServerBrowserPage.ts +++ b/apps/server/src/preview/ServerBrowserPage.ts @@ -509,6 +509,8 @@ type SnapshotInput = { readonly cdp: CDPSession; /** Headless pages render at the scale the server set; a desktop page at its display's. */ readonly renderScale: number | "display"; + /** Defaults to true. False returns the snapshot without capturing a screenshot. */ + readonly includeScreenshot?: boolean; readonly consoleEntries: ReadonlyArray; readonly networkEntries: ReadonlyArray; readonly actionTimeline: PreviewAutomationSnapshot["actionTimeline"]; @@ -554,7 +556,9 @@ const capture = async ( "url" | "title" | "loading" | "visibleText" | "interactiveElements" > >, - captureViewport(input.page, input.cdp, { format: "png", scale, viewport, offset }), + input.includeScreenshot === false + ? Promise.resolve(null) + : captureViewport(input.page, input.cdp, { format: "png", scale, viewport, offset }), ]); void pageAndScreenshot.catch(constVoid); // Leaves time for the second read; a frame that never loads holds the tree until then. @@ -601,11 +605,14 @@ const capture = async ( consoleEntries: [...input.consoleEntries], networkEntries: [...input.networkEntries], actionTimeline: [...input.actionTimeline], - screenshot: { - mimeType: "image/png", - data, - ...pngSize(data), - }, + screenshot: + data === null + ? null + : { + mimeType: "image/png", + data, + ...pngSize(data), + }, }; }; diff --git a/packages/contracts/src/previewAutomation.ts b/packages/contracts/src/previewAutomation.ts index 0e9ff94d6ffe..2538cdbaa776 100644 --- a/packages/contracts/src/previewAutomation.ts +++ b/packages/contracts/src/previewAutomation.ts @@ -73,6 +73,13 @@ const PreviewAutomationTabTargetFields = { export const PreviewAutomationTabTargetInput = Schema.Struct(PreviewAutomationTabTargetFields); export type PreviewAutomationTabTargetInput = typeof PreviewAutomationTabTargetInput.Type; +export const PreviewAutomationSnapshotInput = Schema.Struct({ + ...PreviewAutomationTabTargetFields, + /** False skips the screenshot capture entirely; the snapshot's screenshot is then null. */ + includeScreenshot: Schema.optional(Schema.Boolean), +}); +export type PreviewAutomationSnapshotInput = typeof PreviewAutomationSnapshotInput.Type; + export const PreviewAutomationTabOwner = Schema.Literals(["agent", "human", "unclaimed"]); export const PreviewAutomationProfile = Schema.Struct({ @@ -724,12 +731,15 @@ export const PreviewAutomationSnapshot = Schema.Struct({ consoleEntries: Schema.Array(PreviewAutomationConsoleEntry), networkEntries: Schema.Array(PreviewAutomationNetworkEntry), actionTimeline: Schema.Array(PreviewAutomationActionEvent), - screenshot: Schema.Struct({ - mimeType: Schema.Literal("image/png"), - data: Schema.String, - width: Schema.Int, - height: Schema.Int, - }), + /** Null when the snapshot was taken without a screenshot capture. */ + screenshot: Schema.NullOr( + Schema.Struct({ + mimeType: Schema.Literal("image/png"), + data: Schema.String, + width: Schema.Int, + height: Schema.Int, + }), + ), }); export type PreviewAutomationSnapshot = typeof PreviewAutomationSnapshot.Type;