<h1 align="center">mcpspy</h1>
<p align="center">
<strong>A zero-config, local-first MCP Traffic Inspector, Latency Profiler & Token Auditor in a single Rust binary.</strong>
</p>
<p align="center">
<a href="LICENSE-MIT"><img alt="License" src="https://img.shields.io/badge/License-MIT%20OR%20Apache--2.0-22c55e?style=for-the-badge"></a>
<a href="Cargo.toml"><img alt="Version" src="https://img.shields.io/badge/Version-0.2.3-0ea5e9?style=for-the-badge"></a>
</p>
<p align="center">
<a href="https://github.com/bigwalkdoe/mcpspy/actions/workflows/ci.yml"><img alt="CI" src="https://img.shields.io/github/actions/workflow/status/bigwalkdoe/mcpspy/ci.yml?branch=main&style=flat-square&label=ci"></a>
<a href="https://crates.io/crates/mcpspy"><img alt="crates.io" src="https://img.shields.io/crates/v/mcpspy.svg"></a>
<a href="https://docs.rs/mcpspy"><img alt="docs.rs" src="https://docs.rs/mcpspy/badge.svg"></a>
</p>
---
## Contents
- [What it is](#-what-it-is)
- [Feature map](#-feature-map)
- [Quick start](#-quick-start)
- [Architecture](#-architecture)
- [License](#-license)
## ▸ What it is
`mcpspy` wraps any stdio MCP server transparently, forwards JSON-RPC 2.0 frames byte-for-byte with sub-millisecond overhead, and records latency, token usage, errors, and redacted payload previews into a local SQLite database. It never writes diagnostics to `stdout` in proxy mode, so it is a drop-in replacement for an MCP server command in OpenCode, Claude Code, or Cursor.
## ✦ Feature map
| **Transparent proxy** | `mcpspy wrap [--name LABEL] -- <CMD> [ARGS...]` forwards `stdin`/`stdout` verbatim; malformed JSON is still passed through untouched. |
| **Shared WAL telemetry store** | `rusqlite` (bundled) with `journal_mode=WAL` and a 5s busy timeout, so multiple wrapped servers can log concurrently. |
| **Token & latency accounting** | Per-call duration, request/response token estimates, byte counts, average/p50/p95 latency, and error detection for both JSON-RPC `error` objects and `result.isError`. |
| **Schema violation audit** | Malformed version, invalid method/params/id, and contradictory responses recorded to a separate table; viewable with `mcpspy tail --violations`. |
| **Secret redaction** | Bearer tokens, `sk-`/`ghp_`/`AKIA...` keys, generic `key=`/`password=` values, and DB connection URLs scrubbed before previews hit disk. `--no-payloads` stores metadata only. |
| **CLI analytics** | Ranked `stats` tables (plus `--json`), recent-call `tail`, and `prune`. |
| **Comparison reports** | `mcpspy compare --baseline A --optimized B` quantifies token savings and p50/p95 changes between two telemetry databases, as a table or with `--json`. |
| **Live TUI** | `mcpspy top` shows per-server token-burn bars, per-tool averages, latency percentiles, and recent calls (`q` quit, `c` clear). |
| **Self-inspecting MCP server** | `mcpspy mcp` exposes `get_mcp_stats` and `get_slow_calls` over stdio so an agent can audit its own token burn. |
| **Config auto-wrapping** | `mcpspy init <CONFIG_FILE>` rewrites existing MCP server entries to run through the proxy, keeping a `.bak` backup. |
| **Version flag** | `mcpspy --version` (or `-V`) prints the crate version. |
## ⚡ Quick start
```bash
cargo install --path .
mcpspy --help
mcpspy --version
```
Or build from source:
```bash
cargo build --release
./target/release/mcpspy --help
```
Feature flags: `tui` (ratatui + crossterm) and `mcp` (rmcp) are enabled by default. Build without them via `cargo build --no-default-features`.
### Wrap an MCP server
```bash
mcpspy wrap --name treecut -- treecut mcp
mcpspy --db /tmp/telemetry.db wrap --name logcut --no-payloads -- logcut mcp
```
### Auto-wrap an existing config
```bash
mcpspy init ~/.config/opencode/opencode.json
mcpspy init ~/.mcp.json
mcpspy init ~/.cursor/mcp.json
```
### Inspect the telemetry
```bash
mcpspy stats
mcpspy stats --server treecut --since 7d --json
mcpspy tail -n 20 --errors-only
mcpspy top
mcpspy compare --baseline /tmp/raw.db --optimized /tmp/sliced.db --json
mcpspy prune --older-than 7d
```
## 🏗 Architecture
```text
src/
├── core/ # proxy, telemetry, redaction, schema audit
├── cli/ # commands, stats, tail, top, compare, prune, init
└── mcp/ # self-inspecting MCP server
```
See `docs/architecture.md` for modules, data flow, invariants, and schema. Usage details in `docs/usage.md`, MCP reference in `docs/mcp.md`, troubleshooting in `docs/troubleshooting.md`.
## License
MIT OR Apache-2.0. See `LICENSE-MIT` and `LICENSE-APACHE`.