pub struct McpServer { /* private fields */ }Expand description
An HTTP server that implements the MCP 2024-11-05 protocol.
Register tools, resources, and prompts with the builder methods, then pass
the server to [Server::run] (or [Server::run_tls]) as an Application.
Requests that do not match the MCP endpoint fall through to the built-in
App controller chain.
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 McpServer. The default MCP endpoint is POST /mcp.
Sourcepub fn at(self, path: impl Into<String>) -> Self
pub fn at(self, path: impl Into<String>) -> Self
Override the HTTP path for the MCP endpoint (default "/mcp").
Sourcepub fn tool<F>(
self,
name: &str,
description: &str,
input_schema: &str,
handler: F,
) -> Self
pub fn tool<F>( self, name: &str, description: &str, input_schema: &str, handler: F, ) -> Self
Register a callable tool.
name— tool identifier (snake_case recommended)description— human-readable description shown to the AIinput_schema— JSON Schema object for the tool’s argumentshandler— closure receiving the rawargumentsJSON string
The handler returns McpContent on success or an error string. An
error is returned to the client as isError: true (not a protocol error).
Sourcepub fn resource<F>(
self,
uri_template: &str,
name: &str,
description: &str,
handler: F,
) -> Self
pub fn resource<F>( self, uri_template: &str, name: &str, description: &str, handler: F, ) -> Self
Register a readable resource.
uri_template uses {param} placeholders, e.g. "user://{id}".
The handler receives the full concrete URI string.
Sourcepub fn prompt<F>(self, name: &str, description: &str, handler: F) -> Self
pub fn prompt<F>(self, name: &str, description: &str, handler: F) -> Self
Register a prompt template.
The handler receives the raw arguments JSON string and returns a
list of PromptMessage values.
Sourcepub fn prompt_with_args<F>(
self,
name: &str,
description: &str,
args: Vec<PromptArgDef>,
handler: F,
) -> Self
pub fn prompt_with_args<F>( self, name: &str, description: &str, args: Vec<PromptArgDef>, handler: F, ) -> Self
Register a prompt template with explicit argument definitions.
Sourcepub fn handle_request(&self, body: &str) -> Response
pub fn handle_request(&self, body: &str) -> Response
Process a raw JSON-RPC body and return an HTTP response.