rig_mcp_server/lib.rs
1//! Serve [Rig](https://github.com/0xPlaygrounds/rig) agents and tools over the
2//! [Model Context Protocol](https://modelcontextprotocol.io), so any MCP client
3//! (Claude Code, Cursor, Zed, Claude Desktop, another agent) can use them.
4//!
5//! - An [`agent`](McpServer::agent) becomes a tool that takes a message and an
6//! optional `session_id`, keeps the conversation, streams its text as
7//! progress notifications, and stops when the client cancels.
8//! - A [`typed_agent`](McpServer::typed_agent) answers with structured content
9//! matching its output schema.
10//! - A plain Rig [`tool`](McpServer::tool) is exposed as itself, with its
11//! annotations, and runs without a model.
12//!
13//! Built on the official MCP Rust SDK, [`rmcp`].
14//!
15//! ```no_run
16//! use rig_mcp_server::{McpServer, ServeError};
17//!
18//! async fn serve(agent: rig_agent::Agent) -> Result<(), ServeError> {
19//! // A local client launches this binary and talks to it over stdio.
20//! McpServer::new("my-agents").agent(agent).serve_stdio().await
21//! }
22//! ```
23//!
24//! Over Streamable HTTP the same server answers at `/mcp`. With a bearer token,
25//! calls without it get HTTP 401:
26//!
27//! ```no_run
28//! use rig_mcp_server::McpServer;
29//!
30//! async fn serve_http(server: McpServer, token: &str) -> Result<(), Box<dyn std::error::Error>> {
31//! let app = server.bearer_token(token).into_router()?;
32//! let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
33//! rig_mcp_server::axum::serve(listener, app).await?;
34//! Ok(())
35//! }
36//! ```
37//!
38//! An HTTP client connects to `http://127.0.0.1:3000/mcp` and sends the token on
39//! every request, as `Authorization: Bearer <token>`. For Claude Code:
40//! `claude mcp add --transport http my-agents http://127.0.0.1:3000/mcp --header "Authorization: Bearer <token>"`.
41//!
42//! Prompts and replies go on the trace only when the agent records them, with
43//! `AgentBuilder::record_content_telemetry(true)`. It is off by default, as in Rig.
44
45#![forbid(unsafe_code)]
46#![cfg_attr(docsrs, feature(doc_cfg))]
47#![deny(missing_docs)]
48#![cfg_attr(
49 test,
50 allow(
51 clippy::expect_used,
52 clippy::panic,
53 clippy::unwrap_used,
54 clippy::unreachable
55 )
56)]
57
58mod catalog;
59mod completion;
60mod content;
61mod history;
62#[cfg(feature = "otel")]
63mod otel;
64mod server;
65mod transport;
66
67pub use catalog::TemplateVars;
68pub use completion::{CompletionRequest, CompletionTarget};
69pub use history::{DEFAULT_HISTORY_TOKENS, DEFAULT_MAX_CONTEXTS};
70pub use server::{
71 BuildError, DEFAULT_MAX_CONCURRENT_CALLS, DEFAULT_MAX_RUN_DURATION, McpServer, Presentation,
72};
73pub use transport::{DEFAULT_MAX_REQUEST_BYTES, MCP_PATH, ServeError};
74
75/// The Axum version [`McpServer::into_router`] builds with, for `axum::serve`.
76pub use axum;
77/// The session stores and history windows agent sessions are kept with.
78pub use rig_memory;
79/// The MCP SDK this crate is built against.
80pub use rmcp;
81
82#[cfg(doctest)]
83#[doc = include_str!("../README.md")]
84mod readme_doctests {}