Skip to main content

agent_client_protocol/mcp_server/
mod.rs

1//! Runtime-agnostic MCP server support.
2//!
3//! This module provides infrastructure for serving MCP directly without tying
4//! the core SDK to a particular MCP implementation or async runtime. With the
5//! `unstable_mcp_over_acp` feature, the same servers can be attached to ACP
6//! session setup requests through the `with_mcp_server` builder methods.
7//! Stable protocol v1 and draft protocol v2 both support global proxy
8//! attachment and per-session attachment. V2 uses
9//! `Proxy.v2().with_mcp_server(...)` or
10//! `V2SessionBuilder::with_mcp_server(...)` for new sessions and
11//! `V2ResumeSessionBuilder::with_mcp_server(...)` for resumed sessions. With
12//! `unstable_session_fork`, `V2ForkSessionBuilder::with_mcp_server(...)`
13//! attaches a server to a forked session. V2 attachment additionally requires
14//! `unstable_protocol_v2`.
15//!
16//! ## Building MCP servers with tools
17//!
18//! The opt-in `schemars` feature provides `McpTool`, `McpToolRegistry`
19//! and its metadata types, and the `tool_fn` / `tool_fn_mut` functions for
20//! automatically generating JSON Schemas from Rust input and output types.
21//! The `agent-client-protocol-rmcp` crate provides the builder APIs for MCP
22//! tools backed by the `rmcp` crate and enables this feature.
23//!
24//! Custom servers using [`crate::mcp_server::McpServerConnect`],
25//! [`crate::mcp_server::McpServer`], and the connection types remain available
26//! without `schemars`, including ACP attachment when
27//! `unstable_mcp_over_acp` is enabled.
28//!
29//! ## Custom MCP Server Implementations
30//!
31//! You can implement [`crate::mcp_server::McpServerConnect`] to create custom MCP
32//! servers:
33//!
34//! ```rust,ignore
35//! use agent_client_protocol::mcp_server::{McpConnectionTo, McpServer, McpServerConnect};
36//! use agent_client_protocol::{DynConnectTo, NullRun, Role, role};
37//!
38//! struct MyCustomServer;
39//!
40//! impl<R: Role> McpServerConnect<R> for MyCustomServer {
41//!     fn name(&self) -> String {
42//!         "my-custom-server".to_string()
43//!     }
44//!
45//!     fn connect(&self, cx: McpConnectionTo<R>) -> DynConnectTo<role::mcp::Client> {
46//!         // Return a component that serves MCP requests
47//!         DynConnectTo::new(my_mcp_component(cx))
48//!     }
49//! }
50//!
51//! let server = McpServer::new(MyCustomServer, NullRun);
52//! ```
53
54#[cfg(feature = "unstable_mcp_over_acp")]
55mod active_session;
56mod connect;
57mod context;
58#[cfg(feature = "schemars")]
59mod registry;
60mod server;
61#[cfg(feature = "unstable_mcp_over_acp")]
62mod service;
63#[cfg(feature = "schemars")]
64mod tool;
65#[cfg(feature = "schemars")]
66mod tool_fn;
67
68pub use connect::McpServerConnect;
69pub use context::{McpConnectionContext, McpConnectionTo};
70#[cfg(feature = "schemars")]
71#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
72pub use registry::{
73    EnabledTools, McpToolMetadata, McpToolRegistry, McpToolSchema, RegisteredMcpTool,
74};
75pub use server::McpServer;
76#[cfg(feature = "unstable_mcp_over_acp")]
77pub use service::{
78    McpOperationCancellation, McpOutcome, McpRequest, McpRequestContext, McpService,
79};
80
81/// The declared MCP provider is no longer available.
82#[cfg(feature = "unstable_mcp_over_acp")]
83pub const MCP_SERVER_UNAVAILABLE: i32 = -33001;
84/// The MCP backend failed independently of an MCP application error.
85#[cfg(feature = "unstable_mcp_over_acp")]
86pub const MCP_BACKEND_FAILURE: i32 = -33002;
87#[cfg(feature = "schemars")]
88#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
89pub use tool::McpTool;
90#[cfg(feature = "schemars")]
91#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
92pub use tool_fn::{tool_fn, tool_fn_mut};