Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/agents/mcp/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,12 @@ async def _cleanup_all(self) -> None:
)
self._errors[server] = exc
finally:
self._refresh_active_servers()
# Cleanup discards every processed server from the connected set, so no cleaned
# server may remain advertised as active regardless of `drop_failed_servers`:
# that flag only governs whether connect-phase failures stay listed.
Comment on lines +392 to +393

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Update the public flag contract to match cleanup behavior

When drop_failed_servers=False, this new cleanup branch makes active_servers empty, but the public MCPServerManager docstring still promises that active_servers “will still include all servers” when the flag is false. That documentation is now misleading for callers inspecting the class contract after cleanup_all(); qualify the flag description as connect-phase behavior and document the post-cleanup result.

Useful? React with 👍 / 👎.

self._active_servers = [
server for server in self._all_servers if server in self._connected_servers
]

async def _run_with_timeout(
self, func: Callable[[], Awaitable[Any]], timeout_seconds: float | None
Expand Down
19 changes: 19 additions & 0 deletions tests/mcp/test_mcp_server_manager_cleanup_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ async def test_cleanup_all_removes_cleaned_servers_from_active_servers() -> None
assert server.connect.await_count == 2


@pytest.mark.asyncio
async def test_cleanup_all_removes_cleaned_servers_from_active_servers_when_keeping_failed() -> (
None
):
"""`drop_failed_servers=False` keeps connect-phase failures listed, but a server that was
cleaned up is disconnected and must not remain advertised as active."""
server = cast(MCPServer, Mock(spec=MCPServer))
server.connect = AsyncMock()
server.cleanup = AsyncMock()

manager = MCPServerManager([server], drop_failed_servers=False)
assert await manager.connect_all() == [server]

await manager.cleanup_all()

assert manager.active_servers == []
assert manager._connected_servers == set()


@pytest.mark.asyncio
async def test_manager_owns_repeated_server_instance_once() -> None:
server = cast(MCPServer, Mock(spec=MCPServer))
Expand Down