Skip to main content

facto_mcp/
lib.rs

1//! Local MCP server for facto -- real-time package ecosystem data for AI
2//! coding agents.
3//!
4//! This crate exposes the [`FactoMcp`] server and its tools over rmcp.
5//! The `facto-run` binary is the thin CLI wrapper that calls
6//! [`serve_stdio`] from this crate; third parties can also embed
7//! `FactoMcp` directly via [`FactoMcp::new`].
8//!
9//! ## Quick start
10//!
11//! ```no_run
12//! # async fn run() -> anyhow::Result<()> {
13//! let config = facto_core::config::Config::load()
14//!     .map_err(|e| anyhow::anyhow!("config: {e}"))?;
15//! facto_mcp::serve_stdio(&config).await
16//! # }
17//! ```
18pub mod server;
19pub mod tools;
20
21pub use server::FactoMcp;
22
23use anyhow::Context;
24use facto_core::config::Config;
25use facto_core::forges::manager::ForgeManager;
26use facto_core::registries::manager::RegistryManager;
27use facto_core::runtimes::manager::RuntimeManager;
28use rmcp::ServiceExt;
29use tokio_util::sync::CancellationToken;
30
31/// Run the MCP server on stdio until the transport is closed or SIGINT is
32/// received. Blocks the calling task.
33///
34/// Builds the registry / forge / runtime managers from `config`, wires
35/// them into a [`FactoMcp`], and serves rmcp over stdin/stdout.
36pub async fn serve_stdio(config: &Config) -> anyhow::Result<()> {
37    let registries = RegistryManager::new(config).context("failed to create registry manager")?;
38    let forges = ForgeManager::new(config).context("failed to create forge manager")?;
39    let runtimes = RuntimeManager::new(config).context("failed to create runtime manager")?;
40
41    let server = FactoMcp::new(registries, forges, runtimes);
42    let transport = rmcp::transport::io::stdio();
43    let ct = CancellationToken::new();
44
45    // Cancel the token on SIGINT so rmcp closes the transport internally.
46    let ct_signal = ct.clone();
47    tokio::spawn(async move {
48        let _ = tokio::signal::ctrl_c().await;
49        tracing::info!("received SIGINT, shutting down");
50        ct_signal.cancel();
51    });
52
53    tracing::info!("facto mcp starting (stdio)");
54    let handle = server
55        .serve_with_ct(transport, ct)
56        .await
57        .context("failed to start MCP server")?;
58
59    let _ = handle.waiting().await;
60    Ok(())
61}