pub struct McpServer<Counterpart: Role, Run = NullRun> { /* private fields */ }Expand description
A runtime-agnostic MCP server.
McpServer wraps an McpServerConnect
implementation. A server whose counterpart is role::mcp::Client can be
connected directly as a standalone MCP component. With the
unstable_mcp_over_acp feature, servers can instead be attached to ACP
session setup through Builder::with_mcp_server or
SessionBuilder::with_mcp_server. When unstable_protocol_v2 is also
enabled, Proxy.v2().with_mcp_server attaches a server globally to draft
v2 setup requests. v2::SessionBuilder::with_mcp_server attaches one to a
single new v2 session, while v2::ResumeSessionBuilder::with_mcp_server
attaches one to a single resumed session. With unstable_session_fork,
v2::ForkSessionBuilder::with_mcp_server attaches one to a single forked
session.
§Creating an MCP Server
The agent-client-protocol-rmcp crate provides builder APIs for MCP tools
backed by the rmcp crate.
Or implement McpServerConnect for custom server behavior:
let server = McpServer::new(MyCustomServerConnect, NullRun);Implementations§
Source§impl<Counterpart: Role, Run> McpServer<Counterpart, Run>where
Run: RunWithConnectionTo<Counterpart>,
impl<Counterpart: Role, Run> McpServer<Counterpart, Run>where
Run: RunWithConnectionTo<Counterpart>,
Sourcepub fn new(c: impl McpServerConnect<Counterpart>, runner: Run) -> Self
pub fn new(c: impl McpServerConnect<Counterpart>, runner: Run) -> Self
Create an MCP server from something that implements the McpServerConnect trait.
§See also
See agent-client-protocol-rmcp to construct MCP servers from Rust code
with rmcp.
Sourcepub fn new_service(
name: impl Into<String>,
service: impl McpService<Counterpart>,
runner: Run,
) -> Self
Available on crate feature unstable_mcp_over_acp only.
pub fn new_service( name: impl Into<String>, service: impl McpService<Counterpart>, runner: Run, ) -> Self
unstable_mcp_over_acp only.Construct a reusable service for native MCP-over-ACP only.
Sourcepub fn new_service_with_standalone(
service: impl McpService<Counterpart>,
standalone: impl McpServerConnect<Counterpart>,
runner: Run,
) -> Self
Available on crate feature unstable_mcp_over_acp only.
pub fn new_service_with_standalone( service: impl McpService<Counterpart>, standalone: impl McpServerConnect<Counterpart>, runner: Run, ) -> Self
unstable_mcp_over_acp only.Construct a reusable native service and a separate standalone factory.
Native requests do not create standalone sessions or perform hidden initialization/discovery exchanges.
Source§impl<Counterpart: Role, Run> McpServer<Counterpart, Run>
impl<Counterpart: Role, Run> McpServer<Counterpart, Run>
Sourcepub fn with_admission<F>(
self,
admit: F,
to_future_hack: impl for<'a> Fn(&'a F, McpAdmission) -> BoxFuture<'a, Result<(), McpRejection>> + Send + Sync + 'static,
) -> Self
Available on crate feature unstable_mcp_over_acp only.
pub fn with_admission<F>( self, admit: F, to_future_hack: impl for<'a> Fn(&'a F, McpAdmission) -> BoxFuture<'a, Result<(), McpRejection>> + Send + Sync + 'static, ) -> Self
unstable_mcp_over_acp only.Adds a check that decides, before each mcp/message request runs,
whether the request is admitted.
Use it for a server that needs the session of a request, or that
refuses a session after its own check. The check is an async closure
of the request. Pass mcp_admission! as the
last argument:
server.with_admission(
async |request| {
request.require_session_id()?;
Ok(())
},
agent_client_protocol::mcp_admission!(),
)A check can capture the provider’s state and await:
server.with_admission(
async move |request| {
let session_id = request.require_session_id()?;
if !sessions.is_active(session_id).await {
return Err(McpRejection::session_rejected("the session is not active"));
}
Ok(())
},
agent_client_protocol::mcp_admission!(),
)The check runs for every request to this server, such as tools/call,
tools/list, and server/discover. It runs after the SDK has checked
the inner request’s own metadata, and before the service, connector,
or tool is called. A rejected request reaches none of them. The SDK
answers it with the outer ACP error Invalid params (-32602), with
the McpRejection’s reason as the data string.
A request holds its request ID while its check is pending, so another
request with that ID is answered as a duplicate. The ID is free again
once the request is answered, also after a rejection. Cancellation by
the Agent, removal of the server’s registration, and connection
shutdown end a pending check as they end a running request: with
Request cancelled (-32800), with -33001, and without a response.
The SDK then drops the check’s future, so a check must not own work
that needs cleanup.
Calling this method again adds another check. The SDK runs them one
after the other in the order they were added. The first rejection
answers the request, and the checks after it do not run. A service’s
own McpService::admit runs after all of them.
The checks of several requests can be pending at the same time, so a check cannot change the state it captures. Put state that changes behind a lock.
A standalone MCP connection has no mcp/message request, so the check
does not run there. A tool that is also served standalone still has to
handle a request without a session.
To test a check without a connection, build its request with
McpAdmission::new.