rust_mcp_sdk/mcp_handlers/
mcp_server_handler.rs1use crate::{
2 mcp_server::server_runtime::ServerRuntimeInternalHandler,
3 mcp_traits::{McpServerHandler, ToMcpServerHandler},
4 schema::{
5 schema_utils::{CallToolError, CustomNotification, CustomRequest},
6 *,
7 },
8 task_store::ServerTaskCreator,
9};
10use async_trait::async_trait;
11use std::sync::Arc;
12
13use crate::{mcp_traits::McpServer, utils::enforce_compatible_protocol_version};
14
15#[allow(unused)]
19#[async_trait]
20pub trait ServerHandler: Send + Sync + 'static {
21 async fn on_initialized(&self, runtime: Arc<dyn McpServer>) {}
27
28 async fn handle_initialize_request(
38 &self,
39 params: InitializeRequestParams,
40 runtime: Arc<dyn McpServer>,
41 ) -> std::result::Result<InitializeResult, RpcError> {
42 let mut server_info = runtime.server_info().to_owned();
43 if let Some(updated_protocol_version) = enforce_compatible_protocol_version(
46 ¶ms.protocol_version,
47 &server_info.protocol_version,
48 )
49 .map_err(|err| {
50 tracing::error!(
51 "Incompatible protocol version : client: {} server: {}",
52 ¶ms.protocol_version,
53 &server_info.protocol_version
54 );
55 RpcError::internal_error().with_message(err.to_string())
56 })? {
57 server_info.protocol_version = updated_protocol_version;
58 }
59
60 runtime
61 .set_client_details(params)
62 .await
63 .map_err(|err| RpcError::internal_error().with_message(format!("{err}")))?;
64
65 Ok(server_info)
66 }
67
68 async fn handle_ping_request(
74 &self,
75 _params: Option<RequestParams>,
76 _runtime: Arc<dyn McpServer>,
77 ) -> std::result::Result<Result, RpcError> {
78 Ok(Result::default())
79 }
80
81 async fn handle_list_resources_request(
86 &self,
87 params: Option<PaginatedRequestParams>,
88 runtime: Arc<dyn McpServer>,
89 ) -> std::result::Result<ListResourcesResult, RpcError> {
90 Err(RpcError::method_not_found().with_message(format!(
91 "No handler is implemented for '{}'.",
92 &ListResourcesRequest::method_value(),
93 )))
94 }
95
96 async fn handle_list_resource_templates_request(
101 &self,
102 params: Option<PaginatedRequestParams>,
103 runtime: Arc<dyn McpServer>,
104 ) -> std::result::Result<ListResourceTemplatesResult, RpcError> {
105 Err(RpcError::method_not_found().with_message(format!(
106 "No handler is implemented for '{}'.",
107 &ListResourceTemplatesRequest::method_value(),
108 )))
109 }
110
111 async fn handle_read_resource_request(
116 &self,
117 params: ReadResourceRequestParams,
118 runtime: Arc<dyn McpServer>,
119 ) -> std::result::Result<ReadResourceResult, RpcError> {
120 Err(RpcError::method_not_found().with_message(format!(
121 "No handler is implemented for '{}'.",
122 &ReadResourceRequest::method_value(),
123 )))
124 }
125
126 async fn handle_subscribe_request(
131 &self,
132 params: SubscribeRequestParams,
133 runtime: Arc<dyn McpServer>,
134 ) -> std::result::Result<Result, RpcError> {
135 Err(RpcError::method_not_found().with_message(format!(
136 "No handler is implemented for '{}'.",
137 &SubscribeRequest::method_value(),
138 )))
139 }
140
141 async fn handle_unsubscribe_request(
146 &self,
147 params: UnsubscribeRequestParams,
148 runtime: Arc<dyn McpServer>,
149 ) -> std::result::Result<Result, RpcError> {
150 Err(RpcError::method_not_found().with_message(format!(
151 "No handler is implemented for '{}'.",
152 &UnsubscribeRequest::method_value(),
153 )))
154 }
155
156 async fn handle_list_prompts_request(
161 &self,
162 params: Option<PaginatedRequestParams>,
163 runtime: Arc<dyn McpServer>,
164 ) -> std::result::Result<ListPromptsResult, RpcError> {
165 Err(RpcError::method_not_found().with_message(format!(
166 "No handler is implemented for '{}'.",
167 &ListPromptsRequest::method_value(),
168 )))
169 }
170
171 async fn handle_get_prompt_request(
176 &self,
177 params: GetPromptRequestParams,
178 runtime: Arc<dyn McpServer>,
179 ) -> std::result::Result<GetPromptResult, RpcError> {
180 Err(RpcError::method_not_found().with_message(format!(
181 "No handler is implemented for '{}'.",
182 &GetPromptRequest::method_value(),
183 )))
184 }
185
186 async fn handle_list_tools_request(
191 &self,
192 params: Option<PaginatedRequestParams>,
193 runtime: Arc<dyn McpServer>,
194 ) -> std::result::Result<ListToolsResult, RpcError> {
195 Err(RpcError::method_not_found().with_message(format!(
196 "No handler is implemented for '{}'.",
197 &ListToolsRequest::method_value(),
198 )))
199 }
200
201 async fn handle_task_augmented_tool_call(
206 &self,
207 params: CallToolRequestParams,
208 task_creator: ServerTaskCreator,
209 runtime: Arc<dyn McpServer>,
210 ) -> std::result::Result<CreateTaskResult, CallToolError> {
211 if !runtime.capabilities().can_run_task_augmented_tools() {
212 return Err(CallToolError::unsupported_task_augmented_tool_call());
213 }
214 Err(CallToolError::from_message(
215 "No handler is implemented for 'task augmented tool call'.",
216 ))
217 }
218
219 async fn handle_call_tool_request(
224 &self,
225 params: CallToolRequestParams,
226 runtime: Arc<dyn McpServer>,
227 ) -> std::result::Result<CallToolResult, CallToolError> {
228 Ok(CallToolError::unknown_tool(format!("Unknown tool: {}", params.name)).into())
229 }
230
231 async fn handle_set_level_request(
236 &self,
237 params: SetLevelRequestParams,
238 runtime: Arc<dyn McpServer>,
239 ) -> std::result::Result<Result, RpcError> {
240 Err(RpcError::method_not_found().with_message(format!(
241 "No handler is implemented for '{}'.",
242 &SetLevelRequest::method_value(),
243 )))
244 }
245
246 async fn handle_complete_request(
251 &self,
252 params: CompleteRequestParams,
253 runtime: Arc<dyn McpServer>,
254 ) -> std::result::Result<CompleteResult, RpcError> {
255 Err(RpcError::method_not_found().with_message(format!(
256 "No handler is implemented for '{}'.",
257 &CompleteRequest::method_value(),
258 )))
259 }
260
261 async fn handle_get_task_request(
263 &self,
264 params: GetTaskParams,
265 runtime: Arc<dyn McpServer>,
266 ) -> std::result::Result<CompleteResult, RpcError> {
267 Err(RpcError::method_not_found().with_message(format!(
268 "No handler is implemented for '{}'.",
269 &GetTaskRequest::method_value(),
270 )))
271 }
272
273 async fn handle_get_task_payload_request(
275 &self,
276 params: GetTaskPayloadParams,
277 runtime: Arc<dyn McpServer>,
278 ) -> std::result::Result<CompleteResult, RpcError> {
279 Err(RpcError::method_not_found().with_message(format!(
280 "No handler is implemented for '{}'.",
281 &GetTaskPayloadRequest::method_value(),
282 )))
283 }
284
285 async fn handle_cancel_task_request(
287 &self,
288 params: CancelTaskParams,
289 runtime: Arc<dyn McpServer>,
290 ) -> std::result::Result<CompleteResult, RpcError> {
291 Err(RpcError::method_not_found().with_message(format!(
292 "No handler is implemented for '{}'.",
293 &CancelTaskRequest::method_value(),
294 )))
295 }
296
297 async fn handle_list_task_request(
299 &self,
300 params: Option<PaginatedRequestParams>,
301 runtime: Arc<dyn McpServer>,
302 ) -> std::result::Result<CompleteResult, RpcError> {
303 Err(RpcError::method_not_found().with_message(format!(
304 "No handler is implemented for '{}'.",
305 &ListTasksRequest::method_value(),
306 )))
307 }
308
309 async fn handle_custom_request(
314 &self,
315 request: CustomRequest,
316 runtime: Arc<dyn McpServer>,
317 ) -> std::result::Result<GenericResult, RpcError> {
318 Err(RpcError::method_not_found()
319 .with_message("No handler is implemented for custom requests.".to_string()))
320 }
321
322 async fn handle_initialized_notification(
327 &self,
328 params: Option<NotificationParams>,
329 runtime: Arc<dyn McpServer>,
330 ) -> std::result::Result<(), RpcError> {
331 Ok(())
332 }
333
334 async fn handle_cancelled_notification(
337 &self,
338 params: CancelledNotificationParams,
339 runtime: Arc<dyn McpServer>,
340 ) -> std::result::Result<(), RpcError> {
341 Ok(())
342 }
343
344 async fn handle_progress_notification(
347 &self,
348 params: ProgressNotificationParams,
349 runtime: Arc<dyn McpServer>,
350 ) -> std::result::Result<(), RpcError> {
351 Ok(())
352 }
353
354 async fn handle_roots_list_changed_notification(
357 &self,
358 params: Option<NotificationParams>,
359 runtime: Arc<dyn McpServer>,
360 ) -> std::result::Result<(), RpcError> {
361 Ok(())
362 }
363
364 async fn handle_task_status_notification(
366 &self,
367 params: TaskStatusNotificationParams,
368 runtime: Arc<dyn McpServer>,
369 ) -> std::result::Result<(), RpcError> {
370 Ok(())
371 }
372
373 async fn handle_custom_notification(
376 &self,
377 notification: CustomNotification,
378 ) -> std::result::Result<(), RpcError> {
379 Ok(())
380 }
381
382 async fn handle_error(
391 &self,
392 error: &RpcError,
393 runtime: Arc<dyn McpServer>,
394 ) -> std::result::Result<(), RpcError> {
395 Ok(())
396 }
397}
398
399impl<T: ServerHandler + 'static> ToMcpServerHandler for T {
400 fn to_mcp_server_handler(self) -> Arc<dyn McpServerHandler + 'static> {
401 Arc::new(ServerRuntimeInternalHandler::new(Box::new(self)))
402 }
403}