Inject AgentContext.id into MCP tool kwargs for external reply routing
Problem
When Agent Zero calls an external MCP server tool, the external tool receives only the arguments specified by the LLM. The LLM has no access to its own AgentContext.id, so it cannot pass it. This makes it impossible for external tools to route asynchronous replies (e.g., from iMessage, SMS, email) back to the originating Agent Zero conversation.
Proposed Fix
In python/helpers/mcp_handler.py, conditionally inject self.agent.context.id into the tool's kwargs before forwarding to the MCP server. The injection must be scoped to specific tools to avoid breaking MCP servers that use strict argument validation (e.g., Pydantic).
class MCPTool(Tool):
async def execute(self, **kwargs: Any):
# Inject context_id for tools that need reply routing
if "imessage" in self.name.lower():
kwargs["_context_id"] = self.agent.context.id
error = ""
try:
response: CallToolResult = await MCPConfig.get_instance().call_tool(
self.name, kwargs
)
Ideally, a more general solution would allow MCP servers to opt-in to receiving the context_id, perhaps via a capability flag in the MCP server configuration. This would avoid hardcoding tool names in the framework.
Why
This enables a whole category of bidirectional integrations (iMessage bridges, SMS gateways, email handlers, webhook callbacks) where an external system needs to route a response back into the correct Agent Zero conversation.
The _context_id is prefixed with an underscore to distinguish it from user-defined tool parameters.
Impact
- No breaking changes when scoped — only tools matching the condition receive the extra argument
- Unscoped injection breaks MCP servers with Pydantic validation (confirmed with Google Workspace)
- Minimal code change
Closing due to inactivity of 90 days.