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 {
2222 #[ cfg( feature = "hyper-server" ) ]
2323 #[ error( "{0}" ) ]
2424 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}" ) ]
2626 IncompatibleProtocolVersion ( String , String ) ,
2727}
2828
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+
2939#[ deprecated( since = "0.2.0" , note = "Use `McpSdkError` instead." ) ]
3040pub type MCPSdkError = McpSdkError ;
Original file line number Diff line number Diff line change @@ -31,22 +31,28 @@ pub trait ServerHandler: Send + Sync + 'static {
3131 initialize_request : InitializeRequest ,
3232 runtime : & dyn McpServer ,
3333 ) -> 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-
3834 let mut server_info = runtime. server_info ( ) . to_owned ( ) ;
3935 // Provide compatibility for clients using older MCP protocol versions.
4036
4137 if let Some ( updated_protocol_version) = enforce_compatible_protocol_version (
4238 & initialize_request. params . protocol_version ,
4339 & server_info. protocol_version ,
4440 )
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;
4850 }
4951
52+ runtime
53+ . set_client_details ( initialize_request. params . clone ( ) )
54+ . map_err ( |err| RpcError :: internal_error ( ) . with_message ( format ! ( "{err}" ) ) ) ?;
55+
5056 Ok ( server_info)
5157 }
5258
Original file line number Diff line number Diff line change @@ -106,7 +106,14 @@ impl McpServer for ServerRuntime {
106106 // create a response to send back to the client
107107 let response: MessageFromServer = match result {
108108 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+ }
110117 } ;
111118
112119 // 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<()> {
4040 let server = server_runtime_core:: create_server ( server_details, transport, handler) ;
4141
4242 // 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 ( ( ) )
4452}
Original file line number Diff line number Diff line change @@ -42,5 +42,13 @@ async fn main() -> SdkResult<()> {
4242 let server: ServerRuntime = server_runtime:: create_server ( server_details, transport, handler) ;
4343
4444 // 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 ( ( ) )
4654}
Original file line number Diff line number Diff line change @@ -36,9 +36,15 @@ impl ServerHandlerCore for MyServerHandler {
3636 & initialize_request. params . protocol_version ,
3737 & server_info. protocol_version ,
3838 )
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;
4248 }
4349
4450 return Ok ( server_info. into ( ) ) ;
Original file line number Diff line number Diff line change 11[toolchain ]
2- channel = " 1.87 .0"
2+ channel = " 1.88 .0"
33components = [" rustfmt" , " clippy" ]
You can’t perform that action at this time.
0 commit comments