How wakeya/magic-claude-code works
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: server.go Admin service · main.go Server entrypoint · manager.go Failover manager · config.go Configuration model
Components 26
Startup & Host Integration
- Server entrypoint · Assembles dependencies and owns process lifecycle.
cmd/server/main.go - Listen overrides
cmd/server/listen_override.go - Connection bootstrap · Selects transparent, CONNECT tunnel, or gateway mode.
internal/bootstrap/bootstrap.go - Platform adapters
internal/bootstrap/adapters.go - Local CA & certificates
internal/cert/manager.go - Host trust facilities · Hosts, certificate trust, and user environment integration.
Configuration & Data
- Configuration model · Defines providers, ordering, mappings, preferences, and mode.
internal/config/config.go - Provider definitions
internal/config/provider.go - SQLite configuration store · Live routing state is updated without restart.
internal/config/sqlite_store.go
Request Execution
- TLS proxy server · Serves intercepted client traffic on the proxy boundary.
internal/proxy/server.go - Compatibility handler · Applies endpoint policy, diagnostics, local responses, and streaming behavior.
internal/proxy/handler.go - OpenAI transforms
internal/proxy/transform/openai_chat.go - Failover manager · Classifies failures and advances through eligible providers.
internal/failover/manager.go - Quota manager · Resolves quotas and balances for routing decisions.
internal/providerquota/manager.go - Rate-limit queues · Per-provider queuing and 429 retry boundary.
internal/proxy/ratelimit/queue.go - AI provider APIs
Administration
- Admin service · Manages routing, quotas, usage, sessions, imports, and updates on 8442.
internal/admin/server.go - Admin auth
internal/admin/auth.go - Embedded dashboard · Serves the compiled dashboard from the Go binary.
internal/frontend/embed.go - Dashboard API client
internal/frontend/src/composables/useApi.ts
Operations & Deployment
- Usage session sync · Scans local Claude JSONL data with parsing and redaction.
internal/usage/session_sync.go - Session scanner · Parses and exports sessions for the dashboard.
internal/session/scanner.go - Local Claude files
- Application updater
internal/updater/updater.go - Release APIs
- Container deployment · Can bind 443 but cannot modify host trust or hosts state.
docker-compose.yml
Connections 30
- 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