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.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
【架构优化】从 PrepareMCPCall 中拆出 MCP server 配置条目转换 mcpCallConfigServers (#581)
fx-kurumied635305a6a7
diff --git a/internal/runtime/mcp_call.go b/internal/runtime/mcp_call.goindex 61608eff..e84bcb55 100644--- a/internal/runtime/mcp_call.go+++ b/internal/runtime/mcp_call.go@@ -68,32 +68,7 @@ func PrepareMCPCall(sandbox Sandbox, workspace string, servers []domain.MCPServeruntimeServers[i].Command = filepath.Join(workspace, filepath.FromSlash(runtimeServers[i].Command))}}- cfg := mcpCallConfigFile{Servers: make([]mcpCallServer, 0, len(runtimeServers))}- for _, server := range runtimeServers {- transport := strings.ToLower(strings.TrimSpace(server.Transport))- if transport == "" {- transport = "stdio"- }- if transport == "http" && strings.TrimSpace(server.URL) == "" {- continue- }- if transport == "stdio" && strings.TrimSpace(server.Command) == "" {- continue- }- cfg.Servers = append(cfg.Servers, mcpCallServer{- ID: server.ID,- Name: server.Name,- MarketID: server.MarketID,- URL: server.URL,- Transport: transport,- Command: server.Command, Args: append([]string(nil), server.Args...),- CWD: MCPRuntimeWorkingDirectory(server), Environment: resolveMap(server.Environment),- AuthType: server.AuthType,- Headers: cloneHeaders(server.Headers),- TimeoutSec: server.TimeoutSec,- Aliases: mcpCallAliases(server),- })- }+ cfg := mcpCallConfigFile{Servers: mcpCallConfigServers(runtimeServers)}raw, err := json.MarshalIndent(cfg, "", " ")if err != nil {return nil, err@@ -129,6 +104,41 @@ func PrepareMCPCall(sandbox Sandbox, workspace string, servers []domain.MCPServe}, nil}+// mcpCallConfigServers converts runtime MCP server definitions into the+// entries written to mcp-servers.json. The transport defaults to stdio, and+// servers missing the URL (http) or command (stdio) they need are skipped.+func mcpCallConfigServers(servers []domain.MCPServerDefinition) []mcpCallServer {+ out := make([]mcpCallServer, 0, len(servers))+ for _, server := range servers {+ transport := strings.ToLower(strings.TrimSpace(server.Transport))+ if transport == "" {+ transport = "stdio"+ }+ if transport == "http" && strings.TrimSpace(server.URL) == "" {+ continue+ }+ if transport == "stdio" && strings.TrimSpace(server.Command) == "" {+ continue+ }+ out = append(out, mcpCallServer{+ ID: server.ID,+ Name: server.Name,+ MarketID: server.MarketID,+ URL: server.URL,+ Transport: transport,+ Command: server.Command,+ Args: append([]string(nil), server.Args...),+ CWD: MCPRuntimeWorkingDirectory(server),+ Environment: resolveMap(server.Environment),+ AuthType: server.AuthType,+ Headers: cloneHeaders(server.Headers),+ TimeoutSec: server.TimeoutSec,+ Aliases: mcpCallAliases(server),+ })+ }+ return out+}+func mcpCallWrapperScript(helperPath, fallback string) string {var b strings.Builderb.WriteString("#!/bin/sh\n")diff --git a/internal/runtime/mcp_call_config_servers_test.go b/internal/runtime/mcp_call_config_servers_test.gonew file mode 100644index 00000000..4d8c2f9a--- /dev/null+++ b/internal/runtime/mcp_call_config_servers_test.go@@ -0,0 +1,103 @@+package runtime++import (+ "encoding/json"+ "testing"++ "agentflow/internal/domain"+)++func TestMCPCallConfigServersNormalizesTransportAndSkipsIncomplete(t *testing.T) {+ servers := []domain.MCPServerDefinition{+ {ID: "stdio-default", Name: "Local", Command: "/bin/mcp", Args: []string{"--serve"}},+ {ID: "http-upper", Name: "Remote", Transport: " HTTP ", URL: "https://mcp.example.com/mcp", AuthType: "bearer", Headers: map[string]string{"X-Team": "a"}, TimeoutSec: 30},+ {ID: "http-missing-url", Transport: "http", URL: " "},+ {ID: "stdio-missing-command", Transport: "stdio", Command: " "},+ }+ got := mcpCallConfigServers(servers)+ if len(got) != 2 {+ t.Fatalf("servers = %+v, want 2 entries", got)+ }+ local, remote := got[0], got[1]+ if local.ID != "stdio-default" || local.Transport != "stdio" || local.Command != "/bin/mcp" || len(local.Args) != 1 || local.Args[0] != "--serve" {+ t.Fatalf("stdio server = %+v", local)+ }+ if remote.ID != "http-upper" || remote.Transport != "http" || remote.URL != "https://mcp.example.com/mcp" || remote.AuthType != "bearer" || remote.TimeoutSec != 30 || remote.Headers["X-Team"] != "a" {+ t.Fatalf("http server = %+v", remote)+ }+ if len(remote.Aliases) == 0 || remote.Aliases[0] != "http-upper" {+ t.Fatalf("http aliases = %v, want id first", remote.Aliases)+ }++ servers[0].Args[0] = "mutated"+ servers[1].Headers["X-Team"] = "mutated"+ if local.Args[0] != "--serve" || remote.Headers["X-Team"] != "a" {+ t.Fatalf("config entries share slices/maps with source definitions: args=%v headers=%v", local.Args, remote.Headers)+ }+}++func TestMCPCallConfigServersEmptyMarshalsAsArray(t *testing.T) {+ raw, err := json.Marshal(mcpCallConfigFile{Servers: mcpCallConfigServers(nil)})+ if err != nil {+ t.Fatal(err)+ }+ if string(raw) != `{"servers":[]}` {+ t.Fatalf("config = %s, want empty servers array", raw)+ }+}++func TestMCPCallConfigServersCarriesIdentityEnvironmentAndWorkingDirectory(t *testing.T) {+ t.Setenv("LOOPFORGE_TEST_MCP_CALL_TOKEN", "tok-123")+ servers := []domain.MCPServerDefinition{+ {+ ID: "mcp-github-1",+ Name: "GitHub",+ MarketID: "market-github",+ Command: "/bin/github-mcp",+ WorkingDirectory: "tools/../srv",+ Environment: map[string]string{+ "PLAIN": "value",+ "TOKEN": "Bearer ${secret:LOOPFORGE_TEST_MCP_CALL_TOKEN}",+ "MISSING": "${secret:LOOPFORGE_TEST_MCP_CALL_UNSET}",+ },+ },+ {+ ID: "Files.Server",+ Command: "bin/files-mcp",+ WorkingDirectory: "data",+ FileRefs: []domain.MCPFileRef{{Path: "bin/files-mcp"}},+ },+ }+ got := mcpCallConfigServers(servers)+ if len(got) != 2 {+ t.Fatalf("servers = %+v, want 2 entries", got)+ }+ gh, files := got[0], got[1]+ if gh.Name != "GitHub" || gh.MarketID != "market-github" {+ t.Fatalf("identity = name %q market %q, want GitHub / market-github", gh.Name, gh.MarketID)+ }+ if gh.CWD != "srv" {+ t.Fatalf("cwd = %q, want cleaned working directory srv", gh.CWD)+ }+ if len(gh.Environment) != 2 || gh.Environment["PLAIN"] != "value" || gh.Environment["TOKEN"] != "Bearer tok-123" {+ t.Fatalf("environment = %v, want PLAIN and resolved TOKEN only", gh.Environment)+ }+ if _, ok := gh.Environment["MISSING"]; ok {+ t.Fatalf("unresolved secret leaked into environment: %v", gh.Environment)+ }+ wantAliases := []string{"mcp-github-1", "market-github", "GitHub"}+ if len(gh.Aliases) < len(wantAliases) {+ t.Fatalf("aliases = %v, want prefix %v", gh.Aliases, wantAliases)+ }+ for i, alias := range wantAliases {+ if gh.Aliases[i] != alias {+ t.Fatalf("aliases = %v, want prefix %v", gh.Aliases, wantAliases)+ }+ }+ if files.CWD != ".agentflow/mcp/filesserver/data" {+ t.Fatalf("file-backed cwd = %q, want .agentflow/mcp/filesserver/data", files.CWD)+ }+ if len(files.Environment) != 0 {+ t.Fatalf("environment without definitions = %v, want none", files.Environment)+ }+}