memvault-proxy 0.4.5

MemVault MCP Proxy: transparent proxy with memory injection and compliance tracking
Documentation
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use axum::Router;
use clap::Parser;
use rmcp::ServiceExt;
use rmcp::transport::streamable_http_server::{
    StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager,
};
use tracing::info;

use memvault_core::compliance::ComplianceStore;
use memvault_core::embedding::{EmbeddingProvider, build_embedder_from_env};
use memvault_core::router::MemoryRouter;
use memvault_core::storage::sqlite::SqliteStore;

use memvault_proxy::config::{ProxyConfig, TransportMode, load_config};
use memvault_proxy::context::SessionContext;
use memvault_proxy::handler::ProxyHandler;
use memvault_proxy::injection::InjectionEngine;
use memvault_proxy::upstream::UpstreamManager;

#[derive(Parser)]
#[command(
    name = "memvault-proxy",
    about = "MemVault MCP Proxy — transparent proxy with memory injection and compliance tracking"
)]
struct Args {
    /// Path to proxy config file
    #[arg(long, default_value = "~/.memvault/proxy.yaml")]
    config: String,

    /// Override transport mode (stdio or sse)
    #[arg(long)]
    transport: Option<String>,

    /// Override port (for SSE mode). Defaults to the config file value, or 3778 when unset.
    #[arg(long)]
    port: Option<u16>,

    /// Override database path
    #[arg(long)]
    db: Option<String>,

    /// Env file to load at startup (default: $MEMVAULT_HOME/.env or
    /// ~/.memvault/.env; an absent file is silently skipped). Values already
    /// set in the environment always win. Covers the flat settings (embedding
    /// / LLM extraction / behavior switches) — upstream topology stays in
    /// the --config proxy.yaml.
    #[arg(long)]
    env_file: Option<String>,
}

fn resolve_path(raw: &str) -> PathBuf {
    if raw.starts_with("~/")
        && let Some(home) = std::env::var_os("HOME").map(PathBuf::from)
    {
        return home.join(&raw[2..]);
    }
    PathBuf::from(raw)
}

fn apply_cli_overrides(config: &mut ProxyConfig, args: &Args) {
    if let Some(ref t) = args.transport {
        config.proxy.transport = match t.as_str() {
            "sse" => TransportMode::Sse,
            _ => TransportMode::Stdio,
        };
    }
    if let Some(p) = args.port {
        config.proxy.port = p;
    }
    if let Some(ref db) = args.db {
        config.proxy.db = db.clone();
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let args = Args::parse();

    // The env file must land before tracing init — RUST_LOG itself may come
    // from it — and before every config reader, which all just read env vars.
    let env_report = memvault_core::env_file::load(args.env_file.as_deref());

    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::from_default_env()
                .add_directive("memvault_proxy=debug".parse()?)
                .add_directive("memvault_core=debug".parse()?),
        )
        .with_writer(std::io::stderr)
        .init();
    memvault_core::env_file::log_report(&env_report);

    let mut proxy_config = load_config(&args.config)?;

    apply_cli_overrides(&mut proxy_config, &args);

    let db_path = resolve_path(&proxy_config.proxy.db);
    if let Some(parent) = db_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    // Initialize core storage
    let store = Arc::new(SqliteStore::new(&db_path)?);

    // Initialize embedding provider(默认本地 Ollama,可选任意 OpenAI 兼容 API)
    let embedder: Option<Arc<dyn EmbeddingProvider>> = build_embedder_from_env().await;

    // Initialize router with optional agent registry
    let registry_path = db_path
        .parent()
        .map(|p| p.join("agents.yaml"))
        .unwrap_or_else(|| PathBuf::from("agents.yaml"));

    let router = if registry_path.exists() {
        info!(path = %registry_path.display(), "loading agent registry");
        let r = MemoryRouter::load_registry_from_yaml(store.clone(), &registry_path)?;
        if let Some(ref emb) = embedder {
            Arc::new(r.with_embedder(emb.clone()))
        } else {
            Arc::new(r)
        }
    } else {
        let r = MemoryRouter::new(store.clone());
        if let Some(ref emb) = embedder {
            Arc::new(r.with_embedder(emb.clone()))
        } else {
            Arc::new(r)
        }
    };

    // Initialize compliance store (same db)
    let compliance = ComplianceStore::new(db_path.to_str().unwrap_or(":memory:"))?;

    // Connect to upstream MCP servers
    info!(
        count = proxy_config.proxy.upstreams.len(),
        "connecting to upstream MCP servers"
    );
    let upstreams = UpstreamManager::connect_all(&proxy_config.proxy.upstreams).await?;

    // Session context and injection engine
    let context = SessionContext::new();
    let injection = InjectionEngine::new(router.clone(), context.clone());
    injection.refresh().await;

    // Build proxy handler
    let proxy_handler =
        ProxyHandler::new(store, router, upstreams, context, injection, compliance).await;

    match proxy_config.proxy.transport {
        TransportMode::Stdio => {
            info!("starting MCP proxy (stdio mode)");
            let transport = rmcp::transport::stdio();
            let service = proxy_handler.serve(transport).await?;
            service.waiting().await?;
        }
        TransportMode::Sse => {
            run_sse_proxy(proxy_handler, proxy_config.proxy.port).await?;
        }
    }

    Ok(())
}

async fn run_sse_proxy(handler: ProxyHandler, port: u16) -> anyhow::Result<()> {
    let session_manager = Arc::new(LocalSessionManager::default());
    let config =
        StreamableHttpServerConfig::default().with_sse_keep_alive(Some(Duration::from_secs(15)));

    let svc = StreamableHttpService::new(move || Ok(handler.clone()), session_manager, config);
    let app = sse_app(svc);

    let addr = std::net::SocketAddr::from(([127, 0, 0, 1], port));
    info!("MCP Proxy (SSE) listening on http://{}/mcp", addr);

    let listener = tokio::net::TcpListener::bind(addr).await?;
    axum::serve(listener, app)
        .with_graceful_shutdown(memvault_proxy::shutdown::shutdown_signal())
        .await?;

    Ok(())
}

/// SSE-mode axum router: `/mcp` is the streamable-HTTP MCP endpoint, `/health`
/// a standalone liveness probe. Extracted as a function so the tests exercise
/// the *merged* router — a lone `/health` route would never catch a `/mcp` ↔
/// `/health` routing conflict.
fn sse_app(svc: StreamableHttpService<ProxyHandler, LocalSessionManager>) -> axum::Router {
    Router::new()
        .route("/mcp", axum::routing::any_service(svc))
        .route("/health", axum::routing::get(health))
}

/// Minimum liveness probe for the SSE server. Returns 200 without touching
/// the database or MCP session state, so the dsh bridge plugin and other
/// launchers can poll it safely during process-readiness detection.
async fn health() -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!({ "status": "ok", "service": "memvault-proxy" }))
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use tower::ServiceExt;

    /// Build the production SSE router backed by a real `ProxyHandler`, so the
    /// `/mcp` + `/health` routes are exercised together (coexistence was
    /// previously confirmed by manual curl only).
    async fn proxy_app() -> axum::Router {
        let store = Arc::new(SqliteStore::in_memory().unwrap());
        let router = Arc::new(MemoryRouter::new(store.clone()));
        let upstreams = UpstreamManager::connect_all(&[]).await.unwrap();
        let context = SessionContext::new();
        let injection = InjectionEngine::new(router.clone(), context.clone());
        let db = std::env::temp_dir().join(format!(
            "memvault_proxy_health_{}.db",
            uuid::Uuid::new_v4().simple()
        ));
        let compliance = ComplianceStore::new(&db.to_string_lossy()).expect("compliance store");
        let handler =
            ProxyHandler::new(store, router, upstreams, context, injection, compliance).await;

        let session = Arc::new(LocalSessionManager::default());
        let svc = StreamableHttpService::new(
            move || Ok::<_, std::io::Error>(handler.clone()),
            session,
            StreamableHttpServerConfig::default(),
        );
        sse_app(svc)
    }

    #[tokio::test]
    async fn health_endpoint_returns_ok() {
        let response = proxy_app()
            .await
            .oneshot(
                Request::builder()
                    .uri("/health")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = axum::body::to_bytes(response.into_body(), 64 * 1024)
            .await
            .unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(json["status"], "ok");
        assert_eq!(json["service"], "memvault-proxy");
    }

    #[tokio::test]
    async fn health_endpoint_rejects_other_methods() {
        let response = proxy_app()
            .await
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/health")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::METHOD_NOT_ALLOWED);
    }
    #[test]
    fn resolve_path_expands_tilde_with_home() {
        let home = std::env::var_os("HOME")
            .map(std::path::PathBuf::from)
            .expect("HOME set");
        assert_eq!(
            resolve_path("~/.memvault/proxy.yaml"),
            home.join(".memvault/proxy.yaml")
        );
    }

    #[test]
    fn resolve_path_keeps_absolute_and_relative() {
        assert_eq!(
            resolve_path("/tmp/p.yaml"),
            std::path::PathBuf::from("/tmp/p.yaml")
        );
        assert_eq!(resolve_path("p.yaml"), std::path::PathBuf::from("p.yaml"));
    }

    #[test]
    fn args_parse_defaults_and_overrides() {
        let args = Args::try_parse_from(["memvault-proxy"]).unwrap();
        assert_eq!(args.config, "~/.memvault/proxy.yaml");
        assert_eq!(args.port, None);
        assert_eq!(args.transport, None);
        assert_eq!(args.db, None);

        let args = Args::try_parse_from([
            "memvault-proxy",
            "--config",
            "/tmp/p.yaml",
            "--transport",
            "sse",
            "--port",
            "5000",
            "--db",
            "/tmp/m.db",
        ])
        .unwrap();
        assert_eq!(args.config, "/tmp/p.yaml");
        assert_eq!(args.transport.as_deref(), Some("sse"));
        assert_eq!(args.port, Some(5000));
        assert_eq!(args.db.as_deref(), Some("/tmp/m.db"));
    }

    /// Regression: config-file `port` must be honored when `--port` is absent
    /// (previously the CLI default unconditionally overwrote the config value).
    #[test]
    fn cli_overrides_respect_config_port() {
        let dir = std::env::temp_dir().join(format!(
            "mv_proxy_cfg_port_{}",
            uuid::Uuid::new_v4().simple()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("proxy.yaml");
        std::fs::write(
            &path,
            "proxy:\n  transport: sse\n  port: 18780\n  upstreams: []\n",
        )
        .unwrap();

        // no --port -> config port wins
        let mut config = load_config(path.to_str().unwrap()).unwrap();
        let args = Args::try_parse_from(["memvault-proxy"]).unwrap();
        apply_cli_overrides(&mut config, &args);
        assert_eq!(config.proxy.port, 18780);

        // explicit --port overrides the config
        let args = Args::try_parse_from(["memvault-proxy", "--port", "5000"]).unwrap();
        apply_cli_overrides(&mut config, &args);
        assert_eq!(config.proxy.port, 5000);

        // no config file -> default 3778 is preserved when --port absent
        let mut config = load_config("/tmp/mv_no_such_proxy_xyz.yaml").unwrap();
        let args = Args::try_parse_from(["memvault-proxy"]).unwrap();
        apply_cli_overrides(&mut config, &args);
        assert_eq!(config.proxy.port, 3778);
    }

    /// The `/mcp` route must coexist with `/health` in the merged router —
    /// a lone `/health` route would silently pass CI while `/mcp` was missing.
    #[tokio::test]
    async fn mcp_route_is_mounted_next_to_health() {
        let response = proxy_app()
            .await
            .oneshot(
                Request::builder()
                    .method("GET")
                    .uri("/mcp")
                    .header("Accept", "text/event-stream")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_ne!(
            response.status(),
            StatusCode::NOT_FOUND,
            "/mcp must be mounted in the SSE router"
        );
    }
}