Skip to content

Commit 5db7986

Browse files
authored
Merge pull request #720 from jeanlaurent/mcp-enhancements
adding a --work-dir to mcp mode
2 parents 7f79933 + 1e99b04 commit 5db7986

3 files changed

Lines changed: 41 additions & 23 deletions

File tree

cmd/root/common.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package root
2+
3+
import (
4+
"fmt"
5+
"log/slog"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
func setupWorkingDirectory(workingDir string) error {
11+
if workingDir == "" {
12+
return nil
13+
}
14+
15+
absWd, err := filepath.Abs(workingDir)
16+
if err != nil {
17+
return fmt.Errorf("invalid working directory: %w", err)
18+
}
19+
20+
info, err := os.Stat(absWd)
21+
if err != nil || !info.IsDir() {
22+
return fmt.Errorf("working directory does not exist or is not a directory: %s", absWd)
23+
}
24+
25+
if err := os.Chdir(absWd); err != nil {
26+
return fmt.Errorf("failed to change working directory: %w", err)
27+
}
28+
29+
_ = os.Setenv("PWD", absWd)
30+
slog.Debug("Working directory set", "path", absWd)
31+
32+
return nil
33+
}

cmd/root/mcp.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
)
1010

1111
type mcpFlags struct {
12-
runConfig config.RuntimeConfig
12+
workingDir string
13+
runConfig config.RuntimeConfig
1314
}
1415

1516
func newMCPCmd() *cobra.Command {
@@ -26,6 +27,7 @@ func newMCPCmd() *cobra.Command {
2627
RunE: flags.runMCPCommand,
2728
}
2829

30+
cmd.PersistentFlags().StringVar(&flags.workingDir, "working-dir", "", "Set the working directory for the session (applies to tools and relative paths)")
2931
addRuntimeConfigFlags(cmd, &flags.runConfig)
3032

3133
return cmd
@@ -35,5 +37,9 @@ func (f *mcpFlags) runMCPCommand(cmd *cobra.Command, args []string) error {
3537
telemetry.TrackCommand("mcp", args)
3638
ctx := cmd.Context()
3739

40+
if err := setupWorkingDirectory(f.workingDir); err != nil {
41+
return err
42+
}
43+
3844
return mcp.StartMCPServer(ctx, args[0], f.runConfig)
3945
}

cmd/root/run.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"log/slog"
88
"os"
9-
"path/filepath"
109

1110
tea "github.com/charmbracelet/bubbletea/v2"
1211
"github.com/spf13/cobra"
@@ -120,27 +119,7 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s
120119
}
121120

122121
func (f *runExecFlags) setupWorkingDirectory() error {
123-
if f.workingDir == "" {
124-
return nil
125-
}
126-
127-
absWd, err := filepath.Abs(f.workingDir)
128-
if err != nil {
129-
return fmt.Errorf("invalid working directory: %w", err)
130-
}
131-
132-
info, err := os.Stat(absWd)
133-
if err != nil || !info.IsDir() {
134-
return fmt.Errorf("working directory does not exist or is not a directory: %s", absWd)
135-
}
136-
137-
if err := os.Chdir(absWd); err != nil {
138-
return fmt.Errorf("failed to change working directory: %w", err)
139-
}
140-
141-
_ = os.Setenv("PWD", absWd)
142-
slog.Debug("Working directory set", "dir", absWd)
143-
return nil
122+
return setupWorkingDirectory(f.workingDir)
144123
}
145124

146125
// resolveAgentFile is a wrapper method that calls the agentfile.Resolve function

0 commit comments

Comments
 (0)