Skip to content

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

Closed

Description

@smithakolan

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).

Activity

  1. added
    mcp
    tools
    on Feb 11, 2026
  2. added theissue type on Feb 11, 2026
  3. self-assigned this
    on Feb 20, 2026
  4. klateefa commented on Feb 20, 2026

    @klateefa

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

  5. added
    answered
    on Feb 20, 2026
  6. Baptiste-ms commented on Feb 25, 2026

    @Baptiste-ms

    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.

  7. surajksharma07 commented on Jul 29, 2026

    @surajksharma07
    Collaborator

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

  8. added
    request clarification
    on Jul 29, 2026
  9. self-assigned this
    on Aug 6, 2026
  10. GWeale commented on Aug 6, 2026

    @GWeale
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

answeredmcprequest clarificationtools

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions