feat: add ui/download-file method for host-mediated file downloads

Add a new ui/download-file JSON-RPC request that allows MCP App views to
request file downloads through the host. Since MCP Apps run in sandboxed
iframes where direct downloads are blocked (allow-downloads not set),
this provides a host-mediated mechanism for file exports.

Spec changes (draft):
- Add ui/download-file request to Requests (View → Host) section
- Add downloadFile capability to HostCapabilities
- Document host behavior requirements (confirmation dialog, filename
  sanitization, base64 decoding, size limits)

SDK changes:
- Add McpUiDownloadFileRequest/Result interfaces to spec.types.ts
- Add downloadFile() method to App class
- Add ondownloadfile setter to AppBridge class
- Add DOWNLOAD_FILE_METHOD constant
- Generate Zod schemas

Example:
- Add download demo to integration-server (exports JSON with server time)
- Capability check: only shows download button when host advertises
  downloadFile capability

Based on developer feedback requesting file export capabilities for
visualization tools, document editors, and data analysis apps.
This commit is contained in:
Anton Pidkuiko MacBook 2026-02-19 21:25:48 +00:00
commit 494c73e7ca
No known key found for this signature in database

View file

@ -137,6 +137,24 @@ function GetTimeAppInner({
log.info("Open link request", isError ? "rejected" : "accepted");
}, [app, linkUrl]);
const canDownload =
app.getHostCapabilities()?.downloadFile !== undefined;
const handleDownloadFile = useCallback(async () => {
const sampleContent = JSON.stringify(
{ time: serverTime, exported: new Date().toISOString() },
null,
2,
);
log.info("Requesting file download...");
const { isError } = await app.downloadFile({
filename: "export.json",
content: sampleContent,
mimeType: "application/json",
});
log.info("Download", isError ? "rejected" : "accepted");
}, [app, serverTime]);
return (
<main
className={styles.main}
@ -182,6 +200,13 @@ function GetTimeAppInner({
/>
<button onClick={handleOpenLink}>Open Link</button>
</div>
{canDownload && (
<div className={styles.action}>
<p>Export current server time as JSON file</p>
<button onClick={handleDownloadFile}>Download File</button>
</div>
)}
</main>
);
}