Skip to content

Commit ffd627d

Browse files
saphidclaude
authored andcommitted
fix(server): preview snapshots no longer cut an emoji in half
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 60aec28 commit ffd627d

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,9 @@ it.effect("bounds the snapshot text even when nothing but logs and the title are
689689
Effect.gen(function* () {
690690
const oversized = {
691691
...snapshotResult,
692-
title: "t".repeat(70_000),
692+
title: `t${"😀".repeat(35_000)}`,
693693
interactiveElements: [],
694-
consoleEntries: [{ level: "log", text: "x".repeat(70_000), timestamp: "t" }],
694+
consoleEntries: [{ level: "log", text: `x${"😀".repeat(35_000)}`, timestamp: "t" }],
695695
};
696696
yield* serveSnapshots("mcp-bounded-logs-client", oversized);
697697

@@ -706,8 +706,10 @@ it.effect("bounds the snapshot text even when nothing but logs and the title are
706706
readonly title: string;
707707
readonly consoleEntries: ReadonlyArray<{ readonly text: string }>;
708708
};
709-
expect(parsed.title.length).toBe(2_049);
710-
expect(parsed.consoleEntries[0]?.text.length).toBe(501);
709+
expect(parsed.title.isWellFormed()).toBe(true);
710+
expect(parsed.title.length).toBe(2_048);
711+
expect(parsed.consoleEntries[0]?.text.isWellFormed()).toBe(true);
712+
expect(parsed.consoleEntries[0]?.text.length).toBe(500);
711713
const notice = snapshot.content[2];
712714
const noticeText = notice?.type === "text" ? notice.text : "";
713715
expect(noticeText).toContain("url or title after 2048 characters");

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,12 @@ const MAX_SNAPSHOT_IDENTIFIER_CHARS = 2_048;
257257

258258
const encodeJsonText = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
259259
const utf8Length = (text: string) => Buffer.byteLength(text, "utf8");
260-
const cutText = (text: string, max: number) =>
261-
text.length > max ? `${text.slice(0, max)}…` : text;
260+
const cutText = (text: string, max: number) => {
261+
if (text.length <= max) return text;
262+
// Half an emoji serializes to a lone surrogate escape, which provider JSON parsers reject.
263+
const last = text.charCodeAt(max - 1);
264+
return `${text.slice(0, last >= 0xd800 && last <= 0xdbff ? max - 1 : max)}…`;
265+
};
262266

263267
const cutEntryStrings = <A>(entry: A): A =>
264268
typeof entry === "object" && entry !== null

0 commit comments

Comments
 (0)