feat(delegate): MCP workspace guard for subagents#3632
Open
Mibayy wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, orreindex, 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_guardflag (thread-local, default False)set_project_root,switch_project,reindexchild.run_conversation(), disabled in finally blockget_functions,search_codebase, etc.) remain availableImplementation
mcp_tool.py:
_tlthread-local storage_SUBAGENT_BLOCKED_TOOL_NAMESfrozensetset_subagent_mcp_guard(active)public API_make_tool_handlerchecks guard before dispatchdelegate_tool.py:
set_subagent_mcp_guardtests/tools/test_mcp_subagent_guard.py:
10 tests covering block behavior, thread isolation, read-only pass-through
Part 2/3 of #3387 review response: