Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(runtime): extract codexMCPServerArgs from codexConfigArgs (#414) (#415)

fx-kurumi21cdaa2a8199
diff --git a/internal/runtime/codex_mcp_server_args_test.go b/internal/runtime/codex_mcp_server_args_test.go
new file mode 100644
index 00000000..c9d9f5fe
--- /dev/null
+++ b/internal/runtime/codex_mcp_server_args_test.go
@@ -0,0 +1,65 @@
+package runtime
+
+import (
+ "reflect"
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func TestCodexMCPServerArgsHTTP(t *testing.T) {
+ got := codexMCPServerArgs(domain.MCPServerDefinition{ID: "docs", Transport: "http", URL: "https://mcp.example/x", TimeoutSec: 30})
+ want := []string{
+ "-c", `mcp_servers.docs.url="https://mcp.example/x"`,
+ "-c", "mcp_servers.docs.tool_timeout_sec=30.0",
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("got %q\nwant %q", got, want)
+ }
+}
+
+func TestCodexMCPServerArgsStdio(t *testing.T) {
+ got := codexMCPServerArgs(domain.MCPServerDefinition{
+ ID: "fs",
+ Transport: "stdio",
+ Command: "node",
+ Args: []string{"server.js", "--root", "a b"},
+ WorkingDirectory: "tools/./fs",
+ Environment: map[string]string{"ZED": "1", "ALPHA": `say "hi"`},
+ })
+ want := []string{
+ "-c", `mcp_servers.fs.command="node"`,
+ "-c", `mcp_servers.fs.cwd="tools/fs"`,
+ "-c", `mcp_servers.fs.args=["server.js","--root","a b"]`,
+ "-c", `mcp_servers.fs.env={ ALPHA = "say \"hi\"", ZED = "1" }`,
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("got %q\nwant %q", got, want)
+ }
+}
+
+func TestCodexMCPServerArgsMinimalStdio(t *testing.T) {
+ got := codexMCPServerArgs(domain.MCPServerDefinition{ID: "bare", Command: "bin"})
+ want := []string{"-c", `mcp_servers.bare.command="bin"`}
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("got %q\nwant %q", got, want)
+ }
+}
+
+func TestCodexConfigArgsAppendsEachMCPServer(t *testing.T) {
+ servers := []domain.MCPServerDefinition{
+ {ID: "docs", Transport: "http", URL: "https://mcp.example/x"},
+ {ID: "fs", Command: "node", Args: []string{"s.js"}, TimeoutSec: 5},
+ }
+ got := codexConfigArgs("hi", nil, servers)
+ want := []string{
+ "-c", `developer_instructions="hi"`,
+ "-c", `shell_environment_policy.inherit="core"`,
+ }
+ for _, m := range servers {
+ want = append(want, codexMCPServerArgs(m)...)
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("got %q\nwant %q", got, want)
+ }
+}
diff --git a/internal/runtime/provider_codex.go b/internal/runtime/provider_codex.go
index 345db285..8ba69579 100644
--- a/internal/runtime/provider_codex.go
+++ b/internal/runtime/provider_codex.go
@@ -154,38 +154,46 @@ func codexConfigArgs(instructions string, skillPaths []string, servers []domain.
args = append(args, "-c", "skills.config=["+strings.Join(entries, ", ")+"]")
}
for _, m := range servers {
- prefix := "mcp_servers." + safeID(m.ID) + "."
- if m.Transport == "http" {
- args = append(args, "-c", prefix+"url="+strconv.Quote(m.URL))
- } else {
- args = append(args, "-c", prefix+"command="+strconv.Quote(m.Command))
- if cwd := MCPRuntimeWorkingDirectory(m); cwd != "" {
- args = append(args, "-c", prefix+"cwd="+strconv.Quote(cwd))
+ args = append(args, codexMCPServerArgs(m)...)
+ }
+ return args
+}
+
+// codexMCPServerArgs renders one MCP server as Codex `-c mcp_servers.<id>.*`
+// overrides; env keys are sorted so the argv is deterministic.
+func codexMCPServerArgs(m domain.MCPServerDefinition) []string {
+ var args []string
+ prefix := "mcp_servers." + safeID(m.ID) + "."
+ if m.Transport == "http" {
+ args = append(args, "-c", prefix+"url="+strconv.Quote(m.URL))
+ } else {
+ args = append(args, "-c", prefix+"command="+strconv.Quote(m.Command))
+ if cwd := MCPRuntimeWorkingDirectory(m); cwd != "" {
+ args = append(args, "-c", prefix+"cwd="+strconv.Quote(cwd))
+ }
+ if len(m.Args) > 0 {
+ parts := make([]string, len(m.Args))
+ for i, value := range m.Args {
+ parts[i] = strconv.Quote(value)
}
- if len(m.Args) > 0 {
- parts := make([]string, len(m.Args))
- for i, value := range m.Args {
- parts[i] = strconv.Quote(value)
- }
- args = append(args, "-c", prefix+"args=["+strings.Join(parts, ",")+"]")
+ args = append(args, "-c", prefix+"args=["+strings.Join(parts, ",")+"]")
+ }
+ env := resolveMap(m.Environment)
+ if len(env) > 0 {
+ keys := make([]string, 0, len(env))
+ for key := range env {
+ keys = append(keys, key)
}
- env := resolveMap(m.Environment)
- if len(env) > 0 {
- keys := make([]string, 0, len(env))
- for key := range env {
- keys = append(keys, key)
- }
- sort.Strings(keys)
- pairs := make([]string, 0, len(keys))
- for _, key := range keys {
- pairs = append(pairs, fmt.Sprintf("%s = %s", key, strconv.Quote(env[key])))
- }
- args = append(args, "-c", prefix+"env={ "+strings.Join(pairs, ", ")+" }")
+ sort.Strings(keys)
+ pairs := make([]string, 0, len(keys))
+ for _, key := range keys {
+ pairs = append(pairs, fmt.Sprintf("%s = %s", key, strconv.Quote(env[key])))
}
+ args = append(args, "-c", prefix+"env={ "+strings.Join(pairs, ", ")+" }")
}
- if m.TimeoutSec > 0 {
- args = append(args, "-c", fmt.Sprintf("%stool_timeout_sec=%d.0", prefix, m.TimeoutSec))
- }
+ }
+ if m.TimeoutSec > 0 {
+ args = append(args, "-c", fmt.Sprintf("%stool_timeout_sec=%d.0", prefix, m.TimeoutSec))
}
return args
}