Skip to main content

McpServer

Struct McpServer 

Source
pub struct McpServer { /* private fields */ }
Expand description

An MCP server that manages tools, resources, prompts, and dispatches calls.

Implementations§

Source§

impl McpServer

Source

pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self

Create a new MCP server with no authentication.

Source

pub fn with_bearer_auth(self, token: impl Into<String>) -> Self

Require bearer token authentication for all requests.

Source

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.

Source

pub fn check_auth(&self, authorization_header: &str) -> bool

Validate an Authorization header value. Always returns true when no auth is configured.

Source

pub fn auth_enabled(&self) -> bool

Returns true if bearer authentication is enabled on this server.

Source

pub fn register_tool(&mut self, tool: ToolDescription)

Register a tool with the server.

Source

pub fn register_resource(&mut self, resource: ResourceDescription)

Register a resource with the server.

Source

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.

Source

pub fn set_async_handler<F, Fut>(&mut self, tool_name: &str, handler: F)
where F: Fn(Value) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Value>> + Send,

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.

Source

pub fn name(&self) -> &str

Get the server name.

Source

pub fn version(&self) -> &str

Get the server version.

Source

pub fn tools(&self) -> &[ToolDescription]

List all registered tools.

Source

pub fn resources(&self) -> &[ResourceDescription]

List all registered resources.

Source

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.

Source

pub fn read_resource(&self, uri: &str, params: Value) -> Result<Value>

Read a resource by URI with the given parameters.

Source

pub fn register_prompt(&mut self, prompt: PromptDescription)

Register a prompt with the server.

Source

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": [...] }.

Source

pub fn prompts(&self) -> &[PromptDescription]

List all registered prompts.

Source

pub fn get_prompt(&self, name: &str, params: Value) -> Result<Value>

Render a prompt by name with the given arguments.

Source

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

Source

pub fn call_tool(&self, name: &str, params: Value) -> Result<Value>

Call a tool by name with the given parameters.

Source

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

Source

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

Source

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

Source

pub fn format_notification(&self, notification: McpNotification) -> String

Format a notification as a JSON-RPC message string.

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

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

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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