@@ -6,7 +6,10 @@ use rust_mcp_schema::{
6
6
schema_utils:: { CallToolError , NotificationFromClient , RequestFromClient , ResultFromServer } ,
7
7
ClientRequest , ListToolsResult , RpcError ,
8
8
} ;
9
- use rust_mcp_sdk:: { mcp_server:: ServerHandlerCore , McpServer } ;
9
+ use rust_mcp_sdk:: {
10
+ mcp_server:: { enforce_compatible_protocol_version, ServerHandlerCore } ,
11
+ McpServer ,
12
+ } ;
10
13
11
14
use crate :: tools:: GreetingTools ;
12
15
@@ -30,26 +33,16 @@ impl ServerHandlerCore for MyServerHandler {
30
33
// Handle the initialization request
31
34
ClientRequest :: InitializeRequest ( initialize_request) => {
32
35
let mut server_info = runtime. server_info ( ) . to_owned ( ) ;
33
- // Provide compatibility for clients using older MCP protocol versions.
34
- match server_info
35
- . protocol_version
36
- . cmp ( & initialize_request. params . protocol_version )
36
+
37
+ if let Some ( updated_protocol_version) = enforce_compatible_protocol_version (
38
+ & initialize_request. params . protocol_version ,
39
+ & server_info. protocol_version ,
40
+ )
41
+ . map_err ( |err| RpcError :: internal_error ( ) . with_message ( err. to_string ( ) ) ) ?
37
42
{
38
- Ordering :: Less => {
39
- let error_message = format ! (
40
- "Incompatible mcp protocl version!\n client:{}\n server:{}" ,
41
- & initialize_request. params. protocol_version,
42
- server_info. protocol_version,
43
- ) ;
44
- let _ = runtime. stderr_message ( error_message) . await ;
45
- return Err ( RpcError :: internal_error ( ) ) ;
46
- }
47
- Ordering :: Equal | Ordering :: Greater => {
48
- // return the same version that was received from the client
49
- server_info. protocol_version =
50
- initialize_request. params . protocol_version ;
51
- }
43
+ server_info. protocol_version = initialize_request. params . protocol_version ;
52
44
}
45
+
53
46
return Ok ( server_info. into ( ) ) ;
54
47
}
55
48
// Handle ListToolsRequest, return list of available tools
0 commit comments