From 0b89f1882dccc1acd0ee109832053edecec04850 Mon Sep 17 00:00:00 2001 From: akshaypachpute-1998 Date: Fri, 22 Aug 2025 13:09:35 -0700 Subject: [PATCH] chore: replaced hard coded value for user_id to the value from the tool context from parent agent. Fixes google/adk-python#2407 Merge https://github.com/google/adk-python/pull/2409 Description: This PR Fixes: #2407 The AgentTool in /google/adk/tools/agent_tool.py uses a hardcoded user_id='tmp_user' when creating a new session for the agent it wraps. This happens within the run_async method. code snippet ... @override async def run_async( self, *, args: dict[str, Any], tool_context: ToolContext, ) -> Any: ... session = await runner.session_service.create_session( app_name=self.agent.name, user_id='tmp_user', # <-- This is hardcoded state=tool_context.state.to_dict(), ) ... Why is this a problem? This hardcoding breaks the chain of user identity. When a parent agent calls a sub-agent via the AgentTool, the original user_id is lost. Any tool or logic inside the sub-agent that needs to perform user-specific actions (e.g., accessing user data from a database, retrieving user-specific memory, checking permissions) will fail or operate on the wrong context because it receives 'tmp_user' instead of the actual user's ID. Impact: This prevents the creation of robust, multi-agent applications where user context must be maintained across different agents and tools. It limits the utility of AgentTool to only stateless sub-agents that do not require user-specific information. Suggested Fix: The user_id should be retrieved from the parent context, which is available via the tool_context parameter passed into run_async. The create_session call should be updated to use the dynamic user_id from the parent session.For example, the fix might involve accessing the user ID from the tool_context. code-snippet session = await runner.session_service.create_session( app_name=self.agent.name, user_id=tool_context._invocation_context.user_id, state=tool_context.state.to_dict(), ) To Reproduce Steps to reproduce the behavior: To reproduce this bug, we need to set up a two-agent system: a ParentAgent that calls a ChildAgent using the AgentTool. The ChildAgent will have a tool designed to simply return the user_id it receives from its context. Expected behavior It should return the user_id of the user calling the agent, but, in current situation we are getting tmp_user COPYBARA_INTEGRATE_REVIEW=https://github.com/google/adk-python/pull/2409 from akshaypachpute-1998:fix-issue-2407-agent-tool-context-propogation 0c3e8656fdf11386e3ab13a3a1f2df99a396dbd1 PiperOrigin-RevId: 798315832 --- src/google/adk/tools/agent_tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/google/adk/tools/agent_tool.py b/src/google/adk/tools/agent_tool.py index 7a8b15b5..89086c90 100644 --- a/src/google/adk/tools/agent_tool.py +++ b/src/google/adk/tools/agent_tool.py @@ -135,7 +135,7 @@ class AgentTool(BaseTool): ) session = await runner.session_service.create_session( app_name=self.agent.name, - user_id='tmp_user', + user_id=tool_context._invocation_context.user_id, state=tool_context.state.to_dict(), )