MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / handle

Function handle

src/core/tools/BaseTool.ts:113–161  ·  view source on GitHub ↗

* Main entry point for tool execution. * * Handles the complete flow: * 1. Partial message handling (if partial) * 2. Parameter parsing (nativeArgs only) * 3. Core execution (execute) * * @param task - Task instance * @param block - ToolUse block from assistant message * @param cal

(task: Task, block: ToolUse<TName>, callbacks: ToolCallbacks)

Source from the content-addressed store, hash-verified

111 * @param callbacks - Tool execution callbacks
112 */
113 async handle(task: Task, block: ToolUse<TName>, callbacks: ToolCallbacks): Promise<void> {
114 // Handle partial messages
115 if (block.partial) {
116 try {
117 await this.handlePartial(task, block)
118 } catch (error) {
119 console.error(`Error in handlePartial:`, error)
120 await callbacks.handleError(
121 `handling partial ${this.name}`,
122 error instanceof Error ? error : new Error(String(error)),
123 )
124 }
125 return
126 }
127
128 // Native-only: obtain typed parameters from `nativeArgs`.
129 let params: ToolParams<TName>
130 try {
131 if (block.nativeArgs !== undefined) {
132 // Native: typed args provided by NativeToolCallParser.
133 params = block.nativeArgs as ToolParams<TName>
134 } else {
135 // If legacy/XML markup was provided via params, surface a clear error.
136 const paramsText = (() => {
137 try {
138 return JSON.stringify(block.params ?? {})
139 } catch {
140 return ""
141 }
142 })()
143 if (paramsText.includes("<") && paramsText.includes(">")) {
144 throw new Error(
145 "XML tool calls are no longer supported. Use native tool calling (nativeArgs) instead.",
146 )
147 }
148 throw new Error("Tool call is missing native arguments (nativeArgs).")
149 }
150 } catch (error) {
151 console.error(`Error parsing parameters:`, error)
152 const errorMessage = `Failed to parse ${this.name} parameters: ${error instanceof Error ? error.message : String(error)}`
153 await callbacks.handleError(`parsing ${this.name} args`, new Error(errorMessage))
154 // Note: handleError already emits a tool_result via formatResponse.toolError in the caller.
155 // Do NOT call pushToolResult here to avoid duplicate tool_result payloads.
156 return
157 }
158
159 // Execute with typed parameters
160 await this.execute(params, task, callbacks)
161 }
162}

Callers

nothing calls this directly

Calls 5

handleErrorMethod · 0.80
errorMethod · 0.65
StringInterface · 0.50
handlePartialMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected