adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

How to pass the user_session_id from agent to mcp server tool

Open AeonMind opened this issue 1 year ago • 3 comments

Background:

adk_python version: 1.1.1

I create multi agent system. one coordinator agent and two sub agents(gis_command_agent and database_search_agent). each sub agent with one MCP Server(each MCP Server include some of MCP tools)

The gis_command_agent handle web map related tasks, the database_search_agent handle search from database related tasks。

when the task route to gis_command_agent and call the MCP server, I need to pass the session_id (created by /apps/{app_name}/users/{user_id}/sessions/{session_id}) to MCP Server. when the task route to database_search_agent and call MCP server, no need pass the session id.

So i add the before_tool_callback function for the gis_command_agent, and add the session id in args, below is the detail code:

def before_tool_call_function(
    tool: BaseTool, args: Dict[str, Any], tool_context: ToolContext
) -> Optional[Dict]:
    
    args["user_session_id"] = tool_context._invocation_context.session.id

Therefore,the gis_command_agent's MCP Server Tools with parameter "user_session_id". that work success.


But I fact the issue is that:

  1. when the first task related map operation correctly to gis_command_agent and completed the task success.
  2. the second task related data search from database, so need to transfer control permission to coordinator agent or database_search_agent by call transfer_to_agent function,

That prompt the error "Error during MCP session cleanup: Attempted to exit cancel scope in a different task than it was entered in Error in event_generator: transfer_to_agent() got an unexpected keyword argument 'user_session_id'"

I try to fix this issue by add the after_tool_callback for gis_command_agent , for remove the "user_session_id". below is the detail code:

def after_tool_call_function(
    tool: BaseTool, args: Dict[str, Any], tool_context: ToolContext, tool_response: Dict
) -> Optional[Dict]:
    if "user_session_id" in args:
        del args["user_session_id"]
    return None

But could not resolve this issue. So my question is that how to remove the user_session_id before call transfer_to_agent or is there any other way to pass the session id to MCP tool from agent.

thanks .

AeonMind avatar May 30 '25 07:05 AeonMind

I've coded Session Memory ToolSet to handle this issue, Not a right way to do it, but it gets the job done.

veevortexiq avatar Jun 02 '25 18:06 veevortexiq

Thanks @veevortexiq . Could you show me the details ?

AeonMind avatar Jun 03 '25 01:06 AeonMind

Hi @AeonMind and thanks to everyone who participated in this thread.

We're currently doing some housekeeping on our issue tracker. Given the rapid pace of development and the number of updates since this was last active, there's a good chance this issue has been resolved in a newer version. To help us keep our backlog focused on current issues, we are closing this as stale.

If you're still facing this problem, could you please update to the very latest version of the library and confirm the issue still exists? If it does, please comment here, and we will be happy to reopen this issue and investigate further.

Thanks for your contribution and understanding!

jwiejulie avatar Aug 10 '25 03:08 jwiejulie