@@ -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 > {
@@ -260,7 +268,7 @@ pub struct McpManager {
260268
261269 cred_path : PathBuf ,
262270
263- initializing_servers : HashMap < String , McpServerActorHandle > ,
271+ initializing_servers : HashMap < String , ( McpServerActorHandle , oneshot :: Sender < LaunchServerResult > ) > ,
264272 servers : HashMap < String , McpServerActorHandle > ,
265273 event_buf : Vec < McpServerActorEvent > ,
266274}
@@ -335,8 +343,10 @@ impl McpManager {
335343 }
336344 let event_tx = self . server_event_tx . clone ( ) ;
337345 let handle = McpServerActor :: spawn ( name. clone ( ) , config, self . cred_path . clone ( ) , event_tx) ;
338- self . initializing_servers . insert ( name, handle) ;
339- Ok ( McpManagerResponse :: LaunchServer )
346+ let ( tx, rx) = oneshot:: channel ( ) ;
347+
348+ self . initializing_servers . insert ( name, ( handle, tx) ) ;
349+ Ok ( McpManagerResponse :: LaunchServer ( rx) )
340350 } ,
341351 McpManagerRequest :: GetToolSpecs { server_name } => match self . servers . get ( & server_name) {
342352 Some ( handle) => Ok ( McpManagerResponse :: ToolSpecs ( handle. get_tool_specs ( ) . await ?) ) ,
@@ -368,17 +378,25 @@ impl McpManager {
368378 list_tools_duration : _,
369379 list_prompts_duration : _,
370380 } => {
371- let Some ( handle) = self . initializing_servers . remove ( server_name) else {
381+ let Some ( ( handle, result_tx ) ) = self . initializing_servers . remove ( server_name) else {
372382 warn ! ( ?server_name, ?evt, "event was not from an initializing MCP server" ) ;
373383 return ;
374384 } ;
375385
386+ if let Err ( e) = result_tx. send ( Ok ( ( ) ) ) {
387+ error ! ( ?server_name, ?e, "failed to send server initialized message" ) ;
388+ }
389+
376390 if self . servers . insert ( server_name. clone ( ) , handle) . is_some ( ) {
377391 warn ! ( ?server_name, "duplicated server. old server dropped" ) ;
378392 }
379393 } ,
380- McpServerActorEvent :: InitializeError { server_name, error : _ } => {
381- self . initializing_servers . remove ( server_name) ;
394+ McpServerActorEvent :: InitializeError { server_name, error } => {
395+ if let Some ( ( _, result_tx) ) = self . initializing_servers . remove ( server_name) {
396+ if let Err ( e) = result_tx. send ( Err ( McpManagerError :: Custom ( error. clone ( ) ) ) ) {
397+ error ! ( ?server_name, ?e, "failed to send server initialized message" ) ;
398+ }
399+ }
382400 } ,
383401 McpServerActorEvent :: OauthRequest { server_name, oauth_url } => {
384402 info ! ( ?server_name, ?oauth_url, "received oauth request" ) ;
@@ -421,14 +439,16 @@ pub enum McpManagerRequest {
421439
422440#[ derive( Debug ) ]
423441pub enum McpManagerResponse {
424- LaunchServer ,
442+ LaunchServer ( oneshot :: Receiver < LaunchServerResult > ) ,
425443 ToolSpecs ( Vec < ToolSpec > ) ,
426444 Prompts ( Vec < Prompt > ) ,
427445 ExecuteTool ( oneshot:: Receiver < ExecuteToolResult > ) ,
428446}
429447
430448pub type ExecuteToolResult = Result < CallToolResult , McpServerActorError > ;
431449
450+ type LaunchServerResult = Result < ( ) , McpManagerError > ;
451+
432452#[ derive( Debug , Clone , Serialize , Deserialize , thiserror:: Error ) ]
433453pub enum McpManagerError {
434454 #[ error( "Server with the name {} is not initialized" , . name) ]
0 commit comments