Skip to content

Commit ad90ff4

Browse files
nullfunclionello
andauthored
fix for vscode mcp setup - location of settings changed with latest vcode (#1318)
Co-authored-by: Lionello Lunesu <[email protected]> Co-authored-by: Lio李歐 <[email protected]>
1 parent a490a39 commit ad90ff4

File tree

3 files changed

+21
-45
lines changed

3 files changed

+21
-45
lines changed

src/cmd/cli/command/commands.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/DefangLabs/defang/src/pkg/cli/compose"
2323
"github.com/DefangLabs/defang/src/pkg/clouds/aws"
2424
"github.com/DefangLabs/defang/src/pkg/logs"
25+
"github.com/DefangLabs/defang/src/pkg/mcp"
2526
"github.com/DefangLabs/defang/src/pkg/scope"
2627
"github.com/DefangLabs/defang/src/pkg/term"
2728
"github.com/DefangLabs/defang/src/pkg/track"
@@ -294,7 +295,7 @@ func SetupCommands(ctx context.Context, version string) {
294295
// MCP Command
295296
mcpCmd.AddCommand(mcpSetupCmd)
296297
mcpCmd.AddCommand(mcpServerCmd)
297-
mcpSetupCmd.Flags().String("client", "", "MCP setup client (supports: claude, windsurf, cursor, vscode)")
298+
mcpSetupCmd.Flags().String("client", "", fmt.Sprintf("MCP setup client %v", mcp.ValidClients))
298299
mcpServerCmd.Flags().Int("auth-server", 0, "auth server port")
299300
mcpSetupCmd.MarkFlagRequired("client")
300301
RootCmd.AddCommand(mcpCmd)

src/cmd/cli/command/mcp.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7-
"strings"
87

98
"github.com/DefangLabs/defang/src/pkg/cli"
109
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
@@ -110,8 +109,7 @@ var mcpSetupCmd = &cobra.Command{
110109
RunE: func(cmd *cobra.Command, args []string) error {
111110
term.Debug("Setting up MCP client")
112111
client, _ := cmd.Flags().GetString("client")
113-
client = strings.ToLower(client)
114-
term.Debug("Client: ", client)
112+
term.Debugf("MCP Client: %q", client)
115113
if err := mcp.SetupClient(client); err != nil {
116114
return err
117115
}

src/pkg/mcp/setup.go

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mcp
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"os"
78
"path/filepath"
@@ -86,7 +87,7 @@ func ParseMCPClient(clientStr string) (MCPClient, error) {
8687
clientStr = strings.ToLower(clientStr)
8788
client := MCPClient(clientStr)
8889
if !slices.Contains(ValidClients, client) {
89-
return "", fmt.Errorf("invalid MCP client: %q. Valid MCP clients are: %v", client, ValidClients)
90+
return "", fmt.Errorf("invalid MCP client: %q. Valid MCP clients are %v", clientStr, ValidClients)
9091
}
9192
return client, nil
9293
}
@@ -240,48 +241,25 @@ func handleVSCodeConfig(configPath string) error {
240241
existingData = make(map[string]interface{})
241242
}
242243

243-
// Check if mcp section exists
244-
mcpData, ok := existingData["mcp"]
244+
// Check if "servers" section exists
245+
serversSection, ok := existingData["servers"]
245246
if !ok {
246-
// Create new mcp section
247-
existingData["mcp"] = map[string]interface{}{
248-
"servers": map[string]interface{}{
249-
"defang": config,
250-
},
251-
}
252-
} else {
253-
// Update existing mcp section
254-
mcpMap, ok := mcpData.(map[string]interface{})
255-
if !ok {
256-
mcpMap = make(map[string]interface{})
257-
}
258-
259-
serversData, ok := mcpMap["servers"]
260-
if !ok {
261-
mcpMap["servers"] = map[string]interface{}{
262-
"defang": config,
263-
}
264-
} else {
265-
serversMap, ok := serversData.(map[string]interface{})
266-
if !ok {
267-
serversMap = make(map[string]interface{})
268-
}
269-
270-
// Add or update the Defang MCP server config
271-
serversMap["defang"] = config
272-
273-
mcpMap["servers"] = serversMap
274-
}
247+
// Create new "servers" section
248+
existingData["servers"] = map[string]interface{}{}
249+
serversSection = existingData["servers"]
250+
}
275251

276-
existingData["mcp"] = mcpMap
252+
if mcpMap, ok := serversSection.(map[string]interface{}); ok {
253+
mcpMap["defang"] = config
254+
existingData["servers"] = mcpMap
255+
} else {
256+
return errors.New("failed to assert 'servers' section as map[string]interface{}")
277257
}
278258
} else {
279259
// File doesn't exist, create a new config with minimal settings
280260
existingData = map[string]interface{}{
281-
"mcp": map[string]interface{}{
282-
"servers": map[string]interface{}{
283-
"defang": config,
284-
},
261+
"servers": map[string]interface{}{
262+
"defang": config,
285263
},
286264
}
287265
}
@@ -351,11 +329,10 @@ func handleStandardConfig(configPath string) error {
351329
return nil
352330
}
353331

354-
func SetupClient(clientValue string) error {
355-
client, err := ParseMCPClient(clientValue)
332+
func SetupClient(clientStr string) error {
333+
client, err := ParseMCPClient(clientStr)
356334
if err != nil {
357-
// cast the client string to MCPClient
358-
return fmt.Errorf("invalid MCP client: %q. Valid MCP clients are: %v", client, ValidClients)
335+
return err
359336
}
360337

361338
track.Evt("MCP Setup Client: ", track.P("client", client))

0 commit comments

Comments
 (0)