Skip to content

Commit 0761e8d

Browse files
use executable's abs path when configuring mcp server (#1295)
1 parent 38a1b77 commit 0761e8d

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

src/pkg/mcp/setup.go

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,36 +141,52 @@ func getClientConfigPath(client string) (string, error) {
141141
}
142142

143143
// getDefangMCPConfig returns the default MCP config for Defang
144-
func getDefangMCPConfig() MCPServerConfig {
145-
return MCPServerConfig{
146-
Command: "npx",
147-
Args: []string{"-y", "defang@latest", "mcp", "serve"},
144+
func getDefangMCPConfig() (*MCPServerConfig, error) {
145+
currentPath, err := os.Executable()
146+
if err != nil {
147+
return nil, err
148148
}
149+
150+
return &MCPServerConfig{
151+
Command: currentPath,
152+
Args: []string{"mcp", "serve"},
153+
}, nil
149154
}
150155

151156
// getVSCodeDefangMCPConfig returns the default MCP config for Defang in VSCode format
152-
func getVSCodeDefangMCPConfig() VSCodeMCPServerConfig {
153-
return VSCodeMCPServerConfig{
154-
Type: "stdio",
155-
Command: "npx",
156-
Args: []string{"-y", "defang@latest", "mcp", "serve"},
157+
func getVSCodeDefangMCPConfig() (*VSCodeMCPServerConfig, error) {
158+
currentPath, err := os.Executable()
159+
if err != nil {
160+
return nil, err
157161
}
162+
return &VSCodeMCPServerConfig{
163+
Type: "stdio",
164+
Command: currentPath,
165+
Args: []string{"mcp", "serve"},
166+
}, nil
158167
}
159168

160169
// getVSCodeServerConfig returns a map with the VSCode-specific MCP server config
161-
func getVSCodeServerConfig() map[string]interface{} {
162-
config := getVSCodeDefangMCPConfig()
170+
func getVSCodeServerConfig() (map[string]interface{}, error) {
171+
config, err := getVSCodeDefangMCPConfig()
172+
if err != nil {
173+
return nil, err
174+
}
163175
return map[string]interface{}{
164176
"type": config.Type,
165177
"command": config.Command,
166178
"args": config.Args,
167-
}
179+
}, nil
168180
}
169181

170182
// handleVSCodeConfig handles the special case for VSCode settings.json
171183
func handleVSCodeConfig(configPath string) error {
172184
// Create or update the config file
173185
var existingData map[string]interface{}
186+
config, err := getVSCodeServerConfig()
187+
if err != nil {
188+
return fmt.Errorf("failed to get VSCode MCP config: %w", err)
189+
}
174190

175191
// Check if the file exists
176192
if _, err := os.Stat(configPath); err == nil {
@@ -192,7 +208,7 @@ func handleVSCodeConfig(configPath string) error {
192208
// Create new mcp section
193209
existingData["mcp"] = map[string]interface{}{
194210
"servers": map[string]interface{}{
195-
"defang": getVSCodeServerConfig(),
211+
"defang": config,
196212
},
197213
}
198214
} else {
@@ -205,7 +221,7 @@ func handleVSCodeConfig(configPath string) error {
205221
serversData, ok := mcpMap["servers"]
206222
if !ok {
207223
mcpMap["servers"] = map[string]interface{}{
208-
"defang": getVSCodeServerConfig(),
224+
"defang": config,
209225
}
210226
} else {
211227
serversMap, ok := serversData.(map[string]interface{})
@@ -214,7 +230,7 @@ func handleVSCodeConfig(configPath string) error {
214230
}
215231

216232
// Add or update the Defang MCP server config
217-
serversMap["defang"] = getVSCodeServerConfig()
233+
serversMap["defang"] = config
218234

219235
mcpMap["servers"] = serversMap
220236
}
@@ -226,7 +242,7 @@ func handleVSCodeConfig(configPath string) error {
226242
existingData = map[string]interface{}{
227243
"mcp": map[string]interface{}{
228244
"servers": map[string]interface{}{
229-
"defang": getVSCodeServerConfig(),
245+
"defang": config,
230246
},
231247
},
232248
}
@@ -302,8 +318,12 @@ func SetupClient(client string) error {
302318
config.MCPServers = make(map[string]MCPServerConfig)
303319
}
304320

321+
defangConfig, err := getDefangMCPConfig()
322+
if err != nil {
323+
return fmt.Errorf("failed to get Defang MCP config: %w", err)
324+
}
305325
// Add or update the Defang MCP server config
306-
config.MCPServers["defang"] = getDefangMCPConfig()
326+
config.MCPServers["defang"] = *defangConfig
307327

308328
// Write the config to the file
309329
data, err := json.MarshalIndent(config, "", " ")

0 commit comments

Comments
 (0)