Skip to main content

agentic_codebase/mcp/
mod.rs

1//! Model Context Protocol server interface.
2//!
3//! Provides a synchronous JSON-RPC 2.0 server implementation for the
4//! Model Context Protocol (MCP). Exposes code graph operations as
5//! tools, resources, and prompts.
6
7pub mod protocol;
8pub mod server;
9pub mod sse;
10pub mod tenant;
11
12pub use protocol::{parse_request, JsonRpcError, JsonRpcRequest, JsonRpcResponse};
13pub use server::McpServer;
14
15use std::io::{self, BufRead, Write};
16
17/// Run the MCP server on stdin/stdout.
18///
19/// Reads newline-delimited JSON-RPC messages from stdin and writes
20/// responses to stdout.  This is the entry point used by the
21/// `agentic-codebase-mcp` binary.
22pub fn serve_stdio() {
23    let mut server = McpServer::new();
24    let stdin = io::stdin();
25    let stdout = io::stdout();
26    let mut stdout = stdout.lock();
27
28    for line in stdin.lock().lines() {
29        let line = match line {
30            Ok(l) => l,
31            Err(_) => break,
32        };
33
34        let trimmed = line.trim();
35        if trimmed.is_empty() {
36            continue;
37        }
38
39        let response = server.handle_raw(trimmed);
40        if response.is_empty() {
41            continue;
42        }
43        if writeln!(stdout, "{}", response).is_err() {
44            break;
45        }
46        if stdout.flush().is_err() {
47            break;
48        }
49    }
50}