Mastra code execution tool
@slicevm/mastra adds a code execution tool to Mastra agents. The model writes JavaScript or TypeScript, the tool runs it in a Slice sandbox and returns the result, console output and any files the code wrote. Each call gets a fresh isolate with no network, file system, environment or subprocess access unless you grant it, and stops at hard fuel, memory and time limits.
The same tool runs code on your machine during development and on Slice Cloud in production.
Install
Section titled “Install”npm install @slicevm/mastra @mastra/core zodnpm install --save-dev @slicevm/cli # only needed to run locallyNode.js 22.13 or later, like Mastra itself.
Use it
Section titled “Use it”import { Agent } from "@mastra/core/agent";import { executeCode } from "@slicevm/mastra";
const agent = new Agent({ id: "analyst", name: "Analyst", instructions: "Use execute_code for calculations and data work.", model: "openai/gpt-5-mini", tools: { executeCode: executeCode() },});
const result = await agent.generate("What is the 50th Fibonacci number? Compute it.", { maxSteps: 5 });console.log(result.text);executeCode() returns a tool made with Mastra’s createTool, so it also works in workflow steps and on a Mastra MCP server. The model sees it as execute_code.
A tool, not a workspace sandbox
Section titled “A tool, not a workspace sandbox”Mastra workspace sandboxes (new Workspace({ sandbox }), such as E2B or Daytona) give an agent a whole machine: shell commands, processes and a file system. Slice runs JavaScript and TypeScript in isolates and has no shell, so it plugs in as a tool. The model sends code, and gets back a result, console output and files, within limits your app sets.
Local or Slice Cloud
Section titled “Local or Slice Cloud”| Setting | Where the code runs |
|---|---|
| No API key | Locally, through the slice binary from @slicevm/cli |
SLICE_API_KEY set, or executeCode({ apiKey }) |
Slice Cloud (https://api.slicevm.com) |
executeCode({ local: true }) |
Locally, even with a key set |
Create a key with eval access in the console. The Free plan is enough to try it.
What the model controls
Section titled “What the model controls”The model sends only code. Limits and grants come from your app, so a model cannot give itself network access or a bigger budget:
executeCode({ timeoutMs: 5_000, memoryKb: 16_384, allowNet: ["api.github.com"], // the description tells the model which hosts it can reach auditEgress: [process.env.GITHUB_TOKEN!], // never sent out, in any encoding});| Option | Default | |
|---|---|---|
fuel, memoryKb, timeoutMs |
the plan maximum on Slice Cloud; 25,000,000 fuel and 4,096 KB locally | Limits per run (plans) |
allowNet |
none | Hosts the code may reach |
auditEgress |
none | Secrets that must never leave the sandbox |
artifacts |
true |
Return files written under /artifacts; false refuses that path too |
deterministicSeed |
none | Seeds Math.random and the clock |
client |
created on first use | A client from createSlice() in @slicevm/sdk |
id |
execute_code |
The tool id |
description |
built in | Replaces the tool description. The built-in one (0.15.3) tells the model how results are returned, that each call starts clean, that Node built-ins and Web APIs are available but npm packages are not, which hosts it may reach, that only /artifacts is writable, and that refusals throw coded errors such as ERR_SLICE_NET_DENIED |
maxOutputChars |
20000 |
Longest console output sent to the model |
What comes back
Section titled “What comes back”The tool result has ok, result (the top-level return or last expression), stdout, error, deniedCapabilities, artifacts and, on Slice Cloud, runId.
- The model gets the run as JSON without file bytes.
- Your app gets everything, including each file’s bytes (Base64), in the tool results.
- A sandbox limit (fuel, memory, time, a refused capability) is not an exception: the model sees
ok: falseanderror, and can fix its code and try again. - An API error (invalid key, quota) throws
SliceApiErrorand reaches the model as a tool error.
import type { ExecuteCodeOutput } from "@slicevm/mastra";
const result = await agent.generate("Plot this monthly revenue as an SVG chart in /artifacts/revenue.svg: ...", { maxSteps: 5,});
for (const step of result.steps) { for (const r of step.toolResults) { const out = r.payload.result as ExecuteCodeOutput; for (const file of out.artifacts) console.log(file.name, file.size); }}On Slice Cloud with a console key, files are also kept for 1, 7 or 30 days by plan, and each one has a signed url.
Source
Section titled “Source”packages/mastra in slicevm/sdk, MIT licensed.