You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Agent Engine Agent fails to use MCP. Error point at bad handling of session management in async context #3950
# 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
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
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?
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?
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.
Activity
llalitkumarrr commentedon Dec 23, 2025
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?
rrondir commentedon Dec 23, 2025
@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
challenger71498 commentedon Dec 26, 2025
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.
llalitkumarrr commentedon Dec 29, 2025
Hello @ryanaiagent ,
Could you please have a look at this PR and guide with further process?
feat: start and close ClientSession in a single task in McpSessionMan…
challenger71498 commentedon Jan 31, 2026
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.
llalitkumarrr commentedon Feb 2, 2026
Hello @rrondir ,
Kindly check with
google-adk==1.23.0and let us know if that fixes the problem.rrondir commentedon Feb 2, 2026
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
challenger71498 commentedon Feb 2, 2026
@rrondir Thank you for your response :)
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.
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 inand 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 inerror, 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?rrondir commentedon Feb 2, 2026
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
llalitkumarrr commentedon Feb 19, 2026
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?
adk-bot commentedon Feb 27, 2026
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.
sasha-gitg commentedon Mar 2, 2026
Resolved by cce430da
LuigiCerone commentedon Mar 11, 2026
I still has the exact issue with version 1.26.0
feat: start and close ClientSession in a single task in McpSessionMan…