Skip to content
TypeScript

fix(server): preview snapshots no longer cut an emoji in half (#29600)

Alex Southwell5bdc2afc2303
diff --git a/apps/server/src/mcp/McpHttpServer.test.ts b/apps/server/src/mcp/McpHttpServer.test.ts
index c3ba3db1c02..e3622194ec4 100644
--- a/apps/server/src/mcp/McpHttpServer.test.ts
+++ b/apps/server/src/mcp/McpHttpServer.test.ts
@@ -692,9 +692,9 @@ it.effect("bounds the snapshot text even when nothing but logs and the title are
Effect.gen(function* () {
const oversized = {
...snapshotResult,
- title: "t".repeat(70_000),
+ title: `t${"πŸ˜€".repeat(35_000)}`,
interactiveElements: [],
- consoleEntries: [{ level: "log", text: "x".repeat(70_000), timestamp: "t" }],
+ consoleEntries: [{ level: "log", text: `x${"πŸ˜€".repeat(35_000)}`, timestamp: "t" }],
};
yield* serveSnapshots("mcp-bounded-logs-client", oversized);
@@ -709,8 +709,10 @@ it.effect("bounds the snapshot text even when nothing but logs and the title are
readonly title: string;
readonly consoleEntries: ReadonlyArray<{ readonly text: string }>;
};
- expect(parsed.title.length).toBe(2_049);
- expect(parsed.consoleEntries[0]?.text.length).toBe(501);
+ expect(parsed.title.isWellFormed()).toBe(true);
+ expect(parsed.title.length).toBe(2_048);
+ expect(parsed.consoleEntries[0]?.text.isWellFormed()).toBe(true);
+ expect(parsed.consoleEntries[0]?.text.length).toBe(500);
const notice = snapshot.content[2];
const noticeText = notice?.type === "text" ? notice.text : "";
expect(noticeText).toContain("url or title after 2048 characters");
diff --git a/apps/server/src/mcp/McpHttpServer.ts b/apps/server/src/mcp/McpHttpServer.ts
index 2ef080a7ef7..9bc3b9da43d 100644
--- a/apps/server/src/mcp/McpHttpServer.ts
+++ b/apps/server/src/mcp/McpHttpServer.ts
@@ -258,8 +258,12 @@ const MAX_SNAPSHOT_IDENTIFIER_CHARS = 2_048;
const encodeJsonText = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
const utf8Length = (text: string) => Buffer.byteLength(text, "utf8");
-const cutText = (text: string, max: number) =>
- text.length > max ? `${text.slice(0, max)}…` : text;
+const cutText = (text: string, max: number) => {
+ if (text.length <= max) return text;
+ // Half an emoji serializes to a lone surrogate escape, which provider JSON parsers reject.
+ const last = text.charCodeAt(max - 1);
+ return `${text.slice(0, last >= 0xd800 && last <= 0xdbff ? max - 1 : max)}…`;
+};
const cutEntryStrings = <A>(entry: A): A =>
typeof entry === "object" && entry !== null