Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4RzcCS5JCAgcxLz7T
55 lines
2.8 KiB
JavaScript
55 lines
2.8 KiB
JavaScript
// A small web host built on the official AppBridge. It is a test harness, not a chat client: there is no model.
|
|
import { AppBridge, PostMessageTransport, getToolUiResourceUri } from "@modelcontextprotocol/ext-apps/app-bridge";
|
|
import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client";
|
|
|
|
const MIME = "text/html;profile=mcp-app";
|
|
window.hostLog = [];
|
|
const log = (line) => window.hostLog.push(line);
|
|
|
|
window.runApp = async ({ mcpUrl, sandboxUrl, toolName, dropCsp }) => {
|
|
const client = new Client({ name: "harness-host", version: "1.0.0" },
|
|
{ capabilities: { extensions: { "io.modelcontextprotocol/ui": { mimeTypes: [MIME] } } } });
|
|
await client.connect(new StreamableHTTPClientTransport(new URL(mcpUrl)));
|
|
|
|
const tool = (await client.listTools()).tools.find((t) => t.name === toolName);
|
|
const uri = getToolUiResourceUri(tool);
|
|
const [content] = (await client.readResource({ uri })).contents;
|
|
if (content.mimeType !== MIME) throw new Error("not an MCP App: " + content.mimeType);
|
|
// dropCsp simulates a server that forgot to declare its domains.
|
|
const csp = dropCsp ? undefined : content._meta?.ui?.csp;
|
|
log(`host: tool ${toolName} -> ${uri}, declared csp ${JSON.stringify(csp) ?? "none"}`);
|
|
|
|
const frame = document.createElement("iframe");
|
|
frame.id = "sandbox";
|
|
frame.setAttribute("sandbox", "allow-scripts allow-same-origin");
|
|
frame.style.cssText = "width:640px;height:260px;border:1px solid #ccc";
|
|
document.body.append(frame);
|
|
|
|
const bridge = new AppBridge(client, { name: "harness-host", version: "1.0.0" },
|
|
{ serverTools: {}, updateModelContext: { text: {} }, message: { text: {} } },
|
|
{ hostContext: { theme: "light" } });
|
|
|
|
bridge.onsandboxready = () => bridge.sendSandboxResourceReady({ html: content.text, csp });
|
|
bridge.oninitialized = async () => {
|
|
await bridge.sendToolInput({ arguments: {} });
|
|
await bridge.sendToolResult(await client.callTool({ name: toolName, arguments: {} }));
|
|
log("host: tool-input and tool-result delivered");
|
|
};
|
|
bridge.onsizechange = ({ height }) => { if (height) frame.style.height = height + "px"; };
|
|
bridge.onupdatemodelcontext = async (p) => { log("host: model context = " + p.content[0].text); return {}; };
|
|
bridge.onmessage = async (p) => { log("host: chat message = " + p.content[0].text); return {}; };
|
|
|
|
// Wire log of what the view side sends, in order.
|
|
window.addEventListener("message", (e) => {
|
|
if (e.source !== frame.contentWindow) return;
|
|
const m = e.data;
|
|
if (m.method) {
|
|
const detail = m.method === "tools/call" ? ` ${m.params.name} ${JSON.stringify(m.params.arguments)} (id ${m.id})` : "";
|
|
log("view -> host: " + m.method + detail);
|
|
}
|
|
});
|
|
|
|
await bridge.connect(new PostMessageTransport(frame.contentWindow, frame.contentWindow));
|
|
frame.src = sandboxUrl + "?csp=" + encodeURIComponent(JSON.stringify(csp ?? {}));
|
|
};
|