MCPcopy Create free account
hub / github.com/TanStack/ai / createToolBridgeCore

Function createToolBridgeCore

packages/ai-sandbox/src/tool-bridge.ts:125–182  ·  view source on GitHub ↗
(
  tools: Array<AnyTool>,
  options: ToolBridgeCoreOptions = {},
)

Source from the content-addressed store, hash-verified

123
124/** Build the transport-agnostic bridge core for the given tools. */
125export function createToolBridgeCore(
126 tools: Array<AnyTool>,
127 options: ToolBridgeCoreOptions = {},
128): ToolBridgeCore {
129 const toolsByName = new Map(tools.map((tool) => [tool.name, tool]))
130 const permission = options.permission
131
132 const permissionDescriptor: ToolDescriptor | undefined = permission
133 ? {
134 name: permission.toolName,
135 description:
136 'Permission prompt: returns {behavior:"allow"|"deny"} for a requested action.',
137 inputSchema: { type: 'object', properties: {} },
138 }
139 : undefined
140
141 return {
142 listTools() {
143 return [
144 ...tools.map((tool) => ({
145 name: tool.name,
146 description: tool.description,
147 inputSchema: toObjectSchema(tool.inputSchema),
148 })),
149 ...(permissionDescriptor ? [permissionDescriptor] : []),
150 ]
151 },
152
153 async callTool(name, args) {
154 if (permission && name === permission.toolName) {
155 const result = await permission.resolve(args ?? {})
156 return { content: [{ type: 'text', text: JSON.stringify(result) }] }
157 }
158 const tool = toolsByName.get(name)
159 if (!tool?.execute) throw new Error(`Unknown tool: ${name}`)
160 try {
161 const result: unknown = await tool.execute(args ?? {}, {
162 context: options.context,
163 abortSignal: options.signal,
164 // No-op default so tools that always call it (e.g. code mode) don't
165 // crash when the transport didn't wire a sink.
166 emitCustomEvent: options.emitCustomEvent ?? (() => {}),
167 })
168 const text =
169 typeof result === 'string' ? result : JSON.stringify(result)
170 return { content: [{ type: 'text', text }] }
171 } catch (error) {
172 const message = error instanceof Error ? error.message : String(error)
173 return {
174 isError: true,
175 content: [
176 { type: 'text', text: `Tool execution failed: ${message}` },
177 ],
178 }
179 }
180 },
181 }
182}

Callers 4

provisionFunction · 0.90
provisionFunction · 0.90
startHostToolBridgeFunction · 0.85

Calls

no outgoing calls

Tested by 1

provisionFunction · 0.72