|
| 1 | +import { McpUiSandboxProxyReadyNotificationSchema } from "@modelcontextprotocol/ext-apps"; |
| 2 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 3 | +import { Tool } from "@modelcontextprotocol/sdk/types.js"; |
| 4 | + |
| 5 | +const MCP_UI_RESOURCE_META_KEY = "ui/resourceUri"; |
| 6 | + |
| 7 | +export async function setupSandboxProxyIframe(sandboxProxyUrl: URL): Promise<{ |
| 8 | + iframe: HTMLIFrameElement; |
| 9 | + onReady: Promise<void>; |
| 10 | +}> { |
| 11 | + const iframe = document.createElement("iframe"); |
| 12 | + iframe.style.width = "100%"; |
| 13 | + iframe.style.height = "600px"; |
| 14 | + iframe.style.border = "none"; |
| 15 | + iframe.style.backgroundColor = "transparent"; |
| 16 | + // iframe.allow = 'microphone' |
| 17 | + iframe.setAttribute("sandbox", "allow-scripts allow-same-origin allow-forms"); |
| 18 | + |
| 19 | + const onReady = new Promise<void>((resolve, _reject) => { |
| 20 | + const initialListener = async (event: MessageEvent) => { |
| 21 | + if (event.source === iframe.contentWindow) { |
| 22 | + if ( |
| 23 | + event.data && |
| 24 | + event.data.method === |
| 25 | + McpUiSandboxProxyReadyNotificationSchema.shape.method._def.value |
| 26 | + ) { |
| 27 | + window.removeEventListener("message", initialListener); |
| 28 | + resolve(); |
| 29 | + } |
| 30 | + } |
| 31 | + }; |
| 32 | + window.addEventListener("message", initialListener); |
| 33 | + }); |
| 34 | + |
| 35 | + iframe.src = sandboxProxyUrl.href; |
| 36 | + |
| 37 | + return { iframe, onReady }; |
| 38 | +} |
| 39 | + |
| 40 | +export type ToolUiResourceInfo = { |
| 41 | + uri: string; |
| 42 | +}; |
| 43 | + |
| 44 | +export async function getToolUiResourceUri( |
| 45 | + client: Client, |
| 46 | + toolName: string, |
| 47 | +): Promise<ToolUiResourceInfo | null> { |
| 48 | + let tool: Tool | undefined; |
| 49 | + let cursor: string | undefined = undefined; |
| 50 | + do { |
| 51 | + const toolsResult = await client.listTools({ cursor }); |
| 52 | + tool = toolsResult.tools.find((t) => t.name === toolName); |
| 53 | + cursor = toolsResult.nextCursor; |
| 54 | + } while (!tool && cursor); |
| 55 | + if (!tool) { |
| 56 | + throw new Error(`tool ${toolName} not found`); |
| 57 | + } |
| 58 | + if (!tool._meta) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + let uri: string; |
| 63 | + if (MCP_UI_RESOURCE_META_KEY in tool._meta) { |
| 64 | + uri = String(tool._meta[MCP_UI_RESOURCE_META_KEY]); |
| 65 | + } else { |
| 66 | + return null; |
| 67 | + } |
| 68 | + if (!uri.startsWith("ui://")) { |
| 69 | + throw new Error( |
| 70 | + `tool ${toolName} has unsupported output template URI: ${uri}`, |
| 71 | + ); |
| 72 | + } |
| 73 | + return { uri }; |
| 74 | +} |
| 75 | + |
| 76 | +export async function readToolUiResourceHtml( |
| 77 | + client: Client, |
| 78 | + opts: { |
| 79 | + uri: string; |
| 80 | + }, |
| 81 | +): Promise<string> { |
| 82 | + const resource = await client.readResource({ uri: opts.uri }); |
| 83 | + |
| 84 | + if (!resource) { |
| 85 | + throw new Error("UI resource not found: " + opts.uri); |
| 86 | + } |
| 87 | + if (resource.contents.length !== 1) { |
| 88 | + throw new Error( |
| 89 | + "Unsupported UI resource content length: " + resource.contents.length, |
| 90 | + ); |
| 91 | + } |
| 92 | + const content = resource.contents[0]; |
| 93 | + let html: string; |
| 94 | + const isHtml = (t?: string) => t === "text/html+mcp"; |
| 95 | + |
| 96 | + if ( |
| 97 | + "text" in content && |
| 98 | + typeof content.text === "string" && |
| 99 | + isHtml(content.mimeType) |
| 100 | + ) { |
| 101 | + html = content.text; |
| 102 | + } else if ( |
| 103 | + "blob" in content && |
| 104 | + typeof content.blob === "string" && |
| 105 | + isHtml(content.mimeType) |
| 106 | + ) { |
| 107 | + html = atob(content.blob); |
| 108 | + } else { |
| 109 | + throw new Error( |
| 110 | + "Unsupported UI resource content format: " + JSON.stringify(content), |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + return html; |
| 115 | +} |
0 commit comments