{"record":{"id":"aa73559619890527","repo":"vercel/ai","slug":"code-mode-tool-error","errorCode":"CODE_MODE_TOOL_ERROR","errorMessage":"Unknown tool: ${toolName}","messagePattern":"Unknown tool: (.+?)","errorType":"exception","errorClass":"CodeModeToolError","httpStatus":null,"severity":"error","filePath":"packages/code-mode/src/tool-invocation.ts","lineNumber":59,"sourceCode":"  codeModeInterrupt,\n  skipApproval = false,\n}: {\n  toolName: string;\n  inputJson: string;\n  tools: CodeModeToolSet;\n  baseExecutionOptions: CodeModeToolExecutionOptions;\n  codeModeOptions: CodeModeOptions;\n  maxToolInputBytes: number;\n  maxToolOutputBytes: number;\n  toolCallId: string;\n  codeModeInterrupt?: CodeModeInterruptExecutionContext;\n  skipApproval?: boolean;\n}): Promise<HostToolInvocationResult> {\n  throwIfAborted(baseExecutionOptions.abortSignal);\n\n  const hostTool = tools[toolName];\n  if (!hostTool) {\n    throw new CodeModeToolError(`Unknown tool: ${toolName}`, {\n      toolName,\n      availableTools: Object.keys(tools),\n    });\n  }\n  if (hostTool.execute == null) {\n    throw new CodeModeToolError(`Tool \"${toolName}\" does not have execute().`, {\n      toolName,\n    });\n  }\n\n  const input = fromJsonPayload(inputJson);\n  assertJsonSerializable(input, maxToolInputBytes, `Tool \"${toolName}\" input`);\n\n  const validation = await raceAgainstAbort(\n    validateToolInput(hostTool.inputSchema, input),\n    baseExecutionOptions.abortSignal,\n  );\n  if (!validation.success) {","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/code-mode/src/tool-invocation.ts#L41-L77","documentation":"invokeHostTool throws CodeModeToolError when the sandboxed code calls a tool name that is not present in the provided tools set. The error data includes `toolName` and `availableTools` so the developer can see what was requested versus what was registered. The code cannot proceed because there is no implementation to invoke.","triggerScenarios":"Generated code calls a helper like `get_weather(...)` that was not included in the `tools` passed to runCodeMode; a typo in the tool name inside the generated code; tools stripped by a policy filter.","commonSituations":"The model generated code referencing a tool it was told about in a prompt but that was never registered; tool set narrowed by permissions/approval filters; renaming a tool without regenerating the code.","solutions":["Add the missing tool to the tools object passed to runCodeMode.","Regenerate or correct the code so it only calls registered tools (list available tools in the code-generation prompt).","Check the error's `availableTools` data to confirm the exact registered names and fix typos.","If tools are intentionally filtered, instruct the model not to reference filtered tools or provide safe stubs."],"exampleFix":"// before\nawait runCodeMode({ js, tools: { search } }); // code calls get_weather\n// after\nawait runCodeMode({ js, tools: { search, get_weather: weatherTool } });","handlingStrategy":"try-catch","validationCode":"if (!(toolName in tools)) throw new Error(`Tool not registered: ${toolName}; available: ${Object.keys(tools).join(', ')}`);","typeGuard":"function isRegisteredTool(name: string, tools: Record<string, unknown>): name is keyof typeof tools & string {\n  return Object.prototype.hasOwnProperty.call(tools, name);\n}","tryCatchPattern":"try {\n  return await runCodeMode({ js, tools });\n} catch (e) {\n  if (CodeModeToolError.isInstance(e) && /^Unknown tool: /.test(e.message)) {\n    // feed availableTools back to the model and let it regenerate the code\n    return regenerateWithHints(e.data.availableTools);\n  }\n  throw e;\n}","preventionTips":["List the exact registered tool names in the code-generation prompt.","Log availableTools from the error data to catch typos quickly.","Keep one canonical tool registry so prompt, code, and registration never diverge.","Regenerate code after renaming or filtering tools."],"tags":["code-mode","tool","unknown-tool","sandbox"],"backgroundTag":"unknown-tool-reference","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}