[FEATURE] Provide agent identity context to MCP tool calls for sub-agent resource isolation

Resolved 💬 11 comments Opened Mar 9, 2026 by yotsuda Closed May 11, 2026

Problem Statement

MCP servers that manage per-agent resources (e.g., persistent console sessions, database connections, file locks) need to distinguish whether a tool call comes from the parent agent or a sub-agent. Currently, there is no mechanism for this.

I develop PowerShell.MCP, an MCP server that provides persistent PowerShell console sessions. When Claude Code spawns sub-agents (via the Agent tool), each sub-agent should get its own isolated console session to avoid command interleaving. To support this, I added an is_subagent boolean parameter to my MCP tools — expecting sub-agents to self-identify by passing is_subagent=true.

However, this approach fundamentally doesn't work because sub-agents don't know they are sub-agents. The Agent tool spawns a new context with a task prompt, but there is no built-in concept of "you are a sub-agent" in that context. The parent agent can include this in the prompt, but it's unreliable. CLAUDE.md instructions don't help either — even if it says "sub-agents should pass is_subagent=true", the sub-agent has no way to know that it is a sub-agent.

Proposed Solution

Include agent identity metadata in MCP tool call requests, so MCP servers can automatically determine the caller's position in the agent hierarchy. For example:

{
  "agent_context": {
    "agent_id": "unique-id-per-agent-instance",
    "parent_agent_id": "parent-unique-id"
  }
}

With this, MCP servers could:

  • Automatically isolate resources per agent without requiring self-identification parameters
  • Distinguish parent vs. sub-agent requests without relying on the AI's self-awareness
  • Prevent resource conflicts in multi-agent scenarios

Alternative Solutions

  • Self-identification parameter (is_subagent): Added to MCP tool parameters, but fails because sub-agents lack self-awareness.
  • Server-issued IDs via a dedicated MCP tool: Requires the parent agent to call a setup tool and pass the ID to sub-agents via prompt instructions — fragile and error-prone.
  • CLAUDE.md instructions: Ineffective because the sub-agent reading the instruction cannot determine that it is a sub-agent.

Use Case Example

  1. User asks Claude Code to investigate a codebase issue
  2. Claude Code spawns 3 sub-agents in parallel via the Agent tool, each exploring different areas
  3. Each sub-agent calls invoke_expression on the PowerShell MCP server
  4. Current behavior: All 3 sub-agents share the parent's console session, causing command interleaving and output corruption
  5. With agent context: The MCP server sees each sub-agent has a unique agent_id and automatically assigns separate console sessions

Additional Context

This issue affects any MCP server that manages stateful, per-agent resources. As AI agents increasingly use sub-agents for parallel task execution, this will become a common requirement for MCP server developers.

This may also be relevant to the MCP specification itself (not just Claude Code), but since Claude Code is the MCP client making the tool calls, the agent context would need to be injected at the client level.

View original on GitHub ↗

This issue has 11 comments on GitHub. Read the full discussion on GitHub ↗