Skip to content

Commit cfad310

Browse files
fix(client-runtime): long MCP app download names no longer split an emoji (pingdotgg#35680)
1 parent 6dfb1a8 commit cfad310

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

‎packages/client-runtime/src/mcpApps/host.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ describe("makeMcpAppHost", () => {
325325

326326
it("forwards model context and download requests, and declares both", async () => {
327327
const contexts: Array<unknown> = [];
328-
const downloads: Array<unknown> = [];
328+
const downloads: Array<ReadonlyArray<{ readonly name: string }>> = [];
329329
const { host, sent } = setup({
330330
updateModelContext: async (update) => void contexts.push(update),
331331
downloadFile: async (files) => {
@@ -349,6 +349,7 @@ describe("makeMcpAppHost", () => {
349349
structuredContent: { overdue: 2, since: undefined },
350350
},
351351
});
352+
const longName = `${"a".repeat(199)}😀.csv`;
352353
const embedded = {
353354
type: "resource",
354355
resource: { uri: "file:///report.csv", mimeType: "text/csv", text: "a,b" },
@@ -364,7 +365,7 @@ describe("makeMcpAppHost", () => {
364365
id: 3,
365366
method: "ui/download-file",
366367
params: {
367-
contents: [embedded, { type: "resource_link", uri: "ui://todos/export", name: "x" }],
368+
contents: [embedded, { type: "resource_link", uri: "ui://todos/export", name: longName }],
368369
},
369370
});
370371
host.receive({ jsonrpc: "2.0", id: 4, method: "ui/download-file", params: { contents: [{}] } });
@@ -380,6 +381,9 @@ describe("makeMcpAppHost", () => {
380381
bytes: new TextEncoder().encode("a,b"),
381382
},
382383
]);
384+
const capped = downloads[1]?.[1]?.name ?? "";
385+
expect(capped.length).toBeLessThanOrEqual(200);
386+
expect(capped.isWellFormed()).toBe(true);
383387
expect(sent.find((m) => m.id === 2)?.result).toEqual({});
384388
expect(sent.find((m) => m.id === 3)?.result).toEqual({ isError: true });
385389
expect(sent.find((m) => m.id === 4)?.error).toMatchObject({ code: -32602 });

‎packages/client-runtime/src/mcpApps/host.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,13 @@ function downloadName(uri: unknown, name: unknown): string {
482482
: typeof uri === "string"
483483
? (uri.split(/[/?#]/).findLast(Boolean) ?? "download")
484484
: "download";
485-
const safe = Array.from(raw, (char) =>
486-
/[\\/:*?"<>|]/.test(char) || char.charCodeAt(0) < 32 ? "-" : char,
487-
).join("");
488-
return safe.slice(0, MAX_DOWNLOAD_NAME) || "download";
485+
let safe = "";
486+
for (const char of raw) {
487+
const next = /[\\/:*?"<>|]/.test(char) || char.charCodeAt(0) < 32 ? "-" : char;
488+
if (safe.length + next.length > MAX_DOWNLOAD_NAME) break;
489+
safe += next;
490+
}
491+
return safe || "download";
489492
}
490493

491494
function readDownloads(contents: unknown): ReadonlyArray<McpAppDownload> | undefined {

0 commit comments

Comments
 (0)