pub struct McpServer { /* private fields */ }Expand description
An MCP server that manages tools, resources, prompts, and dispatches calls.
Implementations§
Source§impl McpServer
impl McpServer
Sourcepub fn new(name: impl Into<String>, version: impl Into<String>) -> Self
pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self
Create a new MCP server with no authentication.
Sourcepub fn with_bearer_auth(self, token: impl Into<String>) -> Self
pub fn with_bearer_auth(self, token: impl Into<String>) -> Self
Require bearer token authentication for all requests.
Sourcepub fn with_generated_auth(self) -> (Self, String)
pub fn with_generated_auth(self) -> (Self, String)
Generate and attach a random bearer token.
Returns the generated token so the caller can distribute it.
Sourcepub fn check_auth(&self, authorization_header: &str) -> bool
pub fn check_auth(&self, authorization_header: &str) -> bool
Validate an Authorization header value. Always returns true when no auth is configured.
Sourcepub fn auth_enabled(&self) -> bool
pub fn auth_enabled(&self) -> bool
Returns true if bearer authentication is enabled on this server.
Sourcepub fn register_tool(&mut self, tool: ToolDescription)
pub fn register_tool(&mut self, tool: ToolDescription)
Register a tool with the server.
Sourcepub fn register_resource(&mut self, resource: ResourceDescription)
pub fn register_resource(&mut self, resource: ResourceDescription)
Register a resource with the server.
Sourcepub fn set_handler(
&mut self,
tool_name: &str,
handler: impl Fn(Value) -> Result<Value> + Send + Sync + 'static,
)
pub fn set_handler( &mut self, tool_name: &str, handler: impl Fn(Value) -> Result<Value> + Send + Sync + 'static, )
Set the handler for a tool by name.
Sourcepub fn set_async_handler<F, Fut>(&mut self, tool_name: &str, handler: F)
pub fn set_async_handler<F, Fut>(&mut self, tool_name: &str, handler: F)
Register an async handler for a tool by name.
handler is a closure returning a future (typically async move { … }).
Async handlers take precedence over sync handlers registered with
Self::set_handler when resolved via Self::call_tool_async.
Sourcepub fn tools(&self) -> &[ToolDescription]
pub fn tools(&self) -> &[ToolDescription]
List all registered tools.
Sourcepub fn resources(&self) -> &[ResourceDescription]
pub fn resources(&self) -> &[ResourceDescription]
List all registered resources.
Sourcepub fn set_resource_handler(
&mut self,
uri: &str,
handler: impl Fn(Value) -> Result<Value> + Send + Sync + 'static,
)
pub fn set_resource_handler( &mut self, uri: &str, handler: impl Fn(Value) -> Result<Value> + Send + Sync + 'static, )
Set the handler for a resource by URI.
Sourcepub fn read_resource(&self, uri: &str, params: Value) -> Result<Value>
pub fn read_resource(&self, uri: &str, params: Value) -> Result<Value>
Read a resource by URI with the given parameters.
Sourcepub fn register_prompt(&mut self, prompt: PromptDescription)
pub fn register_prompt(&mut self, prompt: PromptDescription)
Register a prompt with the server.
Sourcepub fn set_prompt_handler(
&mut self,
prompt_name: &str,
handler: impl Fn(Value) -> Result<Value> + Send + Sync + 'static,
)
pub fn set_prompt_handler( &mut self, prompt_name: &str, handler: impl Fn(Value) -> Result<Value> + Send + Sync + 'static, )
Set the handler for a prompt by name.
The handler receives the prompts/get arguments object and returns the
result value — typically { "description": ..., "messages": [...] }.
Sourcepub fn prompts(&self) -> &[PromptDescription]
pub fn prompts(&self) -> &[PromptDescription]
List all registered prompts.
Sourcepub fn get_prompt(&self, name: &str, params: Value) -> Result<Value>
pub fn get_prompt(&self, name: &str, params: Value) -> Result<Value>
Render a prompt by name with the given arguments.
Sourcepub fn has_tool(&self, name: &str) -> bool
pub fn has_tool(&self, name: &str) -> bool
Whether a tool with name is registered (has a sync or async handler).
Lets a transport distinguish an unknown tool (a protocol-level invalid
params error) from a tool that ran and failed (reported in-band with
isError: true).
Sourcepub fn call_tool(&self, name: &str, params: Value) -> Result<Value>
pub fn call_tool(&self, name: &str, params: Value) -> Result<Value>
Call a tool by name with the given parameters.
Sourcepub async fn call_tool_async(&self, name: &str, params: Value) -> Result<Value>
pub async fn call_tool_async(&self, name: &str, params: Value) -> Result<Value>
Call a tool by name, awaiting an async handler if one is registered and otherwise falling back to the synchronous handler. Errors if the tool is unknown. This is the entry point used by async transports (e.g. HTTP/SSE).
Sourcepub fn negotiate_protocol_version(
&self,
requested: Option<&str>,
) -> &'static str
pub fn negotiate_protocol_version( &self, requested: Option<&str>, ) -> &'static str
Resolve the protocol version to report in initialize.
Echoes requested when it is one of SUPPORTED_PROTOCOL_VERSIONS;
otherwise returns LATEST_PROTOCOL_VERSION (per the MCP spec, the
server proposes its own latest when it cannot honor the client’s).
Sourcepub fn initialize_response(&self, requested_version: Option<&str>) -> Value
pub fn initialize_response(&self, requested_version: Option<&str>) -> Value
Build the initialize response, negotiating the protocol version against
the client’s requested version.
The advertised capabilities reflect what the server actually supports:
tools and resources are always present; prompts is included only
when at least one prompt is registered.
Source§impl McpServer
impl McpServer
Sourcepub fn format_notification(&self, notification: McpNotification) -> String
pub fn format_notification(&self, notification: McpNotification) -> String
Format a notification as a JSON-RPC message string.
Auto Trait Implementations§
impl !RefUnwindSafe for McpServer
impl !UnwindSafe for McpServer
impl Freeze for McpServer
impl Send for McpServer
impl Sync for McpServer
impl Unpin for McpServer
impl UnsafeUnpin for McpServer
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.