Commit 5787554
committed
fix(mcp): handle CancelledError in MCPSessionManager.create_session
When an MCP server returns an HTTP error (e.g., 401, 403), the MCP SDK
uses anyio cancel scopes internally, which raise CancelledError. Since
CancelledError is a BaseException (not Exception) in Python 3.8+, the
existing `except Exception` block does not catch it.
This causes the error to propagate uncaught, leading to:
- Application hangs
- ASGI callable returned without completing response errors
- Inability to handle MCP connection failures gracefully
This fix explicitly catches asyncio.CancelledError and converts it to
a ConnectionError with a descriptive message, allowing proper error
handling and cleanup.
Fixes #37081 parent a9b853f commit 5787554
File tree
2 files changed
+55
-0
lines changed- src/google/adk/tools/mcp_tool
- tests/unittests/tools/mcp_tool
2 files changed
+55
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
363 | 363 | | |
364 | 364 | | |
365 | 365 | | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
366 | 385 | | |
367 | 386 | | |
368 | 387 | | |
| |||
Lines changed: 36 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
293 | 329 | | |
294 | 330 | | |
295 | 331 | | |
| |||
0 commit comments