Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2d6d23

Browse files
authored
feat!: update ServerHandler and ServerHandlerCore traits (#96)
* feat: update server handler traits to accept Arc<dyn McpServer> * feat: remove on_server_started in favor of on_initialized * Unblock Stream Loop in start Function with Task Spawning * chore: update start_stresam * chore: cleanup
1 parent 5dacceb commit a2d6d23

File tree

20 files changed

+247
-165
lines changed

20 files changed

+247
-165
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub struct MyServerHandler;
180180
#[async_trait]
181181
impl ServerHandler for MyServerHandler {
182182
// Handle ListToolsRequest, return list of available tools as ListToolsResult
183-
async fn handle_list_tools_request(&self, request: ListToolsRequest, runtime: &dyn McpServer) -> Result<ListToolsResult, RpcError> {
183+
async fn handle_list_tools_request(&self, request: ListToolsRequest, runtime: Arc<dyn McpServer>) -> Result<ListToolsResult, RpcError> {
184184

185185
Ok(ListToolsResult {
186186
tools: vec![SayHelloTool::tool()],
@@ -191,7 +191,7 @@ impl ServerHandler for MyServerHandler {
191191
}
192192

193193
/// Handles requests to call a specific tool.
194-
async fn handle_call_tool_request( &self, request: CallToolRequest, runtime: &dyn McpServer, ) -> Result<CallToolResult, CallToolError> {
194+
async fn handle_call_tool_request( &self, request: CallToolRequest, runtime: Arc<dyn McpServer>, ) -> Result<CallToolResult, CallToolError> {
195195

196196
if request.tool_name() == SayHelloTool::tool_name() {
197197
Ok( CallToolResult::text_content( vec![TextContent::from("Hello World!".to_string())] ))

crates/rust-mcp-sdk/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub struct MyServerHandler;
180180
#[async_trait]
181181
impl ServerHandler for MyServerHandler {
182182
// Handle ListToolsRequest, return list of available tools as ListToolsResult
183-
async fn handle_list_tools_request(&self, request: ListToolsRequest, runtime: &dyn McpServer) -> Result<ListToolsResult, RpcError> {
183+
async fn handle_list_tools_request(&self, request: ListToolsRequest, runtime: Arc<dyn McpServer>) -> Result<ListToolsResult, RpcError> {
184184

185185
Ok(ListToolsResult {
186186
tools: vec![SayHelloTool::tool()],
@@ -191,7 +191,7 @@ impl ServerHandler for MyServerHandler {
191191
}
192192

193193
/// Handles requests to call a specific tool.
194-
async fn handle_call_tool_request( &self, request: CallToolRequest, runtime: &dyn McpServer, ) -> Result<CallToolResult, CallToolError> {
194+
async fn handle_call_tool_request( &self, request: CallToolRequest, runtime: Arc<dyn McpServer> ) -> Result<CallToolResult, CallToolError> {
195195

196196
if request.tool_name() == SayHelloTool::tool_name() {
197197
Ok( CallToolResult::text_content( vec![TextContent::from("Hello World!".to_string())] ))

crates/rust-mcp-sdk/src/hyper_servers/routes/hyper_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ pub async fn start_new_session(
166166

167167
let h: Arc<dyn McpServerHandler> = state.handler.clone();
168168
// create a new server instance with unique session_id and
169-
let runtime: Arc<ServerRuntime> = Arc::new(server_runtime::create_server_instance(
169+
let runtime: Arc<ServerRuntime> = server_runtime::create_server_instance(
170170
Arc::clone(&state.server_details),
171171
h,
172172
session_id.to_owned(),
173-
));
173+
);
174174

175175
tracing::info!("a new client joined : {}", &session_id);
176176

crates/rust-mcp-sdk/src/hyper_servers/routes/sse_routes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ pub async fn handle_sse(
9999
.unwrap();
100100
let h: Arc<dyn McpServerHandler> = state.handler.clone();
101101
// create a new server instance with unique session_id and
102-
let server: Arc<ServerRuntime> = Arc::new(server_runtime::create_server_instance(
102+
let server: Arc<ServerRuntime> = server_runtime::create_server_instance(
103103
Arc::clone(&state.server_details),
104104
h,
105105
session_id.to_owned(),
106-
));
106+
);
107107

108108
state
109109
.session_store

crates/rust-mcp-sdk/src/mcp_handlers/mcp_server_handler.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::schema::{schema_utils::CallToolError, *};
22
use async_trait::async_trait;
33
use serde_json::Value;
4+
use std::sync::Arc;
45

56
use 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
}

crates/rust-mcp-sdk/src/mcp_handlers/mcp_server_handler_core.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::mcp_traits::mcp_server::McpServer;
12
use crate::schema::schema_utils::*;
23
use crate::schema::*;
34
use async_trait::async_trait;
4-
5-
use crate::mcp_traits::mcp_server::McpServer;
5+
use std::sync::Arc;
66

77
/// Defines the `ServerHandlerCore` trait for handling Model Context Protocol (MCP) server operations.
88
/// Unlike `ServerHandler`, this trait offers no default implementations, providing full control over MCP message handling
@@ -14,7 +14,7 @@ pub trait ServerHandlerCore: Send + Sync + 'static {
1414
/// The `runtime` parameter provides access to the server's runtime environment, allowing
1515
/// interaction with the server's capabilities.
1616
/// The default implementation does nothing.
17-
async fn on_initialized(&self, _runtime: &dyn McpServer) {}
17+
async fn on_initialized(&self, _runtime: Arc<dyn McpServer>) {}
1818

1919
/// Asynchronously handles an incoming request from the client.
2020
///
@@ -26,7 +26,7 @@ pub trait ServerHandlerCore: Send + Sync + 'static {
2626
async fn handle_request(
2727
&self,
2828
request: RequestFromClient,
29-
runtime: &dyn McpServer,
29+
runtime: Arc<dyn McpServer>,
3030
) -> std::result::Result<ResultFromServer, RpcError>;
3131

3232
/// Asynchronously handles an incoming notification from the client.
@@ -36,7 +36,7 @@ pub trait ServerHandlerCore: Send + Sync + 'static {
3636
async fn handle_notification(
3737
&self,
3838
notification: NotificationFromClient,
39-
runtime: &dyn McpServer,
39+
runtime: Arc<dyn McpServer>,
4040
) -> std::result::Result<(), RpcError>;
4141

4242
/// Asynchronously handles an error received from the client.
@@ -46,11 +46,6 @@ pub trait ServerHandlerCore: Send + Sync + 'static {
4646
async fn handle_error(
4747
&self,
4848
error: &RpcError,
49-
runtime: &dyn McpServer,
49+
runtime: Arc<dyn McpServer>,
5050
) -> std::result::Result<(), RpcError>;
51-
async fn on_server_started(&self, runtime: &dyn McpServer) {
52-
let _ = runtime
53-
.stderr_message("Server started successfully".into())
54-
.await;
55-
}
5651
}

0 commit comments

Comments
 (0)