rust_mcp_sdk/mcp_traits/
mcp_handler.rs

1use async_trait::async_trait;
2
3#[cfg(feature = "server")]
4use rust_mcp_schema::schema_utils::{NotificationFromClient, RequestFromClient, ResultFromServer};
5
6#[cfg(feature = "client")]
7use rust_mcp_schema::schema_utils::{NotificationFromServer, RequestFromServer, ResultFromClient};
8
9use rust_mcp_schema::RpcError;
10
11use crate::error::SdkResult;
12
13#[cfg(feature = "client")]
14use super::mcp_client::McpClient;
15#[cfg(feature = "server")]
16use super::mcp_server::McpServer;
17
18#[cfg(feature = "server")]
19#[async_trait]
20pub trait McpServerHandler: Send + Sync {
21    async fn on_server_started(&self, runtime: &dyn McpServer);
22    async fn handle_request(
23        &self,
24        client_jsonrpc_request: RequestFromClient,
25        runtime: &dyn McpServer,
26    ) -> std::result::Result<ResultFromServer, RpcError>;
27    async fn handle_error(&self, jsonrpc_error: RpcError, runtime: &dyn McpServer)
28        -> SdkResult<()>;
29    async fn handle_notification(
30        &self,
31        client_jsonrpc_notification: NotificationFromClient,
32        runtime: &dyn McpServer,
33    ) -> SdkResult<()>;
34}
35
36#[cfg(feature = "client")]
37#[async_trait]
38pub trait McpClientHandler: Send + Sync {
39    async fn handle_request(
40        &self,
41        server_jsonrpc_request: RequestFromServer,
42        runtime: &dyn McpClient,
43    ) -> std::result::Result<ResultFromClient, RpcError>;
44    async fn handle_error(&self, jsonrpc_error: RpcError, runtime: &dyn McpClient)
45        -> SdkResult<()>;
46    async fn handle_notification(
47        &self,
48        server_jsonrpc_notification: NotificationFromServer,
49        runtime: &dyn McpClient,
50    ) -> SdkResult<()>;
51
52    async fn handle_process_error(
53        &self,
54        error_message: String,
55        runtime: &dyn McpClient,
56    ) -> SdkResult<()>;
57}