import { Sandbox } from "e2b";
import { Mesa } from "@mesadev/sdk";
const mesa = new Mesa({ apiKey: process.env.MESA_API_KEY });
// --- Outside the sandbox: set up Mesa resources ---
// Create a repo (or use an existing one)
const repo = await mesa.repos.create({ name: "agent-workspace" });
// Create a scoped, short-lived API key for the sandbox
const ephemeralKey = await mesa.apiKeys.create({
name: "sandbox-session",
scopes: ["read", "write"],
expires_in_seconds: 3600, // 1 hour
// Optional: scope the key to a specific repo for better security
// repo_ids: [repoId1, repoId2, ...],
});
// --- Inside the sandbox: install and mount Mesa ---
const sandbox = await Sandbox.create();
// Install Mesa dependencies and the CLI.
// E2B exposes /dev/fuse as root-only by default, so we also fix permissions.
await sandbox.commands.run(
[
"apt-get update",
"apt-get install -y --no-install-recommends ca-certificates curl fuse3 gpg",
"sed -i 's/^#user_allow_other/user_allow_other/' /etc/fuse.conf",
"chmod 666 /dev/fuse",
"curl -fsSL https://mesa.dev/install.sh | sh -s -- --yes",
].join(" && "),
{ user: "root" }
);
// Start Mesa as a background daemon.
// MESA_ORGS configures the org and API key in one step.
await sandbox.commands.run("mesa mount -d -y", {
envs: {
MESA_ORGS: `my-org:${ephemeralKey.key}`,
},
});
// --- Run your agent ---
await sandbox.commands.run(
'cd ~/.local/share/mesa/mnt/my-org/agent-workspace \
&& claude "Implement the feature described in TODO.md"'
);