Skip to main content

A secure Python code execution service that provides a self-hosted alternative to OpenAI's Code Interpreter

Project description

CodeBox-AI

A secure Python code execution service that provides a self-hosted alternative to OpenAI's Code Interpreter or Anthropic's Claude analysis tool. Built with FastAPI and IPython kernels, it supports session-based code execution and integrates with LLM function calling.

It also now supports the Model Context Protocol (MCP) for seamless integration with LLM applications.

Features

  • Session-based Python code execution in Docker containers
  • IPython kernel for rich output support
  • Dynamic package installation with security controls
    • Package allowlist/blocklist system
    • Version control for security vulnerabilities
    • Support for pip and conda installations
  • State persistence between executions
  • Support for plotting and visualization
  • Code security validation
    • AST-based code analysis
    • Protection against dangerous imports and operations
    • Support for Jupyter magic commands and shell operations
  • Host directory mounting
    • Mount local directories into the container
    • Read-only or read-write access control
    • Security validations to prevent access to sensitive paths

MCP Server (Model Context Protocol)

CodeBox-AI now supports the Model Context Protocol (MCP), allowing LLM applications (like Claude Desktop) to interact with your code execution service in a standardized way.

Running the MCP Server

You can run the MCP server in several ways:

  • Standalone (for MCP clients or Claude Desktop):

    uv run mcp dev mcp_server.py
    

    This starts the MCP server in development mode for local testing and debugging.

  • Register with Claude Desktop:

    uv run mcp install mcp_server.py --name "CodeBox-AI"
    

    This will make your server available to Claude Desktop as a custom tool.

  • Combined FastAPI + MCP server:

    uv run run.py
    

    This starts both the FastAPI API and the MCP server (MCP available at /mcp).

  • MCP server only:

    uv run run.py --mode mcp
    

MCP Features

  • execute_code: Execute Python code and return results
  • session://{session_id}: Get info about a session
  • sessions://: List all active sessions

Example: Testing with MCP Inspector

  1. Start the MCP server:
    uv run mcp dev mcp_server.py
    
  2. Open the MCP Inspector and connect to your local server.

Example: Registering with Claude Desktop

  1. Configure the MCP server in the Claude Desktop settings:

Edit the file ~/Library/Application Support/Claude/claude_desktop_config.json. The following is an example configuration:

{
  "mcpServers": {
    "CodeBox-AI": {
      "command": "uv",
      "args": [
        "run",
        "--project",
        "/Users/username/src/codebox-ai",
        "/Users/username/src/codebox-ai/mcp_server.py",
        "--mount",
        "/Users/username/Downloads"
      ]
    }
  }
}

Unfortunately, all paths need to be absolute. This example shows how to mount the Downloads directory into the container.

  1. Open Claude Desktop and the server should appear as a custom tool.

Prerequisites

  • Python 3.9+
  • Docker
  • uv - Fast Python package installer and resolver

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/codebox-ai.git
cd codebox-ai
  1. Install dependencies with uv:
# Install uv if you don't have it yet
curl -LsSf https://astral.sh/uv/install.sh | sh

# Create a virtual environment and install dependencies in one step
uv sync
  1. Start the server:
uv run -m codeboxai.main

The API will be available at http://localhost:8000

Development setup

For development, install with the development extras:

uv sync --extra "dev docs"

Docker "file not found" error

If you encounter a "file not found" DockerException when running the server on MacOS, you might need to set the DOCKER_HOST environment variable. First, find out which context you are using by running:

docker context ls

Then set the DOCKER_HOST environment variable to the correct endpoint:

export DOCKER_HOST="unix:///Users/tconte/.docker/run/docker.sock"

Usage

Example: OpenAI GPT Integration

  1. Create a .env file in the project root:
AZURE_OPENAI_ENDPOINT=https://xxx.cognitiveservices.azure.com/
AZURE_OPENAI_API_KEY=foo
AZURE_OPENAI_DEPLOYMENT=gpt-4o
OPENAI_API_VERSION=2024-05-01-preview
  1. Install additional requirements:
uv sync --extra "examples"
  1. Run the example:
uv run examples/example_openai.py

This will start an interactive session where you can chat with GPT-4 and have it execute Python code. The script maintains state between executions, so variables and imports persist across interactions.

If you want to expose a local directory to the container, you can do so by using the CODEBOX_MOUNT_PATH environment variable. For example, to mount your Downloads directory:

export CODEBOX_MOUNT_PATH=/Users/username/Downloads
uv run examples/example_openai.py

Demo screencast

Direct API Usage

  1. Create a new session:
curl -X POST http://localhost:8000/sessions \
  -H "Content-Type: application/json" \
  -d '{
    "dependencies": ["numpy", "pandas"]
  }'
  1. Execute code in the session:
curl -X POST http://localhost:8000/execute \
  -H "Content-Type: application/json" \
  -d '{
    "code": "x = 42\nprint(f\"Value of x: {x}\")",
    "session_id": "YOUR_SESSION_ID"
  }'
  1. Check execution status:
curl -X GET http://localhost:8000/execute/YOUR_REQUEST_ID/status
  1. Get execution results:
curl -X GET http://localhost:8000/execute/YOUR_REQUEST_ID/results
  1. Execute more code in the same session:
curl -X POST http://localhost:8000/execute \
  -H "Content-Type: application/json" \
  -d '{
    "code": "print(f\"x is still: {x}\")",
    "session_id": "YOUR_SESSION_ID"
  }'
  1. Create a session with mounted directories:
curl -X POST http://localhost:8000/sessions \
  -H "Content-Type: application/json" \
  -d '{
    "execution_options": {
      "mount_points": [
        {
          "host_path": "/Users/tconte/Downloads",
          "container_path": "/data/downloads",
          "read_only": true
        }
      ],
      "timeout": 300
    }
  }'
  1. Execute code that accesses mounted files:
curl -X POST http://localhost:8000/execute \
  -H "Content-Type: application/json" \
  -d '{
    "code": "import os\nprint(\"Files in mounted directory:\")\nfor file in os.listdir(\"/data/downloads\"):\n    print(f\" - {file}\")",
    "session_id": "YOUR_SESSION_ID"
  }'

API Endpoints

  • POST /sessions - Create a new session
  • POST /execute - Execute code in a session
  • GET /execute/{request_id}/status - Get execution status
  • GET /execute/{request_id}/results - Get execution results
  • DELETE /sessions/{session_id} - Cleanup a session

Security Notes

  • Code execution is containerized using Docker
  • Each session runs in an isolated environment
  • Basic resource limits are implemented
  • Network access is available but can be restricted
  • Input code validation is implemented for basic security

License

MIT License - See LICENSE file for details.

A Note on Authorship

This code was pair-programmed with Claude 3.5 Sonnet (yes, an AI helping to build tools for other AIs - very meta). While I handled the product decisions and architecture reviews, Claude did most of the heavy lifting in terms of code generation and documentation. Even this README was written by Claude, which makes this acknowledgment a bit like an AI writing about an AI writing about AI tools... we need to go deeper 🤖✨

Humans were (mostly) present during the development process. No AIs were harmed in the making of this project, though a few might have gotten slightly dizzy from the recursion.


A prototype implementation, not intended for production use without additional security measures.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iflow_mcp_tomconte_codebox_ai-0.1.0.tar.gz (26.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iflow_mcp_tomconte_codebox_ai-0.1.0-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file iflow_mcp_tomconte_codebox_ai-0.1.0.tar.gz.

File metadata

  • Download URL: iflow_mcp_tomconte_codebox_ai-0.1.0.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_tomconte_codebox_ai-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7d8fb16f8d948b80e1c1c2c7163d6f534056778c8d21a780c52745020c927ef3
MD5 e7211995440e6456bada751b3c931795
BLAKE2b-256 d27c87956ec2fc661800c9ba65208aadbe31a6d0588036c656d384ff7f8d8792

See more details on using hashes here.

File details

Details for the file iflow_mcp_tomconte_codebox_ai-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: iflow_mcp_tomconte_codebox_ai-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_tomconte_codebox_ai-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef70f03ecf03651a342079a7ee4419470748b4ae2ed164ce4f17c984d1eab02f
MD5 6cce1974067885f4b54beb21bc822e37
BLAKE2b-256 49fae42718021be5dcc3d2d09f1bc83da99ef86ecbc48b452606f1bd6d3fe9ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page