Give your agent a sandbox
Give an AI SDK, Mastra or OpenAI Agents SDK agent a sandbox to run shell commands and edit files on E2B, Daytona or Docker, with sandboxTools.
To give an AI agent a sandbox where it can run shell commands, create one with createSandbox from @opencoredev/sandbox-sdk and pass sandboxTools(box) to your agent. The model gets run, read_file, edit_file and four more tools that execute inside the sandbox instead of in your app’s process, and the same agent code runs on E2B, Daytona, Vercel Sandbox or a Docker container through local(). You don’t write tool definitions: sandboxTools returns them in your framework’s own format.
Each framework has its own subpath, so you only load the one you use:
| Framework | Import | Returns | Page |
|---|---|---|---|
| AI SDK | @opencoredev/sandbox-sdk/ai |
A tool set for tools |
AI SDK |
| Mastra | @opencoredev/sandbox-sdk/mastra |
A tool map for new Agent |
Mastra |
| OpenAI Agents SDK | @opencoredev/sandbox-sdk/openai |
A tool array for new Agent |
OpenAI Agents |
The tools behave the same in every framework. This page uses the AI SDK. The framework pages show the same agent in Mastra and the OpenAI Agents SDK.
Hand the model the tools
npm install @opencoredev/sandbox-sdk ai zodpnpm add @opencoredev/sandbox-sdk ai zodyarn add @opencoredev/sandbox-sdk ai zodbun add @opencoredev/sandbox-sdk ai zodimport { generateText, stepCountIs } from "ai";
import { createSandbox } from "@opencoredev/sandbox-sdk";
import { e2b } from "@opencoredev/sandbox-sdk/e2b";
import { sandboxTools } from "@opencoredev/sandbox-sdk/ai";
await using box = await createSandbox(e2b());
await box.git.clone("https://github.com/acme/web.git", { dir: "app", depth: 1 });
const { text } = await generateText({
model: "anthropic/claude-opus-5-5",
tools: sandboxTools(box, { root: "/workspace/app" }),
stopWhen: stepCountIs(30),
prompt: "Run the tests in /workspace/app and fix the first failure.",
});
console.log(text);
The model string goes through the Vercel AI Gateway, so set AI_GATEWAY_API_KEY on your server. Any AI SDK provider works in its place.
The agent calls run to execute the tests, read_file to look at the failure, and edit_file to change it. You get its final summary in text.
The tools
Seven tools are on by default. Each does one thing and has flat parameters, and each description tells the model when not to use it.
| Tool | Parameters | What it does |
|---|---|---|
run |
command, cwd?, timeoutMs? |
Runs a command and waits for it to exit |
read_file |
path, offset?, limit? |
Returns numbered lines and a hash of the file |
write_file |
path, content |
Writes a whole file |
edit_file |
path, find, replace |
Replaces text that appears exactly once |
start_process |
name, command, cwd?, port? |
Starts a background process, waits for it, returns its URL |
process_logs |
name, cursor? |
Reads a process’s output from a cursor |
stop_process |
name |
Stops a background process |
Four more are available when you ask for them: search, list_files, changes, and preview.
What the model sees
Tool results are short so they don’t fill the context window. Every run result opens with a verdict line, then the output:
FAILED exit=1 4.2s cwd=/workspace/app
output:
FAIL src/login.test.ts
expected 200, received 500
Output is stripped of color codes and capped at 16 KB. Past that, the model gets the start and the end with a [N bytes cut, use process_logs] marker in between. Errors follow the same “what happened, why, what to do next” format as SandboxError, and secrets never appear.
Limit what the agent can do
import { generateText, stepCountIs } from "ai";
import { createSandbox } from "@opencoredev/sandbox-sdk";
import { e2b } from "@opencoredev/sandbox-sdk/e2b";
import { sandboxTools } from "@opencoredev/sandbox-sdk/ai";
await using box = await createSandbox(e2b(), { network: { allow: ["registry.npmjs.org"] } });
const tools = sandboxTools(box, {
root: "/workspace/app",
allow: ["run", "read_file", "edit_file", "search"],
beforeRun: (command) => (command.includes("git push") ? false : command),
});
const { text } = await generateText({
model: "anthropic/claude-opus-5-5",
tools,
stopWhen: stepCountIs(20),
prompt: "Find why the build fails and fix it.",
});
console.log(text);
rootkeeps every file path and working directory the model passes inside one directory. It is not a security boundary: a shell command can stillcdout or use an absolute path, so rely on the sandbox andbeforeRunfor that.allowanddenypick which tools exist. Here the model can read, edit, search, and run, but can’t start processes.beforeRunsees eachrunandstart_processcommand before it runs. Returnfalseto refuse it, a string to run instead, ortrueto allow it.- Pair tools with a network policy so a command can’t reach hosts you didn’t allow.
The full option list, including per-tool limits, is in the agent tools reference.
Show full output in your UI
The model gets a compact result, but people often want the whole log. Pass onEvent to receive every chunk of output as it happens and forward it to your UI. With the AI SDK, the tools also stream preliminary results, so a useChat UI shows output while the command is still running. The AI app builder guide wires this up end to end.
Common questions
Do the agent’s commands run on my server? No. Every tool call goes to the sandbox you created, a cloud microVM or container on E2B, Daytona, Vercel Sandbox and the other cloud providers. With local() it is a Docker container on the machine that runs your code, so read Security before you hand it untrusted prompts.
How is this different from the AI SDK’s own bash tool? The AI SDK ships experimental_createBashTool in @ai-sdk/tool-bash: one bash tool over a sandbox session from an adapter package, such as @ai-sdk/sandbox-vercel for Vercel Sandbox. sandboxTools gives the model separate tools for commands, files and background processes with compact results, works in Mastra and the OpenAI Agents SDK too, and runs on any Sandbox SDK provider. Mastra’s Workspace and the OpenAI Agents SDK’s SandboxAgent are the same kind of framework-owned option; the Mastra and OpenAI Agents pages compare them.
Does every tool work on every provider? run and the file tools do. start_process needs background processes, which agentOS doesn’t have, and on Vercel Sandbox and local() you list the dev server’s port in ports when you create the sandbox. A tool the provider can’t support fails with unsupported. The capability matrix has the details.
Can I test the agent without a cloud account? Yes. Create the sandbox from memory() and your handlers answer the commands, with no key and no network. See Testing with memory().