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 `agent-client-protocol-rmcp` crate provides the builder APIs for MCP
19//! tools backed by the `rmcp` crate.
20//!
21//! ## Custom MCP Server Implementations
22//!
23//! You can implement [`McpServerConnect`](`crate::mcp_server::McpServerConnect`) to create custom MCP servers:
24//!
25//! ```rust,ignore
26//! use agent_client_protocol::mcp_server::{McpConnectionTo, McpServer, McpServerConnect};
27//! use agent_client_protocol::{DynConnectTo, NullRun, Role, role};
28//!
29//! struct MyCustomServer;
30//!
31//! impl<R: Role> McpServerConnect<R> for MyCustomServer {
32//! fn name(&self) -> String {
33//! "my-custom-server".to_string()
34//! }
35//!
36//! fn connect(&self, cx: McpConnectionTo<R>) -> DynConnectTo<role::mcp::Client> {
37//! // Return a component that serves MCP requests
38//! DynConnectTo::new(my_mcp_component(cx))
39//! }
40//! }
41//!
42//! let server = McpServer::new(MyCustomServer, NullRun);
43//! ```
44
45#[cfg(feature = "unstable_mcp_over_acp")]
46mod active_session;
47mod connect;
48mod context;
49mod registry;
50mod server;
51mod tool;
52mod tool_fn;
53
54pub use connect::McpServerConnect;
55pub use context::{McpConnectionContext, McpConnectionTo};
56pub use registry::{
57 EnabledTools, McpToolMetadata, McpToolRegistry, McpToolSchema, RegisteredMcpTool,
58};
59pub use server::McpServer;
60pub use tool::McpTool;
61pub use tool_fn::{tool_fn, tool_fn_mut};