@@ -329,10 +329,9 @@ export class McpHub {
329329 autoApprove : autoApproveConfig . includes ( tool . name ) ,
330330 } ) )
331331
332- // console.log(`[MCP] Fetched tools for ${serverName}:`, tools)
333332 return tools
334333 } catch ( error ) {
335- // console.error(`Failed to fetch tools for ${serverName}:`, error)
334+ console . error ( `Failed to fetch tools for ${ serverName } :` , error )
336335 return [ ]
337336 }
338337 }
@@ -377,6 +376,53 @@ export class McpHub {
377376 }
378377 }
379378
379+ async updateServerConnectionsRPC ( newServers : Record < string , McpServerConfig > ) : Promise < void > {
380+ this . isConnecting = true
381+ this . removeAllFileWatchers ( )
382+ const currentNames = new Set ( this . connections . map ( ( conn ) => conn . server . name ) )
383+ const newNames = new Set ( Object . keys ( newServers ) )
384+
385+ // Delete removed servers
386+ for ( const name of currentNames ) {
387+ if ( ! newNames . has ( name ) ) {
388+ await this . deleteConnection ( name )
389+ console . log ( `Deleted MCP server: ${ name } ` )
390+ }
391+ }
392+
393+ // Update or add servers
394+ for ( const [ name , config ] of Object . entries ( newServers ) ) {
395+ const currentConnection = this . connections . find ( ( conn ) => conn . server . name === name )
396+
397+ if ( ! currentConnection ) {
398+ // New server
399+ try {
400+ if ( config . transportType === "stdio" ) {
401+ this . setupFileWatcher ( name , config )
402+ }
403+ await this . connectToServer ( name , config )
404+ } catch ( error ) {
405+ console . error ( `Failed to connect to new MCP server ${ name } :` , error )
406+ }
407+ } else if ( ! deepEqual ( JSON . parse ( currentConnection . server . config ) , config ) ) {
408+ // Existing server with changed config
409+ try {
410+ if ( config . transportType === "stdio" ) {
411+ this . setupFileWatcher ( name , config )
412+ }
413+ await this . deleteConnection ( name )
414+ await this . connectToServer ( name , config )
415+ console . log ( `Reconnected MCP server with updated config: ${ name } ` )
416+ } catch ( error ) {
417+ console . error ( `Failed to reconnect MCP server ${ name } :` , error )
418+ }
419+ }
420+ // If server exists with same config, do nothing
421+ }
422+
423+ this . isConnecting = false
424+ }
425+
380426 async updateServerConnections ( newServers : Record < string , McpServerConfig > ) : Promise < void > {
381427 this . isConnecting = true
382428 this . removeAllFileWatchers ( )
@@ -719,7 +765,7 @@ export class McpHub {
719765 }
720766 }
721767
722- public async updateServerTimeout ( serverName : string , timeout : number ) : Promise < void > {
768+ public async updateServerTimeoutRPC ( serverName : string , timeout : number ) : Promise < McpServer [ ] > {
723769 try {
724770 // Validate timeout against schema
725771 const setConfigResult = BaseConfigSchema . shape . timeout . safeParse ( timeout )
@@ -742,7 +788,18 @@ export class McpHub {
742788
743789 await fs . writeFile ( settingsPath , JSON . stringify ( config , null , 2 ) )
744790
745- await this . updateServerConnections ( config . mcpServers )
791+ await this . updateServerConnectionsRPC ( config . mcpServers )
792+
793+ const serverOrder = Object . keys ( config . mcpServers || { } )
794+ const updatedMcpServers = [ ...this . connections ]
795+ . sort ( ( a , b ) => {
796+ const indexA = serverOrder . indexOf ( a . server . name )
797+ const indexB = serverOrder . indexOf ( b . server . name )
798+ return indexA - indexB
799+ } )
800+ . map ( ( connection ) => connection . server )
801+
802+ return updatedMcpServers
746803 } catch ( error ) {
747804 console . error ( "Failed to update server timeout:" , error )
748805 if ( error instanceof Error ) {
0 commit comments