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//! `v2::SessionBuilder::with_mcp_server(...)` for new sessions and
11//! `v2::ResumeSessionBuilder::with_mcp_server(...)` for resumed sessions. With
12//! `unstable_session_fork`, `v2::ForkSessionBuilder::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;
56#[cfg(feature = "unstable_mcp_over_acp")]
57mod admission;
58mod connect;
59mod context;
60#[cfg(feature = "schemars")]
61mod registry;
62mod server;
63#[cfg(feature = "unstable_mcp_over_acp")]
64mod service;
65#[cfg(feature = "schemars")]
66mod tool;
67#[cfg(feature = "schemars")]
68mod tool_fn;
69
70#[cfg(feature = "unstable_mcp_over_acp")]
71pub use admission::{McpAdmission, McpRejection};
72pub use connect::McpServerConnect;
73pub use context::{McpConnectionContext, McpConnectionTo};
74#[cfg(feature = "schemars")]
75#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
76pub use registry::{
77    McpToolMetadata, McpToolRegistry, McpToolSchema, RegisteredMcpTool, UnknownToolError,
78};
79pub use server::McpServer;
80#[cfg(feature = "unstable_mcp_over_acp")]
81pub use service::{
82    McpOperationCancellation, McpOutcome, McpRequest, McpRequestContext, McpService,
83};
84
85/// `-33000`: an MCP-over-ACP binding resource limit was exceeded.
86#[cfg(feature = "unstable_mcp_over_acp")]
87pub const MCP_RESOURCE_LIMIT_EXCEEDED: crate::ErrorCode = crate::ErrorCode::new(-33000);
88/// `-33001`: the declared MCP server registration is unavailable.
89#[cfg(feature = "unstable_mcp_over_acp")]
90pub const MCP_SERVER_UNAVAILABLE: crate::ErrorCode = crate::ErrorCode::new(-33001);
91/// `-33002`: the MCP backend or transport failed without a valid MCP outcome.
92#[cfg(feature = "unstable_mcp_over_acp")]
93pub const MCP_BACKEND_FAILURE: crate::ErrorCode = crate::ErrorCode::new(-33002);
94#[cfg(feature = "schemars")]
95#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
96pub use tool::McpTool;
97#[cfg(feature = "schemars")]
98#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
99pub use tool_fn::{tool_fn, tool_fn_mut};
100
101/// Expands to the first block when this crate was built with
102/// `unstable_mcp_over_acp`, and to the `else` block otherwise.
103///
104/// MCP integrations in other crates use this to pick the native
105/// MCP-over-ACP path from this crate's features rather than their own, so
106/// feature unification cannot leave them on the per-request fallback while
107/// ACP attachment is available. Hidden from the docs, but
108/// `agent-client-protocol-rmcp` depends on it, so it is a stable cross-crate
109/// contract for the 5.x line: renaming it or changing its syntax is a
110/// breaking change.
111#[cfg(feature = "unstable_mcp_over_acp")]
112#[doc(hidden)]
113#[macro_export]
114macro_rules! __if_mcp_over_acp {
115    ({ $($then:tt)* } else { $($else:tt)* }) => {
116        $($then)*
117    };
118}
119
120/// Expands to the first block when this crate was built with
121/// `unstable_mcp_over_acp`, and to the `else` block otherwise.
122///
123/// MCP integrations in other crates use this to pick the native
124/// MCP-over-ACP path from this crate's features rather than their own, so
125/// feature unification cannot leave them on the per-request fallback while
126/// ACP attachment is available. Hidden from the docs, but
127/// `agent-client-protocol-rmcp` depends on it, so it is a stable cross-crate
128/// contract for the 5.x line: renaming it or changing its syntax is a
129/// breaking change.
130#[cfg(not(feature = "unstable_mcp_over_acp"))]
131#[doc(hidden)]
132#[macro_export]
133macro_rules! __if_mcp_over_acp {
134    ({ $($then:tt)* } else { $($else:tt)* }) => {
135        $($else)*
136    };
137}