Skip to main content

rust_mcp_sdk/mcp_traits/
mcp_handler.rs

1use async_trait::async_trait;
2
3#[cfg(feature = "server")]
4use rust_mcp_schema::schema_utils::{ClientJsonrpcNotification, ClientJsonrpcRequest};
5
6#[cfg(feature = "client")]
7use rust_mcp_schema::schema_utils::ServerJsonrpcRequest;
8
9#[cfg(feature = "server")]
10use crate::schema::ServerResult;
11
12#[cfg(feature = "client")]
13use crate::schema::schema_utils::{NotificationFromServer, ResultFromClient};
14
15use crate::error::SdkResult;
16use crate::schema::RpcError;
17#[cfg(feature = "server")]
18use std::sync::Arc;
19
20#[cfg(feature = "client")]
21use super::mcp_client::McpClient;
22#[cfg(feature = "server")]
23use super::mcp_server::McpServer;
24
25#[cfg(feature = "server")]
26#[async_trait]
27pub trait McpServerHandler: Send + Sync {
28    async fn handle_request(
29        &self,
30        client_jsonrpc_request: ClientJsonrpcRequest,
31        runtime: Arc<dyn McpServer>,
32    ) -> std::result::Result<ServerResult, RpcError>;
33    async fn handle_error(
34        &self,
35        jsonrpc_error: &RpcError,
36        runtime: Arc<dyn McpServer>,
37    ) -> SdkResult<()>;
38    async fn handle_notification(
39        &self,
40        client_jsonrpc_notification: ClientJsonrpcNotification,
41        runtime: Arc<dyn McpServer>,
42    ) -> SdkResult<()>;
43
44    /// SEP-2243 custom-header validation: returns the `x-mcp-header`
45    /// annotations of the given tool known to this handler, so the HTTP
46    /// layer can validate `Mcp-Param-*` headers against the request body.
47    ///
48    /// Defaults to empty (no annotated tools). Override this in the
49    /// user-facing `ServerHandler` trait; the internal implementation
50    /// delegates to it via `ServerRuntimeInternalHandler`.
51    fn tool_header_annotations(
52        &self,
53        _tool_name: &str,
54    ) -> Vec<crate::tool_param_headers::ToolParamHeader> {
55        Vec::new()
56    }
57}
58
59// Custom trait for converting ServerHandler
60#[cfg(feature = "server")]
61pub trait ToMcpServerHandler {
62    fn to_mcp_server_handler(self) -> Arc<dyn McpServerHandler + 'static>;
63}
64
65// Custom trait for converting ServerHandlerCore
66#[cfg(feature = "server")]
67pub trait ToMcpServerHandlerCore {
68    fn to_mcp_server_handler(self) -> Arc<dyn McpServerHandler + 'static>;
69}
70
71#[cfg(feature = "client")]
72#[async_trait]
73pub trait McpClientHandler: Send + Sync {
74    async fn handle_request(
75        &self,
76        server_jsonrpc_request: ServerJsonrpcRequest,
77        runtime: &dyn McpClient,
78    ) -> std::result::Result<ResultFromClient, RpcError>;
79    async fn handle_error(
80        &self,
81        jsonrpc_error: &RpcError,
82        runtime: &dyn McpClient,
83    ) -> SdkResult<()>;
84    async fn handle_notification(
85        &self,
86        server_jsonrpc_notification: NotificationFromServer,
87        runtime: &dyn McpClient,
88    ) -> SdkResult<()>;
89
90    async fn handle_process_error(
91        &self,
92        error_message: String,
93        runtime: &dyn McpClient,
94    ) -> SdkResult<()>;
95}
96
97// Custom trait for converting ClientHandler
98#[cfg(feature = "client")]
99pub trait ToMcpClientHandler {
100    fn to_mcp_client_handler(self) -> Box<dyn McpClientHandler + 'static>;
101}
102
103// Custom trait for converting ClientHandlerCore
104#[cfg(feature = "client")]
105pub trait ToMcpClientHandlerCore {
106    fn to_mcp_client_handler(self) -> Box<dyn McpClientHandler + 'static>;
107}