Skip to content

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

Open
lucasgomide wants to merge 2 commits intomainfrom
lg-mcp-event-loop
Open

fix: use persistent event loop for MCP operations to prevent cancel scope errors#4634
lucasgomide wants to merge 2 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
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.

1 participant