pub struct Server { /* private fields */ }Expand description
A built MCP server. Cheap to clone; all clones share one registry.
Server implements tower_service::Service<McpRequest>, so tower
middleware (timeouts, rate limits, tracing) can wrap it.
Implementations§
Source§impl Server
impl Server
Sourcepub fn legacy_clients_enabled(&self) -> bool
pub fn legacy_clients_enabled(&self) -> bool
Whether legacy (2025-11-25, 2025-06-18) clients are served.
Source§impl Server
impl Server
Sourcepub fn builder(
name: impl Into<String>,
version: impl Into<String>,
) -> ServerBuilder
pub fn builder( name: impl Into<String>, version: impl Into<String>, ) -> ServerBuilder
Starts building a server.
Sourcepub fn mirrored_params(&self, tool: &str) -> Option<&[MirroredParam]>
pub fn mirrored_params(&self, tool: &str) -> Option<&[MirroredParam]>
Parameters of tool mirrored into Mcp-Param-* HTTP headers, or
None if the tool does not exist.
Sourcepub fn supported_versions(&self) -> &[String]
pub fn supported_versions(&self) -> &[String]
The protocol versions this server accepts.
Sourcepub async fn handle_bytes(
&self,
bytes: &[u8],
transport: TransportContext,
cancellation: CancellationToken,
) -> Option<Response>
pub async fn handle_bytes( &self, bytes: &[u8], transport: TransportContext, cancellation: CancellationToken, ) -> Option<Response>
Parses raw bytes and handles the message. Returns the response to
send, or None for notifications, cancelled requests and stray
responses.
Sourcepub async fn handle(&self, request: McpRequest) -> Option<Response>
pub async fn handle(&self, request: McpRequest) -> Option<Response>
Handles a parsed message.
Source§impl Server
impl Server
Sourcepub async fn serve_http(
self,
addr: impl ToSocketAddrs + Clone + Debug,
) -> Result<()>
pub async fn serve_http( self, addr: impl ToSocketAddrs + Clone + Debug, ) -> Result<()>
Serves Streamable HTTP at path /mcp with the default security
settings (localhost origins and hosts only) until the process stops.
Bind to a loopback address (e.g. 127.0.0.1:8000) unless the server
is meant to be reachable from other machines.
§Errors
Returns an error when the address cannot be bound.
Source§impl Server
impl Server
Sourcepub async fn serve_stdio(self) -> Result<()>
pub async fn serve_stdio(self) -> Result<()>
Serves requests over the process’s stdin and stdout until stdin is closed.
When stdin is an interactive terminal (someone ran the server by hand), a short explanation is printed on stderr: an MCP server waits silently for JSON-RPC messages, which looks like a hang.
§Errors
Returns an error when reading stdin or writing stdout fails.