Skip to content

fix: use AsyncExitStack for MCP client lifecycle management (#300)#305

Open
thinkingmik wants to merge 1 commit intoopenagents-org:developfrom
thinkingmik:fix/issue-300-mcp-streamable-http-cancelled-error
Open

fix: use AsyncExitStack for MCP client lifecycle management (#300)#305
thinkingmik wants to merge 1 commit intoopenagents-org:developfrom
thinkingmik:fix/issue-300-mcp-streamable-http-cancelled-error

Conversation

@thinkingmik
Copy link
Copy Markdown

Summary

Replaces manual `aenter`/`aexit` calls with `AsyncExitStack` for all MCP transport types (stdio, sse, streamable_http).

Problem

When connecting to MCP servers via streamable HTTP transport, the session initialization fails with `asyncio.exceptions.CancelledError`. This happens because calling `aenter` manually on `streamablehttp_client` breaks anyio's `TaskGroup` invariant — the task group must be exited in the same task it was entered.

Solution

  • Replaced manual context manager handling with `AsyncExitStack.enter_async_context()` across all three transport types
  • Added proper cleanup of exit stacks on setup failure
  • Unified cleanup logic in `cleanup_mcp_clients()` to handle exit-stack-managed clients

Related

Closes #300"

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 9, 2026

@thinkingmik is attempting to deploy a commit to the Raphael's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown

@MontaEllis8 MontaEllis8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Code Review 通过

代码审查通过,建议合并。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CancelledError when connecting to MCP servers via streamable HTTP transport

2 participants