Code execution for agent frameworks
An agent that writes code needs somewhere safe to run it. A Stillbox sandbox is an isolated Linux machine with Python, Node.js and common tools, which keeps its files between runs and sleeps when idle. Agent frameworks can use one in two ways: through MCP, which most frameworks speak, or through the SDKs’ code tool.
Make a sandbox for the agent first, and give the agent its own API key with the least access it needs:
stillbox create --name analyst --agent python # prints the sandbox IDThe examples read STILLBOX_API_KEY, STILLBOX_API_URL (your region’s address, ending in /v1) and the sandbox ID
from the environment.
Through MCP
Section titled “Through MCP”The sandbox’s tools (run_command, read_file, write_file, list_files, ask_agent) reach the agent with no
Stillbox code in your app.
OpenAI Agents SDK (Python)
import osfrom agents import Agent, Runnerfrom agents.mcp import MCPServerStreamableHttp
url = f"{os.environ['STILLBOX_API_URL']}/sandboxes/{os.environ['SANDBOX']}/mcp"async with MCPServerStreamableHttp( params={"url": url, "headers": {"Authorization": f"Bearer {os.environ['STILLBOX_API_KEY']}"}}) as sandbox: agent = Agent(name="Analyst", instructions="Use the sandbox to run code.", mcp_servers=[sandbox]) result = await Runner.run(agent, "Load sales.csv and report revenue by month")LangGraph and LangChain, with langchain-mcp-adapters:
from langchain_mcp_adapters.client import MultiServerMCPClient
client = MultiServerMCPClient({"sandbox": { "url": url, "transport": "streamable_http", "headers": {"Authorization": f"Bearer {os.environ['STILLBOX_API_KEY']}"},}})tools = await client.get_tools() # give these to your agent or ToolNodeMastra, with @mastra/mcp:
import { MCPClient } from "@mastra/mcp";
const mcp = new MCPClient({ servers: { sandbox: { url: new URL(`${process.env.STILLBOX_API_URL}/sandboxes/${process.env.SANDBOX}/mcp`), requestInit: { headers: { Authorization: `Bearer ${process.env.STILLBOX_API_KEY}` } }, }, },});const tools = await mcp.getTools(); // give these to your AgentThrough the SDK’s code tool
Section titled “Through the SDK’s code tool”code_tool (Python) and codeTool (TypeScript) make one tool, run_code(code, language), for Python, JavaScript,
bash or sh. Code runs with a time limit (two minutes unless you set another), and files and installed packages stay
between calls.
CrewAI
from crewai.tools import toolfrom stillbox import Stillbox, code_tool
sandbox = Stillbox().sandboxes.get(os.environ["SANDBOX"])run_code = tool("run_code")(code_tool(sandbox)) # the docstring becomes the tool's descriptionThe same function works with LangChain’s @tool and the OpenAI Agents SDK’s function_tool. In TypeScript,
codeTool(sandbox) returns the tool’s name, description, JSON Schema parameters and an execute function to
wrap for your framework. Without a framework:
result = sandbox.run_code("import platform; print(platform.python_version())")print(result.exit_code, result.stdout)What the agent can and can’t do
Section titled “What the agent can and can’t do”The sandbox is the agent’s whole world: it can’t reach your other sandboxes, your network or the cloud’s metadata, and its internet access follows the sandbox’s rules. Real keys never enter it; use an environment to give it placeholders for the APIs it may call. Every command it runs is in the sandbox’s command history and, through MCP, in the audit log.