Add mcp-apps module: Spring AI 2.1.0 MCP server with MCP Apps HTML views, app-only tool callbacks, CSP declarations, SDK client tests and a Chromium harness on the official AppBridge
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4RzcCS5JCAgcxLz7T
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
// 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 ?? {}));
|
||||
};
|
||||
Reference in New Issue
Block a user