Skip to main content

micromegas/servers/mcp/
mod.rs

1//! MCP (Model Context Protocol) server over Streamable HTTP, for agents that cannot run the
2//! `micromegas-query` CLI.
3//!
4//! The module holds the tool logic, guardrails and rendering and knows nothing about the host
5//! application. The host validates the bearer token (putting the resulting
6//! [`micromegas_auth::types::AuthContext`] in the request extensions) and supplies the tool
7//! providers; queries are forwarded to FlightSQL with the caller's token, which stays the
8//! enforcement point.
9
10/// configuration read from `MICROMEGAS_MCP_*`
11pub mod config;
12/// FlightSQL access used by the query tools
13pub mod flight;
14/// the `ToolProvider` seam between this module and its host
15pub mod provider;
16/// rejection of anything but read-only queries
17pub mod read_only;
18/// capped markdown rendering of query results
19pub mod render;
20/// MCP handler
21pub mod server;
22/// skills served over the MCP skills extension
23pub mod skills;
24/// query and discovery tools
25pub mod tools;
26
27use anyhow::Result;
28use axum::Router;
29use config::McpConfig;
30use provider::ToolProvider;
31use rmcp::transport::streamable_http_server::session::never::NeverSessionManager;
32use rmcp::transport::streamable_http_server::{StreamableHttpServerConfig, StreamableHttpService};
33use std::sync::Arc;
34
35/// Adds the MCP route at `config.path`. The route does not authenticate: the host must layer a
36/// bearer validation middleware on the returned router.
37pub fn register_routes(
38    router: Router,
39    config: &McpConfig,
40    providers: Vec<Arc<dyn ToolProvider>>,
41) -> Result<Router> {
42    let server = server::McpServer::new(config, providers)?;
43    // The body cap is enforced by the service while streaming (413), so no extra layer.
44    // Stateless: every tool call carries its full arguments, so no per-client state is kept.
45    let http_config = StreamableHttpServerConfig::default()
46        .with_legacy_session_mode(false)
47        .with_allowed_hosts(config.allowed_hosts.clone())
48        .with_allowed_origins(config.allowed_origins.clone())
49        .with_max_request_body_bytes(config.max_request_body_bytes);
50    let service = StreamableHttpService::new(
51        move || Ok(server.clone()),
52        Arc::new(NeverSessionManager::default()),
53        http_config,
54    );
55    Ok(router.route_service(&config.path, service))
56}