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

ADK 1.24.x MCP StreamableHTTP cancel scope error with adk web + Cloud Run

Open smithakolan opened this issue 9 months ago • 4 comments

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 web local

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

  1. Deploy the MCP server to Cloud Run (Streamable HTTP).
  2. Deploy the agent to Cloud Run.
  3. Run the agent via adk web locally (pointing to the Cloud Run agent).
  4. Trigger tool discovery / any MCP tool call.
  5. 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 web tool discovery).
  • Running under adk web likely increases the chance of cross-task teardown (tool discovery + reload/background tasks).

smithakolan avatar Feb 11 '26 17:02 smithakolan

@smithakolan Thanks for Reporting! This issue is caused by an AnyIO version mismatch. AnyIO 4.x introduces stricter cancellation rules, which leads to the error: “Attempted to exit cancel scope in a different task than it was entered in.” Pinning AnyIO to a 3.x version resolves the problem. Use: anyio>=3.7,<4. Thank you.

klateefa avatar Feb 20 '26 06:02 klateefa

There are some obvious dependency issues regarding anyio>=3.7,<4 @klateefa and the latest versions of google ADK.

19.08 The conflict is caused by:
19.08     The user requested anyio==3.7.1
19.08     starlette 0.52.1 depends on anyio<5 and >=3.6.2
19.08     google-adk 1.24.1 depends on anyio<5.0.0 and >=4.9.0

@smithakolan did you find a way ? Thanks for the help.

Baptiste-ms avatar Feb 25 '26 10:02 Baptiste-ms

@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!

surajksharma07 avatar Jul 29 '26 13:07 surajksharma07

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.

GWeale avatar Aug 06 '26 21:08 GWeale