Allow named MCP servers in Claude stages
Add validated per-stage MCP allowlists while keeping builtin tools separate. Enable matching Claude Code permission rules only for Claude stages, reject unsupported harnesses, and document the unattended-stage safety model.
This commit is contained in:
@@ -88,12 +88,36 @@ func claudeTools(tools []string) ([]string, error) {
|
||||
case "websearch":
|
||||
names = append(names, "WebSearch")
|
||||
default:
|
||||
if _, _, ok, err := MCPToolName(tool); err != nil {
|
||||
return nil, err
|
||||
} else if ok {
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("Claude Code has no audited builtin for %s; choose read/glob/grep/edit/shell/webfetch/websearch", tool)
|
||||
}
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func claudeMCPAllowRules(tools []string) ([]string, error) {
|
||||
var rules []string
|
||||
for _, name := range tools {
|
||||
server, tool, ok, err := MCPToolName(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
rule := "mcp__" + server + "__*"
|
||||
if tool != "" {
|
||||
rule = "mcp__" + server + "__" + tool
|
||||
}
|
||||
rules = append(rules, rule)
|
||||
}
|
||||
return rules, nil
|
||||
}
|
||||
|
||||
// claudeAllowRules are the pre-approved permission rules. File edits are
|
||||
// approved only below the run directory and the stage's artifacts directory;
|
||||
// everything else a declared tool does not need approval for stays denied by
|
||||
@@ -126,10 +150,16 @@ func (c *ClaudeStageClient) args(config ModelConfig, tools []string, effort stri
|
||||
"--model", config.Model,
|
||||
"--permission-mode", "dontAsk",
|
||||
"--setting-sources", "",
|
||||
"--strict-mcp-config",
|
||||
"--disable-slash-commands",
|
||||
"--tools", strings.Join(tools, ",")}
|
||||
if rules := claudeAllowRules(tools, c.Directory, c.ArtifactsDir); len(rules) > 0 {
|
||||
mcpRules, _ := claudeMCPAllowRules(c.Tools)
|
||||
if len(mcpRules) == 0 {
|
||||
tail := append([]string(nil), args[10:]...)
|
||||
args = append(append(append([]string(nil), args[:10]...), "--strict-mcp-config"), tail...)
|
||||
}
|
||||
rules := claudeAllowRules(tools, c.Directory, c.ArtifactsDir)
|
||||
rules = append(rules, mcpRules...)
|
||||
if len(rules) > 0 {
|
||||
args = append(args, "--allowedTools")
|
||||
args = append(args, rules...)
|
||||
}
|
||||
|
||||
@@ -217,6 +217,30 @@ func TestClaudeStageDeclaredToolsAreTheOnlyTools(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeStageNamedMCPToolsAreAllowlistedSeparately(t *testing.T) {
|
||||
client, root, _ := fakeClaudeClient(t, "success")
|
||||
client.Tools = []string{"read", "mcp:ai-memory", "mcp:linear/list_issues"}
|
||||
if _, err := client.Generate(context.Background(), claudeConfig(nil), "task"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calls := fakeClaudeArgs(t, root)
|
||||
if len(calls) != 1 {
|
||||
t.Fatal(calls)
|
||||
}
|
||||
args := calls[0]
|
||||
if slices.Contains(args, "--strict-mcp-config") {
|
||||
t.Fatal("MCP-enabled stage must not use strict MCP config")
|
||||
}
|
||||
for _, want := range []string{"--tools", "Read", "--allowedTools", "Read", "mcp__ai-memory__*", "mcp__linear__list_issues"} {
|
||||
if !slices.Contains(args, want) {
|
||||
t.Fatalf("missing %q in args %q", want, args)
|
||||
}
|
||||
}
|
||||
if slices.Contains(args, "mcp:ai-memory") || slices.Contains(args, "mcp:linear/list_issues") {
|
||||
t.Fatalf("MCP entries leaked into builtin tools: %q", args)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeStageRefusesWhatItCannotEnforce(t *testing.T) {
|
||||
for name, change := range map[string]func(*ClaudeStageClient, *ModelConfig){
|
||||
"write paths": func(c *ClaudeStageClient, _ *ModelConfig) { c.WritePaths = []string{"app"} },
|
||||
|
||||
@@ -118,6 +118,14 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
|
||||
return ModelSpec{}, err
|
||||
}
|
||||
}
|
||||
if backend != ClaudeBackend {
|
||||
tools, _, _ := ToolNames(def)
|
||||
if mcp, err := MCPTools(tools); err != nil {
|
||||
return ModelSpec{}, err
|
||||
} else if len(mcp) > 0 {
|
||||
return ModelSpec{}, fmt.Errorf("named MCP tools are supported only by the claude harness; remove mcp: entries or set harness=claude")
|
||||
}
|
||||
}
|
||||
if backend == ClaudeBackend {
|
||||
if def.Configs["provider"] != ClaudeProvider {
|
||||
return ModelSpec{}, fmt.Errorf("the claude harness needs provider=%s", ClaudeProvider)
|
||||
|
||||
+32
-1
@@ -148,7 +148,11 @@ func ToolNames(def Definition) ([]string, bool, error) {
|
||||
switch name {
|
||||
case "websearch", "webfetch", "read", "glob", "grep", "edit", "shell":
|
||||
default:
|
||||
return nil, true, fmt.Errorf("unsupported tool %q", name)
|
||||
if _, _, ok, err := MCPToolName(name); err != nil {
|
||||
return nil, true, err
|
||||
} else if !ok {
|
||||
return nil, true, fmt.Errorf("unsupported tool %q", name)
|
||||
}
|
||||
}
|
||||
if seen[name] {
|
||||
return nil, true, fmt.Errorf("duplicate tool %s", name)
|
||||
@@ -165,6 +169,33 @@ func ToolNames(def Definition) ([]string, bool, error) {
|
||||
// text-only stage.
|
||||
var legacyTools = []string{"read", "glob", "grep", "edit", "shell"}
|
||||
|
||||
var mcpToolName = regexp.MustCompile(`^mcp:([A-Za-z0-9_-]+)(?:/([A-Za-z0-9_-]+))?$`)
|
||||
|
||||
// MCPToolName validates and splits a named MCP allowlist entry.
|
||||
func MCPToolName(name string) (server, tool string, ok bool, err error) {
|
||||
if !strings.HasPrefix(name, "mcp:") {
|
||||
return "", "", false, nil
|
||||
}
|
||||
match := mcpToolName.FindStringSubmatch(name)
|
||||
if match == nil {
|
||||
return "", "", true, fmt.Errorf("invalid MCP tool %q; use mcp:<server> or mcp:<server>/<tool> with letters, digits, _ or -", name)
|
||||
}
|
||||
return match[1], match[2], true, nil
|
||||
}
|
||||
|
||||
// MCPTools returns named MCP entries in declaration order.
|
||||
func MCPTools(tools []string) ([]string, error) {
|
||||
var result []string
|
||||
for _, name := range tools {
|
||||
if _, _, ok, err := MCPToolName(name); err != nil {
|
||||
return nil, err
|
||||
} else if ok {
|
||||
result = append(result, name)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// effectiveTools is the single materialisation point for the tools a model
|
||||
// stage is permitted. A present tools block (including an explicit empty block)
|
||||
// is returned exactly as parsed by ToolNames; an absent block yields the legacy
|
||||
|
||||
@@ -26,6 +26,23 @@ func TestToolsValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNamedMCPToolsValidation(t *testing.T) {
|
||||
for _, name := range []string{"mcp:", "mcp:/tool", "mcp:server/", "mcp:server/tool/extra", "mcp:server tool"} {
|
||||
if _, _, _, err := MCPToolName(name); err == nil {
|
||||
t.Fatalf("accepted malformed MCP tool %q", name)
|
||||
}
|
||||
}
|
||||
def := Definition{Type: ModelType, Blocks: map[string]string{
|
||||
"prompt": "task", "configs": "provider=opencode-go\nmodel=model",
|
||||
}, Configs: map[string]string{
|
||||
"provider": "opencode-go", "model": "model",
|
||||
}}
|
||||
def.Blocks["tools"] = "mcp:linear/list_issues"
|
||||
if err := (ModelStage{}).Validate(def); err == nil || !strings.Contains(err.Error(), "only by the claude harness") {
|
||||
t.Fatalf("non-Claude MCP stage error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelStageBindsExplicitToolPolicyToAgentSession(t *testing.T) {
|
||||
for _, tools := range []string{"websearch\nwebfetch", ""} {
|
||||
client := &AgentClient{Tools: []string{"read", "edit", "shell"}, WritePaths: []string{"one.go"}}
|
||||
|
||||
Reference in New Issue
Block a user