Repository navigation
Description
After upgrading ADK from 1.14.0 to 1.24.0, an MCP integration that previously worked now fails during MCP session creation with:
Failed to create MCP session: Attempted to exit cancel scope in a different task than it was entered in
This is regarding the codelab: https://codelabs.developers.google.com/codelabs/cloud-run/use-mcp-server-on-cloud-run-with-an-adk-agent#0
This happens in the Cloud Run codelab architecture:
- MCP server deployed on Cloud Run (Streamable HTTP transport)
- Agent deployed on Cloud Run (Python ADK)
- Agent exercised locally via
adk web(tool discovery + calling MCP tools)
It appears related to MCP session lifecycle / cancellation behavior introduced around the MCP refactors in recent commits (notably changes in MCPToolset session creation/execution wrapper).
Environment
- ADK Python: 1.24.0 (works on 1.14.0)
- MCP server: Cloud Run, Streamable HTTP endpoint
- Local client:
adk web - Deployment: agent on Cloud Run, MCP server on Cloud Run,
adk weblocal
What I expected
MCPToolset should consistently create an MCP session and list tools / execute calls against the Cloud Run MCP server, as it did on ADK 1.14.0.
What happened
When adk web initializes tools (or when the agent first tries to use MCP tools), MCP session creation fails with:
Failed to create MCP session: Attempted to exit cancel scope in a different task than it was entered in
This prevents the agent from functioning.
Repro steps
- Deploy the MCP server to Cloud Run (Streamable HTTP).
- Deploy the agent to Cloud Run.
- Run the agent via
adk weblocally (pointing to the Cloud Run agent). - Trigger tool discovery / any MCP tool call.
- Observe the failure when creating MCP session.
Minimal snippet (agent.py MCP wiring)
from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, StreamableHTTPConnectionParams
import google.auth
import google.auth.transport.requests
import google.oauth2.id_token
mcp_server_url = os.getenv("MCP_SERVER_URL")
def get_id_token():
target_url = os.getenv("MCP_SERVER_URL")
audience = target_url.split('/mcp/')[0]
request = google.auth.transport.requests.Request()
return google.oauth2.id_token.fetch_id_token(request, audience)
mcp_tools = MCPToolset(
connection_params=StreamableHTTPConnectionParams(
url=mcp_server_url,
headers={"Authorization": f"Bearer {get_id_token()}"},
)
)Notes / suspected cause
- The exception message matches an AnyIO cancellation-scope/task-boundary issue (cancel scope exiting in a different task).
- This started after upgrading to 1.24.0 and may be triggered by new MCPToolset session execution behavior (session create + call wrapped in cancellation/timeout logic, or changed task boundaries during
adk webtool discovery). - Running under
adk weblikely increases the chance of cross-task teardown (tool discovery + reload/background tasks).
Activity
klateefa commentedon Feb 20, 2026
@smithakolan Thanks for Reporting!
This issue is caused by an
AnyIOversion mismatch.AnyIO 4.xintroduces stricter cancellation rules, which leads to the error: “Attempted to exit cancel scope in a different task than it was entered in.” PinningAnyIOto a3.xversion resolves the problem. Use:anyio>=3.7,<4.Thank you.
Baptiste-ms commentedon Feb 25, 2026
There are some obvious dependency issues regarding
anyio>=3.7,<4@klateefa and the latest versions of google ADK.@smithakolan did you find a way ?
Thanks for the help.
surajksharma07 commentedon Jul 29, 2026
@smithakolan Dug into this one — the anyio pin isn't actually the right fix, and it'll conflict with google-adk[mcp]'s own anyio>=4.9 requirement, which matches what Baptiste-ms ran into.
The real root cause was fixed upstream in v1.34.0 and again more thoroughly in v2.2.0 (closes #5886), by dropping the asyncio.wait_for wrapping that was tripping AnyIO's cancel-scope rules.
Could you bump to google-adk[mcp]>=2.2.0 (ideally 2.5.0) with default feature-flag settings and confirm the cancel-scope error is gone on your end?
If it's still showing up on a current version, that'd be a different, newer bug worth its own look!
GWeale commentedon Aug 6, 2026
the cause was wrapping the mcp client context entry and session.initialize in asyncio.wait_for, which cancels from a nested task. upgrade to v2.2.0 or later rather than pinning anyio. open a new issue if it resurfaces.