Skip to content

fix: use persistent event loop for MCP operations to prevent cancel scope errors#4634

Closed
lucasgomide wants to merge 3 commits intomainfrom
lg-mcp-event-loop
Closed

fix: use persistent event loop for MCP operations to prevent cancel scope errors#4634
lucasgomide wants to merge 3 commits intomainfrom
lg-mcp-event-loop

Conversation

@lucasgomide
Copy link
Contributor

Replace per-call asyncio.run() with a single persistent background event loop for all MCP operations. The MCP SDK's streamable HTTP transport uses anyio task groups whose cancel scopes must be entered and exited on the same event loop and task. Creating a throwaway loop per tool call caused "Attempted to exit cancel scope in a different task" RuntimeErrors during cleanup, preventing MCP tools from working reliably

…cope errors

Replace per-call asyncio.run() with a single persistent background event
loop for all MCP operations. The MCP SDK's streamable HTTP transport uses
anyio task groups whose cancel scopes must be entered and exited on the
same event loop and task. Creating a throwaway loop per tool call caused
"Attempted to exit cancel scope in a different task" RuntimeErrors during
cleanup, preventing MCP tools from working reliably
@cursor
Copy link

cursor bot commented Mar 2, 2026

You have run out of free Bugbot PR reviews for this billing cycle. This will reset on March 28.

To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@lucasgomide lucasgomide closed this Mar 2, 2026
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.

2 participants