Skip to content

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

@smithakolan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    mcp[Component] Issues about MCP supporttools[Component] This issue is related to tools

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions