@@ -25,7 +25,7 @@ export class MCPConfigManager {
2525 // Handle empty or whitespace-only files
2626 if ( ! configContent || configContent . trim ( ) === '' ) {
2727 console . log ( '[MCPConfigManager] Config file saved as empty, writing default config' ) ;
28- await this . saveConfig ( minimalMCPConfig ) ;
28+ await this . saveConfigWithWorkspaceCheck ( minimalMCPConfig ) ;
2929 return ;
3030 }
3131
@@ -36,13 +36,13 @@ export class MCPConfigManager {
3636 console . log ( '[MCPConfigManager] Config reloaded from file save' ) ;
3737 } else {
3838 console . log ( '[MCPConfigManager] No mcp config in saved file, writing default' ) ;
39- await this . saveConfig ( minimalMCPConfig ) ;
39+ await this . saveConfigWithWorkspaceCheck ( minimalMCPConfig ) ;
4040 }
4141 } catch ( error ) {
4242 console . error ( '[MCPConfigManager] Error reloading config on file save:' , error ) ;
4343 // If there's an error, write the default config
4444 try {
45- await this . saveConfig ( minimalMCPConfig ) ;
45+ await this . saveConfigWithWorkspaceCheck ( minimalMCPConfig ) ;
4646 } catch ( saveError ) {
4747 console . error ( '[MCPConfigManager] Error writing default config:' , saveError ) ;
4848 }
@@ -63,7 +63,7 @@ export class MCPConfigManager {
6363 if ( ! configContent || configContent . trim ( ) === '' ) {
6464 console . log ( '[MCPConfigManager] Config file is empty, creating default' ) ;
6565 this . config = minimalMCPConfig ;
66- await this . saveConfig ( this . config ) ;
66+ await this . saveConfigWithWorkspaceCheck ( this . config ) ;
6767 return this . config ;
6868 }
6969
@@ -86,17 +86,17 @@ export class MCPConfigManager {
8686 } else {
8787 console . log ( '[MCPConfigManager] No mcp config in file, creating default' ) ;
8888 this . config = minimalMCPConfig ;
89- await this . saveConfig ( this . config ) ;
89+ await this . saveConfigWithWorkspaceCheck ( this . config ) ;
9090 }
9191 } catch ( parseError ) {
9292 console . error ( '[MCPConfigManager] Error parsing config file, creating default:' , parseError ) ;
9393 this . config = minimalMCPConfig ;
94- await this . saveConfig ( this . config ) ;
94+ await this . saveConfigWithWorkspaceCheck ( this . config ) ;
9595 }
9696 } else {
9797 console . log ( '[MCPConfigManager] Config file does not exist, creating default' ) ;
9898 this . config = minimalMCPConfig ;
99- await this . saveConfig ( this . config ) ;
99+ await this . saveConfigWithWorkspaceCheck ( this . config ) ;
100100 }
101101
102102 return this . config ;
@@ -107,6 +107,39 @@ export class MCPConfigManager {
107107 }
108108 }
109109
110+ private async isWorkspaceReady ( ) : Promise < boolean > {
111+ try {
112+ const workspace = await this . plugin . call ( 'filePanel' , 'getCurrentWorkspace' ) ;
113+ return workspace && workspace . name . trim ( ) !== '' ;
114+ } catch ( error ) {
115+ return false ;
116+ }
117+ }
118+
119+ private async waitForWorkspace ( timeout : number = 10000 , interval : number = 200 ) : Promise < boolean > {
120+ const startTime = Date . now ( ) ;
121+
122+ while ( Date . now ( ) - startTime < timeout ) {
123+ if ( await this . isWorkspaceReady ( ) ) {
124+ console . log ( '[MCPConfigManager] Workspace is ready' ) ;
125+ return true ;
126+ }
127+ await new Promise ( resolve => setTimeout ( resolve , interval ) ) ;
128+ }
129+ return false ;
130+ }
131+
132+ async saveConfigWithWorkspaceCheck ( config : MCPConfig ) : Promise < void > {
133+ const workspaceReady = await this . waitForWorkspace ( ) ;
134+
135+ if ( ! workspaceReady ) {
136+ this . config = config ;
137+ return ;
138+ }
139+
140+ await this . saveConfig ( config ) ;
141+ }
142+
110143 async saveConfig ( config : MCPConfig ) : Promise < void > {
111144 try {
112145 const exists = await this . plugin . call ( 'fileManager' , 'exists' , this . configPath ) ;
@@ -151,7 +184,7 @@ export class MCPConfigManager {
151184 return ;
152185 }
153186
154- await this . saveConfig ( defaultMCPConfig ) ;
187+ await this . saveConfigWithWorkspaceCheck ( defaultMCPConfig ) ;
155188 console . log ( '[MCPConfigManager] Default config file created' ) ;
156189 } catch ( error ) {
157190 console . error ( `[MCPConfigManager] Error creating default config: ${ error . message } ` ) ;
0 commit comments