Skip to content

Commit 5df7955

Browse files
committed
fix(opencode): 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 forwarded 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. Wildcard '*' allow rules are also forwarded. Fixes anomalyco#16491, anomalyco#3808
1 parent 6340758 commit 5df7955

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import type { Agent } from "./agent"
88
* 1. The parent session's deny rules and external_directory rules.
99
* Parent agent restrictions only govern that agent; the subagent's own
1010
* permissions determine its capabilities.
11-
* 2. Default `todowrite` and `task` denies if the subagent's own ruleset
11+
* 2. The parent session's allow rules for MCP tools — subagents need
12+
* explicit allow permissions to execute MCP tools (context7_resolve-library-id,
13+
* matrix_matrix_read, etc.). Without this, subagents can see MCP tools in
14+
* their tool list but get permission denied on execution. (#16491, #3808)
15+
* 3. Default `todowrite` and `task` denies if the subagent's own ruleset
1216
* doesn't already permit them.
1317
*/
1418
export function deriveSubagentSessionPermission(input: {
@@ -17,7 +21,13 @@ export function deriveSubagentSessionPermission(input: {
1721
}): PermissionV1.Ruleset {
1822
const canTask = input.subagent.permission.some((rule) => rule.permission === "task")
1923
const canTodo = input.subagent.permission.some((rule) => rule.permission === "todowrite")
24+
const parentSessionMcpAllows = input.parentSessionPermission.filter(
25+
(rule) =>
26+
rule.action === "allow" &&
27+
(rule.permission === "*" || rule.permission.includes("_")),
28+
)
2029
return [
30+
...parentSessionMcpAllows,
2131
...input.parentSessionPermission.filter(
2232
(rule) => rule.permission === "external_directory" || rule.action === "deny",
2333
),

0 commit comments

Comments
 (0)