mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
fix(mcp): expand home paths for local stdio server commands
This commit is contained in:
+19
-1
@@ -25,6 +25,24 @@ type headerTransport struct {
|
||||
headers map[string]string
|
||||
}
|
||||
|
||||
func expandHomeCommandPath(command string) string {
|
||||
if command == "" || command[0] != '~' {
|
||||
return command
|
||||
}
|
||||
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return command
|
||||
}
|
||||
if command == "~" {
|
||||
return home
|
||||
}
|
||||
if strings.HasPrefix(command, "~/") || strings.HasPrefix(command, "~\\") {
|
||||
return filepath.Join(home, command[2:])
|
||||
}
|
||||
return command
|
||||
}
|
||||
|
||||
func (t *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
// Clone the request to avoid modifying the original
|
||||
req = req.Clone(req.Context())
|
||||
@@ -324,7 +342,7 @@ func (m *Manager) ConnectServer(
|
||||
"command": cfg.Command,
|
||||
})
|
||||
// Create command with context
|
||||
cmd := exec.CommandContext(ctx, cfg.Command, cfg.Args...)
|
||||
cmd := exec.CommandContext(ctx, expandHomeCommandPath(cfg.Command), cfg.Args...)
|
||||
|
||||
// Build environment variables with proper override semantics
|
||||
// Use a map to ensure config variables override file variables
|
||||
|
||||
@@ -136,6 +136,22 @@ func TestLoadEnvFileNotFound(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandHomeCommandPath(t *testing.T) {
|
||||
homeDir := t.TempDir()
|
||||
t.Setenv("HOME", homeDir)
|
||||
t.Setenv("USERPROFILE", homeDir)
|
||||
|
||||
want := filepath.Join(homeDir, "bin", "my-mcp")
|
||||
got := expandHomeCommandPath("~" + string(os.PathSeparator) + filepath.Join("bin", "my-mcp"))
|
||||
if got != want {
|
||||
t.Fatalf("expandHomeCommandPath() = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
if got := expandHomeCommandPath("npx"); got != "npx" {
|
||||
t.Fatalf("expandHomeCommandPath() should leave bare commands unchanged, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvFilePriority(t *testing.T) {
|
||||
// Create a temporary .env file
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
Reference in New Issue
Block a user