-
Notifications
You must be signed in to change notification settings - Fork 439
fix: return MCP connection errors to LLM instead of raising #1531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jsonmp-k8
wants to merge
1
commit into
kagent-dev:main
Choose a base branch
from
jsonmp-k8:fix/1530-mcp-tool-call-cpu-spin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
168 changes: 168 additions & 0 deletions
168
python/packages/kagent-adk/tests/unittests/test_mcp_connection_error_handling.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| """Tests for ConnectionSafeMcpTool — connection errors are returned as | ||
| error text to the LLM instead of raised, preventing tight retry loops. | ||
|
|
||
| See: https://github.com/kagent-dev/kagent/issues/1530 | ||
| """ | ||
|
|
||
| import asyncio | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import httpx | ||
| import pytest | ||
| from google.adk.tools.mcp_tool.mcp_tool import McpTool | ||
| from google.adk.tools.mcp_tool.mcp_toolset import McpToolset | ||
| from mcp.shared.exceptions import McpError | ||
|
|
||
| from kagent.adk._mcp_toolset import ConnectionSafeMcpTool, KAgentMcpToolset | ||
|
|
||
|
|
||
| def _make_connection_safe_tool(side_effect): | ||
| """Create a ConnectionSafeMcpTool with a mocked super().run_async.""" | ||
| tool = ConnectionSafeMcpTool.__new__(ConnectionSafeMcpTool) | ||
| tool.name = "test-tool" | ||
jsonmp-k8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| tool._mcp_tool = MagicMock() | ||
| tool._mcp_tool.name = "test-tool" | ||
| tool._mcp_session_manager = AsyncMock() | ||
| tool._header_provider = None | ||
| tool._auth_config = None | ||
| tool._confirmation_config = None | ||
| tool._progress_callback = None | ||
| tool._parent_run_async = AsyncMock(side_effect=side_effect) | ||
| return tool | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_connection_reset_error_returns_error_dict(): | ||
| """ConnectionResetError should be caught and returned as error text.""" | ||
| tool = _make_connection_safe_tool(ConnectionResetError("Connection reset by peer")) | ||
|
|
||
| with patch.object(McpTool, "run_async", tool._parent_run_async): | ||
| result = await tool.run_async(args={"key": "value"}, tool_context=MagicMock()) | ||
|
|
||
| assert "error" in result | ||
| assert "ConnectionResetError" in result["error"] | ||
| assert "Connection reset by peer" in result["error"] | ||
| assert "Do not retry" in result["error"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_connection_refused_error_returns_error_dict(): | ||
| """ConnectionRefusedError should be caught and returned as error text.""" | ||
| tool = _make_connection_safe_tool(ConnectionRefusedError("Connection refused")) | ||
|
|
||
| with patch.object(McpTool, "run_async", tool._parent_run_async): | ||
| result = await tool.run_async(args={}, tool_context=MagicMock()) | ||
|
|
||
| assert "error" in result | ||
| assert "ConnectionRefusedError" in result["error"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_timeout_error_returns_error_dict(): | ||
| """TimeoutError should be caught and returned as error text.""" | ||
| tool = _make_connection_safe_tool(TimeoutError("timed out")) | ||
|
|
||
| with patch.object(McpTool, "run_async", tool._parent_run_async): | ||
| result = await tool.run_async(args={}, tool_context=MagicMock()) | ||
|
|
||
| assert "error" in result | ||
| assert "TimeoutError" in result["error"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_httpx_connect_error_returns_error_dict(): | ||
| """httpx.ConnectError should be caught via httpx.TransportError.""" | ||
| tool = _make_connection_safe_tool(httpx.ConnectError("connection refused")) | ||
|
|
||
| with patch.object(McpTool, "run_async", tool._parent_run_async): | ||
| result = await tool.run_async(args={}, tool_context=MagicMock()) | ||
|
|
||
| assert "error" in result | ||
| assert "ConnectError" in result["error"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_httpx_read_error_returns_error_dict(): | ||
| """httpx.ReadError (connection reset by peer) should be caught.""" | ||
| tool = _make_connection_safe_tool(httpx.ReadError("peer closed connection")) | ||
|
|
||
| with patch.object(McpTool, "run_async", tool._parent_run_async): | ||
| result = await tool.run_async(args={}, tool_context=MagicMock()) | ||
|
|
||
| assert "error" in result | ||
| assert "ReadError" in result["error"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_httpx_connect_timeout_returns_error_dict(): | ||
| """httpx.ConnectTimeout should be caught via httpx.TransportError.""" | ||
| tool = _make_connection_safe_tool(httpx.ConnectTimeout("timed out")) | ||
|
|
||
| with patch.object(McpTool, "run_async", tool._parent_run_async): | ||
| result = await tool.run_async(args={}, tool_context=MagicMock()) | ||
|
|
||
| assert "error" in result | ||
| assert "ConnectTimeout" in result["error"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_mcp_error_returns_error_dict(): | ||
| """McpError (raised by MCP session on stream drop / read timeout) should be caught.""" | ||
| from mcp.types import ErrorData | ||
|
|
||
| tool = _make_connection_safe_tool(McpError(ErrorData(code=-1, message="session read timeout"))) | ||
|
|
||
| with patch.object(McpTool, "run_async", tool._parent_run_async): | ||
| result = await tool.run_async(args={}, tool_context=MagicMock()) | ||
|
|
||
| assert "error" in result | ||
| assert "McpError" in result["error"] | ||
| assert "session read timeout" in result["error"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_non_connection_error_still_raises(): | ||
| """Non-connection errors (e.g. ValueError) should still propagate.""" | ||
| tool = _make_connection_safe_tool(ValueError("bad argument")) | ||
|
|
||
| with patch.object(McpTool, "run_async", tool._parent_run_async): | ||
| with pytest.raises(ValueError, match="bad argument"): | ||
| await tool.run_async(args={}, tool_context=MagicMock()) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_cancelled_error_still_raises(): | ||
| """CancelledError must propagate — it's not a connection error.""" | ||
| tool = _make_connection_safe_tool(asyncio.CancelledError("cancelled")) | ||
|
|
||
| with patch.object(McpTool, "run_async", tool._parent_run_async): | ||
| with pytest.raises(asyncio.CancelledError): | ||
| await tool.run_async(args={}, tool_context=MagicMock()) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_tools_wraps_mcp_tools(): | ||
| """KAgentMcpToolset.get_tools should wrap McpTool instances with ConnectionSafeMcpTool.""" | ||
| # Create a real McpTool instance (bypassing __init__) so isinstance checks work | ||
| fake_mcp_tool = McpTool.__new__(McpTool) | ||
| fake_mcp_tool.name = "wrapped-tool" | ||
| fake_mcp_tool._some_attr = "value" | ||
|
|
||
| # A non-McpTool object that should pass through unchanged | ||
| fake_other_tool = MagicMock() | ||
| fake_other_tool.name = "other-tool" | ||
|
|
||
| toolset = KAgentMcpToolset.__new__(KAgentMcpToolset) | ||
|
|
||
| async def mock_super_get_tools(self_arg, readonly_context=None): | ||
| return [fake_mcp_tool, fake_other_tool] | ||
|
|
||
| with patch.object(McpToolset, "get_tools", mock_super_get_tools): | ||
| tools = await toolset.get_tools() | ||
|
|
||
| assert len(tools) == 2 | ||
| assert isinstance(tools[0], ConnectionSafeMcpTool) | ||
| assert tools[0].name == "wrapped-tool" | ||
| assert tools[0]._some_attr == "value" | ||
| # Non-McpTool should pass through unchanged | ||
| assert tools[1] is fake_other_tool | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.