11use crate :: schema:: { schema_utils:: CallToolError , * } ;
22use async_trait:: async_trait;
33use serde_json:: Value ;
4+ use std:: sync:: Arc ;
45
56use crate :: { mcp_traits:: mcp_server:: McpServer , utils:: enforce_compatible_protocol_version} ;
67
@@ -15,7 +16,7 @@ pub trait ServerHandler: Send + Sync + 'static {
1516 /// The `runtime` parameter provides access to the server's runtime environment, allowing
1617 /// interaction with the server's capabilities.
1718 /// The default implementation does nothing.
18- async fn on_initialized ( & self , runtime : & dyn McpServer ) { }
19+ async fn on_initialized ( & self , runtime : Arc < dyn McpServer > ) { }
1920
2021 /// Handles the InitializeRequest from a client.
2122 ///
@@ -29,7 +30,7 @@ pub trait ServerHandler: Send + Sync + 'static {
2930 async fn handle_initialize_request (
3031 & self ,
3132 initialize_request : InitializeRequest ,
32- runtime : & dyn McpServer ,
33+ runtime : Arc < dyn McpServer > ,
3334 ) -> std:: result:: Result < InitializeResult , RpcError > {
3435 let mut server_info = runtime. server_info ( ) . to_owned ( ) ;
3536 // Provide compatibility for clients using older MCP protocol versions.
@@ -65,7 +66,7 @@ pub trait ServerHandler: Send + Sync + 'static {
6566 async fn handle_ping_request (
6667 & self ,
6768 _: PingRequest ,
68- _: & dyn McpServer ,
69+ _: Arc < dyn McpServer > ,
6970 ) -> std:: result:: Result < Result , RpcError > {
7071 Ok ( Result :: default ( ) )
7172 }
@@ -77,7 +78,7 @@ pub trait ServerHandler: Send + Sync + 'static {
7778 async fn handle_list_resources_request (
7879 & self ,
7980 request : ListResourcesRequest ,
80- runtime : & dyn McpServer ,
81+ runtime : Arc < dyn McpServer > ,
8182 ) -> std:: result:: Result < ListResourcesResult , RpcError > {
8283 runtime. assert_server_request_capabilities ( request. method ( ) ) ?;
8384 Err ( RpcError :: method_not_found ( ) . with_message ( format ! (
@@ -93,7 +94,7 @@ pub trait ServerHandler: Send + Sync + 'static {
9394 async fn handle_list_resource_templates_request (
9495 & self ,
9596 request : ListResourceTemplatesRequest ,
96- runtime : & dyn McpServer ,
97+ runtime : Arc < dyn McpServer > ,
9798 ) -> std:: result:: Result < ListResourceTemplatesResult , RpcError > {
9899 runtime. assert_server_request_capabilities ( request. method ( ) ) ?;
99100 Err ( RpcError :: method_not_found ( ) . with_message ( format ! (
@@ -109,7 +110,7 @@ pub trait ServerHandler: Send + Sync + 'static {
109110 async fn handle_read_resource_request (
110111 & self ,
111112 request : ReadResourceRequest ,
112- runtime : & dyn McpServer ,
113+ runtime : Arc < dyn McpServer > ,
113114 ) -> std:: result:: Result < ReadResourceResult , RpcError > {
114115 runtime. assert_server_request_capabilities ( request. method ( ) ) ?;
115116 Err ( RpcError :: method_not_found ( ) . with_message ( format ! (
@@ -125,7 +126,7 @@ pub trait ServerHandler: Send + Sync + 'static {
125126 async fn handle_subscribe_request (
126127 & self ,
127128 request : SubscribeRequest ,
128- runtime : & dyn McpServer ,
129+ runtime : Arc < dyn McpServer > ,
129130 ) -> std:: result:: Result < Result , RpcError > {
130131 runtime. assert_server_request_capabilities ( request. method ( ) ) ?;
131132 Err ( RpcError :: method_not_found ( ) . with_message ( format ! (
@@ -141,7 +142,7 @@ pub trait ServerHandler: Send + Sync + 'static {
141142 async fn handle_unsubscribe_request (
142143 & self ,
143144 request : UnsubscribeRequest ,
144- runtime : & dyn McpServer ,
145+ runtime : Arc < dyn McpServer > ,
145146 ) -> std:: result:: Result < Result , RpcError > {
146147 runtime. assert_server_request_capabilities ( request. method ( ) ) ?;
147148 Err ( RpcError :: method_not_found ( ) . with_message ( format ! (
@@ -157,7 +158,7 @@ pub trait ServerHandler: Send + Sync + 'static {
157158 async fn handle_list_prompts_request (
158159 & self ,
159160 request : ListPromptsRequest ,
160- runtime : & dyn McpServer ,
161+ runtime : Arc < dyn McpServer > ,
161162 ) -> std:: result:: Result < ListPromptsResult , RpcError > {
162163 runtime. assert_server_request_capabilities ( request. method ( ) ) ?;
163164 Err ( RpcError :: method_not_found ( ) . with_message ( format ! (
@@ -173,7 +174,7 @@ pub trait ServerHandler: Send + Sync + 'static {
173174 async fn handle_get_prompt_request (
174175 & self ,
175176 request : GetPromptRequest ,
176- runtime : & dyn McpServer ,
177+ runtime : Arc < dyn McpServer > ,
177178 ) -> std:: result:: Result < GetPromptResult , RpcError > {
178179 runtime. assert_server_request_capabilities ( request. method ( ) ) ?;
179180 Err ( RpcError :: method_not_found ( ) . with_message ( format ! (
@@ -189,7 +190,7 @@ pub trait ServerHandler: Send + Sync + 'static {
189190 async fn handle_list_tools_request (
190191 & self ,
191192 request : ListToolsRequest ,
192- runtime : & dyn McpServer ,
193+ runtime : Arc < dyn McpServer > ,
193194 ) -> std:: result:: Result < ListToolsResult , RpcError > {
194195 runtime. assert_server_request_capabilities ( request. method ( ) ) ?;
195196 Err ( RpcError :: method_not_found ( ) . with_message ( format ! (
@@ -205,7 +206,7 @@ pub trait ServerHandler: Send + Sync + 'static {
205206 async fn handle_call_tool_request (
206207 & self ,
207208 request : CallToolRequest ,
208- runtime : & dyn McpServer ,
209+ runtime : Arc < dyn McpServer > ,
209210 ) -> std:: result:: Result < CallToolResult , CallToolError > {
210211 runtime
211212 . assert_server_request_capabilities ( request. method ( ) )
@@ -220,7 +221,7 @@ pub trait ServerHandler: Send + Sync + 'static {
220221 async fn handle_set_level_request (
221222 & self ,
222223 request : SetLevelRequest ,
223- runtime : & dyn McpServer ,
224+ runtime : Arc < dyn McpServer > ,
224225 ) -> std:: result:: Result < Result , RpcError > {
225226 runtime. assert_server_request_capabilities ( request. method ( ) ) ?;
226227 Err ( RpcError :: method_not_found ( ) . with_message ( format ! (
@@ -236,7 +237,7 @@ pub trait ServerHandler: Send + Sync + 'static {
236237 async fn handle_complete_request (
237238 & self ,
238239 request : CompleteRequest ,
239- runtime : & dyn McpServer ,
240+ runtime : Arc < dyn McpServer > ,
240241 ) -> std:: result:: Result < CompleteResult , RpcError > {
241242 runtime. assert_server_request_capabilities ( request. method ( ) ) ?;
242243 Err ( RpcError :: method_not_found ( ) . with_message ( format ! (
@@ -252,7 +253,7 @@ pub trait ServerHandler: Send + Sync + 'static {
252253 async fn handle_custom_request (
253254 & self ,
254255 request : Value ,
255- runtime : & dyn McpServer ,
256+ runtime : Arc < dyn McpServer > ,
256257 ) -> std:: result:: Result < Value , RpcError > {
257258 Err ( RpcError :: method_not_found ( )
258259 . with_message ( "No handler is implemented for custom requests." . to_string ( ) ) )
@@ -265,7 +266,7 @@ pub trait ServerHandler: Send + Sync + 'static {
265266 async fn handle_initialized_notification (
266267 & self ,
267268 notification : InitializedNotification ,
268- runtime : & dyn McpServer ,
269+ runtime : Arc < dyn McpServer > ,
269270 ) -> std:: result:: Result < ( ) , RpcError > {
270271 Ok ( ( ) )
271272 }
@@ -275,7 +276,7 @@ pub trait ServerHandler: Send + Sync + 'static {
275276 async fn handle_cancelled_notification (
276277 & self ,
277278 notification : CancelledNotification ,
278- runtime : & dyn McpServer ,
279+ runtime : Arc < dyn McpServer > ,
279280 ) -> std:: result:: Result < ( ) , RpcError > {
280281 Ok ( ( ) )
281282 }
@@ -285,7 +286,7 @@ pub trait ServerHandler: Send + Sync + 'static {
285286 async fn handle_progress_notification (
286287 & self ,
287288 notification : ProgressNotification ,
288- runtime : & dyn McpServer ,
289+ runtime : Arc < dyn McpServer > ,
289290 ) -> std:: result:: Result < ( ) , RpcError > {
290291 Ok ( ( ) )
291292 }
@@ -295,7 +296,7 @@ pub trait ServerHandler: Send + Sync + 'static {
295296 async fn handle_roots_list_changed_notification (
296297 & self ,
297298 notification : RootsListChangedNotification ,
298- runtime : & dyn McpServer ,
299+ runtime : Arc < dyn McpServer > ,
299300 ) -> std:: result:: Result < ( ) , RpcError > {
300301 Ok ( ( ) )
301302 }
@@ -320,18 +321,8 @@ pub trait ServerHandler: Send + Sync + 'static {
320321 async fn handle_error (
321322 & self ,
322323 error : & RpcError ,
323- runtime : & dyn McpServer ,
324+ runtime : Arc < dyn McpServer > ,
324325 ) -> std:: result:: Result < ( ) , RpcError > {
325326 Ok ( ( ) )
326327 }
327-
328- /// Called when the server has successfully started.
329- ///
330- /// Sends a "Server started successfully" message to stderr.
331- /// Customize this function in your specific handler to implement behavior tailored to your MCP server's capabilities and requirements.
332- async fn on_server_started ( & self , runtime : & dyn McpServer ) {
333- let _ = runtime
334- . stderr_message ( "Server started successfully" . into ( ) )
335- . await ;
336- }
337328}
0 commit comments