Skip to content

Building AI agents, atomically

fixing test cases for sse and adding test cases for http stream

jon fox6cdef5bb2b26
diff --git a/atomic-agents/tests/lib/factories/test_mcp_tool_factory.py b/atomic-agents/tests/lib/factories/test_mcp_tool_factory.py
index 33d8991..aea3a39 100644
--- a/atomic-agents/tests/lib/factories/test_mcp_tool_factory.py
+++ b/atomic-agents/tests/lib/factories/test_mcp_tool_factory.py
@@ -409,3 +409,31 @@ def test_run_tool_with_persistent_session_no_event_loop(monkeypatch):
assert "'NoneType' object has no attribute 'run_until_complete'" in str(exc.value)
finally:
loop.close()
+
+
+def test_http_stream_connection_error_handling(monkeypatch):
+ """Test HTTP stream connection error handling in MCPToolFactory."""
+ from atomic_agents.lib.factories.tool_definition_service import ToolDefinitionService
+
+ # Mock ToolDefinitionService.fetch_definitions to raise ConnectionError for HTTP_STREAM
+ original_fetch = ToolDefinitionService.fetch_definitions
+
+ async def mock_fetch_definitions(self):
+ if self.transport_type == MCPTransportType.HTTP_STREAM:
+ raise ConnectionError("HTTP stream connection failed")
+ return await original_fetch(self)
+
+ monkeypatch.setattr(ToolDefinitionService, "fetch_definitions", mock_fetch_definitions)
+
+ factory = MCPToolFactory("http://test-endpoint", MCPTransportType.HTTP_STREAM)
+
+ with pytest.raises(ConnectionError, match="HTTP stream connection failed"):
+ factory._fetch_tool_definitions()
+
+
+def test_http_stream_endpoint_formatting():
+ """Test that HTTP stream endpoints are properly formatted with /mcp/ suffix."""
+ factory = MCPToolFactory("http://test-endpoint", MCPTransportType.HTTP_STREAM)
+
+ # Verify the factory was created with correct transport type
+ assert factory.transport_type == MCPTransportType.HTTP_STREAM
diff --git a/atomic-agents/tests/lib/factories/test_tool_definition_service.py b/atomic-agents/tests/lib/factories/test_tool_definition_service.py
index 8541201..e1060a0 100644
--- a/atomic-agents/tests/lib/factories/test_tool_definition_service.py
+++ b/atomic-agents/tests/lib/factories/test_tool_definition_service.py
@@ -63,7 +63,7 @@ class TestToolDefinitionService:
mock_client_session_cls.return_value = mock_session
# Create service
- service = ToolDefinitionService("http://test-endpoint")
+ service = ToolDefinitionService("http://test-endpoint", transport_type=MCPTransportType.SSE)
# Mock the fetch_definitions_from_session to return directly
original_method = service.fetch_definitions_from_session
@@ -89,6 +89,47 @@ class TestToolDefinitionService:
# Restore the original method
service.fetch_definitions_from_session = original_method
+ @pytest.mark.asyncio
+ @patch("atomic_agents.lib.factories.tool_definition_service.streamablehttp_client")
+ @patch("atomic_agents.lib.factories.tool_definition_service.ClientSession")
+ async def test_fetch_via_http_stream(self, mock_client_session_cls, mock_http_client, mock_client_session):
+ # Setup
+ mock_transport = MockAsyncContextManager(return_value=(AsyncMock(), AsyncMock(), AsyncMock()))
+ mock_http_client.return_value = mock_transport
+
+ mock_session = MockAsyncContextManager(return_value=mock_client_session)
+ mock_client_session_cls.return_value = mock_session
+
+ # Create service with HTTP_STREAM transport
+ service = ToolDefinitionService("http://test-endpoint", transport_type=MCPTransportType.HTTP_STREAM)
+
+ # Mock the fetch_definitions_from_session to return directly
+ original_method = service.fetch_definitions_from_session
+ service.fetch_definitions_from_session = AsyncMock(
+ return_value=[
+ MCPToolDefinition(
+ name="MockTool",
+ description="Mock tool for testing",
+ input_schema={"type": "object", "properties": {"param": {"type": "string"}}},
+ )
+ ]
+ )
+
+ # Execute
+ result = await service.fetch_definitions()
+
+ # Verify
+ assert len(result) == 1
+ assert isinstance(result[0], MCPToolDefinition)
+ assert result[0].name == "MockTool"
+ assert result[0].description == "Mock tool for testing"
+
+ # Verify HTTP client was called with correct endpoint (should have /mcp/ suffix)
+ mock_http_client.assert_called_once_with("http://test-endpoint/mcp/")
+
+ # Restore the original method
+ service.fetch_definitions_from_session = original_method
+
@pytest.mark.asyncio
async def test_fetch_via_stdio(self):
# Create service
@@ -183,14 +224,21 @@ class TestToolDefinitionService:
@pytest.mark.asyncio
async def test_sse_connection_error(self):
with patch("atomic_agents.lib.factories.tool_definition_service.sse_client", side_effect=ConnectionError):
- svc = ToolDefinitionService("http://host")
+ svc = ToolDefinitionService("http://host", transport_type=MCPTransportType.SSE)
+ with pytest.raises(ConnectionError):
+ await svc.fetch_definitions()
+
+ @pytest.mark.asyncio
+ async def test_http_stream_connection_error(self):
+ with patch("atomic_agents.lib.factories.tool_definition_service.streamablehttp_client", side_effect=ConnectionError):
+ svc = ToolDefinitionService("http://host", transport_type=MCPTransportType.HTTP_STREAM)
with pytest.raises(ConnectionError):
await svc.fetch_definitions()
@pytest.mark.asyncio
async def test_generic_error_wrapped(self):
with patch("atomic_agents.lib.factories.tool_definition_service.sse_client", side_effect=OSError("BOOM")):
- svc = ToolDefinitionService("http://host")
+ svc = ToolDefinitionService("http://host", transport_type=MCPTransportType.SSE)
with pytest.raises(RuntimeError):
await svc.fetch_definitions()