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