@@ -7,6 +7,7 @@ import { MCPConfigService } from '../core/mcp';
77import { ServerRestartService } from './server-restart-service' ;
88import { AuthenticationError } from '../types/auth' ;
99import { TeamMCPConfig , MCPServerConfig } from '../types/mcp' ;
10+ import { detectDeviceInfo } from '../utils/device-detection' ;
1011
1112export interface RefreshOptions {
1213 url ?: string ;
@@ -55,40 +56,79 @@ export class RefreshService {
5556 const backendUrl = options . url || credentials . baseUrl || 'https://cloud.deploystack.io' ;
5657 const api = new DeployStackAPI ( credentials , backendUrl ) ;
5758
58- console . log ( chalk . blue ( `🔄 Refreshing MCP configuration for team: ${ chalk . cyan ( credentials . selectedTeam . name ) } ` ) ) ;
59+ console . log ( chalk . blue ( `🔄 Refreshing MCP configuration using new gateway endpoint ` ) ) ;
5960
60- // Step 1: Get current configuration for comparison
61- const oldConfig = await this . mcpService . getMCPConfig ( credentials . selectedTeam . id ) ;
62-
63- // Step 2: Download new configuration
64- spinner = ora ( 'Downloading latest MCP configuration...' ) . start ( ) ;
61+ // Step 1: Detect device and get device ID
62+ spinner = ora ( 'Detecting device and downloading latest MCP configurations...' ) . start ( ) ;
6563
6664 try {
67- const newConfig = await this . mcpService . downloadAndStoreMCPConfig (
68- credentials . selectedTeam . id ,
69- credentials . selectedTeam . name ,
70- api ,
71- false
72- ) ;
65+ // Detect current device information
66+ const deviceInfo = await detectDeviceInfo ( ) ;
67+
68+ // Download merged configurations using the new gateway endpoint
69+ // Backend will automatically find device by hardware_id
70+ const gatewayConfig = await this . mcpService . downloadGatewayMCPConfig ( deviceInfo . hardware_id , api , false ) ;
71+
72+ const readyServers = gatewayConfig . servers . filter ( s => s . status === 'ready' ) ;
73+ const invalidServers = gatewayConfig . servers . filter ( s => s . status === 'invalid' ) ;
74+
75+ spinner . succeed ( `Gateway MCP configurations refreshed (${ readyServers . length } ready, ${ invalidServers . length } invalid)` ) ;
76+ console . log ( chalk . green ( '✅ MCP configuration has been refreshed using new three-tier system' ) ) ;
7377
74- spinner . succeed ( `MCP configuration refreshed (${ newConfig . servers . length } server${ newConfig . servers . length === 1 ? '' : 's' } )` ) ;
75- console . log ( chalk . green ( '✅ MCP configuration has been refreshed' ) ) ;
78+ if ( invalidServers . length > 0 ) {
79+ console . log ( chalk . yellow ( `\n⚠️ ${ invalidServers . length } server${ invalidServers . length === 1 ? '' : 's' } marked as invalid:` ) ) ;
80+ invalidServers . forEach ( server => {
81+ console . log ( chalk . gray ( ` • ${ server . name } - Missing required user configurations` ) ) ;
82+ } ) ;
83+ console . log ( chalk . gray ( `💡 Configure these servers in the web UI to make them available` ) ) ;
84+ }
7685
77- // Step 3: Detect changes and prompt for restart if needed
78- const changes = this . detectConfigurationChanges ( oldConfig , newConfig ) ;
86+ // Step 2: Check if gateway restart is needed
87+ const isRunning = this . restartService . isServerRunning ( ) ;
7988
80- if ( changes . hasChanges ) {
81- await this . handleConfigurationChanges ( changes ) ;
89+ if ( isRunning ) {
90+ console . log ( chalk . yellow ( '\n⚠️ Gateway restart required for changes to take effect.' ) ) ;
91+
92+ // Prompt user for restart
93+ const { shouldRestart } = await inquirer . prompt ( [
94+ {
95+ type : 'confirm' ,
96+ name : 'shouldRestart' ,
97+ message : 'Do you want to restart the DeployStack Gateway now?' ,
98+ default : true
99+ }
100+ ] ) ;
101+
102+ if ( shouldRestart ) {
103+ console . log ( chalk . blue ( '\n🔄 Restarting gateway with updated configuration...' ) ) ;
104+
105+ try {
106+ const result = await this . restartService . restartGatewayServer ( ) ;
107+
108+ if ( result . restarted ) {
109+ console . log ( chalk . green ( '✅ Gateway restarted successfully with new configuration' ) ) ;
110+
111+ if ( result . mcpServersStarted !== undefined ) {
112+ console . log ( chalk . blue ( `🤖 Ready to serve ${ result . mcpServersStarted } MCP server${ result . mcpServersStarted === 1 ? '' : 's' } ` ) ) ;
113+ }
114+ }
115+ } catch ( error ) {
116+ console . log ( chalk . red ( `❌ Failed to restart gateway: ${ error instanceof Error ? error . message : String ( error ) } ` ) ) ;
117+ console . log ( chalk . gray ( '💡 You can restart manually with "deploystack restart"' ) ) ;
118+ }
119+ } else {
120+ console . log ( chalk . gray ( '💡 Configuration updated. Restart gateway manually with "deploystack restart" when ready.' ) ) ;
121+ }
82122 } else {
83- console . log ( chalk . gray ( '📋 No configuration changes detected ' ) ) ;
123+ console . log ( chalk . gray ( '💡 Gateway is not currently running. Changes will take effect when you start it. ' ) ) ;
84124 }
85125
86126 // Show summary
87127 console . log ( chalk . gray ( `\n📊 Configuration Summary:` ) ) ;
88- console . log ( chalk . gray ( ` Team : ${ newConfig . team_name } ` ) ) ;
89- console . log ( chalk . gray ( ` Installations : ${ newConfig . installations . length } ` ) ) ;
90- console . log ( chalk . gray ( ` Servers: ${ newConfig . servers . length } ` ) ) ;
91- console . log ( chalk . gray ( ` Last Updated: ${ new Date ( newConfig . last_updated ) . toLocaleString ( ) } ` ) ) ;
128+ console . log ( chalk . gray ( ` Hardware ID : ${ deviceInfo . hardware_id } ` ) ) ;
129+ console . log ( chalk . gray ( ` Ready Servers : ${ readyServers . length } ` ) ) ;
130+ console . log ( chalk . gray ( ` Invalid Servers: ${ invalidServers . length } ` ) ) ;
131+ console . log ( chalk . gray ( ` Last Updated: ${ new Date ( gatewayConfig . lastUpdated ) . toLocaleString ( ) } ` ) ) ;
92132
93133 } catch ( error ) {
94134 spinner . fail ( 'Failed to refresh MCP configuration' ) ;
0 commit comments