@@ -244,17 +244,10 @@ func handleVSCodeConfig(configPath string) error {
244
244
}
245
245
246
246
// Check if the file exists
247
- if _ , err := os .Stat (configPath ); err == nil {
248
- // File exists, read it
249
- data , err := os .ReadFile (configPath )
250
- if err != nil {
251
- return fmt .Errorf ("failed to read config file: %w" , err )
252
- }
253
-
254
- // Parse the JSON into a generic map to preserve all settings
247
+ if data , err := os .ReadFile (configPath ); err == nil {
248
+ // File exists, parse it
255
249
if err := json .Unmarshal (data , & existingData ); err != nil {
256
- // If we can't parse it, start fresh
257
- existingData = make (map [string ]any )
250
+ return fmt .Errorf ("failed to unmarshal existing vscode config %w" , err )
258
251
}
259
252
260
253
// Check if "servers" section exists
@@ -271,6 +264,8 @@ func handleVSCodeConfig(configPath string) error {
271
264
} else {
272
265
return errors .New ("failed to assert 'servers' section as map[string]any" )
273
266
}
267
+ } else if ! os .IsNotExist (err ) {
268
+ return fmt .Errorf ("failed to read config file: %w" , err )
274
269
} else {
275
270
// File doesn't exist, create a new config with minimal settings
276
271
existingData = map [string ]any {
@@ -296,28 +291,33 @@ func handleVSCodeConfig(configPath string) error {
296
291
297
292
func handleStandardConfig (configPath string ) error {
298
293
// For all other clients, use the standard format
294
+ var existingData map [string ]any
299
295
var config MCPConfig
300
296
301
297
// Check if the file exists
302
- if _ , err := os .Stat (configPath ); err == nil {
303
- // File exists, read it
304
- data , err := os .ReadFile (configPath )
305
- if err != nil {
306
- return fmt .Errorf ("failed to read config file: %w" , err )
298
+ if data , err := os .ReadFile (configPath ); err == nil {
299
+ // Parse the JSON into a generic map to preserve all settings
300
+ if err := json .Unmarshal (data , & existingData ); err != nil {
301
+ return fmt .Errorf ("failed to unmarshal existing config: %w" , err )
307
302
}
308
303
309
- // Parse the JSON
310
- if err := json .Unmarshal (data , & config ); err != nil {
311
- // If we can't parse it, start fresh
312
- config = MCPConfig {
313
- MCPServers : make (map [string ]MCPServerConfig ),
304
+ // Try to extract MCPServers from existing data
305
+ if mcpServersData , ok := existingData ["mcpServers" ]; ok {
306
+ // Convert back to MCPConfig structure
307
+ mcpServersJSON , err := json .Marshal (map [string ]any {"mcpServers" : mcpServersData })
308
+ if err != nil {
309
+ return fmt .Errorf ("failed to marshal mcpServers: %w" , err )
310
+ }
311
+ err = json .Unmarshal (mcpServersJSON , & config )
312
+ if err != nil {
313
+ return fmt .Errorf ("failed to unmarshal mcpServers: %w" , err )
314
314
}
315
315
}
316
+ } else if ! os .IsNotExist (err ) {
317
+ return fmt .Errorf ("failed to read config file: %w" , err )
316
318
} else {
317
319
// File doesn't exist, create a new config
318
- config = MCPConfig {
319
- MCPServers : make (map [string ]MCPServerConfig ),
320
- }
320
+ existingData = make (map [string ]any )
321
321
}
322
322
323
323
if config .MCPServers == nil {
@@ -331,8 +331,11 @@ func handleStandardConfig(configPath string) error {
331
331
// Add or update the Defang MCP server config
332
332
config .MCPServers ["defang" ] = * defangConfig
333
333
334
+ // Update the existingData with the new MCPServers
335
+ existingData ["mcpServers" ] = config .MCPServers
336
+
334
337
// Write the config to the file
335
- data , err := json .MarshalIndent (config , "" , " " )
338
+ data , err := json .MarshalIndent (existingData , "" , " " )
336
339
if err != nil {
337
340
return fmt .Errorf ("failed to marshal config: %w" , err )
338
341
}
0 commit comments