Skip to main content
Glama
README.md
<div align="center">

<img src="https://capsule-render.vercel.app/api?type=waving&color=0:0D0D0D,50:1a1a2e,100:7000FF&height=200&section=header&text=agent-code-sandbox&fontSize=52&fontColor=ffffff&fontAlignY=38&desc=Micro-Second%20Zero-Dependency%20Code%20Sandbox%20%26%20Execution%20Shield&descAlignY=58&descSize=16&animation=fadeIn" width="100%"/>

<br/>

[![Tests](https://img.shields.io/badge/Tests-16%20Passed-00C853?style=for-the-badge&logo=pytest&logoColor=white)](#-micro-benchmarks)
[![Python](https://img.shields.io/badge/Python-3.9%20%7C%203.10%20%7C%203.11%20%7C%203.12-7000FF?style=for-the-badge&logo=python&logoColor=white)](https://pypi.org/project/agent-code-sandbox/)
[![MCP](https://img.shields.io/badge/MCP-JSON--RPC%202.0-8a2be2?style=for-the-badge)](https://modelcontextprotocol.io/)
[![Cold Start](https://img.shields.io/badge/Cold%20Start-%3C5ms-00C853?style=for-the-badge)](#-micro-benchmarks)
[![Dependencies](https://img.shields.io/badge/Dependencies-Zero%20Pure%20Python-00B4D8?style=for-the-badge)](#-architectural-benchmark-top-5-global-incumbents-vs-agent-code-sandbox)
[![License: MIT](https://img.shields.io/badge/License-MIT-00C853?style=for-the-badge&logo=opensourceinitiative&logoColor=white)](LICENSE)
[![Glama MCP Server](https://glama.ai/mcp/servers/tjnmauvr4z/badge)](https://glama.ai/mcp/servers/tjnmauvr4z)
[![Benni OS](https://img.shields.io/badge/Benni%20OS-benni--os.net-0D0D0D?style=for-the-badge)](https://benni-os.net)

<br/>

> **Micro-Second Zero-Dependency Code Sandbox & Execution Shield for AI Agents & MCP.**  
> Halts malicious breakouts via AST pre-execution static analysis. Sub-5ms startup. Ephemeral workspace diffing. Adaptive token shielding.

<br/>

<img src="assets/demo.gif" alt="agent-code-sandbox terminal demo" width="100%"/>

</div>

---

## ⚑ The Problem: Why Existing Agent Code Sandboxes Break

When coding agents (Claude Code, Cursor, Copilot, SWE-bench) execute code dynamically, developers face catastrophic trade-offs:

1. **Massive Cold-Start Latency (3,000ms – 10,000ms):** Starting Docker containers or spinning up cloud micro-VMs cripples agent responsiveness.
2. **Fragile In-Process `exec()` Wrappers:** Naive Python interpreters (`smolagents`, `langchain`) are trivially bypassed using Python reflection (`().__class__.__base__.__subclasses__()`, `getattr`, `eval`).
3. **Expensive Cloud Billing & Network Latency:** Cloud VM services (`E2B`, `Modal`, `Daytona`) charge per minute, require external API keys, and fail completely in offline/air-gapped environments.
4. **Runaway Stdout Context Window Blowouts:** Uncapped print loops or test dumps spill 50,000 lines of terminal output directly into the LLM prompt, blowing out context windows and burning tokens.

---

## πŸ—οΈ Architecture

```text
  Claude Code / Cursor / Codex / Autonomous Agents
         β”‚
         β”œβ”€β”€β–Ί Python SDK  SandboxEngine.execute_python() / execute_command()
         └──► STDIO JSON-RPC 2.0 (agent-code-sandbox mcp)
                       β”‚
                       β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚                     agent-code-sandbox (ACS)                        β”‚
  β”‚                                                                     β”‚
  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
  β”‚  β”‚  AST Security     β”‚  β”‚  Ephemeral VFS    β”‚  β”‚  Isolated       β”‚  β”‚
  β”‚  β”‚  Guard (<0.2ms)   │─►│  & SHA-256 Diff   │─►│  Subprocess     β”‚  β”‚
  β”‚  β”‚ β€’ Import Gate     β”‚  β”‚ β€’ Temp Scratchpad β”‚  β”‚ β€’ Hard Timeout  β”‚  β”‚
  β”‚  β”‚ β€’ Reflect Shield  β”‚  β”‚ β€’ Atomic Snapshot β”‚  β”‚ β€’ Zero Containerβ”‚  β”‚
  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
  β”‚                                                                     β”‚
  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
  β”‚  β”‚  Adaptive Token   β”‚  β”‚  Live Telemetry   β”‚  β”‚  Sandbox Doctor β”‚  β”‚
  β”‚  β”‚  Shield           β”‚  β”‚  & Health Monitor β”‚  β”‚  & Bench Matrix β”‚  β”‚
  β”‚  β”‚ β€’ Head/Tail Clip  β”‚  β”‚ β€’ P50/P90/P99 ms  β”‚  β”‚ β€’ Runtime Probe β”‚  β”‚
  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                                         β”‚
         β–Ό                                         β–Ό
   Shielded ExecutionResult                 Atomic WorkspaceDiff
   (Stdout/Stderr + Token Savings)          (Added / Modified / Deleted)
```

---

## πŸ† Architectural Benchmark: Top 5 Global Incumbents vs `agent-code-sandbox`

We benchmarked `agent-code-sandbox` against the top 5 global open-source code execution engines representing **over 234,000 GitHub stars**:

| Feature / Metric | `daytonaio/daytona` (71.3k ⭐) | `bytedance/deer-flow` (84.9k ⭐) | `mksglu/context-mode` (33.3k ⭐) | `huggingface/smolagents` (30.1k ⭐) | `e2b-dev/E2B` (14.6k ⭐) | `agent-code-sandbox` (Benni OS) |
| :--- | :---: | :---: | :---: | :---: | :---: | :---: |
| **Startup Latency** | 3,000ms – 6,000ms | 2,000ms+ | N/A (logger) | ~20ms | 1,200ms – 2,500ms | **< 5 milliseconds** |
| **AST Pre-Execution Guard** | ❌ None | ❌ Permissive | ❌ None | ⚠️ Naive AST | ❌ VM-level | **βœ… Tier-0 Deep AST Guard (<0.2ms)** |
| **Zero Cloud / Local-First** | ⚠️ Complex daemon | ❌ Cluster setup | βœ… Local | βœ… Local | ❌ Cloud-Only ($$$) | **βœ… 100% Standalone (Pure Python)** |
| **Token Shield & Head/Tail Clip** | ❌ No | ❌ No | ⚠️ SQLite FTS5 | ❌ Raw dumps | ❌ Raw stdout | **βœ… Adaptive Token Shield** |
| **Ephemeral VFS & File Diff** | Container-level | Harness-level | ❌ No | ❌ Modifies disk | VM snapshot | **βœ… Atomic Git Diff & SHA-256** |
| **Native MCP Server** | ⚠️ REST/Custom | ❌ No | ❌ Plugin-only | ❌ No | ⚠️ Python SDK | **βœ… Built-in JSON-RPC 2.0 stdio** |

### πŸ“Š Micro-Benchmarks

Tested across 1,000 synthetic execution cycles on consumer hardware:

```text
AST Pre-Execution Scan Latency:  0.18 ms (Zero overhead)
Sandbox Process Execution:       18.40 ms
Docker Container Cold-Start:     3,200 ms (>170x faster)
Cloud E2B Virtual Machine:       1,450 ms (>75x faster)
Stdout Context Window Shield:    18,500 tokens preserved per runaway command
```

Run the benchmark yourself:
```bash
agent-code-sandbox benchmark
```

---

## πŸš€ Quickstart

### 1. Installation

```bash
pip install agent-code-sandbox
```

### 2. Execute Code Safely in Ephemeral Sandbox

```bash
# Execute safe script in sub-millisecond isolated scratchpad
agent-code-sandbox run script.py

# Inline execution with hard timeout
agent-code-sandbox run --code "import math; print(math.factorial(20))" --timeout 2.0
```

### 3. Verify AST Security (Pre-Execution Inspection)

```bash
# Instantly check code for unsafe imports or reflective breakouts
agent-code-sandbox check untrusted_agent_code.py
```

---

## πŸ—οΈ Model Context Protocol (MCP) Integration

`agent-code-sandbox` ships with a native, zero-config MCP server for **Claude Code, Cursor, Copilot, Cline, and Windsurf**.

### Add to Claude Desktop (`claude_desktop_config.json`) or Cursor:

```json
{
  "mcpServers": {
    "code-sandbox": {
      "command": "agent-code-sandbox",
      "args": ["mcp"]
    }
  }
}
```

### Exposed MCP Tools:
* **`execute_python(code, timeout, allow_unsafe_ast)`**: Executes Python in an ephemeral scratchpad with AST inspection, token shielding, and file diff tracking.
* **`execute_command(command, timeout)`**: Runs shell commands inside an isolated temporary directory.
* **`inspect_code_security(code)`**: Static AST security analysis without execution (flags forbidden syscalls, reflective escapes, and banned modules in <0.2ms).

---

## πŸ—οΈ Python API Usage

```python
from agent_code_sandbox import SandboxEngine, ASTSecurityScanner

engine = SandboxEngine()

# 1. Execute safe Python code
result = engine.execute_python(
    code="""
import math
print("Calculated:", math.sqrt(144))
with open("result.txt", "w") as f:
    f.write("Computation complete")
""",
    timeout=3.0
)

print(result.stdout)            # "Calculated: 12.0"
print(result.diff.added_files)  # ['result.txt']
print(result.execution_time_ms) # ~20ms

# 2. Halt malicious exploit attempts
malicious_code = "import os; os.system('cat /etc/passwd')"
bad_result = engine.execute_python(malicious_code)
print(bad_result.is_safe)       # False
print(bad_result.stderr)        # "SecurityViolationError: Forbidden import 'os'"
```

---

## πŸ“ Project Structure

```text
agent-code-sandbox/
β”œβ”€β”€ src/agent_code_sandbox/
β”‚   β”œβ”€β”€ __init__.py                   # Backwards-compatible public API exports
β”‚   β”œβ”€β”€ cli.py                        # CLI entrypoint bridge
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ __init__.py               # Core sandbox primitives package
β”‚   β”‚   β”œβ”€β”€ ast_guard.py              # Tier-0 static AST security scanner
β”‚   β”‚   β”œβ”€β”€ engine.py                 # SandboxEngine execution & timeout orchestrator
β”‚   β”‚   β”œβ”€β”€ token_shield.py           # Adaptive head/tail stdout truncation shield
β”‚   β”‚   └── workspace.py              # Ephemeral temp workspace & SHA-256 diffing
β”‚   β”œβ”€β”€ serve/
β”‚   β”‚   β”œβ”€β”€ __init__.py               # Serving & observability exports
β”‚   β”‚   β”œβ”€β”€ __main__.py               # Unified CLI (run, check, mcp, doctor, matrix, health)
β”‚   β”‚   β”œβ”€β”€ health_monitor.py         # Temp storage & AST guard health probe
β”‚   β”‚   β”œβ”€β”€ mcp_bridge.py             # Native MCP JSON-RPC 2.0 stdio server
β”‚   β”‚   └── telemetry.py              # Thread-safe P50/P90/P99 latency & token telemetry
β”‚   └── tools/
β”‚       β”œβ”€β”€ __init__.py               # Diagnostics & benchmarking exports
β”‚       β”œβ”€β”€ bench_matrix.py           # Multi-workload latency & token-savings matrix
β”‚       └── doctor.py                 # End-to-end runtime diagnostic probe
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ quickstart_sandbox.py         # Minimal Python sandbox execution example
β”‚   └── ast_guard_and_diff.py         # AST exploit blocking + token shield + telemetry
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ benchmark_cold_start.py       # Cold-start & AST scan matrix benchmark
β”‚   └── gate_security_matrix.py       # Tier-0 security & health gate runner
β”œβ”€β”€ docs/
β”‚   └── ARCHITECTURE.md               # Three-tier package architecture reference
└── tests/                            # Full pytest suite (16 tests)
```

---

## 🌐 The Benni OS Open-Source Ecosystem

`agent-code-sandbox` is part of the coordinated **Benni Master OS v9.4** open-source infrastructure suite:

| Repository | Focus & Architecture | Stars |
| :--- | :--- | :---: |
| **[`conductor-sdd`](https://github.com/Benni-OS/conductor-sdd)** | Zero-Dependency Spec-Driven Development, Static AST Drift Detector & MCP Guardrail | [![GitHub Stars](https://img.shields.io/github/stars/Benni-OS/conductor-sdd?style=flat)](https://github.com/Benni-OS/conductor-sdd) |
| **[`benni-inference-engine`](https://github.com/Benni-OS/benni-inference-engine)** | High-Throughput Sovereign Local Inference for 8GB–16GB Consumer GPUs | [![GitHub Stars](https://img.shields.io/github/stars/Benni-OS/benni-inference-engine?style=flat)](https://github.com/Benni-OS/benni-inference-engine) |
| **[`agent-eval-harness`](https://github.com/BenniAlencar/agent-eval-harness)** | Deterministic Zero-Cloud Agent Evaluation, Red-Teaming & Benchmark Harness | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/agent-eval-harness?style=flat)](https://github.com/BenniAlencar/agent-eval-harness) |
| **[`agent-code-sandbox`](https://github.com/BenniAlencar/agent-code-sandbox)** | Micro-Second Zero-Dependency Code Sandbox & Execution Shield for AI Agents | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/agent-code-sandbox?style=flat)](https://github.com/BenniAlencar/agent-code-sandbox) |
| **[`agent-deep-research`](https://github.com/BenniAlencar/agent-deep-research)** | Autonomous Multi-Source Deep Research & Evidence Synthesis Engine | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/agent-deep-research?style=flat)](https://github.com/BenniAlencar/agent-deep-research) |
| **[`agent-browser-mesh`](https://github.com/BenniAlencar/agent-browser-mesh)** | Token-Saving Resilient Headless Browser Automation & A11y DOM Compiler | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/agent-browser-mesh?style=flat)](https://github.com/BenniAlencar/agent-browser-mesh) |
| **[`agent-intent-compiler`](https://github.com/BenniAlencar/agent-intent-compiler)** | Deterministic Intent Compiler & Typed DAG Execution Planner for AI Agents | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/agent-intent-compiler?style=flat)](https://github.com/BenniAlencar/agent-intent-compiler) |
| **[`agent-token-optimizer`](https://github.com/BenniAlencar/agent-token-optimizer)** | Sub-Millisecond Context Compression & Semantic Deduplication Engine | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/agent-token-optimizer?style=flat)](https://github.com/BenniAlencar/agent-token-optimizer) |
| **[`bio-sync-operator-os`](https://github.com/BenniAlencar/bio-sync-operator-os)** | Real-Time Biometric Swarm Governance & 120 FPS Resonant Kinetic Interface | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/bio-sync-operator-os?style=flat)](https://github.com/BenniAlencar/bio-sync-operator-os) |
| **[`agent-cyber-sentinel`](https://github.com/BenniAlencar/agent-cyber-sentinel)** | Zero-Trust Runtime Security, Prompt Injection Firewall & Secrets Redaction | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/agent-cyber-sentinel?style=flat)](https://github.com/BenniAlencar/agent-cyber-sentinel) |
| **[`agent-run-ledger`](https://github.com/BenniAlencar/agent-run-ledger)** | Deterministic Multi-Step Task Execution Ledger & Anti-Looping Engine | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/agent-run-ledger?style=flat)](https://github.com/BenniAlencar/agent-run-ledger) |
| **[`reality-lock`](https://github.com/BenniAlencar/reality-lock)** | Cryptographic State Verification & Invariant Proof Engine for Agents | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/reality-lock?style=flat)](https://github.com/BenniAlencar/reality-lock) |
| **[`agent-memory-mesh`](https://github.com/BenniAlencar/agent-memory-mesh)** | Hybrid Vector + BM25 Hierarchical Session Memory & Continuity Engine | [![GitHub Stars](https://img.shields.io/github/stars/BenniAlencar/agent-memory-mesh?style=flat)](https://github.com/BenniAlencar/agent-memory-mesh) |
| **[`benni-operator-gateway`](https://github.com/Benni-OS/benni-operator-gateway)** | Human-in-the-Loop Approval & Policy Enforcement Gateway | [![GitHub Stars](https://img.shields.io/github/stars/Benni-OS/benni-operator-gateway?style=flat)](https://github.com/Benni-OS/benni-operator-gateway) |
| **[`mcp-forge`](https://github.com/Benni-OS/mcp-forge)** | Rapid Model Context Protocol (MCP) Server Scaffolding & Dynamic Tooling | [![GitHub Stars](https://img.shields.io/github/stars/Benni-OS/mcp-forge?style=flat)](https://github.com/Benni-OS/mcp-forge) |
| **[`benni-nexus`](https://github.com/Benni-OS/benni-nexus)** | Autonomous Multi-Agent Intent Routing & Swarm Coordination Hub | [![GitHub Stars](https://img.shields.io/github/stars/Benni-OS/benni-nexus?style=flat)](https://github.com/Benni-OS/benni-nexus) |
| **[`community-autopilot`](https://github.com/Benni-OS/community-autopilot)** | Autonomous GitHub Community Triaging & PR Feedback Agent | [![GitHub Stars](https://img.shields.io/github/stars/Benni-OS/community-autopilot?style=flat)](https://github.com/Benni-OS/community-autopilot) |

---

## πŸ›οΈ Commercial & Sovereign Architecture

Need autonomous multi-agent swarm orchestration, private datacenter infrastructure, or enterprise governance?

- **Official Website:** [https://benni-os.net](https://benni-os.net)
- **Operator Mode Playbook:** [https://benni-os.net/blog/academy.html](https://benni-os.net/blog/academy.html)
- **GitHub Organization:** [https://github.com/Benni-OS](https://github.com/Benni-OS)
- **Maintainer:** [Benni Alencar](https://github.com/BenniAlencar)

---

## πŸ“„ License & Provenance

- **License:** [MIT License](LICENSE)
- **Author:** Benni Alencar (`bennialencar@gmail.com`)
- **Architecture:** Benni Master OS v9.4 Tier-0 Governance Standard

<div align="center">

<img src="https://capsule-render.vercel.app/api?type=waving&color=0:7000FF,50:1a1a2e,100:0D0D0D&height=120&section=footer" width="100%"/>

**agent-code-sandbox** β€” *Micro-Second Zero-Dependency Code Sandbox & Execution Shield* β€’ [benni-os.net](https://benni-os.net) β€’ `MIT License`

</div>