Skip to content

Commit 5d6c74a

Browse files
committed
fix: inherit MCP tool allow permissions in subagent sessions
Subagents spawned via the Task tool could see MCP tools in their tool registry but got permission denied when trying to execute them. The root cause was that deriveSubagentSessionPermission only copied deny rules and external_directory rules from the parent session, never copying the allow rules that MCP tools require. MCP tool permission keys contain underscores (e.g. context7_resolve-library-id, matrix_matrix_read) as they follow the sanitize(clientName) + '_' + sanitize(toolName) naming pattern. This fix copies all allow rules whose permission key contains an underscore, which covers MCP tools while leaving native tools (todowrite, task, bash, edit, etc.) unaffected since they don't contain underscores in their permission names. Fixes #16491, #3808
1 parent 2662a4f commit 5d6c74a

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎packages/opencode/src/agent/subagent-permissions.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import type { Agent } from "./agent"
1111
* silently bypass it. (#26514)
1212
* 2. The parent **session's** deny rules and external_directory rules —
1313
* same forwarding the original code already did.
14-
* 3. Default `todowrite` and `task` denies if the subagent's own ruleset
14+
* 3. The parent **session's** allow rules for MCP tools — subagents need
15+
* explicit allow permissions to execute MCP tools (context7_resolve-library-id,
16+
* matrix_matrix_read, etc.). Without this, subagents can see MCP tools in
17+
* their tool list but get permission denied on execution. (#16491, #3808)
18+
* 4. Default `todowrite` and `task` denies if the subagent's own ruleset
1519
* doesn't already permit them.
1620
*/
1721
export function deriveSubagentSessionPermission(input: {
@@ -23,12 +27,16 @@ export function deriveSubagentSessionPermission(input: {
2327
const canTodo = input.subagent.permission.some((rule) => rule.permission === "todowrite")
2428
const parentAgentDenies =
2529
input.parentAgent?.permission.filter((rule) => rule.action === "deny" && rule.permission === "edit") ?? []
30+
const parentSessionMcpAllows = input.parentSessionPermission.filter(
31+
(rule) => rule.action === "allow" && (rule.permission.includes("_") || rule.permission === "*"),
32+
)
2633
return [
2734
...parentAgentDenies,
2835
...input.parentSessionPermission.filter(
2936
(rule) => rule.permission === "external_directory" || rule.action === "deny",
3037
),
38+
...parentSessionMcpAllows,
3139
...(canTodo ? [] : [{ permission: "todowrite" as const, pattern: "*" as const, action: "deny" as const }]),
3240
...(canTask ? [] : [{ permission: "task" as const, pattern: "*" as const, action: "deny" as const }]),
3341
]
34-
}
42+
}

0 commit comments

Comments
 (0)