createCodeMode()
Added in: @mastra/core@1.38.0
The createCodeMode() function returns a tool and generated instructions that let an agent run multi-tool computations as one TypeScript function. The generated code runs in a workspace sandbox, and each external_* call runs the real tool on the host with validation, request context, and tracing.
For a conceptual overview, see Code mode.
Usage exampleUsage example的直接链接
Create a code mode tool, add its generated instructions to the agent, and register the returned tool with the same id.
import { Agent } from '@mastra/core/agent'
import { createCodeMode, createTool } from '@mastra/core/tools'
import { LocalSandbox } from '@mastra/core/workspace'
import { z } from 'zod'
const getTopProducts = createTool({
id: 'getTopProducts',
description: 'Get top selling products',
inputSchema: z.object({ limit: z.number() }),
outputSchema: z.object({
products: z.array(z.object({ id: z.string(), name: z.string(), totalSales: z.number() })),
}),
execute: async ({ limit }) => fetchTopProducts(limit),
})
const getProductRatings = createTool({
id: 'getProductRatings',
description: 'Get ratings for a product',
inputSchema: z.object({ productId: z.string() }),
outputSchema: z.object({ ratings: z.array(z.object({ score: z.number() })) }),
execute: async ({ productId }) => fetchRatings(productId),
})
const { tool, instructions } = createCodeMode({
tools: { getTopProducts, getProductRatings },
sandbox: new LocalSandbox(),
})
export const shopAgent = new Agent({
id: 'shop-assistant',
name: 'shop-assistant',
instructions: ['You are a helpful shopping assistant.', instructions],
model: 'openai/gpt-5.6-sol',
tools: { execute_typescript: tool },
})
ParametersParameters的直接链接
config:
tools:
external_<id> functions. Only these tools can be called.sandbox?:
IsolatedVmCodeModeTransport). Pass new LocalSandbox() to run on the host explicitly.timeout?:
id?:
transport?:
requiresSandbox: false (such as IsolatedVmCodeModeTransport and QuickJsCodeModeTransport) run without a sandbox.ReturnsReturns的直接链接
Returns a CodeModeResult object.
tool:
execute_typescript.instructions:
external_* declarations for the configured tools.CodeModeToolResultCodeModeToolResult的直接链接
The generated tool returns a CodeModeToolResult.
success:
result?:
logs?:
console.log, console.info, console.warn, and console.error, in order.error?:
message:
name?:
line?:
Inspecting instructionsInspecting instructions的直接链接
Use createCodeModeInstructions() to inspect the exact prompt content generated for a set of tools.
import { createCodeModeInstructions } from '@mastra/core/tools'
console.log(
createCodeModeInstructions({
tools: { getTopProducts, getProductRatings },
}),
)
The instructions include the usage contract and one typed declare function external_<id>(...) line for each configured tool. Tool ids are sanitized into valid TypeScript function names, and conflicting sanitized names throw an error.
createCodeModeTool()createCodeModeTool()的直接链接
Use createCodeModeTool() when you only need the tool and want to manage instructions separately. Most agents should use createCodeMode() so the tool and matching instructions stay together.
import { createCodeModeTool } from '@mastra/core/tools'
import { LocalSandbox } from '@mastra/core/workspace'
export const codeModeTool = createCodeModeTool({
tools: { getTopProducts, getProductRatings },
sandbox: new LocalSandbox(),
})