Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/cmd/cli/command/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ var mcpSetupCmd = &cobra.Command{
client, _ := cmd.Flags().GetString("client")

if client != "" {

// Aliases mapping
switch client {
case "code":
Expand Down
26 changes: 19 additions & 7 deletions src/pkg/mcp/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,15 @@ func handleVSCodeConfig(configPath string) error {

// Check if the file exists
if data, err := os.ReadFile(configPath); err == nil {
// File exists, parse it
if err := json.Unmarshal(data, &existingData); err != nil {
return fmt.Errorf("failed to unmarshal existing vscode config %w", err)
// Check if file is empty or only contains whitespace
if len(strings.TrimSpace(string(data))) == 0 {
// File is empty, treat as new config
existingData = make(map[string]any)
} else {
// File is not empty, attempt to parse it
if err := json.Unmarshal(data, &existingData); err != nil {
return fmt.Errorf("failed to unmarshal existing vscode config: %w", err)
}
}

// Check if "servers" section exists
Expand Down Expand Up @@ -310,9 +316,15 @@ func handleStandardConfig(configPath string) error {

// Check if the file exists
if data, err := os.ReadFile(configPath); err == nil {
// Parse the JSON into a generic map to preserve all settings
if err := json.Unmarshal(data, &existingData); err != nil {
return fmt.Errorf("failed to unmarshal existing config: %w", err)
// Check if file is empty or only contains whitespace
if len(strings.TrimSpace(string(data))) == 0 {
// File is empty, treat as new config
existingData = make(map[string]any)
} else {
// Parse the JSON into a generic map to preserve all settings
if err := json.Unmarshal(data, &existingData); err != nil {
return fmt.Errorf("failed to unmarshal existing config: %w", err)
}
}

// Try to extract MCPServers from existing data
Expand Down Expand Up @@ -399,7 +411,7 @@ func SetupClient(clientStr string) error {
}
}

term.Infof("Restart %s for the changes to take effect.\n", client)
term.Infof("Ensure that %s is upgraded to the latest version and restarted so the changes can take effect.\n", client)

return nil
}
34 changes: 34 additions & 0 deletions src/pkg/mcp/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,24 @@ func TestWriteConfig(t *testing.T) {
existingData: `{"servers": "not an object}`,
expectedError: true,
},
{
name: "vscode_config_new_file_empty",
fileExists: true,
vscodeConfig: true,
existingData: "",
expectedData: `{
"servers": {
"defang": {
"args": [
"mcp",
"serve"
],
"command": %s,
"type": "stdio"
}
}
}`,
},
{
name: "standard_config_new_file",
fileExists: false,
Expand Down Expand Up @@ -593,6 +611,22 @@ func TestWriteConfig(t *testing.T) {
existingData: `{"mcpServers": "not an object}`,
expectedError: true,
},
{
name: "standard_config_new_file_empty",
fileExists: true,
existingData: "",
expectedData: `{
"mcpServers": {
"defang": {
"command": %s,
"args": [
"mcp",
"serve"
]
}
}
}`,
},
}
for _, tt := range test {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading