Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix(toolkits): MCPToolkit connects once when max_retries=0 (#4117)

Shahil kadia272a1540301a
diff --git a/camel/toolkits/mcp_toolkit.py b/camel/toolkits/mcp_toolkit.py
index 4aa096f5d..2b333f30b 100644
--- a/camel/toolkits/mcp_toolkit.py
+++ b/camel/toolkits/mcp_toolkit.py
@@ -453,7 +453,10 @@ class MCPToolkit(BaseToolkit):
Returns True on success, raises on final failure.
"""
last_exc: Optional[BaseException] = None
- for attempt in range(1, self._max_retries + 1):
+ # max_retries counts retries *after* the first attempt, so the
+ # client always gets at least one connection attempt.
+ total_attempts = self._max_retries + 1
+ for attempt in range(1, total_attempts + 1):
try:
await asyncio.wait_for(
client.__aenter__(), timeout=self._per_client_timeout
@@ -471,7 +474,7 @@ class MCPToolkit(BaseToolkit):
logger.warning(
f"Client {i+1} timed out after "
f"{self._per_client_timeout}s "
- f"(attempt {attempt}/{self._max_retries})"
+ f"(attempt {attempt}/{total_attempts})"
)
except asyncio.CancelledError:
await client._cleanup_connection()
@@ -482,12 +485,12 @@ class MCPToolkit(BaseToolkit):
await client._cleanup_connection()
logger.warning(
f"Client {i+1} connection failed "
- f"(attempt {attempt}/{self._max_retries}): {e}"
+ f"(attempt {attempt}/{total_attempts}): {e}"
)
- if attempt < self._max_retries:
+ if attempt < total_attempts:
await asyncio.sleep(self._retry_delay)
raise ConnectionError(
- f"Client {i+1} failed after {self._max_retries} attempts"
+ f"Client {i+1} failed after {total_attempts} attempts"
) from last_exc
async def _connect_all_clients(self):
diff --git a/test/toolkits/test_mcp_toolkit.py b/test/toolkits/test_mcp_toolkit.py
index bf6a15ac3..2220fc50b 100644
--- a/test/toolkits/test_mcp_toolkit.py
+++ b/test/toolkits/test_mcp_toolkit.py
@@ -211,6 +211,69 @@ class TestMCPToolkitConnectionManagement:
# Verify the first client's __aexit__ was called for cleanup
mock_client1.__aexit__.assert_called_once()
+ @pytest.mark.asyncio
+ async def test_connect_with_zero_max_retries_attempts_once(self):
+ """max_retries=0 must still make the initial connection attempt."""
+ mock_client = MagicMock()
+ mock_client.__aenter__ = AsyncMock(return_value=mock_client)
+ mock_client.__aexit__ = AsyncMock(return_value=None)
+ mock_client.is_connected.return_value = True
+
+ toolkit = MCPToolkit(
+ clients=[mock_client],
+ max_retries=0,
+ retry_delay=0,
+ skip_failed=False,
+ )
+
+ await toolkit.connect()
+
+ assert toolkit._is_connected
+ mock_client.__aenter__.assert_awaited_once()
+
+ @pytest.mark.asyncio
+ async def test_connect_zero_max_retries_fails_after_one_attempt(self):
+ """max_retries=0 makes one attempt, then gives up without retrying."""
+ mock_client = MagicMock()
+ mock_client.__aenter__ = AsyncMock(side_effect=RuntimeError("boom"))
+ mock_client._cleanup_connection = AsyncMock()
+ mock_client.is_connected.return_value = False
+
+ toolkit = MCPToolkit(
+ clients=[mock_client],
+ max_retries=0,
+ retry_delay=0,
+ skip_failed=False,
+ )
+
+ with pytest.raises(MCPConnectionError):
+ await toolkit.connect()
+
+ # Exactly one attempt even though max_retries=0.
+ assert mock_client.__aenter__.await_count == 1
+ mock_client._cleanup_connection.assert_awaited_once()
+
+ @pytest.mark.asyncio
+ async def test_max_retries_counts_retries_after_first_attempt(self):
+ """max_retries counts retries after the first attempt."""
+ mock_client = MagicMock()
+ mock_client.__aenter__ = AsyncMock(side_effect=RuntimeError("boom"))
+ mock_client._cleanup_connection = AsyncMock()
+ mock_client.is_connected.return_value = False
+
+ toolkit = MCPToolkit(
+ clients=[mock_client],
+ max_retries=2,
+ retry_delay=0,
+ skip_failed=False,
+ )
+
+ with pytest.raises(MCPConnectionError):
+ await toolkit.connect()
+
+ # 1 initial attempt + 2 retries.
+ assert mock_client.__aenter__.await_count == 3
+
@pytest.mark.asyncio
async def test_connect_already_connected(self):
"""Test connecting when already connected."""