@@ -182,14 +182,22 @@ impl McpManagerHandle {
182182 & mut self ,
183183 name : String ,
184184 config : McpServerConfig ,
185- ) -> Result < McpManagerResponse , McpManagerError > {
186- self . request_tx
185+ ) -> Result < oneshot:: Receiver < LaunchServerResult > , McpManagerError > {
186+ match self
187+ . request_tx
187188 . send_recv ( McpManagerRequest :: LaunchServer {
188189 server_name : name,
189190 config,
190191 } )
191192 . await
192- . unwrap_or ( Err ( McpManagerError :: Channel ) )
193+ . unwrap_or ( Err ( McpManagerError :: Channel ) ) ?
194+ {
195+ McpManagerResponse :: LaunchServer ( rx) => Ok ( rx) ,
196+ other => Err ( McpManagerError :: Custom ( format ! (
197+ "received unexpected response: {:?}" ,
198+ other
199+ ) ) ) ,
200+ }
193201 }
194202
195203 pub async fn get_tool_specs ( & self , server_name : String ) -> Result < Vec < ToolSpec > , McpManagerError > {
@@ -257,7 +265,7 @@ pub struct McpManager {
257265
258266 cred_path : PathBuf ,
259267
260- initializing_servers : HashMap < String , McpServerActorHandle > ,
268+ initializing_servers : HashMap < String , ( McpServerActorHandle , oneshot :: Sender < LaunchServerResult > ) > ,
261269 servers : HashMap < String , McpServerActorHandle > ,
262270 event_buf : Vec < McpServerActorEvent > ,
263271}
@@ -332,8 +340,10 @@ impl McpManager {
332340 }
333341 let event_tx = self . server_event_tx . clone ( ) ;
334342 let handle = McpServerActor :: spawn ( name. clone ( ) , config, self . cred_path . clone ( ) , event_tx) ;
335- self . initializing_servers . insert ( name, handle) ;
336- Ok ( McpManagerResponse :: LaunchServer )
343+ let ( tx, rx) = oneshot:: channel ( ) ;
344+
345+ self . initializing_servers . insert ( name, ( handle, tx) ) ;
346+ Ok ( McpManagerResponse :: LaunchServer ( rx) )
337347 } ,
338348 McpManagerRequest :: GetToolSpecs { server_name } => match self . servers . get ( & server_name) {
339349 Some ( handle) => Ok ( McpManagerResponse :: ToolSpecs ( handle. get_tool_specs ( ) . await ?) ) ,
@@ -365,17 +375,25 @@ impl McpManager {
365375 list_tools_duration : _,
366376 list_prompts_duration : _,
367377 } => {
368- let Some ( handle) = self . initializing_servers . remove ( server_name) else {
378+ let Some ( ( handle, result_tx ) ) = self . initializing_servers . remove ( server_name) else {
369379 warn ! ( ?server_name, ?evt, "event was not from an initializing MCP server" ) ;
370380 return ;
371381 } ;
372382
383+ if let Err ( e) = result_tx. send ( Ok ( ( ) ) ) {
384+ error ! ( ?server_name, ?e, "failed to send server initialized message" ) ;
385+ }
386+
373387 if self . servers . insert ( server_name. clone ( ) , handle) . is_some ( ) {
374388 warn ! ( ?server_name, "duplicated server. old server dropped" ) ;
375389 }
376390 } ,
377- McpServerActorEvent :: InitializeError { server_name, error : _ } => {
378- self . initializing_servers . remove ( server_name) ;
391+ McpServerActorEvent :: InitializeError { server_name, error } => {
392+ if let Some ( ( _, result_tx) ) = self . initializing_servers . remove ( server_name) {
393+ if let Err ( e) = result_tx. send ( Err ( McpManagerError :: Custom ( error. clone ( ) ) ) ) {
394+ error ! ( ?server_name, ?e, "failed to send server initialized message" ) ;
395+ }
396+ }
379397 } ,
380398 McpServerActorEvent :: OauthRequest { server_name, oauth_url } => {
381399 info ! ( ?server_name, ?oauth_url, "received oauth request" ) ;
@@ -418,14 +436,16 @@ pub enum McpManagerRequest {
418436
419437#[ derive( Debug ) ]
420438pub enum McpManagerResponse {
421- LaunchServer ,
439+ LaunchServer ( oneshot :: Receiver < LaunchServerResult > ) ,
422440 ToolSpecs ( Vec < ToolSpec > ) ,
423441 Prompts ( Vec < Prompt > ) ,
424442 ExecuteTool ( oneshot:: Receiver < ExecuteToolResult > ) ,
425443}
426444
427445pub type ExecuteToolResult = Result < CallToolResult , McpServerActorError > ;
428446
447+ type LaunchServerResult = Result < ( ) , McpManagerError > ;
448+
429449#[ derive( Debug , Clone , Serialize , Deserialize , thiserror:: Error ) ]
430450pub enum McpManagerError {
431451 #[ error( "Server with the name {} is not initialized" , . name) ]
0 commit comments