A secure, configurable MCP server that executes untrusted user code in isolated sandboxes.
- Executes code in Python, Node.js, Go, and C++
- Sandboxing via Docker, Podman, or local execution
- Configurable resource limits (time, memory)
- Network isolation by default
- Base64-encoded tar for initial file system state
- Full stdout/stderr capture with exit codes
- Base64-encoded artifact tar of final working directory
- MCP protocol compliant with stdio and HTTP transports
- MCP Protocol: mark3labs/mcp-go
- Logging: uber-go/zap
- Dependency Injection: uber-go/fx
- Configuration: spf13/viper
The server is configured via config.yaml:
server:
transport: "stdio" # or "http"
http_port: 8080
sandbox:
backend: "docker" # or "podman", "local"
timeout_sec: 10
memory_mb: 512
max_artifact_size_mb: 20
network_enabled: false
enable_local_backend: false
languages:
python:
image: "python:3.11-slim"
prefix_code: "..."
postfix_code: "..."
environment: # Optional environment variables
PYTHONPATH: "/workdir"
PYTHONIOENCODING: "utf-8"
# ... other language configurations with environment variablesEach language supports an optional environment section to set custom environment variables for the execution environment. These variables are passed to the execution runtime and can be used to control language-specific behavior.
go run cmd/server/main.goUpdate config.yaml:
server:
transport: "http"
http_port: 8080Then run:
go run cmd/server/main.goThe server exposes a single tool: execute_sandboxed_code
{
"code": "print('Hello, World!')",
"language": "python",
"workdir_tar": "base64-encoded-tar-optional"
}{
"stdout": "Hello, World!\n",
"stderr": "",
"exit_code": 0,
"artifacts_tar": "base64-encoded-tar-of-workdir"
}- Code runs in isolated containers
- Resource limits (time, memory)
- Network disabled by default
- File system access restricted
- Non-root execution
- Path traversal protection
# Build the binary
go build -o codebox-server cmd/server/main.go
# Build Docker image
docker build -t codebox .To run with local executor (not recommended for production):
sandbox:
backend: "local"
enable_local_backend: trueNote: Local executor is insecure and only for development purposes.
This repository uses pre-commit hooks to ensure code quality and consistency. To set up the pre-commit hooks:
-
Install pre-commit:
pip install pre-commit
-
Install the git hooks:
./install-hooks.sh
Or install manually:
pre-commit install
The hooks will automatically run before each commit to:
- Format Go code with
gofmt - Format imports with
goimports - Run
go vetfor error checking - Run
golangci-lintfor code linting - Run all tests with
go test - Ensure the code builds successfully
To run the hooks manually on all files:
pre-commit run --all-filesBefore submitting pull requests, please ensure:
-
All tests pass:
go test ./... -
The code builds successfully:
go build ./cmd/server
-
Code is properly formatted:
gofmt -s -w . goimports -w -local github.com/isdmx/codebox .
-
Linting passes:
golangci-lint run
Dependencies are managed with Go modules. To add a new dependency:
go get github.com/username/package@version
go mod tidyTo update dependencies:
go get -u
go mod tidyAutomatic dependency updates are handled by Dependabot.