Skip to main content

Agent frameworks & SDKs

Mastra

Connect a Mastra workspace to an E2B sandbox. With @mastra/e2b, the commands your Mastra agent runs execute in an isolated cloud sandbox instead of on your machine.

Maintained by
Mastra
How it works
Mastra workspace sandbox
Package
@mastra/e2b
Language
TypeScript

Set it up

Get started

Paste the prompt into your coding agent, or follow the steps yourself.

Paste this into your coding agent

Add E2B sandboxes to this Mastra project.

  1. 1.Install @mastra/core and @mastra/e2b using the project's package manager.
  2. 2.The sandbox needs the E2B_API_KEY environment variable. If it isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode it.
  3. 3.Follow the Mastra guide at https://docs.e2b.dev/agents/mastra. Give my agent a Workspace with an E2BSandbox, call workspace.init() before it runs, and workspace.destroy() in a finally block.
  4. 4.Run one task and show me the output that came from the sandbox.

Or set it up yourself

  1. 1. Install Mastra and the E2B provider

    npm init -y && npm pkg set type=module
    npm install @mastra/core @mastra/e2b zod
    npm install --save-dev tsx typescript @types/node
  2. 2. Set your API keys

    export E2B_API_KEY="e2b_***"
    export OPENAI_API_KEY="sk-***"
  3. 3. Give the agent a sandbox workspace

    import { Agent } from '@mastra/core/agent'
    import { Workspace } from '@mastra/core/workspace'
    import { E2BSandbox } from '@mastra/e2b'
    
    const workspace = new Workspace({
      sandbox: new E2BSandbox({
        id: `mastra-coding-agent-${Date.now()}`,
        timeout: 600_000,
      }),
    })
    
    const agent = new Agent({
      id: 'coding-agent',
      name: 'Coding Agent',
      model: 'openai/gpt-5.4-mini',
      instructions: 'You are a software engineer working in an isolated E2B sandbox.',
      workspace,
    })
  4. 4. Run the agent

    const task = process.argv.slice(2).join(' ') || 'Create a small Express API with a health endpoint and test it'
    
    try {
      await workspace.init()
    
      const stream = await agent.stream(task, { maxSteps: 30 })
    
      for await (const chunk of stream.textStream) {
        process.stdout.write(chunk)
      }
    } finally {
      await workspace.destroy()
    }

Security

What runs in the sandbox

Your Mastra agent runs locally; the commands it chooses run in an isolated sandbox.

  • Commandsexecute_command calls from the agent, routed by @mastra/e2b to E2B command execution.
  • FilesA sandbox-only workspace has no read_file or write_file tools, so the agent works on files through the shell.
  • Lifetimeworkspace.init() creates or reconnects the sandbox and workspace.destroy() kills it. timeout is in milliseconds.
  • TemplatePass a template ID or name to E2BSandbox to preinstall runtimes and dependencies.

3B+ sandboxes started

Give every agent a machine