Open History to see the last commit
1export interface CreateMcpAppBridgeOptions {
2 threadId: string
3 callEndpoint: string
4 chat: {
5 sendMessage: (
6 content: string,
7 body?: Record<string, unknown>,
8 ) => Promise<void>
9 }
10 fetchImpl?: typeof fetch
11 onLink?: (url: string) => void
12}
13
14export interface McpAppBridge {
15 callTool: (input: {
16 serverId?: string
17 toolName: string
18 args?: Record<string, unknown>
19 /**
20 * Reserved — forwarded to the call handler for correlation purposes but
21 * not consumed by the handler. Accepted on the wire; the handler does not
22 * read it (mirrors the `meta` convention on `UIResourcePart`).
23 */
24 messageId?: string
25 }) => Promise<unknown>
26 sendPrompt: (text: string) => Promise<void>
27 openLink: (url: string) => { isError: boolean }
28}
29
30interface ToolCallResponse {
31 ok: boolean
32 result?: unknown
33 error?: string
34}
35
36function isToolCallResponse(value: unknown): value is ToolCallResponse {
37 return (
38 value !== null &&
39 typeof value === 'object' &&
40 'ok' in value &&
41 typeof value.ok === 'boolean'
42 )
43}
44
45// Links arrive from an untrusted sandboxed widget. Only hand http(s)/mailto
46// URLs to the host's onLink; reject javascript:/data:/file:/etc. so a widget
47// can't smuggle a script-executing or local-resource URL through the bridge.
48const SAFE_LINK_SCHEMES = new Set(['http:', 'https:', 'mailto:'])
49function isSafeLink(url: string): boolean {
50 try {
51 return SAFE_LINK_SCHEMES.has(new URL(url).protocol)
52 } catch {
53 return false
54 }
55}
56
57export function createMcpAppBridge(
58 options: CreateMcpAppBridgeOptions,
59): McpAppBridge {
60 const { threadId, callEndpoint, chat, fetchImpl, onLink } = options
61 const doFetch = fetchImpl ?? fetch
62
63 return {
64 async callTool(input) {
65 const response = await doFetch(callEndpoint, {
66 method: 'POST',
67 headers: { 'content-type': 'application/json' },
68 body: JSON.stringify({
69 threadId,
70 serverId: input.serverId,
71 toolName: input.toolName,
72 args: input.args,
73 messageId: input.messageId,
74 }),
75 })
76
77 if (!response.ok) {
78 throw new Error(`MCP app tool call failed: HTTP ${response.status}`)
79 }
80
81 const raw: unknown = await response.json()
82 if (!isToolCallResponse(raw)) {
83 throw new Error('MCP app tool call failed')
84 }
85
86 if (!raw.ok) {
87 throw new Error(raw.error ?? 'MCP app tool call failed')
88 }
89
90 return raw.result
91 },
92
93 async sendPrompt(text) {
94 await chat.sendMessage(text)
95 },
96
97 openLink(url) {
98 if (!isSafeLink(url)) {
99 console.warn(
100 '[mcp-app-bridge] openLink rejected: unsupported URL scheme',
101 url,
102 )
103 return { isError: true }
104 }
105 if (onLink) {
106 try {
107 onLink(url)
108 return { isError: false }
109 } catch (err) {
110 console.warn('[mcp-app-bridge] openLink: onLink handler threw', err)
111 return { isError: true }
112 }
113 }
114 console.warn(
115 '[mcp-app-bridge] openLink ignored: no onLink handler configured',
116 )
117 return { isError: true }
118 },
119 }
120}