Skip to content

Commit 54a48a4

Browse files
committed
rename: AppRenderer
1 parent d32e43a commit 54a48a4

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

examples/simple-host/src/react/UITemplatedToolCallRenderer.tsx renamed to examples/simple-host/src/AppRenderer.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ import {
1717
getToolUiResourceUri,
1818
readToolUiResourceHtml,
1919
setupSandboxProxyIframe,
20-
} from "../app-host-utils";
20+
} from "./app-host-utils";
2121

2222
type RequestExtra = Parameters<
2323
Parameters<AppBridge["setRequestHandler"]>[1]
2424
>[1];
2525

2626
/**
27-
* Props for the UITemplatedToolCallRenderer component.
27+
* Props for the AppRenderer component.
2828
*/
29-
export interface UITemplatedToolCallRendererProps {
29+
export interface AppRendererProps {
3030
/** URL to the sandbox proxy HTML that will host the tool UI iframe */
3131
sandboxProxyUrl: URL;
3232

@@ -66,7 +66,7 @@ export interface UITemplatedToolCallRendererProps {
6666
*
6767
* @example
6868
* ```tsx
69-
* <UITemplatedToolCallRenderer
69+
* <AppRenderer
7070
* sandboxProxyUrl={new URL('http://localhost:8765/sandbox_proxy.html')}
7171
* client={mcpClient}
7272
* toolName="create-chart"
@@ -99,8 +99,8 @@ export interface UITemplatedToolCallRendererProps {
9999
* @param props - Component props
100100
* @returns React element containing the sandboxed tool UI iframe
101101
*/
102-
export const UITemplatedToolCallRenderer = (
103-
props: UITemplatedToolCallRendererProps,
102+
export const AppRenderer = (
103+
props: AppRendererProps,
104104
) => {
105105
const {
106106
client,
@@ -224,7 +224,7 @@ export const UITemplatedToolCallRenderer = (
224224
// Step 5: Store proxy in state
225225
setAppBridge(appBridge);
226226
} catch (err) {
227-
console.error("[UITemplatedToolCallRenderer] Error:", err);
227+
console.error("[AppRenderer] Error:", err);
228228
if (!mounted) return;
229229
const error = err instanceof Error ? err : new Error(String(err));
230230
setError(error);

examples/simple-host/src/example-host-react.tsx

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1+
import { useEffect, useState } from "react";
2+
import { createRoot } from "react-dom/client";
3+
14
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
25
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
36
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
47
import { Tool } from "@modelcontextprotocol/sdk/types.js";
5-
import { useEffect, useState } from "react";
6-
import { createRoot } from "react-dom/client";
7-
import { UIActionResult } from "../src/mcp-ui-types.js";
8-
import { UITemplatedToolCallRenderer } from "../src/react/UITemplatedToolCallRenderer.js";
8+
import { AppRenderer,AppRendererProps } from "../src/AppRenderer";
9+
import { AppBridge } from "../../../dist/src/app-bridge";
910

10-
const SANDBOX_PROXY_URL = URL.parse("/sandbox.html", location.href);
11+
const SANDBOX_PROXY_URL = URL.parse("/sandbox.html", location.href)!;
1112

1213
/**
13-
* Example React application demonstrating the UITemplatedToolCallRenderer component.
14+
* Example React application demonstrating the AppRenderer component.
1415
*
1516
* This shows how to:
1617
* - Connect to an MCP server
1718
* - List available tools
18-
* - Render tool UIs using UITemplatedToolCallRenderer
19+
* - Render tool UIs using AppRenderer
1920
* - Handle UI actions from the tool
2021
*/
2122
function ExampleApp() {
@@ -93,23 +94,19 @@ function ExampleApp() {
9394
setActiveTools((prev) => prev.filter((t) => t.id !== id));
9495
};
9596

96-
const handleUIAction = async (toolId: string, action: UIActionResult) => {
97-
console.log(`[React Host] UI Action from ${toolId}:`, action);
98-
99-
if (action.type === "intent") {
100-
console.log(
101-
"[React Host] Intent:",
102-
action.payload.intent,
103-
action.payload.params,
104-
);
105-
} else if (action.type === "link") {
106-
console.log("[React Host] Opening link:", action.payload.url);
107-
window.open(action.payload.url, "_blank", "noopener,noreferrer");
108-
} else if (action.type === "prompt") {
109-
console.log("[React Host] Prompt:", action.payload.prompt);
110-
} else if (action.type === "notify") {
111-
console.log("[React Host] Notification:", action.payload.message);
112-
}
97+
const handleMessage: AppRendererProps["onmessage"] = async (params, _extra) => {
98+
console.log("[React Host] Message:", params);
99+
return {};
100+
};
101+
102+
const handleLoggingMessage: AppRendererProps["onloggingmessage"] = (params) => {
103+
console.log("[React Host] Logging message:", params);
104+
};
105+
106+
const handleOpenLink: AppRendererProps["onopenlink"] = async (params, _extra) => {
107+
console.log("[React Host] Open link request:", params);
108+
window.open(params.url, "_blank", "noopener,noreferrer");
109+
return { isError: false };
113110
};
114111

115112
const handleError = (toolId: string, err: Error) => {
@@ -223,13 +220,15 @@ function ExampleApp() {
223220
</button>
224221
</div>
225222

226-
<UITemplatedToolCallRenderer
223+
<AppRenderer
227224
sandboxProxyUrl={SANDBOX_PROXY_URL}
228225
client={client}
229226
toolName={tool.name}
230227
toolInput={tool.input}
231-
onUIAction={(action) => handleUIAction(tool.id, action)}
232-
onError={(err) => handleError(tool.id, err)}
228+
onmessage={handleMessage}
229+
onloggingmessage={handleLoggingMessage}
230+
onopenlink={handleOpenLink}
231+
onerror={(err) => handleError(tool.id, err)}
233232
/>
234233
</div>
235234
))}

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const McpUiOpenLinkRequestSchema = RequestSchema.extend({
2020
export type McpUiOpenLinkRequest = z.infer<typeof McpUiOpenLinkRequestSchema>;
2121

2222
export const McpUiOpenLinkResultSchema = z.object({
23-
isError: z.boolean(),
23+
isError: z.boolean().optional(),
2424
});
2525
export type McpUiOpenLinkResult = z.infer<typeof McpUiOpenLinkResultSchema>;
2626

@@ -36,7 +36,7 @@ export type McpUiMessageRequest = z.infer<typeof McpUiMessageRequestSchema>;
3636
export const McpUiMessageResultSchema = z.object({
3737
// Note: we don't return the result from follow up messages as they might leak info from the chat.
3838
// We do tell the caller if it errored, though.
39-
isError: z.boolean(),
39+
isError: z.boolean().optional(),
4040
});
4141
export type McpUiMessageResult = z.infer<typeof McpUiMessageResultSchema>;
4242

0 commit comments

Comments
 (0)