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(domain): split ValidateMCPServer into transport, scope and auth checks (#298)

fx-kurumi1c6067cc46e8
diff --git a/internal/domain/assets.go b/internal/domain/assets.go
index 28cb7227..e160435e 100644
--- a/internal/domain/assets.go
+++ b/internal/domain/assets.go
@@ -291,42 +291,66 @@ func ValidateMCPServer(v MCPServerDefinition) error {
if strings.TrimSpace(v.ID) == "" || strings.TrimSpace(v.Name) == "" {
return fmt.Errorf("MCP 需要 id 和 name")
}
+ if err := validateMCPTransport(v); err != nil {
+ return err
+ }
+ if v.TimeoutSec < 0 || v.TimeoutSec > 600 {
+ return fmt.Errorf("timeoutSec 需在 0–600 之间")
+ }
+ if err := validateMCPScope(v); err != nil {
+ return err
+ }
+ return validateMCPAuth(v)
+}
+
+// validateMCPTransport checks the launch settings required by v.Transport.
+func validateMCPTransport(v MCPServerDefinition) error {
switch strings.TrimSpace(v.Transport) {
case "http":
if !strings.HasPrefix(v.URL, "http://") && !strings.HasPrefix(v.URL, "https://") {
return fmt.Errorf("HTTP MCP 需要有效的 url")
}
+ return nil
case "stdio", "":
- if strings.TrimSpace(v.Command) == "" {
- return fmt.Errorf("stdio MCP 需要 command")
- }
- if strings.ContainsRune(v.InstallScript, '\x00') {
- return fmt.Errorf("MCP 安装脚本不能包含 NUL")
+ return validateMCPStdio(v)
+ default:
+ return fmt.Errorf("transport 只能是 stdio 或 http")
+ }
+}
+
+// validateMCPStdio checks the command, install script and workspace-relative
+// paths of a stdio MCP server.
+func validateMCPStdio(v MCPServerDefinition) error {
+ if strings.TrimSpace(v.Command) == "" {
+ return fmt.Errorf("stdio MCP 需要 command")
+ }
+ if strings.ContainsRune(v.InstallScript, '\x00') {
+ return fmt.Errorf("MCP 安装脚本不能包含 NUL")
+ }
+ if cwd := strings.TrimSpace(v.WorkingDirectory); cwd != "" {
+ if err := ValidateRelativeFilePath(cwd); err != nil {
+ return fmt.Errorf("workingDirectory: %w", err)
}
- if cwd := strings.TrimSpace(v.WorkingDirectory); cwd != "" {
- if err := ValidateRelativeFilePath(cwd); err != nil {
- return fmt.Errorf("workingDirectory: %w", err)
- }
+ }
+ for _, entry := range v.PathEntries {
+ if err := ValidateRelativeFilePath(entry); err != nil {
+ return fmt.Errorf("pathEntries: %w", err)
}
- for _, entry := range v.PathEntries {
- if err := ValidateRelativeFilePath(entry); err != nil {
- return fmt.Errorf("pathEntries: %w", err)
- }
+ }
+ for _, ref := range v.FileRefs {
+ if err := ValidateRelativeFilePath(ref.Path); err != nil {
+ return err
}
- for _, ref := range v.FileRefs {
- if err := ValidateRelativeFilePath(ref.Path); err != nil {
- return err
- }
- if ref.Size < 0 || ref.Size > MCPFileMaxBytes {
- return fmt.Errorf("MCP file %s exceeds the 1MB limit", ref.Path)
- }
+ if ref.Size < 0 || ref.Size > MCPFileMaxBytes {
+ return fmt.Errorf("MCP file %s exceeds the 1MB limit", ref.Path)
}
- default:
- return fmt.Errorf("transport 只能是 stdio 或 http")
- }
- if v.TimeoutSec < 0 || v.TimeoutSec > 600 {
- return fmt.Errorf("timeoutSec 需在 0–600 之间")
}
+ return nil
+}
+
+// validateMCPScope checks that project and personal servers carry exactly the
+// owner fields their scope allows.
+func validateMCPScope(v MCPServerDefinition) error {
switch strings.TrimSpace(v.Scope) {
case "":
case "project":
@@ -349,6 +373,12 @@ func ValidateMCPServer(v MCPServerDefinition) error {
default:
return fmt.Errorf("scope 只能为空、project 或 personal")
}
+ return nil
+}
+
+// validateMCPAuth checks v.AuthType and keeps GitHub token auth on HTTP
+// servers without a stored Authorization header.
+func validateMCPAuth(v MCPServerDefinition) error {
switch strings.TrimSpace(v.AuthType) {
case "", "github-token":
default:
diff --git a/internal/domain/mcp_validate_test.go b/internal/domain/mcp_validate_test.go
new file mode 100644
index 00000000..8d7c5ee2
--- /dev/null
+++ b/internal/domain/mcp_validate_test.go
@@ -0,0 +1,62 @@
+package domain
+
+import "testing"
+
+// TestValidateMCPServerErrorOrder locks the message and precedence of every
+// ValidateMCPServer branch so the transport/scope/auth helpers stay a pure
+// extraction.
+func TestValidateMCPServerErrorOrder(t *testing.T) {
+ stdio := func(mut func(*MCPServerDefinition)) MCPServerDefinition {
+ v := MCPServerDefinition{ID: "x", Name: "X", Command: "npx"}
+ mut(&v)
+ return v
+ }
+ cases := []struct {
+ name string
+ in MCPServerDefinition
+ want string
+ }{
+ {"valid stdio", stdio(func(*MCPServerDefinition) {}), ""},
+ {"missing name wins over transport", MCPServerDefinition{ID: "x", Transport: "bogus"}, "MCP 需要 id 和 name"},
+ {"blank id", stdio(func(v *MCPServerDefinition) { v.ID = " " }), "MCP 需要 id 和 name"},
+ {"http bad url", MCPServerDefinition{ID: "x", Name: "X", Transport: " http ", URL: "ftp://a"}, "HTTP MCP 需要有效的 url"},
+ {"stdio missing command", stdio(func(v *MCPServerDefinition) { v.Command = " " }), "stdio MCP 需要 command"},
+ {"install script nul", stdio(func(v *MCPServerDefinition) { v.InstallScript = "a\x00b" }), "MCP 安装脚本不能包含 NUL"},
+ {"bad cwd", stdio(func(v *MCPServerDefinition) { v.WorkingDirectory = "../up" }), `workingDirectory: invalid file path "../up"`},
+ {"bad path entry", stdio(func(v *MCPServerDefinition) { v.PathEntries = []string{"bin", "/abs"} }), `pathEntries: invalid file path "/abs"`},
+ {"bad file ref path", stdio(func(v *MCPServerDefinition) { v.FileRefs = []MCPFileRef{{Path: "../x"}} }), `invalid file path "../x"`},
+ {"file ref too large", stdio(func(v *MCPServerDefinition) { v.FileRefs = []MCPFileRef{{Path: "a.js", Size: MCPFileMaxBytes + 1}} }), "MCP file a.js exceeds the 1MB limit"},
+ {"file ref negative", stdio(func(v *MCPServerDefinition) { v.FileRefs = []MCPFileRef{{Path: "a.js", Size: -1}} }), "MCP file a.js exceeds the 1MB limit"},
+ {"unknown transport", stdio(func(v *MCPServerDefinition) { v.Transport = "sse" }), "transport 只能是 stdio 或 http"},
+ {"transport before timeout", stdio(func(v *MCPServerDefinition) { v.Command = ""; v.TimeoutSec = 601 }), "stdio MCP 需要 command"},
+ {"timeout too large", stdio(func(v *MCPServerDefinition) { v.TimeoutSec = 601 }), "timeoutSec 需在 0–600 之间"},
+ {"timeout negative", stdio(func(v *MCPServerDefinition) { v.TimeoutSec = -1 }), "timeoutSec 需在 0–600 之间"},
+ {"timeout before scope", stdio(func(v *MCPServerDefinition) { v.TimeoutSec = 601; v.Scope = "bogus" }), "timeoutSec 需在 0–600 之间"},
+ {"project missing id", stdio(func(v *MCPServerDefinition) { v.Scope = "project" }), "项目级 MCP 需要 projectId"},
+ {"project with owner", stdio(func(v *MCPServerDefinition) { v.Scope = "project"; v.ProjectID = "p"; v.OwnerUserID = 1 }), "项目级 MCP 不能设置 ownerUserId"},
+ {"valid project", stdio(func(v *MCPServerDefinition) { v.Scope = " project "; v.ProjectID = "p" }), ""},
+ {"personal missing owner", stdio(func(v *MCPServerDefinition) { v.Scope = "personal" }), "个人级 MCP 需要 ownerUserId"},
+ {"personal with project", stdio(func(v *MCPServerDefinition) { v.Scope = "personal"; v.OwnerUserID = 1; v.ProjectID = "p" }), "个人级 MCP 不能设置 projectId"},
+ {"personal market", stdio(func(v *MCPServerDefinition) { v.Scope = "personal"; v.OwnerUserID = 1; v.MarketID = "m" }), "市场 MCP 只能安装到项目,个人级 MCP 必须为自定义"},
+ {"valid personal", stdio(func(v *MCPServerDefinition) { v.Scope = "personal"; v.OwnerUserID = 1 }), ""},
+ {"unknown scope", stdio(func(v *MCPServerDefinition) { v.Scope = "global" }), "scope 只能为空、project 或 personal"},
+ {"scope before auth", stdio(func(v *MCPServerDefinition) { v.Scope = "global"; v.AuthType = "basic" }), "scope 只能为空、project 或 personal"},
+ {"unknown auth", stdio(func(v *MCPServerDefinition) { v.AuthType = "basic" }), "authType must be empty or github-token"},
+ {"github token on stdio", stdio(func(v *MCPServerDefinition) { v.AuthType = "github-token" }), "GitHub 令牌认证仅支持 HTTP MCP"},
+ {"github token header", MCPServerDefinition{ID: "x", Name: "X", Transport: "http", URL: "https://a", AuthType: "github-token", Headers: map[string]string{" Authorization ": "t"}}, "GitHub 令牌认证不允许在资产 Headers 中保存 Authorization"},
+ {"padded github token on http", MCPServerDefinition{ID: "x", Name: "X", Transport: "http", URL: "https://a", AuthType: " github-token ", Headers: map[string]string{"Authorization": "t"}}, ""},
+ {"valid github token", MCPServerDefinition{ID: "x", Name: "X", Transport: "http", URL: "https://a", AuthType: "github-token", Headers: map[string]string{"X-Other": "v"}}, ""},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ err := ValidateMCPServer(tc.in)
+ got := ""
+ if err != nil {
+ got = err.Error()
+ }
+ if got != tc.want {
+ t.Fatalf("ValidateMCPServer() error = %q, want %q", got, tc.want)
+ }
+ })
+ }
+}