mcpspy 0.2.3

Zero-config stdio MCP proxy, latency profiler, and token usage auditor in Rust
Documentation
//! Telemetry data models, summary reports, and JSON Schema types.
//!
//! Pure data structures — no UI, no CLI, no transport dependencies.

use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// A single recorded JSON-RPC 2.0 call, captured end-to-end by the proxy.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct RpcCall {
    pub session_id: String,
    pub server_name: String,
    pub rpc_id: Option<String>,
    pub method: String,
    pub tool_name: Option<String>,
    pub started_at_ms: i64,
    pub duration_ms: f64,
    pub req_bytes: i64,
    pub resp_bytes: i64,
    pub req_tokens: i64,
    pub resp_tokens: i64,
    pub is_error: bool,
    pub error_message: Option<String>,
    pub req_preview: Option<String>,
    pub resp_preview: Option<String>,
}

/// Insert-only input for recording a completed RPC call into the DB.
#[derive(Debug, Clone)]
pub struct RpcRecord {
    pub session_id: String,
    pub server_name: String,
    pub rpc_id: Option<String>,
    pub method: String,
    pub tool_name: Option<String>,
    pub started_at_ms: i64,
    pub duration_ms: f64,
    pub req_bytes: i64,
    pub resp_bytes: i64,
    pub req_tokens: i64,
    pub resp_tokens: i64,
    pub is_error: bool,
    pub error_message: Option<String>,
    pub req_preview: Option<String>,
    pub resp_preview: Option<String>,
}

/// Aggregated per-server summary for `mcpspy stats`.
#[derive(Debug, Clone, Serialize)]
pub struct ServerSummary {
    pub server_name: String,
    pub total_calls: i64,
    pub total_req_tokens: i64,
    pub total_resp_tokens: i64,
    pub avg_duration_ms: f64,
    pub p50_duration_ms: f64,
    pub p95_duration_ms: f64,
    pub error_count: i64,
}

/// Aggregated per-tool summary for `mcpspy stats`.
#[derive(Debug, Clone, Serialize)]
pub struct ToolSummary {
    pub server_name: String,
    pub tool_name: String,
    pub total_calls: i64,
    pub total_req_tokens: i64,
    pub total_resp_tokens: i64,
    pub avg_duration_ms: f64,
    pub p50_duration_ms: f64,
    pub p95_duration_ms: f64,
    pub error_count: i64,
}

/// A single recent call for `mcpspy tail`.
#[derive(Debug, Clone, Serialize)]
pub struct RecentCall {
    pub started_at: DateTime<Utc>,
    pub server_name: String,
    pub method: String,
    pub tool_name: Option<String>,
    pub duration_ms: f64,
    pub req_tokens: i64,
    pub resp_tokens: i64,
    pub is_error: bool,
}

/// A JSON-RPC 2.0 schema violation observed while proxying a frame.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaViolation {
    pub session_id: String,
    pub server_name: String,
    pub rpc_id: Option<String>,
    /// `request` or `response`.
    pub direction: String,
    /// Machine-readable violation code, e.g. `bad_version`.
    pub code: String,
    /// Human-readable detail about the violation.
    pub detail: String,
    /// Redacted raw-frame preview (omitted with `--no-payloads`).
    pub raw_preview: Option<String>,
    pub started_at_ms: i64,
}

/// Pending outbound call awaiting its response (used by the JSON-RPC correlator).
#[derive(Debug, Clone)]
pub struct PendingCall {
    pub started_at: std::time::Instant,
    pub method: String,
    pub tool_name: Option<String>,
    pub req_bytes: i64,
    pub req_tokens: i64,
    pub req_preview: Option<String>,
}

impl Default for PendingCall {
    fn default() -> Self {
        Self {
            started_at: std::time::Instant::now(),
            method: String::new(),
            tool_name: None,
            req_bytes: 0,
            req_tokens: 0,
            req_preview: None,
        }
    }
}