@@ -43,6 +43,7 @@ export class MCPInferencer extends RemoteInferencer implements ICompletions, IGe
4343 private mcpClients : Map < string , MCPClient > = new Map ( ) ;
4444 private connectionStatuses : Map < string , IMCPConnectionStatus > = new Map ( ) ;
4545 private resourceCache : Map < string , IMCPResourceContent > = new Map ( ) ;
46+ private toolsCache : Map < string , IMCPTool [ ] > = new Map ( ) ;
4647 private intentAnalyzer : IntentAnalyzer = new IntentAnalyzer ( ) ;
4748 private resourceScoring : ResourceScoring = new ResourceScoring ( ) ;
4849 private remixMCPServer ?: any ; // Internal RemixMCPServer instance
@@ -71,12 +72,19 @@ export class MCPInferencer extends RemoteInferencer implements ICompletions, IGe
7172 } ) ;
7273
7374 // Set up event listeners
74- client . on ( 'connected' , ( serverName : string , result : IMCPInitializeResult ) => {
75+ client . on ( 'connected' , async ( serverName : string , result : IMCPInitializeResult ) => {
7576 this . connectionStatuses . set ( serverName , {
7677 status : 'connected' ,
7778 serverName,
7879 capabilities : result . capabilities
7980 } ) ;
81+ // Populate tools cache on connect
82+ try {
83+ const tools = await client . listTools ( ) ;
84+ this . toolsCache . set ( serverName , tools ) ;
85+ } catch ( error ) {
86+ this . toolsCache . set ( serverName , [ ] ) ;
87+ }
8088 this . event . emit ( 'mcpServerConnected' , serverName , result ) ;
8189 } ) ;
8290
@@ -87,6 +95,7 @@ export class MCPInferencer extends RemoteInferencer implements ICompletions, IGe
8795 error : error . message ,
8896 lastAttempt : Date . now ( )
8997 } ) ;
98+ this . toolsCache . delete ( serverName ) ;
9099 this . event . emit ( 'mcpServerError' , serverName , error ) ;
91100 } ) ;
92101
@@ -95,6 +104,7 @@ export class MCPInferencer extends RemoteInferencer implements ICompletions, IGe
95104 status : 'disconnected' ,
96105 serverName
97106 } ) ;
107+ this . toolsCache . delete ( serverName ) ;
98108 this . event . emit ( 'mcpServerDisconnected' , serverName ) ;
99109 } ) ;
100110 }
@@ -648,15 +658,34 @@ export class MCPInferencer extends RemoteInferencer implements ICompletions, IGe
648658
649659 for ( const [ serverName , client ] of this . mcpClients ) {
650660 if ( client . isConnected ( ) ) {
661+ result [ serverName ] = this . toolsCache . get ( serverName ) || [ ] ;
662+ }
663+ }
664+
665+ return result ;
666+ }
667+
668+ async refreshToolsCache ( serverName ?: string ) : Promise < void > {
669+ if ( serverName ) {
670+ const client = this . mcpClients . get ( serverName ) ;
671+ if ( client ?. isConnected ( ) ) {
651672 try {
652- result [ serverName ] = await client . listTools ( ) ;
673+ this . toolsCache . set ( serverName , await client . listTools ( ) ) ;
653674 } catch ( error ) {
654- result [ serverName ] = [ ] ;
675+ this . toolsCache . set ( serverName , [ ] ) ;
676+ }
677+ }
678+ } else {
679+ for ( const [ name , client ] of this . mcpClients ) {
680+ if ( client . isConnected ( ) ) {
681+ try {
682+ this . toolsCache . set ( name , await client . listTools ( ) ) ;
683+ } catch ( error ) {
684+ this . toolsCache . set ( name , [ ] ) ;
685+ }
655686 }
656687 }
657688 }
658-
659- return result ;
660689 }
661690
662691 async executeTool ( serverName : string , toolCall : IMCPToolCall ) : Promise < IMCPToolResult > {
0 commit comments