fix(mcp): build the active server list without re-consuming the iterable - #4235
Merged
seratch merged 1 commit intoAug 6, 2026
Merged
Conversation
MCPServerManager.__init__ accepts Iterable[MCPServer] but called list(servers) twice, so a generator or any other one-shot iterable was already exhausted by the time the second call ran and _active_servers started out empty. all_servers was populated correctly from the first pass, which is why the second pass should read from it rather than from the caller's iterable. The empty list is visible before connect_all runs, and it also survives a failed connect because drop_failed_servers=False restores the list captured at that point.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MCPServerManager.__init__acceptsIterable[MCPServer]but callslist(servers)twice, so a generator or any other one-shot iterable is already exhausted by the second call and_active_serversstarts out empty._all_serversis populated correctly by the first pass, so the second list should be built from it rather than from the caller's iterable again.The empty list is observable straight after construction, and it also survives a real operation: when
connect_allfails, thedrop_failed_servers=Falserestore path replays the list captured at entry, soactive_serversends up empty where an equivalentlistargument leaves it intact. After a successfulconnect_allthe value is masked, because_refresh_active_serversrebuilds it from_all_servers.Fail-before evidence: with
src/agents/mcp/manager.pyreverted tomainand the new tests kept,test_manager_accepts_one_shot_iterablesandtest_manager_restores_one_shot_iterable_servers_after_a_failed_connectboth fail withassert [] == [<tests.mcp.test_mcp_server_manager.FlakyServer object at 0x...>]. The third new test covers the successfulasync withpath and passes either way, so it is a regression guard rather than a reproducer. With the fix all 62 tests intests/mcp/test_mcp_server_manager.pypass, andmake lintandmake typecheckare green.