rust_mcp_sdk/mcp_handlers/
mcp_client_handler.rs

1use crate::schema::{
2    CancelledNotification, CreateMessageRequest, CreateMessageResult, ListRootsRequest,
3    ListRootsResult, LoggingMessageNotification, PingRequest, ProgressNotification,
4    PromptListChangedNotification, ResourceListChangedNotification, ResourceUpdatedNotification,
5    Result, RpcError, ToolListChangedNotification,
6};
7use async_trait::async_trait;
8use serde_json::Value;
9
10use crate::mcp_traits::mcp_client::McpClient;
11
12/// Defines the `ClientHandler` trait for handling Model Context Protocol (MCP) operations on a client.
13/// This trait provides default implementations for request and notification handlers in an MCP client,
14/// allowing developers to override methods for custom behavior.
15#[allow(unused)]
16#[async_trait]
17pub trait ClientHandler: Send + Sync + 'static {
18    //**********************//
19    //** Request Handlers **//
20    //**********************//
21    async fn handle_ping_request(
22        &self,
23        request: PingRequest,
24        runtime: &dyn McpClient,
25    ) -> std::result::Result<Result, RpcError> {
26        Ok(Result::default())
27    }
28
29    async fn handle_create_message_request(
30        &self,
31        request: CreateMessageRequest,
32        runtime: &dyn McpClient,
33    ) -> std::result::Result<CreateMessageResult, RpcError> {
34        runtime.assert_client_request_capabilities(request.method())?;
35        Err(RpcError::method_not_found().with_message(format!(
36            "No handler is implemented for '{}'.",
37            request.method(),
38        )))
39    }
40
41    async fn handle_list_roots_request(
42        &self,
43        request: ListRootsRequest,
44        runtime: &dyn McpClient,
45    ) -> std::result::Result<ListRootsResult, RpcError> {
46        runtime.assert_client_request_capabilities(request.method())?;
47        Err(RpcError::method_not_found().with_message(format!(
48            "No handler is implemented for '{}'.",
49            request.method(),
50        )))
51    }
52
53    async fn handle_custom_request(
54        &self,
55        request: Value,
56        runtime: &dyn McpClient,
57    ) -> std::result::Result<ListRootsResult, RpcError> {
58        Err(RpcError::method_not_found()
59            .with_message("No handler is implemented for custom requests.".to_string()))
60    }
61
62    //***************************//
63    //** Notification Handlers **//
64    //***************************//
65
66    async fn handle_cancelled_notification(
67        &self,
68        notification: CancelledNotification,
69        runtime: &dyn McpClient,
70    ) -> std::result::Result<(), RpcError> {
71        Ok(())
72    }
73
74    async fn handle_progress_notification(
75        &self,
76        notification: ProgressNotification,
77        runtime: &dyn McpClient,
78    ) -> std::result::Result<(), RpcError> {
79        Ok(())
80    }
81
82    async fn handle_resource_list_changed_notification(
83        &self,
84        notification: ResourceListChangedNotification,
85        runtime: &dyn McpClient,
86    ) -> std::result::Result<(), RpcError> {
87        Ok(())
88    }
89
90    async fn handle_resource_updated_notification(
91        &self,
92        notification: ResourceUpdatedNotification,
93        runtime: &dyn McpClient,
94    ) -> std::result::Result<(), RpcError> {
95        Ok(())
96    }
97
98    async fn handle_prompt_list_changed_notification(
99        &self,
100        notification: PromptListChangedNotification,
101        runtime: &dyn McpClient,
102    ) -> std::result::Result<(), RpcError> {
103        Ok(())
104    }
105
106    async fn handle_tool_list_changed_notification(
107        &self,
108        notification: ToolListChangedNotification,
109        runtime: &dyn McpClient,
110    ) -> std::result::Result<(), RpcError> {
111        Ok(())
112    }
113
114    async fn handle_logging_message_notification(
115        &self,
116        notification: LoggingMessageNotification,
117        runtime: &dyn McpClient,
118    ) -> std::result::Result<(), RpcError> {
119        Ok(())
120    }
121
122    async fn handle_custom_notification(
123        &self,
124        notification: Value,
125        runtime: &dyn McpClient,
126    ) -> std::result::Result<(), RpcError> {
127        Ok(())
128    }
129
130    //********************//
131    //** Error Handlers **//
132    //********************//
133    async fn handle_error(
134        &self,
135        error: RpcError,
136        runtime: &dyn McpClient,
137    ) -> std::result::Result<(), RpcError> {
138        Ok(())
139    }
140
141    async fn handle_process_error(
142        &self,
143        error_message: String,
144        runtime: &dyn McpClient,
145    ) -> std::result::Result<(), RpcError> {
146        if !runtime.is_shut_down().await {
147            eprintln!("Process error: {}", error_message);
148        }
149        Ok(())
150    }
151}