mcpspy 0.2.2

Zero-config stdio MCP proxy, latency profiler, and token usage auditor in Rust
Documentation
# Usage guide

## Global options

| Option | Default | Description |
| --- | --- | --- |
| `--db <PATH>` | `~/.mcpspy/telemetry.db` | Telemetry database. A leading `~` is expanded to `$HOME`. Parent directories are created on open. |

The `--db` flag is global and may appear before or after the subcommand.

## `wrap` — transparent proxy

```bash
mcpspy wrap [--name LABEL] [--no-payloads] -- <CMD> [ARGS...]
```

| Flag | Default | Description |
| --- | --- | --- |
| `--name LABEL` | `mcp-server` | Server label stored in telemetry. |
| `--no-payloads` | off | Store metrics only; no request/response previews. |

Everything after `--` is the child command. `stdout` carries **only** the child's JSON-RPC lines; diagnostics go to `stderr` and telemetry goes to SQLite.

```bash
mcpspy wrap --name treecut -- treecut mcp
mcpspy --db /tmp/trace.db wrap --name logcut --no-payloads -- logcut mcp
```

The proxy correlates requests and responses by JSON-RPC `id` (string or numeric). Notifications without an `id` are forwarded but not recorded. Malformed JSON lines are forwarded verbatim and never recorded as calls.

## `init` — auto-wrap an MCP config

```bash
mcpspy init <CONFIG_FILE>
```

Reads a JSON config, rewrites each server entry under `mcpServers` or `mcp` to run through `mcpspy wrap`, and saves the original as `<CONFIG_FILE>.bak`.

Before:

```json
{ "mcpServers": { "treecut": { "command": "treecut", "args": ["mcp"], "env": {} } } }
```

After:

```json
{ "mcpServers": { "treecut": { "command": "mcpspy", "args": ["wrap", "--name", "treecut", "--", "treecut", "mcp"], "env": {} } } }
```

Other fields (`env`, etc.) are preserved. Already-wrapped servers are skipped, so the command is idempotent. Supported formats include OpenCode, `.mcp.json`, and `.cursor/mcp.json`.

## `stats` — ranked summary

```bash
mcpspy stats [--server NAME] [--since DURATION] [--json]
```

| Flag | Default | Description |
| --- | --- | --- |
| `--server NAME` | all | Filter to one server label. |
| `--since DURATION` | `24h` | Time window ending now. |
| `--json` | off | Emit `{ "servers": [...], "tools": [...] }` instead of a table. |

The table reports calls, request/response tokens, average, p50 and p95 latency, and error counts per server and per tool. It also shows the number of schema violations in the window. Results are sorted by call count descending.

Durations accept one or more `<number><unit>` segments with units `ms`, `s`, `m`, `h`, `d`, `w`. Segments may be separated by whitespace:

```bash
mcpspy stats --since 1h30m
mcpspy stats --since 2d12h --json
mcpspy stats --server treecut --since '1w 2d'
```

Malformed values and overflow are rejected with a clear error.

## `tail` — recent calls

```bash
mcpspy tail [-n N] [--server NAME] [--errors-only]
```

| Flag | Default | Description |
| --- | --- | --- |
| `-n`, `--n N` | `20` | Maximum number of rows. |
| `--server NAME` | all | Filter to one server label. |
| `--errors-only` | off | Show only calls with errors. |
| `--violations` | off | Show recorded JSON-RPC schema violations instead of calls. |

Columns: timestamp, server, method, tool, duration, request/response tokens, and an `ERR` marker. With `--violations`, the columns are timestamp, server, direction, id, code, and detail.

```bash
mcpspy tail --violations -n 50
```

## `prune` — delete old telemetry

```bash
mcpspy prune --older-than DURATION
```

Deletes call records and schema violations started before `now - DURATION`, then runs `VACUUM`. Prints the combined number of deleted rows.

```bash
mcpspy prune --older-than 7d
mcpspy prune --older-than 30d12h
```

## `top` — live dashboard

```bash
mcpspy top [--db PATH]
```

Auto-refreshing ratatui view with three panes:

1. **Servers** — call counts, request/response tokens, token-burn bars, p95 latency, errors.
2. **Tools** — per-tool call counts, total tokens, average latency.
3. **Recent calls** — recent methods/tools with latency and token counts.

Keys: `q` quits, `c` clears the data panes. Requires a TTY and the default `tui` feature.

## `mcp` — self-inspecting MCP server

```bash
mcpspy mcp [--db PATH]
```

Serves two tools over stdio so an agent can query its own telemetry. See [MCP and agent reference](mcp.md).

## Typical workflow

```bash
# 1. Wrap existing servers
mcpspy init ~/.config/opencode/opencode.json

# 2. Work normally in the MCP host, then audit
mcpspy stats --since 24h
mcpspy tail -n 50 --errors-only

# 3. Clean up old data
mcpspy prune --older-than 7d
```