Skip to content

Commit 085e0e0

Browse files
t3dotggclaude
andcommitted
fix(mcp): shed page text before locators
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 1f11278 commit 085e0e0

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,23 @@ it.effect("bounds the snapshot text even when nothing but logs and the title are
514514
).pipe(Effect.provide(TestLayer)),
515515
);
516516

517-
it.effect("bounds page text made of wide characters", () =>
517+
it.effect("bounds page text made of wide characters before dropping locators", () =>
518518
Effect.scoped(
519519
Effect.gen(function* () {
520520
// The character caps alone leave 8,000 three-byte characters, about 24 KB.
521521
yield* serveSnapshots("mcp-wide-text-client", {
522522
...snapshotResult,
523523
visibleText: "界".repeat(9_000),
524+
interactiveElements: Array.from({ length: 20 }, (_, i) => ({
525+
tag: "button",
526+
role: "button",
527+
name: `Button ${i}`,
528+
selector: `#button-${i}`,
529+
x: 0,
530+
y: 0,
531+
width: 10,
532+
height: 10,
533+
})),
524534
});
525535

526536
const snapshot = yield* callSnapshot({ includeImage: false });
@@ -530,9 +540,12 @@ it.effect("bounds page text made of wide characters", () =>
530540
expect(Buffer.byteLength(body, "utf8")).toBeLessThanOrEqual(
531541
McpHttpServer.MAX_SNAPSHOT_TEXT_BYTES,
532542
);
533-
expect((decodeJsonText(body) as { readonly visibleText: string }).visibleText).toMatch(
534-
/^界+…$/,
535-
);
543+
const parsed = decodeJsonText(body) as {
544+
readonly visibleText: string;
545+
readonly interactiveElements: ReadonlyArray<unknown>;
546+
};
547+
expect(parsed.visibleText).toMatch(/^界+…$/);
548+
expect(parsed.interactiveElements).toHaveLength(20);
536549
expect(notice?.type === "text" ? notice.text : "").toContain(
537550
"visibleText after 4000 characters",
538551
);

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ const boundSnapshotMetadata = (metadata: SnapshotMetadata) => {
213213
};
214214

215215
// Per-field caps do not sum below the ceiling: three log arrays of 40 capped
216-
// entries alone can pass 60 KB. Shed the least useful lists first, halving
217-
// one list per round, until the JSON fits. Page text is halved last: the
218-
// caps count characters, and wide characters can keep it over the ceiling
219-
// alone. The identifier caps bound the rest, so this terminates.
216+
// entries alone can pass 60 KB, and the caps count characters, not bytes.
217+
// Halve one thing per round until the JSON fits: logs first, then page
218+
// text, then the locators. The identifier caps bound the rest, so this
219+
// terminates.
220220
const shedOrder = [
221221
"actionTimeline",
222222
"networkEntries",
@@ -247,9 +247,14 @@ const boundSnapshotMetadata = (metadata: SnapshotMetadata) => {
247247
const key =
248248
shedOrder.find(
249249
(candidate) => candidate !== "interactiveElements" && lists[candidate].length > 0,
250-
) ?? (lists.interactiveElements.length > 0 ? "interactiveElements" : undefined);
251-
if (key === undefined) {
252-
if (visibleTextChars === 0) break;
250+
) ??
251+
(visibleTextChars > 0
252+
? "visibleText"
253+
: lists.interactiveElements.length > 0
254+
? "interactiveElements"
255+
: undefined);
256+
if (key === undefined) break;
257+
if (key === "visibleText") {
253258
visibleTextChars = Math.floor(visibleTextChars / 2);
254259
} else {
255260
const keep = Math.floor(lists[key].length / 2);

0 commit comments

Comments
 (0)