@@ -40,7 +40,10 @@ use serde::{
4040} ;
4141use thiserror:: Error ;
4242use tokio:: signal:: ctrl_c;
43- use tokio:: sync:: Mutex ;
43+ use tokio:: sync:: {
44+ Mutex ,
45+ RwLock ,
46+ } ;
4447use tracing:: {
4548 error,
4649 warn,
@@ -406,6 +409,8 @@ impl ToolManagerBuilder {
406409 let new_tool_specs_clone = new_tool_specs. clone ( ) ;
407410 let has_new_stuff = Arc :: new ( AtomicBool :: new ( false ) ) ;
408411 let has_new_stuff_clone = has_new_stuff. clone ( ) ;
412+ let pending = Arc :: new ( RwLock :: new ( HashSet :: < String > :: new ( ) ) ) ;
413+ let pending_clone = pending. clone ( ) ;
409414 let ( mut msg_rx, messenger_builder) = ServerMessengerBuilder :: new ( 20 ) ;
410415 tokio:: spawn ( async move {
411416 while let Some ( msg) = msg_rx. recv ( ) . await {
@@ -415,6 +420,7 @@ impl ToolManagerBuilder {
415420 // list calls.
416421 match msg {
417422 UpdateEventMessage :: ToolsListResult { server_name, result } => {
423+ pending_clone. write ( ) . await . remove ( & server_name) ;
418424 let mut specs = result
419425 . tools
420426 . into_iter ( )
@@ -464,14 +470,16 @@ impl ToolManagerBuilder {
464470 server_name : _,
465471 result : _,
466472 } => { } ,
467- UpdateEventMessage :: DisplayTaskEnded => {
468- load_msg_sender. take ( ) ;
473+ UpdateEventMessage :: InitStart { server_name } => {
474+ pending_clone. write ( ) . await . insert ( server_name. clone ( ) ) ;
475+ if let Some ( sender) = & load_msg_sender {
476+ let _ = sender. send ( LoadingMsg :: Add ( server_name) ) ;
477+ }
469478 } ,
470479 }
471480 }
472481 } ) ;
473482 for ( mut name, init_res) in pre_initialized {
474- let _ = tx. send ( LoadingMsg :: Add ( name. clone ( ) ) ) ;
475483 match init_res {
476484 Ok ( mut client) => {
477485 let messenger = messenger_builder. build_with_name ( client. get_server_name ( ) . to_owned ( ) ) ;
@@ -592,6 +600,7 @@ impl ToolManagerBuilder {
592600 clients,
593601 prompts,
594602 loading_display_task,
603+ pending_clients : pending,
595604 loading_status_sender,
596605 new_tool_specs,
597606 has_new_stuff,
@@ -638,8 +647,19 @@ pub struct ToolManager {
638647 /// These clients are used to communicate with MCP servers.
639648 pub clients : HashMap < String , Arc < CustomToolClient > > ,
640649
650+ #[ allow( dead_code) ]
651+ /// A list of client names that are still in the process of being initialized
652+ pub pending_clients : Arc < RwLock < HashSet < String > > > ,
653+
654+ /// Flag indicating whether new tool specifications have been added since the last update.
655+ /// When set to true, it signals that the tool manager needs to refresh its internal state
656+ /// to incorporate newly available tools from MCP servers.
641657 pub has_new_stuff : Arc < AtomicBool > ,
642658
659+ /// Storage for newly discovered tool specifications from MCP servers that haven't yet been
660+ /// integrated into the main tool registry. This field holds a thread-safe reference to a map
661+ /// of server names to their tool specifications and name mappings, allowing concurrent updates
662+ /// from server initialization processes.
643663 new_tool_specs : NewToolSpecs ,
644664
645665 /// Cache for prompts collected from different servers.
@@ -1059,6 +1079,10 @@ impl ToolManager {
10591079 ) ;
10601080 Ok ( ( ) )
10611081 }
1082+
1083+ pub async fn _pending_clients ( & self ) -> Vec < String > {
1084+ self . pending_clients . read ( ) . await . iter ( ) . cloned ( ) . collect :: < Vec < _ > > ( )
1085+ }
10621086}
10631087
10641088#[ inline]
@@ -1150,7 +1174,7 @@ fn process_tool_specs(
11501174 server_name, msg
11511175 ) ;
11521176 if is_in_display {
1153- Some ( LoadingMsg :: Error {
1177+ Some ( LoadingMsg :: Warn {
11541178 name : server_name. to_string ( ) ,
11551179 msg : eyre:: eyre!( msg) ,
11561180 } )
0 commit comments