Skip to main content

McpServer

Struct McpServer 

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

MCP Server with bidirectional async communication.

The server uses internal channels for message passing and spawns an internal task to handle the message loop. Transport servers wrap this core and bridge the channels to actual I/O.

§Example

use mcp::server::{McpServer, McpServerConfig};

let config = McpServerConfig::builder()
    .name("my-server")
    .version("1.0.0")
    .with_tool(MyTool)
    .build();

let (server, channels) = McpServer::new(config);

// Server is now running, use channels to communicate
channels.inbound_tx.send(ClientInbound::Request(request)).await?;
let outbound = channels.outbound_rx.recv().await;

Implementations§

Source§

impl McpServer

Source

pub fn new(config: McpServerConfig) -> (Arc<Self>, McpServerChannels)

Creates a new MCP server and starts the message loop.

Returns the server instance and channel handles for communication. The internal message loop task is spawned automatically.

Source

pub fn status(&self) -> ServerStatus

Returns the current server status.

Source

pub async fn fault_reason(&self) -> Option<String>

Returns the fault reason if the server is in a faulted state.

Source

pub fn stop(&self)

Stops the server gracefully.

Source

pub fn name(&self) -> &str

Returns the server name.

Source

pub fn version(&self) -> &str

Returns the server version.

Source

pub fn uptime(&self) -> Duration

Returns the server uptime.

Source

pub fn server_info(&self) -> McpServerInfo

Returns server info for MCP protocol.

Source

pub async fn send_request( &self, method: impl Into<String>, params: Option<Value>, timeout: Duration, ) -> Result<JsonRpcResponse, ServerError>

Sends a request to the client and waits for the response.

This is used for server-to-client requests like sampling or elicitation. Times out after the specified duration to prevent resource leaks.

Source

pub async fn send_request_default_timeout( &self, method: impl Into<String>, params: Option<Value>, ) -> Result<JsonRpcResponse, ServerError>

Sends a request with a default 30-second timeout.

Source

pub async fn send_notification( &self, method: impl Into<String>, params: Option<Value>, ) -> Result<(), ServerError>

Sends a notification to the client (no response expected).

Source

pub async fn send_progress( &self, token: impl Into<String>, progress: f64, message: Option<String>, ) -> Result<(), ServerError>

Sends a progress notification.

Source

pub async fn send_log( &self, level: &str, message: impl Into<String>, logger: Option<&str>, data: Option<Value>, ) -> Result<(), ServerError>

Sends a log message notification.

Source

pub async fn call_tool(&self, name: &str, args: Value) -> ToolCallResult

Calls a tool directly by name (for embedded usage).

Source

pub fn list_tools(&self) -> Vec<McpToolDefinition>

Lists all available tools.

Source

pub fn registry(&self) -> &ToolRegistry

Gets the tool registry.

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

Source§

type Output = T

Should always be Self
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