quota-router MCP Server
by mmqfans
README.md
# quota-router
> **English** | [简体中文](README.zh-CN.md)
**Universal quota-aware LLM router, zero-dependency local streaming proxy, and MCP control plane for AI coding harnesses.**
Supports **Antigravity (`agy`)**, **Hermes Agent**, **OpenAI Codex**, **Claude Code (`cc`)**, **DSH**, **OpenCode**, and any OpenAI- or Anthropic-compatible client.
## Why quota-router?
Developers who juggle multiple AI subscriptions and API plans frequently run into three issues:
- One plan's short-term rolling window gets rate-limited while another plan's monthly quota sits unused and expires.
- Switching plans manually breaks flow, risks breaking harness configs, or invalidates a long-context prompt cache mid-session.
- Heavy cross-protocol gateways frequently drop `usage`, corrupt reasoning fields, or fail silently with 502s.
`quota-router` solves this with a **dual-plane, zero-dependency** architecture:
- **Data plane**: Local HTTP/1.1 streaming proxy (`/v1/chat/completions`, `/v1/responses`, `/v1/messages`, `/v1/models`, `/readyz`, `/_status`, `/_dashboard`) with same-protocol passthrough and pre-first-byte failover.
- **Control plane**: Stdio JSON-RPC 2.0 MCP server, CLI, and universal `SKILL.md` sharing the same SQLite state store and Tiered Pacing engine so closed OAuth/App pools participate in unified scheduling.
## Key Features
- **Zero mandatory dependencies**: Pure Python 3.11+ standard library (`tomllib`, `sqlite3`, `http.client`, `ssl`). Optional `PyYAML` extra for `.yaml` configs.
- **Protocol-preserving passthrough**: Routes strictly to upstreams supporting the incoming protocol; relays SSE chunk-by-chunk, preserves `usage`, and supports opt-in error-code normalization.
- **Tiered Pacing & EWMA burn-rate engine**: Group models into task tiers (`auto:long`, `auto:fast`, `auto:boss`, or `auto`). Candidates are ranked by multi-window pace gap, expiring-plan urgency, EWMA time-to-wall prediction, and peak-hour multipliers.
- **Tiered session stickiness**: Sticky tiers (`auto:long`) pin each conversation to protect prompt cache; burst tiers (`auto:fast`) balance dynamically per request.
- **Pre-first-byte failover**: Automatically fails over on `429 / 5xx / empty stream / connection drop` before the first chunk is written; passes through `400/413/422` directly; returns `503` with `Retry-After` if all pools are exhausted.
- **Three-tier quota sensing**: Background Pull probes (CLI/HTTP adapters), local SQLite metering + `Retry-After`/`*-ratelimit-*` header feedback (`metering`), and CLI/MCP calibration for closed pools (`manual`).
- **Safe harness integration**: Zero-invasion `qrouter run <harness>` launcher + `--dry-run`/backup/`revert` persistent integrators for `agy`, `hermes`, and `codex`.
- **Built-in observability**: `qrouter status` / `watch`, zero-build Web Dashboard at `/_dashboard`, SQLite telemetry, and redacted JSONL audit logs.
- **Credentials by reference only**: Config files never store plaintext API keys—only `env`, `env_file`, or `command` references.
## Quickstart
```bash
# Install from GitHub or local checkout (Python 3.11+)
uv tool install git+https://github.com/mmqfans/quota-router.git
# or locally: uv tool install -e .
# Initialize starter config and check credentials/probes
qrouter init
qrouter doctor --probe
# Start background proxy and check live status
qrouter start
qrouter status
```
Open `http://localhost:41580/_dashboard` in your browser for the live quota dashboard.
### Connect Harnesses
```bash
# Zero-invasion trial (does not modify any config file)
qrouter run codex --tier long
qrouter run claude --tier fast
# Preview persistent integration (dry-run by default)
qrouter integrate agy
qrouter integrate hermes
qrouter integrate codex
# Apply with automatic backup (revert anytime via `qrouter revert <harness>`)
qrouter integrate codex --apply
```
## CLI Reference
| Command | Description |
|---|---|
| `qrouter init` | Create starter `~/.config/quota-router/config.toml` |
| `qrouter doctor [--probe]` | Validate config, credential presence (masked), and probe health |
| `qrouter start` / `stop` | Manage background proxy daemon |
| `qrouter serve` | Run proxy in foreground |
| `qrouter status [--refresh] [--json]` | Show pool quotas, 5h burn rates, cooldowns, and tier picks |
| `qrouter watch [-n 5]` | Live refreshing terminal dashboard |
| `qrouter route <model_or_tier>` | Inspect exact pacing scores for a model or `auto:<tier>` |
| `qrouter calibrate <pool> ...` | Calibrate closed/manual pools or mark throttling |
| `qrouter run <harness> [--tier long]` | Zero-invasion harness launcher with auto-start |
| `qrouter integrate <harness> [--apply]` | Preview or apply harness integration (`agy`, `hermes`, `codex`) |
| `qrouter revert <harness>` | Restore original harness config from latest backup |
| `qrouter mcp` | Start stdio JSON-RPC 2.0 MCP server |
| `qrouter launchd [--write]` | Generate optional macOS LaunchAgent plist |
## Documentation
- [Architecture & Protocol Design](docs/ARCHITECTURE.md)
- [Tiered Pacing & EWMA Algorithm](docs/PACING_ALGORITHM.md)
- [Harness Integration Guide](docs/HARNESS_INTEGRATION.md)
- [Writing Custom Probe Adapters](docs/ADAPTER_GUIDE.md)
- [Universal Cross-Harness Skill](skills/quota-router/SKILL.md)
- [Contributing](CONTRIBUTING.md) | [Changelog](CHANGELOG.md)
## Security
- Binds to loopback (`127.0.0.1`) by default. Non-loopback hosts refuse to start unless `server.auth_token_env` is configured.
- Plaintext API keys are never written to config files, SQLite, JSONL audit logs, `/_status`, or MCP responses.
## License
[MIT](LICENSE)