fix(mcp): keep preview snapshots usable by the agent and let it save them #10501
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 5 commits into
main fromrefs/pull/10501/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/mcp/McpHttpServer.ts line 396Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -183,20 +328,2 @@183328 };184- return Effect.succeed(185- new McpSchema.CallToolResult({186- isError: false,187- structuredContent: metadata,188- content: [189- { type: "text", text: JSON.stringify(metadata) },190- ...(payload?.includeImage === false191- ? []192- : [193- {194- type: "image" as const,195- data: new Uint8Array(Buffer.from(screenshot.data, "base64")),196- mimeType: screenshot.mimeType,197- },198- ]),199- ],200- }),201- );202- },329+ const bounded = boundSnapshotMetadata(metadata);apps/server/src/mcp/McpHttpServer.ts line 386Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -162,14 +304,16 @@162304 Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),163- Effect.matchCauseEffect({164- onFailure: previewSnapshotFailure,165- onSuccess: ({ encodedResult }) => {166- const snapshot = encodedResult as {305+ Effect.flatMap(({ encodedResult }) =>306+ Effect.gen(function* () {307+ const snapshot = encodedResult as SnapshotMetadata & {308+ readonly url: string;167309 readonly screenshot: {168310 readonly mimeType: "image/png";169311 readonly data: string;170312 readonly width: number;171313 readonly height: number;172314 };173- readonly [key: string]: unknown;174315 };175316 const { screenshot, ...page } = snapshot;317+ const png = new Uint8Array(Buffer.from(screenshot.data, "base64"));318+ const screenshotPath =319+ payload?.save === true ? yield* saveScreenshot(snapshot.url, png) : undefined;
- apps/server/src/mcp/McpHttpServer.ts lines 180 to 186OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -97,0 +166,21 @@166+ if (bounded.visibleText.length > MAX_SNAPSHOT_VISIBLE_TEXT_CHARS) {167+ omitted.push(168+ `visibleText after ${MAX_SNAPSHOT_VISIBLE_TEXT_CHARS} characters (use preview_evaluate for more)`,169+ );170+ bounded = {171+ ...bounded,172+ visibleText: cutText(bounded.visibleText, MAX_SNAPSHOT_VISIBLE_TEXT_CHARS),173+ };174+ }175+176+ let text = JSON.stringify(bounded);177+ if (utf8Length(text) > MAX_SNAPSHOT_TEXT_BYTES) {178+ // Interactive elements are the last thing worth keeping; halve until it fits.179+ let elements = bounded.interactiveElements;180+ while (utf8Length(text) > MAX_SNAPSHOT_TEXT_BYTES && elements.length > 0) {181+ elements = elements.slice(0, Math.floor(elements.length / 2));182+ text = JSON.stringify({ ...bounded, interactiveElements: elements });183+ }184+ omitted.push(185+ `${bounded.interactiveElements.length - elements.length} of ${bounded.interactiveElements.length} interactive elements`,186+ );apps/server/src/mcp/McpHttpServer.ts line 226OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -97,0 +206,21 @@206+ .toLowerCase()207+ .replace(/[^a-z0-9]+/g, "-")208+ .replace(/^-+|-+$/g, "")209+ .slice(0, MAX_SCREENSHOT_SITE_SLUG_LENGTH)210+ .replace(/-+$/g, "");211+ return slug || "site";212+ } catch {213+ return "site";214+ }215+};216+217+/** Writes the snapshot PNG under the browser artifacts directory and returns its path. */218+const saveScreenshot = Effect.fn("McpHttpServer.saveScreenshot")(function* (219+ pageUrl: string,220+ data: Uint8Array,221+) {222+ const config = yield* ServerConfig.ServerConfig;223+ const fileSystem = yield* FileSystem.FileSystem;224+ const path = yield* Path.Path;225+ const millis = yield* Clock.currentTimeMillis;226+ const fileName = `browser-screenshot-${screenshotSiteSlug(pageUrl)}-${millis.toString(36)}.png`;
jakeleventhal(GitHub profile, opens in a new tab) requested changes
apps/server/src/mcp/McpHttpServer.test.ts line 4Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -4 +4 @@44 import { EnvironmentId, PreviewTabId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";- apps/server/src/mcp/McpHttpServer.ts lines 199 to 202Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -97,0 +167,21 @@167+ if (168+ metadata.interactiveElements.some(169+ (element) => element.name.length > MAX_SNAPSHOT_ELEMENT_NAME_CHARS,170+ )171+ ) {172+ omitted.push(`element names longer than ${MAX_SNAPSHOT_ELEMENT_NAME_CHARS} characters`);173+ }174+ if (metadata.visibleText.length > MAX_SNAPSHOT_VISIBLE_TEXT_CHARS) {175+ omitted.push(176+ `visibleText after ${MAX_SNAPSHOT_VISIBLE_TEXT_CHARS} characters (use preview_evaluate for more)`,177+ );178+ }179+ const bounded = {180+ ...withoutTree,181+ url: cutText(metadata.url, MAX_SNAPSHOT_IDENTIFIER_CHARS),182+ title: cutText(metadata.title, MAX_SNAPSHOT_IDENTIFIER_CHARS),183+ visibleText: cutText(metadata.visibleText, MAX_SNAPSHOT_VISIBLE_TEXT_CHARS),184+ interactiveElements: metadata.interactiveElements.map((element) => ({185+ ...element,186+ name: cutText(element.name, MAX_SNAPSHOT_ELEMENT_NAME_CHARS),187+ })),
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed