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

Agent Engine Agent fails to use MCP. Error point at bad handling of session management in async context

Open rrondir opened this issue 11 months ago • 13 comments

Running the example

# Your ADK agent connects to the remote MCP service via Streamable HTTP
McpToolset(
    connection_params=StreamableHTTPConnectionParams(
        url="https://your-mcp-server-url.run.app/mcp",
        headers={"Authorization": "Bearer your-auth-token"}
    ),
)

My agent in agent engine is having issues answering questions, I'm using Gemini Enterprise as the interface.

After each call to the mcp the agent fails giving me the option to regenerate the response, it seems it tries to re use the previous session but fails with error messages such as:

Error: an error occured during closing of asynchronous generator <async_generator object streamable_http_client at ...>

RuntimeError: Attempted to exit cancel scope in a different task than it was entered in

RuntimeError: ahtrow(): asynchrnous generator is already running

I'm deploying using all the latest versions from google-genai, google-adk and google-cloud-aiplatform

I can share more details, but I can't see anything special about my setup

Additional context When running this locally via ADK web I see no issues at all. I'm using PSC I to connect to the MCP running in a cloud run server. The connections are not stateless

rrondir avatar Dec 17 '25 13:12 rrondir

Hello @rrondir ,

Could you please clarify if this issue is specific to the custom remote MCP server, or if it occurs across different MCP servers?

llalitkumarrr avatar Dec 23 '25 09:12 llalitkumarrr

@llalitkumarrr Across different MCP servers. I can't make this work reliably, I even tried creating random headers to create a new session each time

rrondir avatar Dec 23 '25 11:12 rrondir

Hi @rrondir,

I had the same issues you've encountered. It seems like the McpSessionManager should handle the ClientSession of MCP library in a single task.

I made a PR #4025 to solve this issue, you may look into it if you are interested in.

challenger71498 avatar Dec 26 '25 10:12 challenger71498

Hello @ryanaiagent ,

Could you please have a look at this PR and guide with further process?

llalitkumarrr avatar Dec 29 '25 06:12 llalitkumarrr

Hi @rrondir,

It seems the fix has been merged and released in v1.23.0. You can try to reproduce the issue with the latest ADK and check whether it is resolved.

challenger71498 avatar Jan 31 '26 15:01 challenger71498

Hello @rrondir ,

Kindly check with google-adk==1.23.0 and let us know if that fixes the problem.

llalitkumarrr avatar Feb 02 '26 04:02 llalitkumarrr

It addressed some of the issues but it seems that the root of the problem lies in the runner config used in the agent engine adk template and how it runs the tasks.

I still cant make the mcp work reliably. Has this been tested internally? An adk agent running in agent engine calling a remote mcp?

I would be happy to provide more details in a gcp support case

rrondir avatar Feb 02 '26 12:02 rrondir

@rrondir Thank you for your response :)

It addressed some of the issues but it seems that the root of the problem lies in the runner config used in the agent engine adk template and how it runs the tasks.

Happy to hear that the PR resolved a portion of your issues. As you said, this problem cannot be resolved completely by the PR, and seems to be related to Vertex AI Agent Engine. I have no experience using it, so I need some time to investigate how it works and what could cause the problem.

Has this been tested internally? An adk agent running in agent engine calling a remote mcp?

I don't know whether maintainers tested internally, but for me, I didn't tested your case completely though. I found your issue by searching Attempted to exit cancel scope in a different task than it was entered in and thought that was only the problem.

Could you share your entire stacktrace when the error occurs? Version 1.23.0 will resolve Attempted to exit cancel scope in a different task than it was entered in error, so if something still does not work properly, there would be another error message. Or are the messages above still exist except the cancel scope one?

challenger71498 avatar Feb 02 '26 13:02 challenger71498

I cant share the entire stacktrace since it’s in a private tenant.

But this is how adk runs in vertexai agent engine https://github.com/googleapis/python-aiplatform/blob/main/vertexai/agent_engines/templates/adk.py

The end of the stacktrace raises a RuntimeError: Event Loop is Closed

My guess is that it stems from the fact that the vertexai runner is using https://github.com/google/adk-python/blob/main/src/google/adk/runners.py#L436 to run the agent events

I’ll keep digging in the meantime

rrondir avatar Feb 02 '26 14:02 rrondir

Hello @rrondir ,

You mentioned some of the issues have been resolved but since you still cannot make MCP work reliably, could you please share details? Is this about the same issue or are you getting a different error?

llalitkumarrr avatar Feb 19 '26 09:02 llalitkumarrr

This issue has been automatically marked as stale because it has not had recent activity for 7 days after a maintainer requested clarification. It will be closed if no further activity occurs within 7 days.

adk-bot avatar Feb 27 '26 06:02 adk-bot

Resolved by https://github.com/google/adk-python/commit/cce430da

sasha-gitg avatar Mar 02 '26 15:03 sasha-gitg

I still has the exact issue with version 1.26.0

LuigiCerone avatar Mar 11 '26 09:03 LuigiCerone