Skip to content

Commit d7633d8

Browse files
committed
fix(server): cap the snapshot title and URL before reducing page content
1 parent 2564fd0 commit d7633d8

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎apps/server/src/mcp/McpHttpServer.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,26 @@ it("cuts the visible text short before giving up the interactive elements", () =
200200
expect(metadata.interactiveElements).toEqual(snapshot.interactiveElements);
201201
});
202202

203+
it("caps a runaway URL before touching page content", () => {
204+
const snapshot = snapshotFixture({
205+
url: `data:text/html,${"x".repeat(200_000)}`,
206+
visibleText: "visible ".repeat(100),
207+
interactiveElements: [element("Save")],
208+
accessibilityTree: { nodes: [{ role: "main" }] },
209+
});
210+
const { metadata, serialized } = McpHttpServer.boundPreviewSnapshotMetadata(snapshot);
211+
212+
expect(Buffer.byteLength(serialized, "utf8")).toBeLessThanOrEqual(
213+
McpHttpServer.PREVIEW_SNAPSHOT_METADATA_MAX_BYTES,
214+
);
215+
expect(metadata.url).toBe(snapshot.url.slice(0, 2_048));
216+
expect(metadata.truncation?.trimmed).toEqual(["url"]);
217+
expect(metadata.truncation?.omitted).toEqual([]);
218+
expect(metadata.visibleText).toBe(snapshot.visibleText);
219+
expect(metadata.interactiveElements).toEqual(snapshot.interactiveElements);
220+
expect(metadata.accessibilityTree).toEqual(snapshot.accessibilityTree);
221+
});
222+
203223
it("gives up the interactive elements last when they alone exceed the limit", () => {
204224
const snapshot = snapshotFixture({
205225
visibleText: "visible ".repeat(2_000),

‎apps/server/src/mcp/McpHttpServer.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import {
3535
* as an image and is never reduced.
3636
*/
3737
export const PREVIEW_SNAPSHOT_METADATA_MAX_BYTES = 100_000;
38+
/** Longest title or URL kept once a snapshot has to be reduced at all. */
39+
const PREVIEW_SNAPSHOT_IDENTIFIER_MAX_CHARS = 2_048;
3840

3941
type PreviewSnapshotMetadata = Omit<PreviewAutomationSnapshot, "screenshot"> & {
4042
readonly screenshot: Omit<PreviewAutomationSnapshot["screenshot"], "data">;
@@ -96,6 +98,23 @@ export function boundPreviewSnapshotMetadata(snapshot: PreviewAutomationSnapshot
9698
return measure();
9799
};
98100

101+
// The title and URL are identifiers, not content. Past this length (a data:
102+
// URL, a runaway title) they carry nothing the agent needs, and either could
103+
// be the whole overrun by itself, so they are capped before any content goes.
104+
for (const field of ["title", "url"] as const) {
105+
if (metadata[field].length > PREVIEW_SNAPSHOT_IDENTIFIER_MAX_CHARS) {
106+
metadata = {
107+
...metadata,
108+
[field]: cutText(metadata[field], PREVIEW_SNAPSHOT_IDENTIFIER_MAX_CHARS),
109+
};
110+
trimmed.push(field);
111+
bytes = measure();
112+
}
113+
}
114+
if (bytes <= PREVIEW_SNAPSHOT_METADATA_MAX_BYTES) {
115+
return { metadata, serialized };
116+
}
117+
99118
if (metadata.accessibilityTree !== null && metadata.accessibilityTree !== undefined) {
100119
bytes = omit("accessibilityTree");
101120
}

0 commit comments

Comments
 (0)