How johanli233/mcp-sandbox works
Project Type and Purpose
• This is a full-stack web application:
• Frontend: A React/TypeScript single-page app (Vite)
• Backend: A Python ASGI service (managed via uv, likely FastAPI under the hood)
• Runtime subsystem: Docker-based “sandboxes” for executing untrusted Python code
• Persistence: A relational database for user and sandbox metadata
• Static file hosting: Serves generated artifacts (plots, CSVs) over HTTP
Main Components
a. Client Layer
• Browser running React/Vite UI (AuthPage, Dashboard, SandboxCard components)
• Communicates via REST (HTTP/HTTPS) and Server-Sent Events (SSE) to backend
b. API Layer
• main.py entrypoint boots ASGI server (e.g., Uvicorn/uv)
• Routers in mcp_sandbox/api:
• auth_routes.py – login/logout
• routes.py – sandbox lifecycle & tool endpoints
• sandbox_file.py – static file mount for results
• Middleware in mcp_sandbox/middleware/auth_middleware.py – enforces JWT/OAuth tokens
c. Core Services
• mcp_sandbox/core:
• docker_manager.py – interacts with Docker daemon
• mcp_tools.py – orchestrates high-level tool calls
• sandbox_modules/ – modular handlers for exec, file_ops, package installs, record keeping, lifecycle management
d. Persistence
• mcp_sandbox/db/database.py – database session factory (Postgres/SQLite)
• ORM models in mcp_sandbox/models (e.g., user.py)
e. Utilities
• mcp_sandbox/utils/config.py – central config constants
• task_manager.py – periodic tasks (cleanup orphaned containers, etc.)
f. Docker Sandbox Layer
• sandbox_images/Dockerfile – base image for Python sandboxes
• Docker daemon running isolated containers per sandbox
g. External Dependencies
• PyPI (for package installation inside containers)
• NPM gateway / MCP Inspector via SSE
Component Interactions & Data Flow
• User authenticates → Frontend calls /login → Auth middleware issues JWT
• Dashboard lists sandboxes → Frontend GET /list_sandboxes → DB query → JSON list
• Install a package → Frontend POST /install_package → Core module schedules docker exec pip install → async status tracked in DB → SSE channel emits status updates
• Execute code → Frontend POST /execute_python_code → Core module shell-execs inside container → captures stdout/stderr → file generation → saves to shared volume
• Static file server (/files/{sandbox_id}/{filename}) serves generated artifacts → Frontend displays links
• SSE endpoint streams logs/events to Frontend in real-time
Architectural Patterns & Principles
• Layered architecture: Presentation (React), Application (FastAPI routers & middleware), Domain (core service modules), Infrastructure (Docker, DB, file storage)
• Modular “sandbox_modules” for injection of new tools/extensions
• Asynchronous event streaming (SSE) for real-time feedback
• Separation of concerns: Middleware handles auth, routers handle HTTP, core modules handle business logic