Skip to content

Commit a291670

Browse files
committed
fix(client-runtime): derive MCP download names before URI suffixes
1 parent 8b44f42 commit a291670

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,5 +361,58 @@ describe("makeMcpAppHost", () => {
361361
expect(sent.find((m) => m.id === 2)?.result).toEqual({});
362362
expect(sent.find((m) => m.id === 3)?.result).toEqual({ isError: true });
363363
expect(sent.find((m) => m.id === 4)?.error).toMatchObject({ code: -32602 });
364+
365+
const filenames = [
366+
["https://example.invalid/report.csv?version=2", undefined, "report.csv"],
367+
["https://example.invalid/report.csv#page=2", undefined, "report.csv"],
368+
["https://example.invalid/report.csv?next=/noise.json#page=3", undefined, "report.csv"],
369+
["urn:example:report.csv", undefined, "urn-example-report.csv"],
370+
["custom:report.csv", undefined, "custom-report.csv"],
371+
["https://example.invalid/report%20final.csv?version=2", undefined, "report%20final.csv"],
372+
["https://example.invalid/report.csv?version=2", " chosen/name?.csv ", " chosen-name-.csv "],
373+
["https://example.invalid/report.csv?version=2", " ", "report.csv"],
374+
["file:///report.csv", "a".repeat(201), "a".repeat(200)],
375+
["", undefined, "download"],
376+
["file:///folder/", undefined, "folder"],
377+
] as const;
378+
for (const [index, [uri, name, expectedName]] of filenames.entries()) {
379+
const id = `download-${index}`;
380+
const downloadIndex = downloads.length;
381+
host.receive({
382+
jsonrpc: "2.0",
383+
id,
384+
method: "ui/download-file",
385+
params: {
386+
contents: [
387+
{
388+
...embedded,
389+
resource: { ...embedded.resource, uri, ...(name === undefined ? {} : { name }) },
390+
},
391+
],
392+
},
393+
});
394+
await flush();
395+
expect(downloads[downloadIndex]).toEqual([
396+
{
397+
_tag: "embedded",
398+
name: expectedName,
399+
mimeType: "text/csv",
400+
bytes: new TextEncoder().encode("a,b"),
401+
},
402+
]);
403+
expect(sent.find((m) => m.id === id)?.result).toEqual({});
404+
}
405+
406+
const linkUri = "https://example.invalid/report.csv?version=2#page=3";
407+
const downloadIndex = downloads.length;
408+
host.receive({
409+
jsonrpc: "2.0",
410+
id: "download-link",
411+
method: "ui/download-file",
412+
params: { contents: [{ type: "resource_link", uri: linkUri, name: "chosen.csv" }] },
413+
});
414+
await flush();
415+
expect(downloads[downloadIndex]).toEqual([{ _tag: "link", name: "chosen.csv", uri: linkUri }]);
416+
expect(sent.find((m) => m.id === "download-link")?.result).toEqual({});
364417
});
365418
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ function downloadName(uri: unknown, name: unknown): string {
468468
typeof name === "string" && name.trim() !== ""
469469
? name
470470
: typeof uri === "string"
471-
? (uri.split(/[/?#]/).findLast(Boolean) ?? "download")
471+
? (uri.split(/[?#]/, 1)[0]?.split("/").findLast(Boolean) ?? "download")
472472
: "download";
473473
const safe = Array.from(raw, (char) =>
474474
/[\\/:*?"<>|]/.test(char) || char.charCodeAt(0) < 32 ? "-" : char,

0 commit comments

Comments
 (0)