Code Box
Enables CrewAI agents to execute code (Python/JS), run SQL queries, manage files, and handle artifacts via MCP tools, leveraging Code Box as a self-hosted code interpreter.
Enables LangGraph agents to execute code (Python/JS), run SQL queries, manage files, and handle artifacts via MCP tools, leveraging Code Box as a self-hosted code interpreter.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Code Boxrun Python code to analyze the uploaded data and generate a plot"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
⭐ If you find this project useful, please consider giving it a star! ⭐
Overview
Code Box is a self-hosted, production-grade replacement for Azure Assistants Code Interpreter. It delivers the same stateful code execution experience — persistent kernels, file I/O, artifact generation — without being locked into the Azure OpenAI Assistants API or paying per-token execution costs.
Built on Open Interpreter and exposed via the Model Context Protocol (MCP), Code Box runs on any infrastructure (your laptop, a VM, Azure Web App, a container) and plugs into any MCP-compatible agent framework: Semantic Kernel, LangGraph, CrewAI, AutoGen, or your own custom client.
Why Code Box over Azure Code Interpreter?
Azure Code Interpreter | Code Box | |
Hosting | Managed by Azure OpenAI — no control | Self-hosted anywhere — full control |
Cost | Per-token + per-session charges | Zero marginal cost — run on your own compute |
LLM coupling | Tightly bound to Azure OpenAI models | No LLM at the server — bring any model, any provider |
Framework lock-in | Assistants API only | Open MCP protocol — works with any agent framework |
SQL queries | Not supported | Built-in |
Artifact storage | Azure-managed, opaque | Transparent local filesystem + optional Azure Blob with SAS URLs |
Customisation | Limited to API parameters | Full control over timeouts, file limits, session lifecycle, blob config |
Multi-tenant isolation | Per-assistant | Per-session — each session gets its own interpreter process and filesystem |
Key Capabilities
Capability | Description |
Stateful Code Execution | Run Python/JS code across multiple calls; variables, imports, and DataFrames persist within a session. |
SQL Query Execution | Run read-only |
File Upload & Download | Download files from URLs into isolated session workspaces for processing. |
Artifact Management | Auto-detect generated files (plots, CSVs, Excel, HTML, PDF, SVG, JSON) and surface them to the agent. |
Azure Blob Integration | Auto-upload artifacts to Azure Blob Storage with time-limited SAS URLs. |
Multi-Tenant Isolation | Each session gets its own interpreter process, filesystem, and lifecycle. |
Architecture
Design Principles
One interpreter per session — complete isolation; no state bleeds across sessions.
Zero LLM dependency — the server executes code directly via Open Interpreter's runtime. Your agent chooses the model; Code Box just runs the code.
Stateless HTTP transport — every request is independent, making it fully compatible with Azure Web App reverse proxies, load balancers, and containers.
Client config clamping —
effective = min(client_value, server_max)ensures server-configured safety limits can never be exceeded by any client.
Prerequisites
Requirement | Details |
Python | 3.10 or higher |
pip | Latest version recommended |
ODBC Driver (if using | Microsoft ODBC Driver 17+ for SQL Server |
Azure Blob Storage (optional) | Storage account + connection string for artifact uploads |
Azure SQL Database (optional) | Database + connection string for |
Quick Start
1. Clone & set up the environment
cd CodeBoxMCP
# Create and activate a virtual environment
python -m venv .venv
# Windows
.venv\Scripts\activate
# Linux / macOS
source .venv/bin/activate
# Install dependencies
pip install -r requirements.txt2. Configure environment variables
Create a .env file in the project root:
# ─── Server ───
MCP_SERVER_NAME=Code Interpreter MCP
MCP_HOST=0.0.0.0
MCP_PORT=8000
# ─── Session Lifecycle ───
SESSION_TTL=3600
IDLE_TIMEOUT=1800
EXEC_TIMEOUT=300
# ─── Azure Blob Storage (optional) ───
AZURE_BLOB_CONNECTION_STRING=<your-connection-string>
AZURE_BLOB_CONTAINER_NAME=code-interpreter-artifacts
BLOB_SAS_EXPIRY_HOURS=24
# ─── Azure SQL Database (optional) ───
AZURE_DATABASE_CONNECTION_STRING=<your-odbc-connection-string>
AZURE_DATABASE_PASSWORD=<your-password>3. Start the server
# Option A — module
python -m codebox
# Option B — script
python server.py
# Option C — shell (Linux/macOS)
bash run.shOn success the server logs:
============================================================
Code Interpreter MCP
Transport : streamable-http
Endpoint : http://localhost:8000/mcp
============================================================MCP Tools Reference
Code Box exposes 7 tools via MCP. All accept and return JSON.
Tool | Purpose |
Run Python/JS in a stateful kernel — variables persist across calls | |
Run read-only SELECT queries against Azure SQL | |
Download a file from a URL into the session's | |
List all generated files in | |
Get session paths ( | |
Show all active sessions | |
Kill a session and free resources |
exec_code
Execute code in a stateful interpreter session. State persists within the same session_id — variables, imports, DataFrames all survive across calls. Don't re-import or re-load what's already in memory; reference existing variables directly.
Parameters:
session_id (string, required) — Unique session identifier; auto-created on first use.
language (string, required) — e.g. "python"
code (string, required) — The code to execute.Generated files (plots, CSVs, etc.) are automatically detected as artifacts. If Azure Blob is configured, each artifact gets a SAS URL in the response (new_artifacts[].sas_url).
exec_sql
Run a read-only SQL query with two-level safety:
Application layer — keyword blocking (rejects
INSERT,UPDATE,DELETE,DROP, etc.).Database layer — always-rollback transaction; no writes can ever persist.
Small results (≤ SQL_MAX_INLINE_ROWS, default 30) are returned inline as JSON. Large results are saved as CSV in input/ — load directly with pd.read_csv(path), no re-upload needed.
upload_file
Download a file from a URL (public or SAS) into the session's input/ folder. Use the returned saved_path in subsequent exec_code calls. Respects MAX_DOWNLOAD_SIZE (default 500 MB) and DOWNLOAD_TIMEOUT (default 120 s).
list_artifacts
Returns metadata for every artifact in the session's output/ directory (filename, path, extension, size, optional SAS URL).
session_info
Returns the session's working directory paths (workdir, input_dir, output_dir) and effective configuration. Recommended as the first call so the agent discovers absolute paths before writing or reading files.
list_sessions
Returns all active sessions with age_seconds, idle_seconds, and workdir.
destroy_session
Explicitly destroys a session — kills the interpreter process and deletes all session files. Call this when the agent is done to release resources.
Session Lifecycle
Sessions are auto-created on first use — just pass any session_id and go. No explicit "create" step required.
First call with session_id Subsequent calls (same ID)
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Auto-Create │ │ Reuse Session│
│ • New kernel │ │ • State kept │
│ • Filesystem │ │ • Timer reset│
└──────────────┘ └──────────────┘
│ │
└──────── Expires or Destroyed ──────┘
│
▼
┌──────────────┐
│ Cleaned Up │
│ • Process killed │
│ • Files deleted │
└──────────────┘TTL & idle timeout — sessions expire after
SESSION_TTLseconds total orIDLE_TIMEOUTseconds of inactivity.Cleanup loop — a background thread reaps expired sessions every
CLEANUP_INTERVALseconds.Session recovery — if a session is not found (expired/cleaned up), create a new one with the same
session_idand re-upload files / re-run setup. Do not assume prior state survived.
Best Practices
First call:
session_infoto discover absolute paths, then import libraries + load data.Next calls: reuse variables already in memory — don't re-import or re-load.
Save outputs to
output/using absolute paths — they become artifacts automatically.Check
new_artifactsinexec_coderesponses for file paths and SAS URLs.Use
exec_sqlfor SQL queries — results land directly in the session, no extra code needed.On error, fix only the broken line — don't re-run everything from scratch.
Call
destroy_sessionwhen done to free interpreter processes and disk space.
Client HTTP Headers
Clients can override server defaults on a per-request basis via HTTP headers. Values are clamped against server maximums.
Header | Overrides |
|
|
|
|
|
|
|
|
|
|
| Azure Blob connection string |
| Azure Blob container name |
| SAS URL expiry |
| Azure SQL connection string |
| Azure SQL password |
Connecting from Agent Frameworks
Code Box works with any MCP-compatible client. Here are quick-start snippets for popular frameworks:
Semantic Kernel (Python)
from semantic_kernel.connectors.mcp import MCPStreamableHttpPlugin
plugin = MCPStreamableHttpPlugin(
name="code_box",
url="http://localhost:8000/mcp",
)
kernel.add_plugin(plugin)LangGraph / LangChain
from langchain_mcp_adapters.client import MultiServerMCPClient
async with MultiServerMCPClient({
"code_box": {
"url": "http://localhost:8000/mcp",
"transport": "streamable_http",
}
}) as client:
tools = client.get_tools()CrewAI
from crewai import Agent
from crewai_tools import MCPServerAdapter
with MCPServerAdapter(
server_params={"url": "http://localhost:8000/mcp", "transport": "streamable_http"}
) as tools:
agent = Agent(role="analyst", tools=tools.tools)AutoGen
from autogen_ext.tools.mcp import SseServerParams, mcp_server_tools
tools = await mcp_server_tools(SseServerParams(url="http://localhost:8000/mcp"))
agent = AssistantAgent("analyst", tools=tools)Project Structure
CodeBoxMCP/
├── server.py # Thin launcher (delegates to codebox.server)
├── run.sh # Shell launcher (Linux/macOS)
├── requirements.txt # Python dependencies
├── .env # Environment configuration (create this)
├── DOCUMENTATION.md # Full integration & usage guide
├── USAGE_GUIDE.html # HTML usage guide
├── architecture.drawio # Architecture diagram (draw.io)
├── codebox/
│ ├── __init__.py # Package metadata & version
│ ├── __main__.py # `python -m codebox` entry point
│ ├── config.py # Centralised configuration (env vars)
│ ├── server.py # FastMCP server, tool definitions, entrypoint
│ ├── session_manager.py # Session lifecycle & interpreter management
│ ├── db_manager.py # Azure SQL query execution & safety
│ ├── helpers.py # Blob storage, artifact detection, downloads
│ └── resources.py # Embedded usage guide resource
└── sessions/ # Runtime session filesystems (auto-created)Configuration Reference
All settings are configurable via environment variables or a .env file.
Variable | Default | Description |
|
| Server display name |
|
| Bind address |
|
| Listening port |
|
| Transport protocol |
|
| Max session lifetime (seconds) |
|
| Max idle time before cleanup (seconds) |
|
| Cleanup loop interval (seconds) |
|
| Max code execution time per call (seconds) |
|
| Max file download time (seconds) |
|
| Max downloadable file size (bytes) |
|
| Row threshold for inline vs. CSV delivery |
See DOCUMENTATION.md for the full reference including Azure Blob and SQL settings.
Security
No LLM at the server — code is executed directly; the server never makes model API calls.
SQL safety — two-level protection: keyword blocking + always-rollback transactions.
Session isolation — each session runs in its own interpreter process with an isolated filesystem.
Config clamping — client-supplied values can never exceed server-configured maximums.
Blob SAS URLs — time-limited, read-only access tokens for artifact downloads.
License
Proprietary — Open Source. See source file headers for the full license notice. Authorized use only.
This server cannot be installed
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Latest Blog Posts
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/MadhanMohanReddy2301/CodeBoxMCP'
If you have feedback or need assistance with the MCP directory API, please join our Discord server