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
impl McpServer
Sourcepub fn new(config: McpServerConfig) -> (Arc<Self>, McpServerChannels)
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.
Sourcepub fn status(&self) -> ServerStatus
pub fn status(&self) -> ServerStatus
Returns the current server status.
Sourcepub async fn fault_reason(&self) -> Option<String>
pub async fn fault_reason(&self) -> Option<String>
Returns the fault reason if the server is in a faulted state.
Sourcepub fn server_info(&self) -> McpServerInfo
pub fn server_info(&self) -> McpServerInfo
Returns server info for MCP protocol.
Sourcepub async fn send_request(
&self,
method: impl Into<String>,
params: Option<Value>,
timeout: Duration,
) -> Result<JsonRpcResponse, ServerError>
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.
Sourcepub async fn send_request_default_timeout(
&self,
method: impl Into<String>,
params: Option<Value>,
) -> Result<JsonRpcResponse, ServerError>
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.
Sourcepub async fn send_notification(
&self,
method: impl Into<String>,
params: Option<Value>,
) -> Result<(), ServerError>
pub async fn send_notification( &self, method: impl Into<String>, params: Option<Value>, ) -> Result<(), ServerError>
Sends a notification to the client (no response expected).
Sourcepub async fn send_progress(
&self,
token: impl Into<String>,
progress: f64,
message: Option<String>,
) -> Result<(), ServerError>
pub async fn send_progress( &self, token: impl Into<String>, progress: f64, message: Option<String>, ) -> Result<(), ServerError>
Sends a progress notification.
Sourcepub async fn send_log(
&self,
level: &str,
message: impl Into<String>,
logger: Option<&str>,
data: Option<Value>,
) -> Result<(), ServerError>
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.
Sourcepub async fn call_tool(&self, name: &str, args: Value) -> ToolCallResult
pub async fn call_tool(&self, name: &str, args: Value) -> ToolCallResult
Calls a tool directly by name (for embedded usage).
Sourcepub fn list_tools(&self) -> Vec<McpToolDefinition>
pub fn list_tools(&self) -> Vec<McpToolDefinition>
Lists all available tools.
Sourcepub fn registry(&self) -> &ToolRegistry
pub fn registry(&self) -> &ToolRegistry
Gets the tool registry.