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.

feat(mcp): credential-authenticated MCP servers (#1033)

jason zhang291c14db970a
diff --git a/internal/domain/assets.go b/internal/domain/assets.go
index bcc458c6..a325dace 100644
--- a/internal/domain/assets.go
+++ b/internal/domain/assets.go
@@ -89,9 +89,13 @@ type MCPServerDefinition struct {
WorkingDirectory string `json:"workingDirectory,omitempty"`
PathEntries []string `json:"pathEntries,omitempty"`
FileRefs []MCPFileRef `json:"fileRefs,omitempty"`
- AuthType string `json:"authType,omitempty"` // empty, github-token
- TimeoutSec int `json:"timeoutSec"`
- UpdatedAt time.Time `json:"updatedAt"`
+ AuthType string `json:"authType,omitempty"` // empty, github-token, credential
+ // CredentialKind names the platform-managed credential (RunCredentials.Extra
+ // key) bound as the Authorization Bearer header when AuthType is
+ // "credential". The stored asset never holds the secret itself.
+ CredentialKind string `json:"credentialKind,omitempty"`
+ TimeoutSec int `json:"timeoutSec"`
+ UpdatedAt time.Time `json:"updatedAt"`
}
type MCPFileRef struct {
@@ -388,17 +392,25 @@ func validateMCPScope(v MCPServerDefinition) error {
return nil
}
-// validateMCPAuth checks v.AuthType and keeps GitHub token auth on HTTP
+// validateMCPAuth checks v.AuthType and keeps token auth on HTTP
// servers without a stored Authorization header.
func validateMCPAuth(v MCPServerDefinition) error {
switch strings.TrimSpace(v.AuthType) {
- case "", "github-token":
+ case "", "github-token", "credential":
default:
- return fmt.Errorf("authType must be empty or github-token")
+ return fmt.Errorf("authType must be empty, github-token or credential")
}
if v.AuthType == "github-token" && strings.TrimSpace(v.Transport) != "http" {
return fmt.Errorf("GitHub 令牌认证仅支持 HTTP MCP")
}
+ if v.AuthType == "credential" {
+ if strings.TrimSpace(v.Transport) != "http" {
+ return fmt.Errorf("credential authentication requires an HTTP MCP")
+ }
+ if strings.TrimSpace(v.CredentialKind) == "" {
+ return fmt.Errorf("credential authentication requires credentialKind")
+ }
+ }
if v.AuthType == "github-token" {
for key := range v.Headers {
if strings.EqualFold(strings.TrimSpace(key), "authorization") {
@@ -406,6 +418,13 @@ func validateMCPAuth(v MCPServerDefinition) error {
}
}
}
+ if v.AuthType == "credential" {
+ for key := range v.Headers {
+ if strings.EqualFold(strings.TrimSpace(key), "authorization") {
+ return fmt.Errorf("credential authentication must not store an Authorization header on the asset")
+ }
+ }
+ }
return nil
}
diff --git a/internal/domain/mcp_validate_test.go b/internal/domain/mcp_validate_test.go
index 8d7c5ee2..2141ff5e 100644
--- a/internal/domain/mcp_validate_test.go
+++ b/internal/domain/mcp_validate_test.go
@@ -41,7 +41,11 @@ func TestValidateMCPServerErrorOrder(t *testing.T) {
{"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"},
+ {"unknown auth", stdio(func(v *MCPServerDefinition) { v.AuthType = "basic" }), "authType must be empty, github-token or credential"},
+ {"credential on stdio", stdio(func(v *MCPServerDefinition) { v.AuthType = "credential"; v.CredentialKind = "gateway" }), "credential authentication requires an HTTP MCP"},
+ {"credential missing kind", MCPServerDefinition{ID: "x", Name: "X", Transport: "http", URL: "https://a", AuthType: "credential"}, "credential authentication requires credentialKind"},
+ {"credential stored header", MCPServerDefinition{ID: "x", Name: "X", Transport: "http", URL: "https://a", AuthType: "credential", CredentialKind: "gateway", Headers: map[string]string{"Authorization": "t"}}, "credential authentication must not store an Authorization header on the asset"},
+ {"valid credential", MCPServerDefinition{ID: "x", Name: "X", Transport: "http", URL: "https://a", AuthType: "credential", CredentialKind: "gateway", Headers: map[string]string{"X-Other": "v"}}, ""},
{"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"}}, ""},
diff --git a/internal/engine/node_execute.go b/internal/engine/node_execute.go
index 0651e783..74c29b5d 100644
--- a/internal/engine/node_execute.go
+++ b/internal/engine/node_execute.go
@@ -747,6 +747,10 @@ func (e *Engine) executeNode(ctx context.Context, runID string, wf domain.Workfl
repositories := current.RepositoryContexts()
boundAssets, skipped, bindErr := runtime.PrepareMCPServers(assets, runtime.MCPTarget{
GitToken: gitTok, RequireCredentials: true,
+ // Credential-authenticated MCP servers resolve their Bearer
+ // token from the responsible user's extra credentials here —
+ // the agent node is the primary consumer of market MCPs.
+ ExtraCredentials: gitCredentials.Extra,
SSHRepositoriesOnly: len(repositories) > 0 && len(sshRepositoryURLs(repositories)) == len(repositories),
})
for _, warning := range skipped {
diff --git a/internal/engine/run_environment.go b/internal/engine/run_environment.go
index cca1afa3..ffce8aaf 100644
--- a/internal/engine/run_environment.go
+++ b/internal/engine/run_environment.go
@@ -239,7 +239,7 @@ func (e *Engine) resolveRunCredentials(run domain.Run) runtime.RunCredentials {
credentials.ClaudeCode = runtime.ProviderCredential{APIKey: user.ClaudeCodeAPIKey, BaseURL: user.ClaudeCodeBaseURL}
credentials.AgentCLI = runtime.AgentCLIUserCredentials(user)
}
- scriptAssets, _, _ := runtime.PrepareMCPServers(e.resolveScriptMCPAssets(run.ExecutionContext.ProjectID, userID), runtime.MCPTarget{GitToken: credentials.GitToken})
+ scriptAssets, _, _ := runtime.PrepareMCPServers(e.resolveScriptMCPAssets(run.ExecutionContext.ProjectID, userID), runtime.MCPTarget{GitToken: credentials.GitToken, ExtraCredentials: credentials.Extra})
credentials.MCPServers = scriptAssets.MCPServers
return credentials
}
diff --git a/internal/httpapi/assistant_mcp.go b/internal/httpapi/assistant_mcp.go
index 3cad29b3..70ede2e7 100644
--- a/internal/httpapi/assistant_mcp.go
+++ b/internal/httpapi/assistant_mcp.go
@@ -387,6 +387,16 @@ func bindAssistantMCP(server domain.MCPServerDefinition, user domain.User) (doma
if githubErr != nil {
return domain.MCPServerDefinition{}, githubErr
}
+ // Credential-authenticated MCP servers resolve the responsible user's
+ // saved credential (RunCredentials.Extra) here — this entry point serves
+ // both tool discovery and actual calls.
+ bundle, _, credErr := runtime.BindCredentialMCPHeaders(bundle, runtime.ExtraRunCredentials(user.ID), true)
+ if credErr != nil {
+ return domain.MCPServerDefinition{}, credErr
+ }
+ if len(bundle.MCPServers) == 0 {
+ return domain.MCPServerDefinition{}, fmt.Errorf("MCP %s 未绑定任何凭证", server.Name)
+ }
server = bundle.MCPServers[0]
headers, err := runtime.ResolveMCPHeaders(server)
if err != nil {
diff --git a/internal/runtime/mcp_call.go b/internal/runtime/mcp_call.go
index e84bcb55..83691a80 100644
--- a/internal/runtime/mcp_call.go
+++ b/internal/runtime/mcp_call.go
@@ -29,19 +29,20 @@ type mcpCallConfigFile struct {
}
type mcpCallServer struct {
- ID string `json:"id"`
- Name string `json:"name"`
- MarketID string `json:"marketId,omitempty"`
- URL string `json:"url"`
- Command string `json:"command,omitempty"`
- Args []string `json:"args,omitempty"`
- CWD string `json:"cwd,omitempty"`
- Environment map[string]string `json:"environment,omitempty"`
- Transport string `json:"transport"`
- AuthType string `json:"authType,omitempty"`
- Headers map[string]string `json:"headers,omitempty"`
- TimeoutSec int `json:"timeoutSec,omitempty"`
- Aliases []string `json:"aliases,omitempty"`
+ ID string `json:"id"`
+ Name string `json:"name"`
+ MarketID string `json:"marketId,omitempty"`
+ URL string `json:"url"`
+ Command string `json:"command,omitempty"`
+ Args []string `json:"args,omitempty"`
+ CWD string `json:"cwd,omitempty"`
+ Environment map[string]string `json:"environment,omitempty"`
+ Transport string `json:"transport"`
+ AuthType string `json:"authType,omitempty"`
+ CredentialKind string `json:"credentialKind,omitempty"`
+ Headers map[string]string `json:"headers,omitempty"`
+ TimeoutSec int `json:"timeoutSec,omitempty"`
+ Aliases []string `json:"aliases,omitempty"`
}
func mcpCallHostDir(workspace string) string {
@@ -121,19 +122,20 @@ func mcpCallConfigServers(servers []domain.MCPServerDefinition) []mcpCallServer
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),
+ 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,
+ CredentialKind: server.CredentialKind,
+ Headers: cloneHeaders(server.Headers),
+ TimeoutSec: server.TimeoutSec,
+ Aliases: mcpCallAliases(server),
})
}
return out
@@ -437,6 +439,9 @@ func mcpCallInvoke(args []string) error {
return err
}
if strings.EqualFold(strings.TrimSpace(server.Transport), "http") && strings.TrimSpace(server.AuthType) != "" && authorizationHeader(server.Headers) == "" {
+ if strings.EqualFold(strings.TrimSpace(server.AuthType), MCPAuthCredential) {
+ return fmt.Errorf("MCP %s 需要凭证 %q:请先在个人凭证中配置", server.Name, server.CredentialKind)
+ }
return fmt.Errorf("MCP %s 需要 GitHub Git Token:请先在个人凭证中配置", server.Name)
}
tool := strings.TrimSpace(args[1])
@@ -510,6 +515,9 @@ func mcpCallTools(args []string) error {
return err
}
if strings.EqualFold(strings.TrimSpace(server.Transport), "http") && strings.TrimSpace(server.AuthType) != "" && authorizationHeader(server.Headers) == "" {
+ if strings.EqualFold(strings.TrimSpace(server.AuthType), MCPAuthCredential) {
+ return fmt.Errorf("MCP %s 需要凭证 %q:请先在个人凭证中配置", server.Name, server.CredentialKind)
+ }
return fmt.Errorf("MCP %s 需要 GitHub Git Token:请先在个人凭证中配置", server.Name)
}
var names []string
diff --git a/internal/runtime/mcp_credentials.go b/internal/runtime/mcp_credentials.go
index 02250042..465ba39a 100644
--- a/internal/runtime/mcp_credentials.go
+++ b/internal/runtime/mcp_credentials.go
@@ -1,5 +1,13 @@
package runtime
+import (
+ "fmt"
+ "sort"
+ "strings"
+
+ "agentflow/internal/domain"
+)
+
func cloneHeaders(in map[string]string) map[string]string {
out := make(map[string]string, len(in)+1)
for k, v := range in {
@@ -7,3 +15,71 @@ func cloneHeaders(in map[string]string) map[string]string {
}
return out
}
+
+// MCPAuthCredential is the MCPServerDefinition.AuthType value that binds a
+// platform-managed credential as the server's Authorization Bearer header.
+// The credential itself is named by MCPServerDefinition.CredentialKind and
+// resolved from RunCredentials.Extra per run, so a stored asset never holds
+// the secret.
+const MCPAuthCredential = "credential"
+
+// BindCredentialMCPHeaders binds the responsible user's saved credential
+// (named by CredentialKind) into HTTP MCP servers that opt in with
+// AuthType "credential". Stored MCP assets are never mutated: the bundle is
+// copied per task, and any Authorization header stored on the asset is
+// replaced so the credential is the only authority.
+//
+// required controls what happens when the responsible user has not saved the
+// declared credential: required fails the whole binding with the server and
+// kind named (agent invocations, assistant calls); otherwise the server is
+// dropped and named in the returned list so the remaining servers keep
+// working (opportunistic script bindings, mirroring github-token). Filtering
+// keeps entries by identity during the walk — display names are not unique
+// (a project and a personal server may share one), so a same-named server
+// that needs no credential or has one never drops alongside its twin.
+func BindCredentialMCPHeaders(assets domain.AgentAssetBundle, extra map[string]string, required bool) (domain.AgentAssetBundle, []string, error) {
+ out := assets
+ out.MCPServers = append([]domain.MCPServerDefinition(nil), assets.MCPServers...)
+ kept := out.MCPServers[:0]
+ var dropped []string
+ for i := range out.MCPServers {
+ server := out.MCPServers[i]
+ if !strings.EqualFold(strings.TrimSpace(server.AuthType), MCPAuthCredential) {
+ kept = append(kept, server)
+ continue
+ }
+ kind := strings.TrimSpace(server.CredentialKind)
+ if kind == "" {
+ return out, nil, fmt.Errorf("MCP %s declares credential authentication without a credentialKind", server.Name)
+ }
+ token := strings.TrimSpace(extra[kind])
+ if token == "" {
+ if required {
+ return out, nil, fmt.Errorf("MCP %s requires the saved credential %q; configure it in personal settings", server.Name, kind)
+ }
+ dropped = append(dropped, server.Name)
+ continue
+ }
+ server.Headers = cloneHeaders(server.Headers)
+ for key := range server.Headers {
+ if strings.EqualFold(strings.TrimSpace(key), "authorization") {
+ delete(server.Headers, key)
+ }
+ }
+ server.Headers["Authorization"] = "Bearer " + token
+ kept = append(kept, server)
+ }
+ out.MCPServers = kept
+ sort.Strings(dropped)
+ return out, dropped, nil
+}
+
+// RequiresMCPCredential reports whether any server needs a saved credential.
+func RequiresMCPCredential(assets domain.AgentAssetBundle) bool {
+ for _, server := range assets.MCPServers {
+ if strings.EqualFold(strings.TrimSpace(server.AuthType), MCPAuthCredential) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/internal/runtime/mcp_credentials_test.go b/internal/runtime/mcp_credentials_test.go
new file mode 100644
index 00000000..7970f0e4
--- /dev/null
+++ b/internal/runtime/mcp_credentials_test.go
@@ -0,0 +1,193 @@
+package runtime
+
+import (
+ "strings"
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func credentialServer(name, kind string, headers map[string]string) domain.MCPServerDefinition {
+ return domain.MCPServerDefinition{
+ ID: "srv-" + name, Name: name, Transport: "http", URL: "https://" + name + ".example",
+ AuthType: MCPAuthCredential, CredentialKind: kind, Headers: headers,
+ }
+}
+
+func TestBindCredentialMCPHeadersInjectsBearer(t *testing.T) {
+ assets := domain.AgentAssetBundle{MCPServers: []domain.MCPServerDefinition{
+ credentialServer("gateway", "corp-gateway", nil),
+ }}
+ out, dropped, err := BindCredentialMCPHeaders(assets, map[string]string{"corp-gateway": "tok-1"}, true)
+ if err != nil {
+ t.Fatalf("BindCredentialMCPHeaders: %v", err)
+ }
+ if len(dropped) != 0 {
+ t.Fatalf("unexpected drops: %v", dropped)
+ }
+ if got := out.MCPServers[0].Headers["Authorization"]; got != "Bearer tok-1" {
+ t.Fatalf("Authorization: got %q, want %q", got, "Bearer tok-1")
+ }
+ // The stored asset must not be mutated.
+ if _, ok := assets.MCPServers[0].Headers["Authorization"]; ok {
+ t.Fatal("stored MCP asset was mutated")
+ }
+}
+
+func TestBindCredentialMCPHeadersReplacesStoredAuthorization(t *testing.T) {
+ assets := domain.AgentAssetBundle{MCPServers: []domain.MCPServerDefinition{
+ credentialServer("gateway", "corp-gateway", map[string]string{"authorization": "stale", "X-Keep": "v"}),
+ }}
+ out, _, err := BindCredentialMCPHeaders(assets, map[string]string{"corp-gateway": "tok-2"}, true)
+ if err != nil {
+ t.Fatalf("BindCredentialMCPHeaders: %v", err)
+ }
+ srv := out.MCPServers[0]
+ if got := srv.Headers["Authorization"]; got != "Bearer tok-2" {
+ t.Fatalf("Authorization: got %q, want %q", got, "Bearer tok-2")
+ }
+ for key := range srv.Headers {
+ if strings.EqualFold(key, "authorization") && key != "Authorization" {
+ t.Fatalf("stale Authorization header %q survived", key)
+ }
+ }
+ if srv.Headers["X-Keep"] != "v" {
+ t.Fatal("unrelated headers must survive")
+ }
+}
+
+func TestBindCredentialMCPHeadersMissingCredentialFailsWhenRequired(t *testing.T) {
+ assets := domain.AgentAssetBundle{MCPServers: []domain.MCPServerDefinition{
+ credentialServer("gateway", "corp-gateway", nil),
+ }}
+ if _, _, err := BindCredentialMCPHeaders(assets, nil, true); err == nil {
+ t.Fatal("missing credential must fail when required")
+ } else if !strings.Contains(err.Error(), "corp-gateway") {
+ t.Fatalf("error must name the server and credential: %v", err)
+ }
+}
+
+// TestBindCredentialMCPHeadersDropsMissingWhenOptional mirrors the script
+// binding semantics: one server's missing credential must not take down the
+// other servers that have theirs — the same contract github-token follows.
+func TestBindCredentialMCPHeadersDropsMissingWhenOptional(t *testing.T) {
+ assets := domain.AgentAssetBundle{MCPServers: []domain.MCPServerDefinition{
+ credentialServer("needs-cred", "absent", nil),
+ credentialServer("has-cred", "corp-gateway", nil),
+ }}
+ out, dropped, err := BindCredentialMCPHeaders(assets, map[string]string{"corp-gateway": "tok-3"}, false)
+ if err != nil {
+ t.Fatalf("optional binding must not fail: %v", err)
+ }
+ if len(dropped) != 1 || dropped[0] != "needs-cred" {
+ t.Fatalf("dropped = %v, want [needs-cred]", dropped)
+ }
+ if len(out.MCPServers) != 1 || out.MCPServers[0].Name != "has-cred" {
+ t.Fatalf("surviving servers = %+v", out.MCPServers)
+ }
+ if got := out.MCPServers[0].Headers["Authorization"]; got != "Bearer tok-3" {
+ t.Fatalf("has-cred Authorization: got %q", got)
+ }
+}
+
+func TestBindCredentialMCPHeadersMissingKindFails(t *testing.T) {
+ assets := domain.AgentAssetBundle{MCPServers: []domain.MCPServerDefinition{
+ credentialServer("gateway", " ", nil),
+ }}
+ if _, _, err := BindCredentialMCPHeaders(assets, map[string]string{"x": "y"}, true); err == nil {
+ t.Fatal("empty credentialKind must fail")
+ }
+}
+
+func TestBindCredentialMCPHeadersIgnoresOtherServers(t *testing.T) {
+ plain := domain.MCPServerDefinition{ID: "p", Name: "plain", Transport: "http", URL: "https://plain.example"}
+ assets := domain.AgentAssetBundle{MCPServers: []domain.MCPServerDefinition{plain}}
+ out, _, err := BindCredentialMCPHeaders(assets, nil, true)
+ if err != nil {
+ t.Fatalf("BindCredentialMCPHeaders: %v", err)
+ }
+ if out.MCPServers[0].Headers != nil {
+ t.Fatal("non-credential servers must stay untouched")
+ }
+}
+
+func TestPrepareMCPServersBindsCredentialAuth(t *testing.T) {
+ assets := domain.AgentAssetBundle{MCPServers: []domain.MCPServerDefinition{
+ credentialServer("gateway", "corp-gateway", nil),
+ }}
+ out, warnings, err := PrepareMCPServers(assets, MCPTarget{ExtraCredentials: map[string]string{"corp-gateway": "tok-4"}})
+ if err != nil {
+ t.Fatalf("PrepareMCPServers: %v", err)
+ }
+ if len(warnings) != 0 {
+ t.Fatalf("unexpected warnings: %v", warnings)
+ }
+ if got := out.MCPServers[0].Headers["Authorization"]; got != "Bearer tok-4" {
+ t.Fatalf("Authorization: got %q, want %q", got, "Bearer tok-4")
+ }
+}
+
+// TestPrepareMCPServersDropsMissingCredentialWhenOptional covers the script
+// path (RequireCredentials unset): a missing credential drops that one server
+// with a warning while the remaining credential servers stay usable.
+func TestPrepareMCPServersDropsMissingCredentialWhenOptional(t *testing.T) {
+ assets := domain.AgentAssetBundle{MCPServers: []domain.MCPServerDefinition{
+ credentialServer("needs-cred", "absent", nil),
+ credentialServer("has-cred", "corp-gateway", nil),
+ }}
+ out, warnings, err := PrepareMCPServers(assets, MCPTarget{ExtraCredentials: map[string]string{"corp-gateway": "tok-5"}})
+ if err != nil {
+ t.Fatalf("PrepareMCPServers: %v", err)
+ }
+ if len(warnings) != 1 || !strings.Contains(warnings[0], "needs-cred") {
+ t.Fatalf("warnings = %v, want one naming needs-cred", warnings)
+ }
+ if len(out.MCPServers) != 1 || out.MCPServers[0].Name != "has-cred" {
+ t.Fatalf("surviving servers = %+v", out.MCPServers)
+ }
+ if got := out.MCPServers[0].Headers["Authorization"]; got != "Bearer tok-5" {
+ t.Fatalf("has-cred Authorization: got %q", got)
+ }
+}
+
+// TestBindCredentialMCPHeadersSameNameServersFilterIndependently covers the
+// case where a project and a personal server share a display name: dropping
+// one must never drop its same-named twin, whether the twin carries its own
+// credential or needs no authentication at all.
+func TestBindCredentialMCPHeadersSameNameServersFilterIndependently(t *testing.T) {
+ missing := credentialServer("gateway", "absent", nil)
+ missing.ID = "project-gateway"
+ extra := map[string]string{"corp-gateway": "tok-6"}
+
+ t.Run("same-named twin has its own credential", func(t *testing.T) {
+ withCred := credentialServer("gateway", "corp-gateway", nil)
+ withCred.ID = "personal-gateway"
+ out, dropped, err := BindCredentialMCPHeaders(domain.AgentAssetBundle{MCPServers: []domain.MCPServerDefinition{missing, withCred}}, extra, false)
+ if err != nil {
+ t.Fatalf("BindCredentialMCPHeaders: %v", err)
+ }
+ if len(dropped) != 1 || dropped[0] != "gateway" {
+ t.Fatalf("dropped = %v, want the one gateway entry", dropped)
+ }
+ if len(out.MCPServers) != 1 || out.MCPServers[0].ID != "personal-gateway" {
+ t.Fatalf("survivors = %+v, want personal-gateway only", out.MCPServers)
+ }
+ if got := out.MCPServers[0].Headers["Authorization"]; got != "Bearer tok-6" {
+ t.Fatalf("twin Authorization = %q, want Bearer tok-6", got)
+ }
+ })
+
+ t.Run("same-named twin needs no authentication", func(t *testing.T) {
+ plain := domain.MCPServerDefinition{ID: "personal-gateway", Name: "gateway", Transport: "http", URL: "https://plain.example"}
+ out, dropped, err := BindCredentialMCPHeaders(domain.AgentAssetBundle{MCPServers: []domain.MCPServerDefinition{missing, plain}}, extra, false)
+ if err != nil {
+ t.Fatalf("BindCredentialMCPHeaders: %v", err)
+ }
+ if len(dropped) != 1 {
+ t.Fatalf("dropped = %v, want one entry", dropped)
+ }
+ if len(out.MCPServers) != 1 || out.MCPServers[0].ID != "personal-gateway" {
+ t.Fatalf("survivors = %+v, want personal-gateway only", out.MCPServers)
+ }
+ })
+}
diff --git a/internal/runtime/platform_mcp.go b/internal/runtime/platform_mcp.go
index a8233d48..d4f909d9 100644
--- a/internal/runtime/platform_mcp.go
+++ b/internal/runtime/platform_mcp.go
@@ -14,6 +14,10 @@ type MCPTarget struct {
// absolute URL the engine resolved.
ServerURL string
GitToken string
+ // ExtraCredentials carries the responsible user's platform-managed
+ // credentials (RunCredentials.Extra) for MCP servers authenticated with
+ // AuthType "credential".
+ ExtraCredentials map[string]string
// RequireCredentials fails when a credential-authenticated MCP has no
// token. Agent invocations require it; scripts and gates bind
// opportunistically.
@@ -44,6 +48,13 @@ func PrepareMCPServers(assets domain.AgentAssetBundle, target MCPTarget) (domain
if githubErr != nil {
return assets, nil, githubErr
}
+ assets, credDropped, credErr := BindCredentialMCPHeaders(assets, target.ExtraCredentials, target.RequireCredentials)
+ if credErr != nil {
+ return assets, nil, credErr
+ }
+ for _, name := range credDropped {
+ warnings = append(warnings, "未配置所需凭证,已跳过 "+name)
+ }
if base := strings.TrimRight(strings.TrimSpace(target.ServerURL), "/"); base != "" {
assets.MCPServers = rebasePlatformMCP(assets.MCPServers, base)
} else {