Give a Mastra agent a sandbox
mastraTools(box) returns Mastra tools backed by a sandbox. Pass them to
new Agent({ tools }); Mastra runs the agent loop and each tool call runs in
the sandbox.
Install
npm install @mastra/core @tangle-network/sandboxSet the key for your model provider, such as OPENAI_API_KEY, and
TANGLE_API_KEY for the sandbox.
Run an agent
import { Agent } from "@mastra/core/agent";
import { Sandbox } from "@tangle-network/sandbox";
import { mastraTools } from "@tangle-network/sandbox/agent";
const client = new Sandbox({
apiKey: process.env.TANGLE_API_KEY!,
baseUrl: "https://sandbox.tangle.tools",
});
const box = await client.create();
try {
const tools = await mastraTools(box, { sessionId: "chat-1" });
const agent = new Agent({
id: "sandbox-coder",
name: "sandbox-coder",
instructions:
"You run code in a sandbox to answer questions. " +
"Use sandbox_run_code with the same session for stateful work.",
model: "openai/gpt-5.4",
tools,
});
const result = await agent.generate("Write a small fizzbuzz test with node:test and run it.");
console.log(result.text);
} finally {
await box.delete();
}Use any model your Mastra setup supports. mastraTools is async because it
loads @mastra/core at runtime.
Python and Node in one sandbox
The sandbox keeps one kernel per session and language, so one agent can run Python and Node in the same session. Templates and images lists what the default image includes.
Choose the tools
const tools = await mastraTools(box, {
sessionId: "chat-1",
allow: ["sandbox_run_code", "sandbox_exec", "sandbox_write"],
});The default set is sandbox_run_code, sandbox_exec, sandbox_read,
sandbox_write, sandbox_list and sandbox_search. Each tool returns
{ result }.
mastraFleetTools(client) gives the agent tools to spawn and run fleets; see
Integrations. For retries and duplicate tool calls,
see Retries and duplicate work.