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 OpenCode/DeepSeek MCP entry rendering from WriteAgentCLIMCPConfig (#395)

fx-kurumie1fd1c071c7e
diff --git a/internal/runtime/agent_cli.go b/internal/runtime/agent_cli.go
index d93f322c..8dbfeed6 100644
--- a/internal/runtime/agent_cli.go
+++ b/internal/runtime/agent_cli.go
@@ -146,48 +146,12 @@ func WriteAgentCLIMCPConfig(kind string, home AgentCLIHome, entries map[string]a
command, _ := entry["command"].(string)
headers := entry["headers"]
switch {
- case kind == AgentCLIOpenCode && url != "":
- server := map[string]any{"type": "remote", "url": url, "enabled": true, "timeout": 600000}
- if headers != nil {
- server["headers"] = headers
- }
- servers[id] = server
- case kind == AgentCLIOpenCode && command != "":
- argv := []any{command}
- if args, ok := entry["args"].([]string); ok {
- for _, arg := range args {
- argv = append(argv, arg)
- }
- } else if args, ok := entry["args"].([]any); ok {
- argv = append(argv, args...)
- }
- server := map[string]any{"type": "local", "command": argv, "enabled": true, "timeout": 600000}
- if env := entry["env"]; env != nil {
- server["environment"] = env
- }
- servers[id] = server
+ case kind == AgentCLIOpenCode && (url != "" || command != ""):
+ servers[id] = openCodeMCPServer(entry)
case kind == AgentCLIZCode && (url != "" || command != ""):
servers[id] = zcodeMCPServer(entry)
case kind == AgentCLIDeepSeek && (url != "" || command != ""):
- name := dshServerNameRe.ReplaceAllString(id, "_")
- if len(name) > 32 {
- name = name[:32]
- }
- config := map[string]any{"serverName": name, "toolCallTimeoutMs": 600000}
- if url != "" {
- config["transport"], config["url"] = "streamable-http", url
- if headers != nil {
- config["headers"] = headers
- }
- } else {
- config["transport"], config["command"] = "stdio", command
- for _, key := range []string{"args", "env", "cwd"} {
- if value := entry[key]; value != nil {
- config[key] = value
- }
- }
- }
- dshInserts = append(dshInserts, map[string]any{"id": "agentflow-mcp-" + name, "name": "@deepseek-ai/dsh-mcp-client", "config": config})
+ dshInserts = append(dshInserts, deepSeekMCPInsert(id, entry))
case url != "":
key := "url"
if kind == AgentCLIAntigravity {
@@ -245,6 +209,57 @@ func WriteAgentCLIMCPConfig(kind string, home AgentCLIHome, entries map[string]a
return writeAgentCLIFile(home, rel, data)
}
+// openCodeMCPServer renders one MCP entry for OpenCode: a remote server for
+// a URL entry, otherwise a local server whose command is the argv array.
+func openCodeMCPServer(entry map[string]any) map[string]any {
+ if url, _ := entry["url"].(string); url != "" {
+ server := map[string]any{"type": "remote", "url": url, "enabled": true, "timeout": 600000}
+ if headers := entry["headers"]; headers != nil {
+ server["headers"] = headers
+ }
+ return server
+ }
+ command, _ := entry["command"].(string)
+ argv := []any{command}
+ if args, ok := entry["args"].([]string); ok {
+ for _, arg := range args {
+ argv = append(argv, arg)
+ }
+ } else if args, ok := entry["args"].([]any); ok {
+ argv = append(argv, args...)
+ }
+ server := map[string]any{"type": "local", "command": argv, "enabled": true, "timeout": 600000}
+ if env := entry["env"]; env != nil {
+ server["environment"] = env
+ }
+ return server
+}
+
+// deepSeekMCPInsert renders one MCP entry as a dsh-mcp-client plugin insert;
+// the server name is the sanitized id capped at 32 characters.
+func deepSeekMCPInsert(id string, entry map[string]any) map[string]any {
+ name := dshServerNameRe.ReplaceAllString(id, "_")
+ if len(name) > 32 {
+ name = name[:32]
+ }
+ config := map[string]any{"serverName": name, "toolCallTimeoutMs": 600000}
+ if url, _ := entry["url"].(string); url != "" {
+ config["transport"], config["url"] = "streamable-http", url
+ if headers := entry["headers"]; headers != nil {
+ config["headers"] = headers
+ }
+ } else {
+ command, _ := entry["command"].(string)
+ config["transport"], config["command"] = "stdio", command
+ for _, key := range []string{"args", "env", "cwd"} {
+ if value := entry[key]; value != nil {
+ config[key] = value
+ }
+ }
+ }
+ return map[string]any{"id": "agentflow-mcp-" + name, "name": "@deepseek-ai/dsh-mcp-client", "config": config}
+}
+
// agyChatPermissions are the Antigravity deny rules of AI 对话.
var agyChatPermissions = map[string]any{"allow": []string{"mcp(*)"}, "deny": []string{"command(*)", "write_file(*)", "read_url(*)"}}
diff --git a/internal/runtime/agent_cli_mcp_render_test.go b/internal/runtime/agent_cli_mcp_render_test.go
new file mode 100644
index 00000000..4dc69bca
--- /dev/null
+++ b/internal/runtime/agent_cli_mcp_render_test.go
@@ -0,0 +1,99 @@
+package runtime
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "reflect"
+ "strings"
+ "testing"
+)
+
+func readAgentCLIMCPDoc(t *testing.T, home, rel string) any {
+ t.Helper()
+ data, err := os.ReadFile(filepath.Join(home, rel))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var doc any
+ if err := json.Unmarshal(data, &doc); err != nil {
+ t.Fatal(err)
+ }
+ return doc
+}
+
+func normalizeJSON(t *testing.T, value any) any {
+ t.Helper()
+ data, err := json.Marshal(value)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var out any
+ if err := json.Unmarshal(data, &out); err != nil {
+ t.Fatal(err)
+ }
+ return out
+}
+
+func TestWriteAgentCLIMCPConfigOpenCodeServers(t *testing.T) {
+ home := t.TempDir()
+ entries := map[string]any{
+ "http": map[string]any{"type": "http", "url": "http://127.0.0.1:1/mcp", "headers": map[string]any{"Authorization": "Bearer x"}},
+ "http-bare": map[string]any{"url": "http://127.0.0.1:2/mcp"},
+ "stdio-str": map[string]any{"command": "node", "args": []string{"a.js", "--flag"}, "env": map[string]any{"K": "V"}, "cwd": "/w"},
+ "stdio-any": map[string]any{"command": "py", "args": []any{"b.py"}},
+ "empty": map[string]any{},
+ "invalid": "not-a-map",
+ }
+ if err := WriteAgentCLIMCPConfig(AgentCLIOpenCode, HostAgentCLIHome(home), entries, AgentCLIMCPOptions{}); err != nil {
+ t.Fatal(err)
+ }
+ got := readAgentCLIMCPDoc(t, home, ".config/opencode/opencode.json").(map[string]any)["mcp"]
+ want := normalizeJSON(t, map[string]any{
+ "http": map[string]any{"type": "remote", "url": "http://127.0.0.1:1/mcp", "enabled": true, "timeout": 600000, "headers": map[string]any{"Authorization": "Bearer x"}},
+ "http-bare": map[string]any{"type": "remote", "url": "http://127.0.0.1:2/mcp", "enabled": true, "timeout": 600000},
+ "stdio-str": map[string]any{"type": "local", "command": []any{"node", "a.js", "--flag"}, "enabled": true, "timeout": 600000, "environment": map[string]any{"K": "V"}},
+ "stdio-any": map[string]any{"type": "local", "command": []any{"py", "b.py"}, "enabled": true, "timeout": 600000},
+ })
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("opencode mcp:\n got %v\nwant %v", got, want)
+ }
+}
+
+func TestWriteAgentCLIMCPConfigDeepSeekInserts(t *testing.T) {
+ home := t.TempDir()
+ longID := "agentflow.platform/" + strings.Repeat("x", 40)
+ entries := map[string]any{
+ "a-http": map[string]any{"url": "http://127.0.0.1:1/mcp", "headers": map[string]any{"H": "1"}},
+ "b.stdio": map[string]any{"command": "node", "args": []string{"s.js"}, "env": map[string]any{"K": "V"}, "cwd": "/w"},
+ longID: map[string]any{"command": "bin"},
+ "skip": map[string]any{},
+ }
+ if err := WriteAgentCLIMCPConfig(AgentCLIDeepSeek, HostAgentCLIHome(home), entries, AgentCLIMCPOptions{}); err != nil {
+ t.Fatal(err)
+ }
+ longName := ("agentflow_platform_" + strings.Repeat("x", 40))[:32]
+ want := normalizeJSON(t, []any{map[string]any{"insert": []any{
+ map[string]any{"id": "agentflow-mcp-a-http", "name": "@deepseek-ai/dsh-mcp-client", "config": map[string]any{
+ "serverName": "a-http", "toolCallTimeoutMs": 600000, "transport": "streamable-http", "url": "http://127.0.0.1:1/mcp", "headers": map[string]any{"H": "1"},
+ }},
+ map[string]any{"id": "agentflow-mcp-" + longName, "name": "@deepseek-ai/dsh-mcp-client", "config": map[string]any{
+ "serverName": longName, "toolCallTimeoutMs": 600000, "transport": "stdio", "command": "bin",
+ }},
+ map[string]any{"id": "agentflow-mcp-b_stdio", "name": "@deepseek-ai/dsh-mcp-client", "config": map[string]any{
+ "serverName": "b_stdio", "toolCallTimeoutMs": 600000, "transport": "stdio", "command": "node", "args": []any{"s.js"}, "env": map[string]any{"K": "V"}, "cwd": "/w",
+ }},
+ }}})
+ rel := agentCLIMCPPaths[AgentCLIDeepSeek]
+ if got := readAgentCLIMCPDoc(t, home, rel); !reflect.DeepEqual(got, want) {
+ t.Fatalf("deepseek mcp:\n got %v\nwant %v", got, want)
+ }
+
+ empty := t.TempDir()
+ if err := WriteAgentCLIMCPConfig(AgentCLIDeepSeek, HostAgentCLIHome(empty), map[string]any{"skip": map[string]any{}}, AgentCLIMCPOptions{}); err != nil {
+ t.Fatal(err)
+ }
+ if got := readAgentCLIMCPDoc(t, empty, rel); !reflect.DeepEqual(got, []any{}) {
+ t.Fatalf("deepseek empty mcp: %v", got)
+ }
+}