pub struct McpServer<S>{ /* 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>
impl<S> McpServer<S>
Sourcepub fn new(
name: impl Into<String>,
version: impl Into<String>,
tools: ToolRegistry<S>,
state: Arc<S>,
) -> McpServer<S>
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.
Sourcepub fn with_capabilities(self, capabilities: ServerCapabilities) -> McpServer<S>
pub fn with_capabilities(self, capabilities: ServerCapabilities) -> McpServer<S>
Override the capabilities advertised in initialize and server/discover.
Sourcepub fn with_instructions(self, instructions: impl Into<String>) -> McpServer<S>
pub fn with_instructions(self, instructions: impl Into<String>) -> McpServer<S>
Set the natural-language instructions advertised to clients.
Sourcepub fn with_supported_versions(self, versions: Vec<String>) -> McpServer<S>
pub fn with_supported_versions(self, versions: Vec<String>) -> McpServer<S>
Override the protocol revisions the server advertises and accepts, in preference order.
Sourcepub fn with_auth_hook(self, auth_hook: Arc<dyn AuthHook<S>>) -> McpServer<S>
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.
Sourcepub fn with_allowed_origins(self, origins: Vec<String>) -> McpServer<S>
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.
Sourcepub fn allowed_origins(&self) -> &[String]
pub fn allowed_origins(&self) -> &[String]
The Origin allowlist the HTTP transport enforces.
Sourcepub fn with_tool_dispatcher(
self,
dispatcher: Arc<dyn ToolDispatcher<S>>,
) -> McpServer<S>
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.
Sourcepub fn with_method_handler(
self,
handler: Arc<dyn MethodHandler<S>>,
) -> McpServer<S>
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.
Sourcepub async fn authenticate(
&self,
request: &JsonRpcRequest,
) -> Result<ToolContext, AuthError>
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.
Sourcepub async fn handle_raw(&self, raw: &str) -> Option<JsonRpcResponse>
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.
Sourcepub async fn handle_request(
&self,
request: JsonRpcRequest,
) -> Option<JsonRpcResponse>
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.
Sourcepub async fn handle_request_with_context(
&self,
request: JsonRpcRequest,
ctx: &ToolContext,
) -> Option<JsonRpcResponse>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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