Skip to content

Commit 3c438c2

Browse files
committed
Update example-host.ts
1 parent 1a4c018 commit 3c438c2

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

examples/simple-host/src/example-host.ts

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ window.addEventListener("load", async () => {
6666

6767
// Step 2: Create proxy server instance
6868
const serverCapabilities = client.getServerCapabilities();
69-
const proxy = new AppBridge(
69+
const appBridge = new AppBridge(
7070
client,
7171
{
7272
name: "Example MCP UI Host",
@@ -80,48 +80,41 @@ window.addEventListener("load", async () => {
8080
);
8181

8282
// Step 3: Register handlers BEFORE connecting
83-
proxy.oninitialized = () => {
83+
appBridge.oninitialized = () => {
8484
console.log("[Example] Inner iframe MCP client initialized");
8585

8686
// Send tool input once iframe is ready
87-
proxy.sendToolInput({ arguments: toolInput });
87+
appBridge.sendToolInput({ arguments: toolInput });
8888
};
8989

90-
proxy.setRequestHandler(McpUiOpenLinkRequestSchema, async (req) => {
91-
console.log("[Example] Open link requested:", req.params.url);
92-
window.open(req.params.url, "_blank", "noopener,noreferrer");
93-
return { isError: false };
94-
});
90+
appBridge.onopenlink = async ({url}) => {
91+
console.log("[Example] Open link requested:", url);
92+
window.open(url, "_blank", "noopener,noreferrer");
93+
return {isError: false};
94+
};
9595

96-
proxy.setRequestHandler(McpUiMessageRequestSchema, async (req) => {
97-
console.log("[Example] Message requested:", req.params);
98-
return { isError: false };
99-
});
96+
appBridge.onmessage = async (params) => {
97+
console.log("[Example] Message requested:", params);
98+
return {isError: false};
99+
};
100100

101101
// Handle size changes by resizing the iframe
102-
proxy.setNotificationHandler(
103-
McpUiSizeChangeNotificationSchema,
104-
async (notif: any) => {
105-
const { width, height } = notif.params;
106-
if (width !== undefined) {
107-
iframe.style.width = `${width}px`;
108-
}
109-
if (height !== undefined) {
110-
iframe.style.height = `${height}px`;
111-
}
112-
},
113-
);
102+
appBridge.onsizechange = ({ width, height }) => {
103+
if (width !== undefined) {
104+
iframe.style.width = `${width}px`;
105+
}
106+
if (height !== undefined) {
107+
iframe.style.height = `${height}px`;
108+
}
109+
};
114110

115-
proxy.setNotificationHandler(
116-
LoggingMessageNotificationSchema,
117-
async (notif: any) => {
118-
console.log("[Tool UI Log]", notif.params);
119-
},
120-
);
111+
appBridge.onloggingmessage = async params => {
112+
console.log("[Tool UI Log]", params);
113+
};
121114

122115
// Step 4: Connect proxy to iframe (triggers MCP initialization)
123116
// Pass iframe.contentWindow as both target and source for proper message filtering
124-
await proxy.connect(
117+
await appBridge.connect(
125118
new PostMessageTransport(iframe.contentWindow!, iframe.contentWindow!),
126119
);
127120

@@ -134,7 +127,7 @@ window.addEventListener("load", async () => {
134127
const html = await readToolUiResourceHtml(client, {
135128
uri: resourceInfo.uri,
136129
});
137-
await proxy.sendSandboxResourceReady({ html });
130+
await appBridge.sendSandboxResourceReady({ html });
138131

139132
console.log("[Example] Tool UI setup complete for:", toolName);
140133
} catch (error) {
@@ -159,7 +152,7 @@ window.addEventListener("load", async () => {
159152
Object.assign(document.createElement("button"), {
160153
innerText: "Add MCP UI View",
161154
onclick: () =>
162-
createToolUI("create-example-ui", { message: "Hello from Host!" }),
155+
createToolUI("create-ui-vanilla", { message: "Hello from Host!" }),
163156
}),
164157
);
165158
}

0 commit comments

Comments
 (0)