# wakeya/magic-claude-code architecture

> Architecture diagram of the GitHub repository wakeya/magic-claude-code, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wakeya/magic-claude-code
- Repository: https://github.com/wakeya/magic-claude-code

## Overview

#### Purpose and Entrypoints
Magic Claude Code (MCC) is a local Go proxy that preserves Claude Code’s first-party request behavior while routing inference to configured third-party model providers. It exposes a TLS proxy on 443, an admin/UI service on 8442, and a lower-privilege gateway fallback. Process startup, flag parsing, dependency assembly, and lifecycle ownership begin in `cmd/server/main.go`, with listen-address overrides in `cmd/server/listen_override.go`.

#### Startup, Trust, and Connection Mode Bootstrap
Startup creates/loads a local CA and server certificate, then attempts transparent operation: hosts redirection, OS trust installation, and client environment persistence. It detects privilege/platform limitations and selects transparent, HTTPS CONNECT tunnel, or gateway mode without blocking proxy startup. This is a privileged OS-integration boundary, especially in binary versus container deployments.
- `internal/bootstrap/bootstrap.go`
- `internal/bootstrap/adapters.go`
- `internal/cert/manager.go`

#### Persistent Configuration and Provider Routing State
Provider definitions, ordering, model mappings, preferences, and connection-mode state are centrally modeled behind stores and persisted in SQLite. This permits admin updates without process restart and supplies the live routing configuration to the proxy/failover layers.
- `internal/config/config.go`
- `internal/config/provider.go`
- `internal/config/sqlite_store.go`

#### Request Proxy and Compatibility Layer
The proxy terminates/intercepts client traffic, applies endpoint policies, detects blocked or malformed responses, and forwards eligible requests to configured provider endpoints. It handles streaming frames/SSE behavior, local responses for selected hardcoded Claude endpoints, request diagnostics, and model/API-shape compatibility. OpenAI chat/responses transformations are isolated from the main HTTP serving layer.
- `internal/proxy/server.go`
- `internal/proxy/handler.go`
- `internal/proxy/transform/openai_chat.go`

#### Provider Selection, Quotas, Rate Limits, and Failover
Request execution uses configured provider priority and compatibility, with provider quota resolution and balance checks informing eligibility. Failures are classified and recorded; the failover manager advances to alternatives. Per-provider rate-limit queues and 429 retry behavior form a separate concurrency-control boundary from request transformation.
- `internal/failover/manager.go`
- `internal/providerquota/manager.go`
- `internal/proxy/ratelimit/queue.go`

#### Admin API and Embedded Dashboard
A password/session-authenticated admin HTTP service manages providers, ordering, quotas, preferences, failover events, configuration import/export, updates, usage, and session browsing. It serves the compiled Vue application embedded in the Go binary; frontend API calls are concentrated in one composable rather than directly coupled to backend stores.
- `internal/admin/server.go`
- `internal/admin/auth.go`
- `internal/frontend/embed.go`
- `internal/frontend/src/composables/useApi.ts`

#### Usage, Session Data, and Updates
Usage records are stored separately and can be augmented by periodic scans of local Claude session JSONL files, with parsing/redaction and SSE support. Session parsing/export supports the dashboard browser. The updater checks release sources and manages application update behavior; external dependencies include third-party AI provider APIs, local Claude project files, OS certificate/hosts facilities, and release APIs.
- `internal/usage/session_sync.go`
- `internal/session/scanner.go`
- `internal/updater/updater.go`

#### Runtime and Deployment Boundaries
The backend is Go; the embedded admin frontend is Vue/TypeScript built with Vite. Deployment supports native binaries on Windows/macOS/Linux and Docker, with persistent data including certificates and SQLite databases. Containers can bind privileged port 443 but cannot inherently modify host trust, hosts files, or user environments; `docker-compose.yml` and `Dockerfile` define that operational boundary.
- `Dockerfile`
- `docker-compose.yml`
- `internal/frontend/package.json`

Main entry points: [`internal/admin/server.go`](https://github.com/wakeya/magic-claude-code/blob/main/internal/admin/server.go) (Admin service), [`cmd/server/main.go`](https://github.com/wakeya/magic-claude-code/blob/main/cmd/server/main.go) (Server entrypoint), [`internal/failover/manager.go`](https://github.com/wakeya/magic-claude-code/blob/main/internal/failover/manager.go) (Failover manager), [`internal/config/config.go`](https://github.com/wakeya/magic-claude-code/blob/main/internal/config/config.go) (Configuration model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_startup["Startup &amp; Host Integration"]
  node_entrypoint["Server entrypoint<br/>Go startup<br/>[main.go]"]
  node_listen_override["Listen overrides<br/>startup options<br/>[listen_override.go]"]
  node_bootstrap["Connection bootstrap<br/>OS integration<br/>[bootstrap.go]"]
  node_bootstrap_adapters["Platform adapters<br/>OS adapters<br/>[adapters.go]"]
  node_cert_manager["Local CA &amp; certificates<br/>TLS trust<br/>[manager.go]"]
  node_host_os{{"Host trust facilities<br/>external OS"}}
end

subgraph group_data["Configuration &amp; Data"]
  node_config_model["Configuration model<br/>routing state<br/>[config.go]"]
  node_provider_model["Provider definitions<br/>routing model<br/>[provider.go]"]
  node_sqlite_store[("SQLite configuration store<br/>persistence<br/>[sqlite_store.go]")]
end

subgraph group_proxy["Request Execution"]
  node_proxy_server["TLS proxy server<br/>HTTP proxy<br/>[server.go]"]
  node_proxy_handler["Compatibility handler<br/>request policy<br/>[handler.go]"]
  node_openai_transform["OpenAI transforms<br/>API compatibility<br/>[openai_chat.go]"]
  node_failover["Failover manager<br/>provider selection<br/>[manager.go]"]
  node_quota_manager["Quota manager<br/>provider eligibility<br/>[manager.go]"]
  node_rate_limit_queue["Rate-limit queues<br/>concurrency control<br/>[queue.go]"]
  node_provider_apis{{"AI provider APIs<br/>external inference"}}
end

subgraph group_admin["Administration"]
  node_admin_server["Admin service<br/>authenticated HTTP API<br/>[server.go]"]
  node_admin_auth["Admin auth<br/>session authentication<br/>[auth.go]"]
  node_frontend_embed["Embedded dashboard<br/>Vue UI bundle<br/>[embed.go]"]
  node_frontend_api["Dashboard API client<br/>Vue composable<br/>[useApi.ts]"]
end

subgraph group_operations["Operations &amp; Deployment"]
  node_usage_sync["Usage session sync<br/>usage ingestion<br/>[session_sync.go]"]
  node_session_scanner["Session scanner<br/>session data<br/>[scanner.go]"]
  node_claude_files["Local Claude files<br/>external local data"]
  node_updater["Application updater<br/>release management<br/>[updater.go]"]
  node_release_apis{{"Release APIs<br/>external service"}}
  node_container_deploy["Container deployment<br/>Docker runtime<br/>[docker-compose.yml]"]
end

node_entrypoint -->|"reads"| node_listen_override
node_entrypoint -->|"initializes"| node_bootstrap
node_entrypoint -->|"loads"| node_config_model
node_entrypoint -->|"starts"| node_proxy_server
node_entrypoint -->|"starts"| node_admin_server
node_bootstrap -->|"uses"| node_bootstrap_adapters
node_bootstrap -->|"provisions"| node_cert_manager
node_bootstrap_adapters -->|"integrates with"| node_host_os
node_cert_manager -->|"supplies TLS"| node_proxy_server
node_config_model -->|"contains"| node_provider_model
node_config_model -->|"persists via"| node_sqlite_store
node_sqlite_store -->|"live state for"| node_admin_server
node_proxy_server -->|"dispatches"| node_proxy_handler
node_proxy_handler -->|"transforms"| node_openai_transform
node_proxy_handler -->|"executes through"| node_failover
node_failover -->|"uses provider priority"| node_config_model
node_failover -->|"checks eligibility"| node_quota_manager
node_failover -->|"submits requests"| node_rate_limit_queue
node_rate_limit_queue -->|"forwards to"| node_provider_apis
node_admin_server -->|"authenticates with"| node_admin_auth
node_admin_server -->|"serves"| node_frontend_embed
node_frontend_embed -->|"contains"| node_frontend_api
node_frontend_api -->|"calls"| node_admin_server
node_usage_sync -->|"scans"| node_claude_files
node_session_scanner -->|"parses"| node_claude_files
node_admin_server -->|"exposes usage"| node_usage_sync
node_admin_server -->|"browses sessions"| node_session_scanner
node_admin_server -->|"manages"| node_updater
node_updater -->|"checks"| node_release_apis
node_container_deploy -->|"runs"| node_entrypoint

click node_entrypoint "https://github.com/wakeya/magic-claude-code/blob/main/cmd/server/main.go"
click node_listen_override "https://github.com/wakeya/magic-claude-code/blob/main/cmd/server/listen_override.go"
click node_bootstrap "https://github.com/wakeya/magic-claude-code/blob/main/internal/bootstrap/bootstrap.go"
click node_bootstrap_adapters "https://github.com/wakeya/magic-claude-code/blob/main/internal/bootstrap/adapters.go"
click node_cert_manager "https://github.com/wakeya/magic-claude-code/blob/main/internal/cert/manager.go"
click node_config_model "https://github.com/wakeya/magic-claude-code/blob/main/internal/config/config.go"
click node_provider_model "https://github.com/wakeya/magic-claude-code/blob/main/internal/config/provider.go"
click node_sqlite_store "https://github.com/wakeya/magic-claude-code/blob/main/internal/config/sqlite_store.go"
click node_proxy_server "https://github.com/wakeya/magic-claude-code/blob/main/internal/proxy/server.go"
click node_proxy_handler "https://github.com/wakeya/magic-claude-code/blob/main/internal/proxy/handler.go"
click node_openai_transform "https://github.com/wakeya/magic-claude-code/blob/main/internal/proxy/transform/openai_chat.go"
click node_failover "https://github.com/wakeya/magic-claude-code/blob/main/internal/failover/manager.go"
click node_quota_manager "https://github.com/wakeya/magic-claude-code/blob/main/internal/providerquota/manager.go"
click node_rate_limit_queue "https://github.com/wakeya/magic-claude-code/blob/main/internal/proxy/ratelimit/queue.go"
click node_admin_server "https://github.com/wakeya/magic-claude-code/blob/main/internal/admin/server.go"
click node_admin_auth "https://github.com/wakeya/magic-claude-code/blob/main/internal/admin/auth.go"
click node_frontend_embed "https://github.com/wakeya/magic-claude-code/blob/main/internal/frontend/embed.go"
click node_frontend_api "https://github.com/wakeya/magic-claude-code/blob/main/internal/frontend/src/composables/useApi.ts"
click node_usage_sync "https://github.com/wakeya/magic-claude-code/blob/main/internal/usage/session_sync.go"
click node_session_scanner "https://github.com/wakeya/magic-claude-code/blob/main/internal/session/scanner.go"
click node_updater "https://github.com/wakeya/magic-claude-code/blob/main/internal/updater/updater.go"
click node_container_deploy "https://github.com/wakeya/magic-claude-code/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entrypoint,node_listen_override,node_bootstrap,node_bootstrap_adapters,node_cert_manager,node_host_os toneBlue
class node_config_model,node_provider_model,node_sqlite_store toneAmber
class node_proxy_server,node_proxy_handler,node_openai_transform,node_failover,node_quota_manager,node_rate_limit_queue,node_provider_apis toneMint
class node_admin_server,node_admin_auth,node_frontend_embed,node_frontend_api toneRose
class node_usage_sync,node_session_scanner,node_claude_files,node_updater,node_release_apis,node_container_deploy toneIndigo
```

## Components

### Startup & Host Integration

- [Server entrypoint](https://github.com/wakeya/magic-claude-code/blob/main/cmd/server/main.go): Go startup · Assembles dependencies and owns process lifecycle. · `cmd/server/main.go`
- [Listen overrides](https://github.com/wakeya/magic-claude-code/blob/main/cmd/server/listen_override.go): startup options · `cmd/server/listen_override.go`
- [Connection bootstrap](https://github.com/wakeya/magic-claude-code/blob/main/internal/bootstrap/bootstrap.go): OS integration · Selects transparent, CONNECT tunnel, or gateway mode. · `internal/bootstrap/bootstrap.go`
- [Platform adapters](https://github.com/wakeya/magic-claude-code/blob/main/internal/bootstrap/adapters.go): OS adapters · `internal/bootstrap/adapters.go`
- [Local CA & certificates](https://github.com/wakeya/magic-claude-code/blob/main/internal/cert/manager.go): TLS trust · `internal/cert/manager.go`
- **Host trust facilities**: external OS · Hosts, certificate trust, and user environment integration.

### Configuration & Data

- [Configuration model](https://github.com/wakeya/magic-claude-code/blob/main/internal/config/config.go): routing state · Defines providers, ordering, mappings, preferences, and mode. · `internal/config/config.go`
- [Provider definitions](https://github.com/wakeya/magic-claude-code/blob/main/internal/config/provider.go): routing model · `internal/config/provider.go`
- [SQLite configuration store](https://github.com/wakeya/magic-claude-code/blob/main/internal/config/sqlite_store.go): persistence · Live routing state is updated without restart. · `internal/config/sqlite_store.go`

### Request Execution

- [TLS proxy server](https://github.com/wakeya/magic-claude-code/blob/main/internal/proxy/server.go): HTTP proxy · Serves intercepted client traffic on the proxy boundary. · `internal/proxy/server.go`
- [Compatibility handler](https://github.com/wakeya/magic-claude-code/blob/main/internal/proxy/handler.go): request policy · Applies endpoint policy, diagnostics, local responses, and streaming behavior. · `internal/proxy/handler.go`
- [OpenAI transforms](https://github.com/wakeya/magic-claude-code/blob/main/internal/proxy/transform/openai_chat.go): API compatibility · `internal/proxy/transform/openai_chat.go`
- [Failover manager](https://github.com/wakeya/magic-claude-code/blob/main/internal/failover/manager.go): provider selection · Classifies failures and advances through eligible providers. · `internal/failover/manager.go`
- [Quota manager](https://github.com/wakeya/magic-claude-code/blob/main/internal/providerquota/manager.go): provider eligibility · Resolves quotas and balances for routing decisions. · `internal/providerquota/manager.go`
- [Rate-limit queues](https://github.com/wakeya/magic-claude-code/blob/main/internal/proxy/ratelimit/queue.go): concurrency control · Per-provider queuing and 429 retry boundary. · `internal/proxy/ratelimit/queue.go`
- **AI provider APIs**: external inference

### Administration

- [Admin service](https://github.com/wakeya/magic-claude-code/blob/main/internal/admin/server.go): authenticated HTTP API · Manages routing, quotas, usage, sessions, imports, and updates on 8442. · `internal/admin/server.go`
- [Admin auth](https://github.com/wakeya/magic-claude-code/blob/main/internal/admin/auth.go): session authentication · `internal/admin/auth.go`
- [Embedded dashboard](https://github.com/wakeya/magic-claude-code/blob/main/internal/frontend/embed.go): Vue UI bundle · Serves the compiled dashboard from the Go binary. · `internal/frontend/embed.go`
- [Dashboard API client](https://github.com/wakeya/magic-claude-code/blob/main/internal/frontend/src/composables/useApi.ts): Vue composable · `internal/frontend/src/composables/useApi.ts`

### Operations & Deployment

- [Usage session sync](https://github.com/wakeya/magic-claude-code/blob/main/internal/usage/session_sync.go): usage ingestion · Scans local Claude JSONL data with parsing and redaction. · `internal/usage/session_sync.go`
- [Session scanner](https://github.com/wakeya/magic-claude-code/blob/main/internal/session/scanner.go): session data · Parses and exports sessions for the dashboard. · `internal/session/scanner.go`
- **Local Claude files**: external local data
- [Application updater](https://github.com/wakeya/magic-claude-code/blob/main/internal/updater/updater.go): release management · `internal/updater/updater.go`
- **Release APIs**: external service
- [Container deployment](https://github.com/wakeya/magic-claude-code/blob/main/docker-compose.yml): Docker runtime · Can bind 443 but cannot modify host trust or hosts state. · `docker-compose.yml`

## Connections

- Server entrypoint → Listen overrides: reads
- Server entrypoint → Connection bootstrap: initializes
- Server entrypoint → Configuration model: loads
- Server entrypoint → TLS proxy server: starts
- Server entrypoint → Admin service: starts
- Connection bootstrap → Platform adapters: uses
- Connection bootstrap → Local CA & certificates: provisions
- Platform adapters → Host trust facilities: integrates with
- Local CA & certificates → TLS proxy server: supplies TLS
- Configuration model → Provider definitions: contains
- Configuration model → SQLite configuration store: persists via
- SQLite configuration store → Admin service: live state for
- TLS proxy server → Compatibility handler: dispatches
- Compatibility handler → OpenAI transforms: transforms
- Compatibility handler → Failover manager: executes through
- Failover manager → Configuration model: uses provider priority
- Failover manager → Quota manager: checks eligibility
- Failover manager → Rate-limit queues: submits requests
- Rate-limit queues → AI provider APIs: forwards to
- Admin service → Admin auth: authenticates with
- Admin service → Embedded dashboard: serves
- Embedded dashboard → Dashboard API client: contains
- Dashboard API client → Admin service: calls
- Usage session sync → Local Claude files: scans
- Session scanner → Local Claude files: parses
- Admin service → Usage session sync: exposes usage
- Admin service → Session scanner: browses sessions
- Admin service → Application updater: manages
- Application updater → Release APIs: checks
- Container deployment → Server entrypoint: runs
