Skip to content

fix(mcp): prevent tool registration race condition on startup#25622

Open
vikarag wants to merge 2 commits into
NousResearch:mainfrom
vikarag:fix-mcp-race
Open

fix(mcp): prevent tool registration race condition on startup#25622
vikarag wants to merge 2 commits into
NousResearch:mainfrom
vikarag:fix-mcp-race

Conversation

@vikarag

@vikarag vikarag commented May 14, 2026

Copy link
Copy Markdown

Root Cause

When an agent starts via CLI, is called. In , the flag was firing immediately after discovery, but before actually populated the ToolRegistry.

Because fetches the definitions immediately upon instantiation, the API payload was receiving because the servers had connected but had not registered their endpoints.

Fix

  1. Moved directly into the loop, executed immediately before .
  2. Added logic inside so that any detached instances or fast CLI calls properly block until MCP servers are fully registered.
  3. If an agent (e.g., cron background task) executes but discovery was never started, now synchronously invokes first.

This closes the gap ensuring the model never generates text-only responses missing tools.

vikarag and others added 2 commits May 14, 2026 17:44
Move _register_server_tools() call from _discover_and_register_server()
into MCPServerTask._register_tools(), invoked inside all four transport
paths (stdio, SSE, new HTTP, legacy HTTP) before _ready.set(). This
ensures the ToolRegistry is populated before start() returns, closing
the window where get_tool_definitions() could see an empty registry.

Also adds wait_for_mcp_ready() as defense-in-depth: get_tool_definitions()
blocks up to 30s for MCP discovery to finish if it's in progress.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround tool/mcp MCP client and OAuth comp/tools Tool registry, model_tools, toolsets comp/agent Core agent loop, run_agent.py, prompt builder labels May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder comp/tools Tool registry, model_tools, toolsets P1 High — major feature broken, no workaround tool/mcp MCP client and OAuth type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants