File tree Expand file tree Collapse file tree 7 files changed +60
-15
lines changed
hello-world-mcp-server-core/src
hello-world-mcp-server/src
hello-world-server-core-sse/src Expand file tree Collapse file tree 7 files changed +60
-15
lines changed Original file line number Diff line number Diff line change @@ -22,9 +22,19 @@ pub enum McpSdkError {
22
22
#[ cfg( feature = "hyper-server" ) ]
23
23
#[ error( "{0}" ) ]
24
24
TransportServerError ( #[ from] TransportServerError ) ,
25
- #[ error( "Incompatible mcp protocol version! \n client:{0}\n server :{1}" ) ]
25
+ #[ error( "Incompatible mcp protocol version: client:{0} server :{1}" ) ]
26
26
IncompatibleProtocolVersion ( String , String ) ,
27
27
}
28
28
29
+ impl McpSdkError {
30
+ /// Returns the RPC error message if the error is of type `McpSdkError::RpcError`.
31
+ pub fn rpc_error_message ( & self ) -> Option < & String > {
32
+ if let McpSdkError :: RpcError ( rpc_error) = self {
33
+ return Some ( & rpc_error. message ) ;
34
+ }
35
+ None
36
+ }
37
+ }
38
+
29
39
#[ deprecated( since = "0.2.0" , note = "Use `McpSdkError` instead." ) ]
30
40
pub type MCPSdkError = McpSdkError ;
Original file line number Diff line number Diff line change @@ -31,22 +31,28 @@ pub trait ServerHandler: Send + Sync + 'static {
31
31
initialize_request : InitializeRequest ,
32
32
runtime : & dyn McpServer ,
33
33
) -> std:: result:: Result < InitializeResult , RpcError > {
34
- runtime
35
- . set_client_details ( initialize_request. params . clone ( ) )
36
- . map_err ( |err| RpcError :: internal_error ( ) . with_message ( format ! ( "{err}" ) ) ) ?;
37
-
38
34
let mut server_info = runtime. server_info ( ) . to_owned ( ) ;
39
35
// Provide compatibility for clients using older MCP protocol versions.
40
36
41
37
if let Some ( updated_protocol_version) = enforce_compatible_protocol_version (
42
38
& initialize_request. params . protocol_version ,
43
39
& server_info. protocol_version ,
44
40
)
45
- . map_err ( |err| RpcError :: internal_error ( ) . with_message ( err. to_string ( ) ) ) ?
46
- {
47
- server_info. protocol_version = initialize_request. params . protocol_version ;
41
+ . map_err ( |err| {
42
+ tracing:: error!(
43
+ "Incompatible protocol version : client: {} server: {}" ,
44
+ & initialize_request. params. protocol_version,
45
+ & server_info. protocol_version
46
+ ) ;
47
+ RpcError :: internal_error ( ) . with_message ( err. to_string ( ) )
48
+ } ) ? {
49
+ server_info. protocol_version = updated_protocol_version;
48
50
}
49
51
52
+ runtime
53
+ . set_client_details ( initialize_request. params . clone ( ) )
54
+ . map_err ( |err| RpcError :: internal_error ( ) . with_message ( format ! ( "{err}" ) ) ) ?;
55
+
50
56
Ok ( server_info)
51
57
}
52
58
Original file line number Diff line number Diff line change @@ -106,7 +106,14 @@ impl McpServer for ServerRuntime {
106
106
// create a response to send back to the client
107
107
let response: MessageFromServer = match result {
108
108
Ok ( success_value) => success_value. into ( ) ,
109
- Err ( error_value) => MessageFromServer :: Error ( error_value) ,
109
+ Err ( error_value) => {
110
+ // Error occurred during initialization.
111
+ // A likely cause could be an unsupported protocol version.
112
+ if !self . is_initialized ( ) {
113
+ return Err ( error_value. into ( ) ) ;
114
+ }
115
+ MessageFromServer :: Error ( error_value)
116
+ }
110
117
} ;
111
118
112
119
// send the response back with corresponding request id
Original file line number Diff line number Diff line change @@ -40,5 +40,13 @@ async fn main() -> SdkResult<()> {
40
40
let server = server_runtime_core:: create_server ( server_details, transport, handler) ;
41
41
42
42
// STEP 5: Start the server
43
- server. start ( ) . await
43
+ if let Err ( start_error) = server. start ( ) . await {
44
+ eprintln ! (
45
+ "{}" ,
46
+ start_error
47
+ . rpc_error_message( )
48
+ . unwrap_or( & start_error. to_string( ) )
49
+ ) ;
50
+ } ;
51
+ Ok ( ( ) )
44
52
}
Original file line number Diff line number Diff line change @@ -42,5 +42,13 @@ async fn main() -> SdkResult<()> {
42
42
let server: ServerRuntime = server_runtime:: create_server ( server_details, transport, handler) ;
43
43
44
44
// STEP 5: Start the server
45
- server. start ( ) . await
45
+ if let Err ( start_error) = server. start ( ) . await {
46
+ eprintln ! (
47
+ "{}" ,
48
+ start_error
49
+ . rpc_error_message( )
50
+ . unwrap_or( & start_error. to_string( ) )
51
+ ) ;
52
+ } ;
53
+ Ok ( ( ) )
46
54
}
Original file line number Diff line number Diff line change @@ -36,9 +36,15 @@ impl ServerHandlerCore for MyServerHandler {
36
36
& initialize_request. params . protocol_version ,
37
37
& server_info. protocol_version ,
38
38
)
39
- . map_err ( |err| RpcError :: internal_error ( ) . with_message ( err. to_string ( ) ) ) ?
40
- {
41
- server_info. protocol_version = initialize_request. params . protocol_version ;
39
+ . map_err ( |err| {
40
+ tracing:: error!(
41
+ "Incompatible protocol version :\n client: {}\n server: {}" ,
42
+ & initialize_request. params. protocol_version,
43
+ & server_info. protocol_version
44
+ ) ;
45
+ RpcError :: internal_error ( ) . with_message ( err. to_string ( ) )
46
+ } ) ? {
47
+ server_info. protocol_version = updated_protocol_version;
42
48
}
43
49
44
50
return Ok ( server_info. into ( ) ) ;
Original file line number Diff line number Diff line change 1
1
[toolchain ]
2
- channel = " 1.87 .0"
2
+ channel = " 1.88 .0"
3
3
components = [" rustfmt" , " clippy" ]
You can’t perform that action at this time.
0 commit comments