From 43acea2a464926f27eb1e751c748da9b0fa0f139 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Tue, 24 Nov 2026 16:26:35 +0800 Subject: [PATCH] fix(runtime): omit empty stdio MCP arguments --- internal/runtime/agent_cli_mcp_render_test.go | 33 +++++++++++++++++++ internal/runtime/gate_mcp_test.go | 9 +++++ internal/runtime/materialize.go | 5 ++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/internal/runtime/agent_cli_mcp_render_test.go b/internal/runtime/agent_cli_mcp_render_test.go index 304dbd220..f638535d5 100644 --- a/internal/runtime/agent_cli_mcp_render_test.go +++ b/internal/runtime/agent_cli_mcp_render_test.go @@ -7,6 +7,8 @@ import ( "reflect" "strings" "testing" + + "agentflow/internal/domain" ) func readAgentCLIMCPDoc(t *testing.T, home, rel string) any { @@ -118,3 +120,34 @@ func TestWriteAgentCLIMCPConfigKimiToolTimeout(t *testing.T) { t.Fatalf("kimi mcp:\n got %v\nwant %v", got, want) } } + +func TestWriteAgentCLIMCPConfigStdioArgs(t *testing.T) { + entries, err := mcpConfigEntries([]domain.MCPServerDefinition{ + {ID: gateMCPServerID, Command: "gate-mcp"}, + {ID: "empty-args", Command: "tool", Args: []string{}}, + {ID: "with-args", Command: "node", Args: []string{"server.js", "--stdio"}}, + }) + if err != nil { + t.Fatal(err) + } + for _, kind := range []string{AgentCLIPi, AgentCLIKimi, AgentCLICursor, AgentCLIAntigravity} { + t.Run(kind, func(t *testing.T) { + home := t.TempDir() + if err := WriteAgentCLIMCPConfig(kind, HostAgentCLIHome(home), entries, AgentCLIMCPOptions{}); err != nil { + t.Fatal(err) + } + doc := readAgentCLIMCPDoc(t, home, agentCLIMCPPaths[kind]).(map[string]any) + servers := doc["mcpServers"].(map[string]any) + for _, id := range []string{gateMCPServerID, "empty-args"} { + server := servers[id].(map[string]any) + if args, ok := server["args"]; ok { + t.Errorf("%s should omit args, got %#v", id, args) + } + } + want := []any{"server.js", "--stdio"} + if got := servers["with-args"].(map[string]any)["args"]; !reflect.DeepEqual(got, want) { + t.Errorf("stdio args = %#v, want %#v", got, want) + } + }) + } +} diff --git a/internal/runtime/gate_mcp_test.go b/internal/runtime/gate_mcp_test.go index 83ba6f4bd..cdca639ea 100644 --- a/internal/runtime/gate_mcp_test.go +++ b/internal/runtime/gate_mcp_test.go @@ -77,6 +77,15 @@ func TestMaterializeInvocationInjectsGateMCP(t *testing.T) { t.Fatalf("mcp config missing %q:\n%s", want, text) } } + var config struct { + MCPServers map[string]map[string]json.RawMessage `json:"mcpServers"` + } + if err := json.Unmarshal(raw, &config); err != nil { + t.Fatal(err) + } + if args, ok := config.MCPServers[gateMCPServerID]["args"]; ok { + t.Fatalf("gate MCP has no command-line arguments, got args %s", args) + } if info, err := os.Stat(filepath.Join(workspace, ".agentflow", "config", "reviewer", "gate-mcp")); err != nil || info.Mode().Perm()&0o111 == 0 { t.Fatal(err) } diff --git a/internal/runtime/materialize.go b/internal/runtime/materialize.go index ea1dd3694..84c70ef24 100644 --- a/internal/runtime/materialize.go +++ b/internal/runtime/materialize.go @@ -448,7 +448,10 @@ func mcpConfigEntries(servers []domain.MCPServerDefinition) (map[string]any, err } } else { entry["command"] = m.Command - entry["args"] = m.Args + // Some MCP clients reject null for this optional field. + if len(m.Args) > 0 { + entry["args"] = m.Args + } if cwd := MCPRuntimeWorkingDirectory(m); cwd != "" { entry["cwd"] = cwd }