# johanli233/mcp-sandbox architecture

> Architecture diagram of the GitHub repository johanli233/mcp-sandbox, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/johanli233/mcp-sandbox
- Repository: https://github.com/johanli233/mcp-sandbox

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Container"
        direction TB
        Browser["Browser"]:::frontend
        AuthPage["AuthPage"]:::frontend
        Dashboard["Dashboard"]:::frontend
        SandboxCard["SandboxCard"]:::frontend
        APIService["API Service Wrapper"]:::frontend
        Browser -->|uses| AuthPage
        Browser -->|uses| Dashboard
        Browser -->|uses| SandboxCard
        AuthPage -->|calls POST /login| APIService
        Dashboard -->|calls GET /list_sandboxes| APIService
        SandboxCard -->|calls POST /create_sandbox<br/>POST /install_package<br/>POST /execute_python_code| APIService
    end

    %% API Layer
    subgraph "Backend Container"
        direction TB
        Main["main.py"]:::backend
        AuthMiddleware["Auth Middleware"]:::backend
        subgraph "Routers"
            direction TB
            AuthRoutes["auth_routes.py"]:::backend
            SandboxRoutes["routes.py"]:::backend
            FileMount["sandbox_file.py"]:::backend
        end
        subgraph "Core Services"
            direction TB
            MCPTools["mcp_tools.py"]:::core
            subgraph "Sandbox Modules"
                direction TB
                Execution["execution.py"]:::core
                FileOps["file_ops.py"]:::core
                Manager["manager.py"]:::core
                Package["package.py"]:::core
                Records["records.py"]:::core
            end
        end
        subgraph "Utilities"
            direction TB
            Config["config.py"]:::backend
            TaskManager["task_manager.py"]:::backend
        end
        Main -->|routes requests| AuthMiddleware
        AuthMiddleware --> AuthRoutes
        AuthMiddleware --> SandboxRoutes
        AuthMiddleware --> FileMount
        SandboxRoutes --> MCPTools
        AuthRoutes --> MCPTools
        FileMount --> StaticStorage
        MCPTools --> Execution
        MCPTools --> FileOps
        MCPTools --> Manager
        MCPTools --> Package
        MCPTools --> Records
        MCPTools -->|uses config| Config
        MCPTools -->|schedules tasks| TaskManager
    end

    %% Persistence
    subgraph "Persistence Layer"
        direction TB
        DBSession["database.py"]:::db
        UserModel["user.py"]:::db
        DBSession --> UserModel
        SandboxRoutes -->|queries/inserts| DBSession
        AuthRoutes -->|queries| DBSession
    end

    %% Docker Sandbox Layer
    subgraph "Docker Host"
        direction TB
        DockerDaemon["Docker Daemon"]:::external
        BaseImage["Dockerfile"]:::external
        Manager -->|docker API| DockerDaemon
        Execution -->|docker exec<br/>pip install via PyPI| DockerDaemon
        DockerDaemon -->|runs containers from| BaseImage
    end

    %% Static File Storage
    StaticStorage["Static File Storage"]:::storage

    %% External Dependencies
    PyPI["PyPI"]:::external
    Package -->|pulls packages| PyPI

    %% Data Flows
    APIService -->|HTTP/HTTPS| Main
    APIService -->|SSE events| FileMount
    Records -->|logs| DBSession
    FileOps -->|reads/writes| StaticStorage
    Execution -->|outputs files| StaticStorage

    %% Click events
    click AuthPage "https://github.com/johanli233/mcp-sandbox/blob/main/ui/src/pages/AuthPage.tsx"
    click Dashboard "https://github.com/johanli233/mcp-sandbox/blob/main/ui/src/pages/Dashboard.tsx"
    click SandboxCard "https://github.com/johanli233/mcp-sandbox/blob/main/ui/src/components/SandboxCard.tsx"
    click APIService "https://github.com/johanli233/mcp-sandbox/blob/main/ui/src/services/api.ts"
    click Main "https://github.com/johanli233/mcp-sandbox/blob/main/main.py"
    click AuthRoutes "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/api/auth_routes.py"
    click SandboxRoutes "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/api/routes.py"
    click FileMount "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/api/sandbox_file.py"
    click AuthMiddleware "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/middleware/auth_middleware.py"
    click MCPTools "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/core/mcp_tools.py"
    click Execution "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/core/sandbox_modules/execution.py"
    click FileOps "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/core/sandbox_modules/file_ops.py"
    click Manager "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/core/sandbox_modules/manager.py"
    click Package "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/core/sandbox_modules/package.py"
    click Records "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/core/sandbox_modules/records.py"
    click DBSession "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/db/database.py"
    click UserModel "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/models/user.py"
    click Config "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/utils/config.py"
    click TaskManager "https://github.com/johanli233/mcp-sandbox/blob/main/mcp_sandbox/utils/task_manager.py"
    click BaseImage "https://github.com/johanli233/mcp-sandbox/tree/main/sandbox_images/Dockerfile"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
    classDef backend fill:#E8F4D0,stroke:#6DA544,color:#6DA544;
    classDef core fill:#FDEBD0,stroke:#E67E22,color:#E67E22;
    classDef db fill:#D0F0C0,stroke:#27AE60,color:#27AE60;
    classDef external fill:#FFF3C4,stroke:#F1C40F,color:#F1C40F;
    classDef storage fill:#E0E0E0,stroke:#7F8C8D,color:#7F8C8D;
```
