Skip to main content

gitcortex_mcp/mcp/
server.rs

1use std::path::PathBuf;
2
3use anyhow::{Context, Result};
4use rmcp::{transport::io::stdio, ServiceExt};
5
6use crate::mcp::tools::GitCortexServer;
7
8pub async fn serve(repo_root: PathBuf, compact: bool) -> Result<()> {
9    let handler = GitCortexServer::new_with_mode(&repo_root, compact)
10        .context("failed to open graph store")?;
11
12    let transport = stdio();
13    tracing::info!("GitCortex MCP server started (stdio, compact={compact})");
14
15    // `serve` returns a `RunningService` that owns the message loop. Dropping
16    // it immediately tears the connection down — the client only ever sees the
17    // `initialize` response, then the process exits. Hold it and `waiting()`
18    // until the transport closes (client disconnects / EOF on stdin).
19    let service = handler.serve(transport).await.context("MCP server error")?;
20    service.waiting().await.context("MCP server stopped")?;
21
22    Ok(())
23}