mcpspy 0.2.2

Zero-config stdio MCP proxy, latency profiler, and token usage auditor in Rust
Documentation
//! `mcpspy wrap` — transparent stdio proxy.

use anyhow::Result;
use std::path::Path;
use std::time::Duration;
use std::time::SystemTime;

use mcpspy::core::db::TelemetryDb;
use mcpspy::core::proxy::run_proxy;

pub fn run(name: &str, db_path: &str, no_payloads: bool, cmd: &[String]) -> Result<()> {
    if cmd.is_empty() {
        anyhow::bail!("no command specified after `--`");
    }

    let (program, args) = cmd.split_first().unwrap();

    // Generate a session id from timestamp + server name
    let session_id = format!(
        "{}-{}",
        SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .map(|d| d.as_millis())
            .unwrap_or(0),
        name
    );

    let db_path = Path::new(db_path);
    let db = TelemetryDb::open(db_path)?;

    // Run the proxy on the Tokio runtime
    let rt = tokio::runtime::Runtime::new()?;
    let proxy_result =
        rt.block_on(
            async move { run_proxy(program, args, name, &session_id, &db, no_payloads).await },
        );

    // Give extra time for final flush after the runtime stops
    rt.block_on(async move {
        tokio::time::sleep(Duration::from_millis(500)).await;
    });

    proxy_result.map_err(|e| anyhow::anyhow!("proxy failed: {e}"))
}