Update example-host.ts

This commit is contained in:
Olivier Chafik 2025-11-24 16:26:57 +00:00
commit 3c438c29c4
No known key found for this signature in database

View file

@ -66,7 +66,7 @@ window.addEventListener("load", async () => {
// Step 2: Create proxy server instance // Step 2: Create proxy server instance
const serverCapabilities = client.getServerCapabilities(); const serverCapabilities = client.getServerCapabilities();
const proxy = new AppBridge( const appBridge = new AppBridge(
client, client,
{ {
name: "Example MCP UI Host", name: "Example MCP UI Host",
@ -80,48 +80,41 @@ window.addEventListener("load", async () => {
); );
// Step 3: Register handlers BEFORE connecting // Step 3: Register handlers BEFORE connecting
proxy.oninitialized = () => { appBridge.oninitialized = () => {
console.log("[Example] Inner iframe MCP client initialized"); console.log("[Example] Inner iframe MCP client initialized");
// Send tool input once iframe is ready // Send tool input once iframe is ready
proxy.sendToolInput({ arguments: toolInput }); appBridge.sendToolInput({ arguments: toolInput });
}; };
proxy.setRequestHandler(McpUiOpenLinkRequestSchema, async (req) => { appBridge.onopenlink = async ({url}) => {
console.log("[Example] Open link requested:", req.params.url); console.log("[Example] Open link requested:", url);
window.open(req.params.url, "_blank", "noopener,noreferrer"); window.open(url, "_blank", "noopener,noreferrer");
return { isError: false }; return {isError: false};
}); };
proxy.setRequestHandler(McpUiMessageRequestSchema, async (req) => { appBridge.onmessage = async (params) => {
console.log("[Example] Message requested:", req.params); console.log("[Example] Message requested:", params);
return { isError: false }; return {isError: false};
}); };
// Handle size changes by resizing the iframe // Handle size changes by resizing the iframe
proxy.setNotificationHandler( appBridge.onsizechange = ({ width, height }) => {
McpUiSizeChangeNotificationSchema, if (width !== undefined) {
async (notif: any) => { iframe.style.width = `${width}px`;
const { width, height } = notif.params; }
if (width !== undefined) { if (height !== undefined) {
iframe.style.width = `${width}px`; iframe.style.height = `${height}px`;
} }
if (height !== undefined) { };
iframe.style.height = `${height}px`;
}
},
);
proxy.setNotificationHandler( appBridge.onloggingmessage = async params => {
LoggingMessageNotificationSchema, console.log("[Tool UI Log]", params);
async (notif: any) => { };
console.log("[Tool UI Log]", notif.params);
},
);
// Step 4: Connect proxy to iframe (triggers MCP initialization) // Step 4: Connect proxy to iframe (triggers MCP initialization)
// Pass iframe.contentWindow as both target and source for proper message filtering // Pass iframe.contentWindow as both target and source for proper message filtering
await proxy.connect( await appBridge.connect(
new PostMessageTransport(iframe.contentWindow!, iframe.contentWindow!), new PostMessageTransport(iframe.contentWindow!, iframe.contentWindow!),
); );
@ -134,7 +127,7 @@ window.addEventListener("load", async () => {
const html = await readToolUiResourceHtml(client, { const html = await readToolUiResourceHtml(client, {
uri: resourceInfo.uri, uri: resourceInfo.uri,
}); });
await proxy.sendSandboxResourceReady({ html }); await appBridge.sendSandboxResourceReady({ html });
console.log("[Example] Tool UI setup complete for:", toolName); console.log("[Example] Tool UI setup complete for:", toolName);
} catch (error) { } catch (error) {
@ -159,7 +152,7 @@ window.addEventListener("load", async () => {
Object.assign(document.createElement("button"), { Object.assign(document.createElement("button"), {
innerText: "Add MCP UI View", innerText: "Add MCP UI View",
onclick: () => onclick: () =>
createToolUI("create-example-ui", { message: "Hello from Host!" }), createToolUI("create-ui-vanilla", { message: "Hello from Host!" }),
}), }),
); );
} }