Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

mcp-apps.tsx

108 lines3.8 KB

1/**
2 * MCPAppResource — Preact wrapper for rendering MCP App UI resources.
3 *
4 * PREACT/COMPAT NOTE:
5 * `@mcp-ui/client@7.1.1` is published React-only: `AppRenderer` is a React
6 * `forwardRef` component. There is NO dedicated preact entry point and NO web
7 * component variant. This wrapper imports `{ AppRenderer }` from
8 * `'@mcp-ui/client'` identically to the React wrapper and relies on the
9 * *consumer's* `preact/compat` alias (resolving `react` / `react-dom` to
10 * `preact/compat`) to render the React component under Preact.
11 * Wiring up that alias is the consumer's responsibility and is NOT
12 * runtime-verified in this repository.
13 */
14import { AppRenderer } from '@mcp-ui/client'
15import type { AppRendererProps } from '@mcp-ui/client'
16import type { UIResourcePart } from '@tanstack/ai'
17import type { McpAppBridge } from '@tanstack/ai-client'
18​
19export interface MCPAppResourceProps {
20 /** The ui-resource part from a UIMessage assistant part. */
21 part: UIResourcePart
22 /**
23 * Framework-agnostic bridge for tool calls, prompt sending, and link opening.
24 * Omit it to render the widget in display-only mode — iframe interactions
25 * that would trigger tool calls or prompts are ignored.
26 */
27 bridge?: McpAppBridge
28 /** Sandbox iframe configuration — must include the proxy page URL. */
29 sandbox: { url: URL }
30 /** Optional structured arguments forwarded to the guest UI once it's ready. */
31 toolInput?: Record<string, unknown>
33​
34function isRecord(value: unknown): value is Record<string, unknown> {
35 return typeof value === 'object' && value !== null && !Array.isArray(value)
37​
38/**
39 * Coalesce an arbitrary bridge result into the `string` required by the
40 * `CallToolResult` text content block. `JSON.stringify` is typed to return
41 * `string` but actually returns `undefined` for inputs like `undefined` or a
42 * function, so we narrow explicitly to avoid `text: undefined`.
43 */
44function resultToText(result: unknown): string {
45 if (typeof result === 'string') return result
46 // `JSON.stringify`'s lib signature claims `string`, but it returns
47 // `undefined` for `undefined`/function inputs; type the call honestly.
48 const stringify: (value: unknown) => string | undefined = JSON.stringify
49 return stringify(result) ?? 'null'
51​
52/**
53 * Renders an MCP App UI resource inside a sandboxed iframe.
54 *
55 * Wraps `@mcp-ui/client`'s `AppRenderer` and wires its callbacks to a
56 * framework-agnostic {@link McpAppBridge}.
57 */
58export function MCPAppResource(props: MCPAppResourceProps) {
59 const { part, bridge, sandbox, toolInput } = props
60​
61 const onCallTool: AppRendererProps['onCallTool'] = bridge
62 ? async (params) => {
63 const result = await bridge.callTool({
64 serverId: part.serverId,
65 toolName: params.name,
66 args: params.arguments,
67 })
68​
69 return {
70 content: [
71 {
72 type: 'text' as const,
73 text: resultToText(result),
74 },
75 ],
76 structuredContent: isRecord(result) ? result : undefined,
77 }
78 }
79 : undefined
80​
81 const onMessage: AppRendererProps['onMessage'] = bridge
82 ? async (params) => {
83 const text = params.content
84 .filter((c): c is { type: 'text'; text: string } => c.type === 'text')
85 .map((c) => c.text)
86 .join('')
87 if (text) await bridge.sendPrompt(text)
88 return {}
89 }
90 : undefined
91​
92 const onOpenLink: AppRendererProps['onOpenLink'] = bridge
93 ? (params) => Promise.resolve(bridge.openLink(params.url))
94 : undefined
95​
96 return (
97 <AppRenderer
98 toolName={part.toolName}
99 sandbox={sandbox}
100 html={part.resource.text}
101 toolResourceUri={part.resource.uri}
102 toolInput={toolInput}
103 onCallTool={onCallTool}
104 onMessage={onMessage}
105 onOpenLink={onOpenLink}
106 />
107 )