# MCP and agent reference
`mcpspy mcp` exposes the telemetry store back to an AI agent over the same stdio JSON-RPC 2.0 protocol used by other MCP servers. It is a self-inspection endpoint: point an MCP host at it and the agent can audit its own token burn and slow calls without leaving the conversation.
## Starting the server
```bash
mcpspy mcp
mcpspy --db /path/to/telemetry.db mcp
```
The server advertises the name `mcpspy` and the current crate version. It uses the same database as `wrap`, `stats`, and `tail`, so all commands must share a `--db` value to see the same data.
## Registering with an MCP host
Use [`mcpspy init`](usage.md#init--auto-wrap-an-mcp-config) to add both the wrapped servers and, manually, this self-inspection server:
```json
{
"mcpServers": {
"mcpspy": { "command": "mcpspy", "args": ["mcp"] },
"treecut": { "command": "mcpspy", "args": ["wrap", "--name", "treecut", "--", "treecut", "mcp"] }
}
}
```
The protocol lifecycle is standard: the host sends `initialize`, then may call `tools/list` and `tools/call`.
## Tools
### `get_mcp_stats`
Returns aggregated calls, tokens, average/p95 latency, and error counts per server and per tool.
| `server` | string or null | all | Filter to one server label. |
| `since_hours` | integer or null | `24` | Window ending now; negative values clamp to zero. |
Example request:
```json
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": { "name": "get_mcp_stats", "arguments": { "server": "treecut", "since_hours": 24 } }
}
```
The tool returns both a text `content` block and `structuredContent` with `servers` and `tools` arrays.
### `get_slow_calls`
Returns the slowest recorded calls with duration and token counts.
| `limit` | unsigned integer or null | `10` | Clamped to `1..=100`. |
| `server` | string or null | all | Filter to one server label. |
Calls are fetched newest-first (up to 500) and then sorted by `duration_ms` descending before truncation.
## Self-audit workflows
- **Token burn review.** Ask the agent to call `get_mcp_stats` with `since_hours: 24` and report the highest `total_resp_tokens` tool, then call it again after switching to a slicing tool such as `logcut slice` to compare.
- **Latency triage.** Call `get_slow_calls` with `limit: 10`, then inspect the corresponding rows with `mcpspy tail --server <NAME>`.
- **Error triage.** Use the CLI's `mcpspy tail --errors-only`; error messages stored in the database are redacted before persistence.
## Safety
The MCP server writes only protocol responses to `stdout`. Diagnostics are routed to `stderr`. Stored payload previews and error messages pass through the same secret redactor used by the proxy, so agents querying telemetry cannot recover scrubbed credentials.