MCPcopy Index your code
hub / github.com/TanStack/ai / generateToolWrappers

Function generateToolWrappers

packages/ai-isolate-cloudflare/src/worker/wrap-code.ts:86–121  ·  view source on GitHub ↗
(
  tools: Array<ToolSchema>,
  toolResults?: Record<string, ToolResultPayload>,
)

Source from the content-addressed store, hash-verified

84 * inputs contain non-deterministic values (e.g. random UUIDs).
85 */
86export function generateToolWrappers(
87 tools: Array<ToolSchema>,
88 toolResults?: Record<string, ToolResultPayload>,
89): string {
90 const wrappers: Array<string> = []
91
92 for (const tool of tools) {
93 assertSafeToolName(tool.name)
94 if (toolResults) {
95 wrappers.push(`
96 async function ${tool.name}(input) {
97 const callId = 'tc_' + (__toolCallIdx++);
98 const result = __toolResults[callId];
99 if (!result) {
100 __pendingToolCalls.push({ id: callId, name: '${tool.name}', args: input });
101 throw new __ToolCallNeeded(callId);
102 }
103 if (!result.success) {
104 throw new Error(result.error || 'Tool call failed');
105 }
106 return result.value;
107 }
108 `)
109 } else {
110 wrappers.push(`
111 async function ${tool.name}(input) {
112 const callId = 'tc_' + (__toolCallIdx++);
113 __pendingToolCalls.push({ id: callId, name: '${tool.name}', args: input });
114 throw new __ToolCallNeeded(callId);
115 }
116 `)
117 }
118 }
119
120 return wrappers.join('\n')
121}
122
123/**
124 * Wrap user code in an async IIFE with tool wrappers

Callers 3

worker.test.tsFile · 0.90
wrapCodeFunction · 0.85

Calls 2

assertSafeToolNameFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected