-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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).