monaa-guard 0.1.0

Open-source Apache-2.0 Pure-Rust MCP 2026-07-28 wire firewall, local token compactor, and BYOK shim
Documentation
use thiserror::Error;

/// JSON-RPC error code when `Mcp-Method` or `Mcp-Name` header diverges from request payload.
pub const ERR_MCP_METHOD_MISMATCH: i64 = -32020;
/// JSON-RPC error code returned when closed-source cloud verification blocks a schema rug-pull.
pub const ERR_CLOUD_RUG_PULL: i64 = -32021;
/// JSON-RPC error code when public wire policy, Origin check, or free daily cap blocks a request.
pub const ERR_POLICY_DENIED: i64 = -32022;
/// JSON-RPC error code when financial account equity or notional exceeds `$5,000` (`500_000` cents) cap.
pub const ERR_ACCOUNT_CAP_EXCEEDED: i64 = -32023;

#[derive(Debug, Error)]
pub enum GuardError {
    #[error("configuration error: {0}")]
    Config(String),

    #[error("wire schema error: {0}")]
    Schema(String),

    #[error("MCP header/body parity mismatch (code -32020): {0}")]
    MethodMismatch(String),

    #[error("blocked by cloud schema verifier (code -32021): {0}")]
    CloudRugPull(String),

    #[error("firewall policy or daily quota violation (code -32022): {0}")]
    PolicyDenied(String),

    #[error("free daily limit reached (upgrade at https://monaa.tech/guard): {0}")]
    FreeQuotaExceeded(String),

    #[error("multi-asset equity or order notional exceeds $5,000 cap (code -32023): {0}")]
    AccountCapExceeded(String),

    #[error("invalid W3C traceparent (code -32022): {0}")]
    TraceInvalid(String),

    #[error("cloud license error: {0}")]
    License(String),

    #[error("cloud verification request failed: {0}")]
    CloudUnavailable(String),

    #[error("io failure: {0}")]
    Io(#[from] std::io::Error),
}

impl GuardError {
    pub fn rpc_code(&self) -> i64 {
        match self {
            Self::MethodMismatch(_) => ERR_MCP_METHOD_MISMATCH,
            Self::CloudRugPull(_) => ERR_CLOUD_RUG_PULL,
            Self::AccountCapExceeded(_) => ERR_ACCOUNT_CAP_EXCEEDED,
            Self::PolicyDenied(_)
            | Self::FreeQuotaExceeded(_)
            | Self::TraceInvalid(_)
            | Self::Schema(_) => ERR_POLICY_DENIED,
            _ => -32603,
        }
    }
}