Skip to main content

McpServer

Struct McpServer 

Source
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>,

Source

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.

Source

pub fn new_service( name: impl Into<String>, service: impl McpService<Counterpart>, runner: Run, ) -> Self

Available on crate feature unstable_mcp_over_acp only.

Construct a reusable service for native MCP-over-ACP only.

Source

pub 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.

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>

Source

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
where F: AsyncFn(McpAdmission) -> Result<(), McpRejection> + Send + Sync + 'static,

Available on crate feature 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.

Trait Implementations§

Source§

impl<Run> ConnectTo<Client> for McpServer<Client, Run>
where Run: RunWithConnectionTo<Client> + 'static,

Source§

async fn connect_to(self, client: impl ConnectTo<Server>) -> Result<(), Error>

Connect this component to another component. Read more
Source§

fn into_channel_and_future(self) -> (Channel, Option<ConnectionDriver>)
where Self: Sized,

Convert this component into a channel endpoint and optional owned driver. Read more
Source§

impl<Counterpart: Role + Debug, Run: Debug> Debug for McpServer<Counterpart, Run>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Counterpart, Run = NullRun> !RefUnwindSafe for McpServer<Counterpart, Run>

§

impl<Counterpart, Run = NullRun> !UnwindSafe for McpServer<Counterpart, Run>

§

impl<Counterpart, Run> Forget for McpServer<Counterpart, Run>

§

impl<Counterpart, Run> Freeze for McpServer<Counterpart, Run>
where PhantomData<Counterpart>: Freeze, Arc<dyn McpServerConnect<Counterpart>>: Freeze, Option<Arc<dyn McpService<Counterpart>>>: Freeze, Run: Freeze,

§

impl<Counterpart, Run> Move for McpServer<Counterpart, Run>

§

impl<Counterpart, Run> Send for McpServer<Counterpart, Run>
where Run: Send, PhantomData<Counterpart>: Send, Arc<dyn McpServerConnect<Counterpart>>: Send, Option<Arc<dyn McpService<Counterpart>>>: Send,

§

impl<Counterpart, Run> Sync for McpServer<Counterpart, Run>
where Run: Sync, PhantomData<Counterpart>: Sync, Arc<dyn McpServerConnect<Counterpart>>: Sync, Option<Arc<dyn McpService<Counterpart>>>: Sync,

§

impl<Counterpart, Run> Unpin for McpServer<Counterpart, Run>
where Run: Unpin, PhantomData<Counterpart>: Unpin, Arc<dyn McpServerConnect<Counterpart>>: Unpin, Option<Arc<dyn McpService<Counterpart>>>: Unpin,

§

impl<Counterpart, Run> UnsafeUnpin for McpServer<Counterpart, Run>
where PhantomData<Counterpart>: UnsafeUnpin, Arc<dyn McpServerConnect<Counterpart>>: UnsafeUnpin, Option<Arc<dyn McpService<Counterpart>>>: UnsafeUnpin, Run: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoMaybeUndefined<T> for T

Source§

fn into_maybe_undefined(self) -> MaybeUndefined<T>

Converts this value into a three-state builder argument.
Source§

impl<T> IntoOption<T> for T

Source§

fn into_option(self) -> Option<T>

Converts this value into an optional builder argument.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more