rust_mcp_sdk/mcp_traits/
mcp_handler.rs1use async_trait::async_trait;
2
3use rust_mcp_schema::schema_utils::{
4 ClientJsonrpcNotification, ClientJsonrpcRequest, ServerJsonrpcRequest,
5};
6
7#[cfg(feature = "server")]
8use crate::schema::schema_utils::ResultFromServer;
9
10#[cfg(feature = "client")]
11use crate::schema::schema_utils::{NotificationFromServer, ResultFromClient};
12
13use crate::error::SdkResult;
14use crate::schema::RpcError;
15use std::sync::Arc;
16
17#[cfg(feature = "client")]
18use super::mcp_client::McpClient;
19#[cfg(feature = "server")]
20use super::mcp_server::McpServer;
21
22#[cfg(feature = "server")]
23#[async_trait]
24pub trait McpServerHandler: Send + Sync {
25 async fn handle_request(
26 &self,
27 client_jsonrpc_request: ClientJsonrpcRequest,
28 runtime: Arc<dyn McpServer>,
29 ) -> std::result::Result<ResultFromServer, RpcError>;
30 async fn handle_error(
31 &self,
32 jsonrpc_error: &RpcError,
33 runtime: Arc<dyn McpServer>,
34 ) -> SdkResult<()>;
35 async fn handle_notification(
36 &self,
37 client_jsonrpc_notification: ClientJsonrpcNotification,
38 runtime: Arc<dyn McpServer>,
39 ) -> SdkResult<()>;
40}
41
42#[cfg(feature = "server")]
44pub trait ToMcpServerHandler {
45 fn to_mcp_server_handler(self) -> Arc<dyn McpServerHandler + 'static>;
46}
47
48#[cfg(feature = "server")]
50pub trait ToMcpServerHandlerCore {
51 fn to_mcp_server_handler(self) -> Arc<dyn McpServerHandler + 'static>;
52}
53
54#[cfg(feature = "client")]
55#[async_trait]
56pub trait McpClientHandler: Send + Sync {
57 async fn handle_request(
58 &self,
59 server_jsonrpc_request: ServerJsonrpcRequest,
60 runtime: &dyn McpClient,
61 ) -> std::result::Result<ResultFromClient, RpcError>;
62 async fn handle_error(
63 &self,
64 jsonrpc_error: &RpcError,
65 runtime: &dyn McpClient,
66 ) -> SdkResult<()>;
67 async fn handle_notification(
68 &self,
69 server_jsonrpc_notification: NotificationFromServer,
70 runtime: &dyn McpClient,
71 ) -> SdkResult<()>;
72
73 async fn handle_process_error(
74 &self,
75 error_message: String,
76 runtime: &dyn McpClient,
77 ) -> SdkResult<()>;
78}
79
80#[cfg(feature = "client")]
82pub trait ToMcpClientHandler {
83 fn to_mcp_client_handler(self) -> Box<dyn McpClientHandler + 'static>;
84}
85
86#[cfg(feature = "client")]
88pub trait ToMcpClientHandlerCore {
89 fn to_mcp_client_handler(self) -> Box<dyn McpClientHandler + 'static>;
90}