Skip to content

feat(delegate): MCP workspace guard for subagents#3632

Open
Mibayy wants to merge 1 commit into
NousResearch:mainfrom
Mibayy:feat/subagent-mcp-guard
Open

feat(delegate): MCP workspace guard for subagents#3632
Mibayy wants to merge 1 commit into
NousResearch:mainfrom
Mibayy:feat/subagent-mcp-guard

Conversation

@Mibayy

@Mibayy Mibayy commented Mar 28, 2026

Copy link
Copy Markdown
Contributor

Safety fix: prevents subagents from corrupting shared codebase-index state.

The Problem

Subagents run in separate threads but share the parent's MCP codebase-index session. If a subagent calls set_project_root, switch_project, or reindex, it corrupts the index for the parent and all sibling subagents mid-execution.

The Solution

Thread-local guard blocks workspace-mutating MCP tools inside subagent threads:

  • _tl.subagent_guard flag (thread-local, default False)
  • Blocked tools: set_project_root, switch_project, reindex
  • Activated before child.run_conversation(), disabled in finally block
  • Read-only tools (get_functions, search_codebase, etc.) remain available

Implementation

mcp_tool.py:

  • Added _tl thread-local storage
  • _SUBAGENT_BLOCKED_TOOL_NAMES frozenset
  • set_subagent_mcp_guard(active) public API
  • _make_tool_handler checks guard before dispatch

delegate_tool.py:

  • Import set_subagent_mcp_guard
  • Call with True before child runs, False in finally

tests/tools/test_mcp_subagent_guard.py:
10 tests covering block behavior, thread isolation, read-only pass-through


Part 2/3 of #3387 review response:

  1. ✅ Skill injection (feat(delegate): skill inheritance for subagents #3631)
  2. ✅ MCP workspace guard (this PR)
  3. Configurable max_depth (next)

Prevents subagents from corrupting shared codebase-index state via:
- Thread-local guard flag (_tl.subagent_guard)
- Blocked tools: set_project_root, switch_project, reindex
- Activated in delegate_tool before child.run_conversation()
- Auto-disabled in finally block (thread-isolated)

Read-only MCP tools remain available.
@alt-glitch alt-glitch added type/feature New feature or request P2 Medium — degraded but workaround exists tool/delegate Subagent delegation tool/mcp MCP client and OAuth labels May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Medium — degraded but workaround exists tool/delegate Subagent delegation tool/mcp MCP client and OAuth type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants