{"record":{"id":"dc8512ccb98aee42","repo":"mastra-ai/mastra","slug":"tool-toolid-is-not-available-in-code-mode","errorCode":null,"errorMessage":"Tool \"${toolId}\" is not available in Code Mode","messagePattern":"Tool \"(.+?)\" is not available in Code Mode","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/tools/code-mode/code-mode.ts","lineNumber":107,"sourceCode":"      let sandbox: WorkspaceSandbox | undefined = config.sandbox;\n      if (!sandbox && transport.requiresSandbox !== false) {\n        const requestContext = ctx?.requestContext ?? new RequestContext();\n        sandbox = await ctx?.workspace?.resolveSandbox({ requestContext });\n        if (!sandbox) {\n          throw new Error(\n            'Code Mode requires a sandbox to run model-authored code, but none was configured. ' +\n              'Pass one to createCodeMode({ tools, sandbox }), or run the agent in a workspace that provides a sandbox. ' +\n              'To execute on the host (host privileges — only for trusted/local use), pass `sandbox: new LocalSandbox()`.',\n          );\n        }\n      }\n\n      // Each external_* call re-enters the real Mastra tool pipeline (validation,\n      // request-context checks, tracing) on the host, with the outer tool's context.\n      const dispatch: CodeModeToolDispatcher = async (toolId, args) => {\n        const tool = toolsById.get(toolId);\n        if (!tool?.execute) {\n          throw new Error(`Tool \"${toolId}\" is not available in Code Mode`);\n        }\n        const result = await tool.execute(args, {\n          mastra: ctx?.mastra,\n          requestContext: ctx?.requestContext,\n          abortSignal: ctx?.abortSignal,\n          workspace: ctx?.workspace,\n        });\n        if (isValidationError(result)) {\n          throw new Error(result.message ?? `Invalid input for tool \"${toolId}\"`);\n        }\n        return result;\n      };\n\n      // The TypeScript program is written to a .ts module by the transport;\n      // the sandbox's node strips the type annotations natively at import.\n      return ctx.observe.span(`code-mode:${id}`, () =>\n        transport.run({\n          sandbox,","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/tools/code-mode/code-mode.ts#L89-L125","documentation":"Inside Code Mode, generated code calls tools via the external_* dispatcher. The dispatcher looks the toolId up in the registered toolsById map and throws when the tool is missing or has no execute function, preventing the model-authored program from invoking tools that were never exposed to Code Mode.","triggerScenarios":"Model-generated code calls external_foo for a tool id that was not included in the tools map passed to createCodeMode, or the registered entry lacks an execute function (e.g. a placeholder/marker tool like webSearchTool).","commonSituations":"The model hallucinates or mistypes a tool name; the tool was filtered out of the Code Mode tool set but the model still saw its stub from a previous turn; passing non-executable tool placeholders into createCodeMode.","solutions":["Include the tool in the tools map passed to createCodeMode so it is registered in toolsById.","Ensure the tool has an execute function; remove marker/placeholder tools from Code Mode's tool set.","Tighten the tool descriptions/stubs so the model only calls registered tool ids.","If the model fabricates names, constrain the prompt or validate the generated program before running."],"exampleFix":"// before\nconst codeMode = createCodeMode({ tools: { weather } }); // model calls external_search\n// after\nconst codeMode = createCodeMode({ tools: { weather, search } });","handlingStrategy":"try-catch","validationCode":"const registered = new Set(Object.keys(tools));\n// before running, ensure generated program only references registered ids:\nconst referenced = [...program.matchAll(/external_(\\w+)/g)].map(m => m[1]);\nconst unknown = referenced.filter(n => !registered.has(n));\nif (unknown.length) throw new Error(`Program references unregistered tools: ${unknown}`);","typeGuard":null,"tryCatchPattern":"try {\n  result = await codeModeTool.execute(args, ctx);\n} catch (e) {\n  if (String(e.message).includes('is not available in Code Mode')) {\n    // regenerate with corrected tool list / re-prompt the model\n  } else throw e;\n}","preventionTips":["Register every tool you advertise to the model with createCodeMode.","Strip placeholder tools without execute functions from Code Mode inputs.","Keep tool stubs and the registered tool map in sync."],"tags":["code-mode","tool-dispatch","missing-tool","runtime"],"backgroundTag":"tool-not-registered","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}