|
| 1 | +# Flight — MCP Flight Recorder & Token Optimizer |
| 2 | + |
| 3 | +## Stack |
| 4 | + |
| 5 | +TypeScript, Node 20+, ESM modules, Vitest, tsup, Commander |
| 6 | + |
| 7 | +## Project Structure |
| 8 | + |
| 9 | +``` |
| 10 | +src/ |
| 11 | + cli.ts — Commander CLI entry point |
| 12 | + proxy.ts — stdio proxy: spawn upstream, bidirectional JSON-RPC forwarding |
| 13 | + json-rpc.ts — streaming JSON-RPC parser |
| 14 | + logger.ts — session logger with async write queue and alert detection |
| 15 | + progressive-disclosure.ts — PD handler: phase logic, usage tracking, tool filtering |
| 16 | + pd-schema.ts — pure schema compression utilities |
| 17 | + file-lock.ts — advisory file locking (O_CREAT|O_EXCL) |
| 18 | + retry.ts — automatic retry manager for transient MCP errors |
| 19 | + hooks.ts — Claude Code hook installation/removal |
| 20 | + init.ts — Claude/Claude Code config file management |
| 21 | + setup.ts — interactive setup wizard |
| 22 | + summary.ts — session summary computation |
| 23 | + stats.ts — usage statistics |
| 24 | + lifecycle.ts — log compression and garbage collection |
| 25 | + export.ts — CSV/JSONL export |
| 26 | + replay.ts — tool call replay from logs |
| 27 | + log-commands.ts — CLI subcommands for log inspection |
| 28 | + index.ts — public API re-exports |
| 29 | +``` |
| 30 | + |
| 31 | +## Commands |
| 32 | + |
| 33 | +```bash |
| 34 | +npm run build # Build with tsup |
| 35 | +npm run test # Run tests (vitest) |
| 36 | +npm run lint # ESLint |
| 37 | +npm run typecheck # tsc --noEmit (src + test) |
| 38 | +npm run check # lint + typecheck + test (use before committing) |
| 39 | +``` |
| 40 | + |
| 41 | +## Key Patterns |
| 42 | + |
| 43 | +- **Handler result objects** — `PDResponseResult` carries rewritten responses, log metadata, and status messages in one return value. Callers branch on fields, not exceptions. |
| 44 | +- **Async write queue** — Logger batches writes with a flush timer; `closeSync()` drains synchronously for signal handlers. |
| 45 | +- **File locking** — `acquireLock(path)` uses `O_CREAT|O_EXCL` with PID-based stale detection. Returns `""` on timeout (best-effort, never blocks). |
| 46 | +- **JSON-RPC streaming** — `parseJsonRpcStream` is a newline-delimited JSON parser on Node readable streams, emitting typed `JsonRpcMessage` events. |
| 47 | +- **Progressive disclosure phases** — Phase 1 (observation), Phase 2 (schema compression), Phase 3 (compression + filtering with `discover_tools`). |
| 48 | + |
| 49 | +## Testing |
| 50 | + |
| 51 | +- Tests live in `test/` alongside source |
| 52 | +- Mock MCP server pattern: spawn a test server, connect via proxy, assert on JSON-RPC messages |
| 53 | +- `test/simulate/` contains validation harnesses for Claude API compatibility |
| 54 | +- Run `npm run test` — all tests should pass before any PR |
| 55 | + |
| 56 | +## Git Conventions |
| 57 | + |
| 58 | +- Commit format: `<type>: <description>` (feat, fix, refactor, docs, test, chore) |
| 59 | +- Messages explain WHY, not what |
| 60 | +- One logical change per commit |
| 61 | + |
| 62 | +See [ARCHITECTURE.md](ARCHITECTURE.md) for deeper design details. |
0 commit comments