mcpspy 0.2.0

Zero-config stdio MCP proxy, latency profiler, and token usage auditor in Rust
Documentation
//! Clap CLI router for `mcpspy`.
//!
//! Commands: `wrap`, `top`, `stats`, `tail`, `prune`, `init`, `mcp`.
//! Full implementations land in Phase 3; this stub wires the structure
//! and delegates to `mcpspy::core`.

use anyhow::Result;
use clap::{Parser, Subcommand};

mod cli_init;
mod cli_mcp;
mod cli_prune;
mod cli_stats;
mod cli_tail;
mod cli_top;
mod cli_util;
mod cli_wrap;
mod mcp;
mod tui;

#[derive(Parser, Debug)]
#[command(
    name = "mcpspy",
    about = "MCP traffic inspector, latency profiler & token auditor"
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Path to the telemetry SQLite database.
    #[arg(long = "db", global = true, default_value = "~/.mcpspy/telemetry.db")]
    db: Option<String>,
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Transparent stdio proxy for an MCP server.
    Wrap {
        /// Label for this server in telemetry.
        #[arg(long = "name", default_value = "mcp-server")]
        name: String,
        /// Do not store request/response payload previews (metadata only).
        #[arg(long = "no-payloads")]
        no_payloads: bool,
        /// Separator: everything after `--` is the child command.
        #[arg(last = true)]
        cmd: Vec<String>,
    },
    /// Print ranked summary tables of calls, tokens, latency, errors.
    Stats {
        #[arg(long = "server")]
        server: Option<String>,
        #[arg(long = "since", default_value = "24h")]
        since: String,
        #[arg(long = "json")]
        json: bool,
    },
    /// Live ratatui dashboard (press `q` to quit, `c` to clear).
    Top {},
    /// Stream recent recorded MCP tool calls.
    Tail {
        #[arg(short = 'n', long, default_value = "20")]
        n: i64,
        #[arg(long = "server")]
        server: Option<String>,
        #[arg(long = "errors-only")]
        errors_only: bool,
        #[arg(long = "violations", help = "Show recorded JSON-RPC schema violations")]
        violations: bool,
    },
    /// Delete telemetry older than the given duration and VACUUM.
    Prune {
        #[arg(long = "older-than", default_value = "7d")]
        older_than: String,
    },
    /// Auto-wrap existing MCP server commands in a config file.
    Init { config_file: String },
    /// Self-inspecting MCP server over stdio.
    Mcp {},
}

fn main() -> Result<()> {
    // Never let diagnostics contaminate the JSON-RPC stdout pipe.
    tracing_subscriber::fmt()
        .with_writer(std::io::stderr)
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        .init();

    let cli = Cli::parse();
    let db_path = cli_util::expand_tilde(&cli.db.unwrap_or_default());

    match cli.command {
        Commands::Wrap {
            name,
            no_payloads,
            cmd,
        } => cli_wrap::run(&name, &db_path, no_payloads, &cmd),
        Commands::Stats {
            server,
            since,
            json,
        } => cli_stats::run(&db_path, server.as_deref(), &since, json),
        Commands::Top {} => cli_top::run(&db_path),
        Commands::Tail {
            n,
            server,
            errors_only,
            violations,
        } => cli_tail::run(&db_path, n, server.as_deref(), errors_only, violations),
        Commands::Prune { older_than } => cli_prune::run(&db_path, &older_than),
        Commands::Init { config_file } => cli_init::run(&config_file),
        Commands::Mcp {} => cli_mcp::run(&db_path),
    }
}