Skip to main content

McpServer

Struct McpServer 

Source
pub struct McpServer<S>
where S: Send + Sync + ?Sized,
{ /* private fields */ }
Expand description

MCP server that dispatches JSON-RPC requests to the appropriate handler

Generic over S — the project-specific server state type, shared as Arc<S>. S is ?Sized, so a host may parameterize it with a resource façade trait object (dyn HostRuntime); a host needing interior mutability parameterizes S with it (e.g. RwLock<Inner>). Owns the shared state and tool registry. Transport layers feed parsed requests into handle_request and send the returned responses.

Implementations§

Source§

impl<S> McpServer<S>
where S: Send + Sync + 'static + ?Sized,

Source

pub fn new( name: impl Into<String>, version: impl Into<String>, tools: ToolRegistry<S>, state: Arc<S>, ) -> McpServer<S>

Create a server with the given name, version, tool registry, and shared state

Defaults to advertising tool support only, no instructions, and the modern + current legacy protocol revisions. Use the with_* builders to override the advertised capabilities, instructions, or supported versions.

Source

pub fn with_capabilities(self, capabilities: ServerCapabilities) -> McpServer<S>

Override the capabilities advertised in initialize and server/discover.

Source

pub fn with_instructions(self, instructions: impl Into<String>) -> McpServer<S>

Set the natural-language instructions advertised to clients.

Source

pub fn with_supported_versions(self, versions: Vec<String>) -> McpServer<S>

Override the protocol revisions the server advertises and accepts, in preference order.

Source

pub fn with_auth_hook(self, auth_hook: Arc<dyn AuthHook<S>>) -> McpServer<S>

Install a host authentication hook for the HTTP transport. With a hook, the transport authenticates every request (rejecting with 401/403); with none, every request runs as the default anonymous context.

Source

pub fn with_allowed_origins(self, origins: Vec<String>) -> McpServer<S>

Restrict the Origins the HTTP transport accepts. An empty list (the default) or one containing "*" allows any origin; a request whose Origin header is present and not listed is rejected with 403.

Source

pub fn allowed_origins(&self) -> &[String]

The Origin allowlist the HTTP transport enforces.

Source

pub fn with_tool_dispatcher( self, dispatcher: Arc<dyn ToolDispatcher<S>>, ) -> McpServer<S>

Install a host ToolDispatcher that owns tools/list and tools/call (per-caller views, quota, execution, usage). When installed it replaces the built-in registry for both tool methods.

Source

pub fn with_method_handler( self, handler: Arc<dyn MethodHandler<S>>, ) -> McpServer<S>

Install a host MethodHandler for methods the engine doesn’t natively serve (resources/*, prompts/*, sampling/*, …). Unknown methods are offered to it before falling through to method-not-found.

Source

pub async fn authenticate( &self, request: &JsonRpcRequest, ) -> Result<ToolContext, AuthError>

Authenticate a request via the configured AuthHook, or yield the default anonymous ToolContext when no hook is installed.

§Errors

Returns the hook’s AuthError (401/403) when authentication fails.

Source

pub async fn handle_raw(&self, raw: &str) -> Option<JsonRpcResponse>

Route a raw JSON string to the appropriate MCP handler

Parses the string as a JsonRpcRequest, dispatches it, and returns the serialized response. Returns None for notifications.

Source

pub async fn handle_request( &self, request: JsonRpcRequest, ) -> Option<JsonRpcResponse>

Route a parsed JSON-RPC request under the default anonymous context.

Convenience for transports without authentication (e.g. stdio). See Self::handle_request_with_context for the authenticated path.

Source

pub async fn handle_request_with_context( &self, request: JsonRpcRequest, ctx: &ToolContext, ) -> Option<JsonRpcResponse>

Route a parsed JSON-RPC request, dispatching tool calls under the given per-call ToolContext (resolved by the transport’s auth hook).

Performs era detection on each request: one carrying modern per-request _meta (revision 2026-07-28) is served statelessly via Self::process_modern; otherwise it follows the legacy initialize/session path. Returns None for notifications (no id).

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for McpServer<S>

§

impl<S> !UnwindSafe for McpServer<S>

§

impl<S> Freeze for McpServer<S>
where S: ?Sized,

§

impl<S> Send for McpServer<S>
where S: ?Sized,

§

impl<S> Sync for McpServer<S>
where S: ?Sized,

§

impl<S> Unpin for McpServer<S>
where S: ?Sized,

§

impl<S> UnsafeUnpin for McpServer<S>
where S: ?Sized,

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<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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