Skip to content

Fix parallel MCP calls across event loops - #372

Open
anbuzin wants to merge 1 commit into
mainfrom
fix-mcp-loop-lock
Open

anbuzin wants to merge 1 commit into
mainfrom
fix-mcp-loop-lock

Conversation

@anbuzin

@anbuzin anbuzin commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

If your worker calls asyncio.run() for each job and the model asks for two MCP tools in one step, a later job could send a tool error back to the model. We were sharing a connection lock that could get bound to the first loop. Each event loop now gets its own lock.

async def job(model):
    tools = await ai.mcp.get_http_tools("http://127.0.0.1:18461/mcp", tool_prefix="a")
    tools += await ai.mcp.get_http_tools("http://127.0.0.1:18462/mcp", tool_prefix="b")
    runner = ai.Agent(tools=tools)
    async with runner.run(model, [ai.user_message("Check the catalogs")]) as stream:
        async for _ in stream: pass

asyncio.run(job(model))  # The model receives two successful tool results.
asyncio.run(job(model))  # before: one tool result reports a RuntimeError (different event loop)
                         # after: two successful tool results

The module-level MCP pool lock bound to the first event loop where
connections contended. A later event loop could then fail with a
bound-to-a-different-event-loop error.

Use weakly held locks keyed by the running event loop. Active and
waiting operations keep a strong local reference to their lock.
Idle locks and their loops are not retained, and same-loop
serialization stays unchanged.
@anbuzin anbuzin added the fix Bug fix label Nov 20, 2026
@vercel

vercel Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
ai-python Ready Ready Preview, v0 Nov 20, 2026 12:08am UTC

This branch was successfully deployed

1 active deployment
Preview — a224edeb Deployed Nov 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant