@@ -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 }
@@ -54,6 +54,7 @@ export class MCPConfigManager {
5454
5555 async loadConfig ( ) : Promise < MCPConfig > {
5656 try {
57+ await this . waitForWorkspace ( ) ;
5758 const exists = await this . plugin . call ( 'fileManager' , 'exists' , this . configPath ) ;
5859
5960 if ( exists ) {
@@ -107,6 +108,44 @@ export class MCPConfigManager {
107108 }
108109 }
109110
111+ private async isWorkspaceReady ( ) : Promise < boolean > {
112+ try {
113+ const workspace = await this . plugin . call ( 'filePanel' , 'getCurrentWorkspace' ) ;
114+ return workspace && workspace . name . trim ( ) !== '' ;
115+ } catch ( error ) {
116+ return false ;
117+ }
118+ }
119+
120+ private async waitForWorkspace ( ) : Promise < boolean > {
121+ if ( await this . isWorkspaceReady ( ) ) {
122+ console . log ( '[MCPConfigManager] Workspace is already ready' ) ;
123+ return true ;
124+ }
125+
126+ return new Promise < boolean > ( ( resolve ) => {
127+ const checkAndResolve = async ( ) => {
128+ if ( await this . isWorkspaceReady ( ) ) {
129+ console . log ( '[MCPConfigManager] Workspace is ready' ) ;
130+ resolve ( true ) ;
131+ }
132+ } ;
133+
134+ this . plugin . once ( 'filePanel' , 'setWorkspace' , checkAndResolve ) ;
135+ } ) ;
136+ }
137+
138+ async saveConfigWithWorkspaceCheck ( config : MCPConfig ) : Promise < void > {
139+ const workspaceReady = await this . waitForWorkspace ( ) ;
140+
141+ if ( ! workspaceReady ) {
142+ this . config = config ;
143+ return ;
144+ }
145+
146+ await this . saveConfig ( config ) ;
147+ }
148+
110149 async saveConfig ( config : MCPConfig ) : Promise < void > {
111150 try {
112151 const exists = await this . plugin . call ( 'fileManager' , 'exists' , this . configPath ) ;
@@ -151,7 +190,7 @@ export class MCPConfigManager {
151190 return ;
152191 }
153192
154- await this . saveConfig ( defaultMCPConfig ) ;
193+ await this . saveConfigWithWorkspaceCheck ( defaultMCPConfig ) ;
155194 console . log ( '[MCPConfigManager] Default config file created' ) ;
156195 } catch ( error ) {
157196 console . error ( `[MCPConfigManager] Error creating default config: ${ error . message } ` ) ;
@@ -179,7 +218,7 @@ export class MCPConfigManager {
179218 const permissions = this . config . security . fileWritePermissions || {
180219 mode : 'ask' as const ,
181220 allowedFiles : [ ] ,
182- lastPrompted : null
221+ lastPrompted : undefined
183222 } ;
184223 return permissions ;
185224 }
@@ -199,15 +238,15 @@ export class MCPConfigManager {
199238 config . security . fileWritePermissions = {
200239 mode : 'ask' ,
201240 allowedFiles : [ ] ,
202- lastPrompted : null
241+ lastPrompted : undefined
203242 } ;
204243 }
205244
206245 const perms = config . security . fileWritePermissions ;
207246 perms . mode = mode ;
208247 perms . lastPrompted = new Date ( ) . toISOString ( ) ;
209248
210- if ( mode === 'allow-specific' && filePath ) {
249+ if ( mode === 'allow-specific' && filePath && perms . allowedFiles ) {
211250 if ( ! perms . allowedFiles . includes ( filePath ) ) {
212251 perms . allowedFiles . push ( filePath ) ;
213252 }
0 commit comments