|
| 1 | +"""Tests for auth and httpx_client_factory params on MCPServerSse and MCPServerStreamableHttp.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import httpx |
| 8 | +import pytest |
| 9 | + |
| 10 | +from agents.mcp import MCPServerSse, MCPServerStreamableHttp |
| 11 | + |
| 12 | + |
| 13 | +class TestMCPServerSseAuthAndFactory: |
| 14 | + """Tests for auth and httpx_client_factory added to MCPServerSseParams.""" |
| 15 | + |
| 16 | + @pytest.mark.asyncio |
| 17 | + async def test_sse_default_no_auth_no_factory(self): |
| 18 | + """SSE create_streams passes only the four base params when no extras are set.""" |
| 19 | + with patch("agents.mcp.server.sse_client") as mock_client: |
| 20 | + mock_client.return_value = MagicMock() |
| 21 | + server = MCPServerSse(params={"url": "http://localhost:8000/sse"}) |
| 22 | + server.create_streams() |
| 23 | + mock_client.assert_called_once_with( |
| 24 | + url="http://localhost:8000/sse", |
| 25 | + headers=None, |
| 26 | + timeout=5, |
| 27 | + sse_read_timeout=300, |
| 28 | + ) |
| 29 | + |
| 30 | + @pytest.mark.asyncio |
| 31 | + async def test_sse_with_auth(self): |
| 32 | + """SSE create_streams forwards the auth parameter when provided.""" |
| 33 | + auth = httpx.BasicAuth(username="user", password="pass") |
| 34 | + with patch("agents.mcp.server.sse_client") as mock_client: |
| 35 | + mock_client.return_value = MagicMock() |
| 36 | + server = MCPServerSse(params={"url": "http://localhost:8000/sse", "auth": auth}) |
| 37 | + server.create_streams() |
| 38 | + mock_client.assert_called_once_with( |
| 39 | + url="http://localhost:8000/sse", |
| 40 | + headers=None, |
| 41 | + timeout=5, |
| 42 | + sse_read_timeout=300, |
| 43 | + auth=auth, |
| 44 | + ) |
| 45 | + |
| 46 | + @pytest.mark.asyncio |
| 47 | + async def test_sse_with_httpx_client_factory(self): |
| 48 | + """SSE create_streams forwards a custom httpx_client_factory when provided.""" |
| 49 | + |
| 50 | + def custom_factory( |
| 51 | + headers: dict[str, str] | None = None, |
| 52 | + timeout: httpx.Timeout | None = None, |
| 53 | + auth: httpx.Auth | None = None, |
| 54 | + ) -> httpx.AsyncClient: |
| 55 | + return httpx.AsyncClient(verify=False) # pragma: no cover |
| 56 | + |
| 57 | + with patch("agents.mcp.server.sse_client") as mock_client: |
| 58 | + mock_client.return_value = MagicMock() |
| 59 | + server = MCPServerSse( |
| 60 | + params={ |
| 61 | + "url": "http://localhost:8000/sse", |
| 62 | + "httpx_client_factory": custom_factory, |
| 63 | + } |
| 64 | + ) |
| 65 | + server.create_streams() |
| 66 | + mock_client.assert_called_once_with( |
| 67 | + url="http://localhost:8000/sse", |
| 68 | + headers=None, |
| 69 | + timeout=5, |
| 70 | + sse_read_timeout=300, |
| 71 | + httpx_client_factory=custom_factory, |
| 72 | + ) |
| 73 | + |
| 74 | + @pytest.mark.asyncio |
| 75 | + async def test_sse_with_auth_and_factory(self): |
| 76 | + """SSE create_streams forwards both auth and httpx_client_factory together.""" |
| 77 | + auth = httpx.BasicAuth(username="user", password="pass") |
| 78 | + |
| 79 | + def custom_factory( |
| 80 | + headers: dict[str, str] | None = None, |
| 81 | + timeout: httpx.Timeout | None = None, |
| 82 | + auth: httpx.Auth | None = None, |
| 83 | + ) -> httpx.AsyncClient: |
| 84 | + return httpx.AsyncClient(verify=False) # pragma: no cover |
| 85 | + |
| 86 | + with patch("agents.mcp.server.sse_client") as mock_client: |
| 87 | + mock_client.return_value = MagicMock() |
| 88 | + server = MCPServerSse( |
| 89 | + params={ |
| 90 | + "url": "http://localhost:8000/sse", |
| 91 | + "headers": {"X-Token": "abc"}, |
| 92 | + "auth": auth, |
| 93 | + "httpx_client_factory": custom_factory, |
| 94 | + } |
| 95 | + ) |
| 96 | + server.create_streams() |
| 97 | + mock_client.assert_called_once_with( |
| 98 | + url="http://localhost:8000/sse", |
| 99 | + headers={"X-Token": "abc"}, |
| 100 | + timeout=5, |
| 101 | + sse_read_timeout=300, |
| 102 | + auth=auth, |
| 103 | + httpx_client_factory=custom_factory, |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +class TestMCPServerStreamableHttpAuth: |
| 108 | + """Tests for the auth parameter added to MCPServerStreamableHttpParams.""" |
| 109 | + |
| 110 | + @pytest.mark.asyncio |
| 111 | + async def test_streamable_http_default_no_auth(self): |
| 112 | + """StreamableHttp create_streams omits auth when not provided.""" |
| 113 | + with patch("agents.mcp.server.streamablehttp_client") as mock_client: |
| 114 | + mock_client.return_value = MagicMock() |
| 115 | + server = MCPServerStreamableHttp(params={"url": "http://localhost:8000/mcp"}) |
| 116 | + server.create_streams() |
| 117 | + mock_client.assert_called_once_with( |
| 118 | + url="http://localhost:8000/mcp", |
| 119 | + headers=None, |
| 120 | + timeout=5, |
| 121 | + sse_read_timeout=300, |
| 122 | + terminate_on_close=True, |
| 123 | + ) |
| 124 | + |
| 125 | + @pytest.mark.asyncio |
| 126 | + async def test_streamable_http_with_auth(self): |
| 127 | + """StreamableHttp create_streams forwards the auth parameter when provided.""" |
| 128 | + auth = httpx.BasicAuth(username="user", password="pass") |
| 129 | + with patch("agents.mcp.server.streamablehttp_client") as mock_client: |
| 130 | + mock_client.return_value = MagicMock() |
| 131 | + server = MCPServerStreamableHttp( |
| 132 | + params={"url": "http://localhost:8000/mcp", "auth": auth} |
| 133 | + ) |
| 134 | + server.create_streams() |
| 135 | + mock_client.assert_called_once_with( |
| 136 | + url="http://localhost:8000/mcp", |
| 137 | + headers=None, |
| 138 | + timeout=5, |
| 139 | + sse_read_timeout=300, |
| 140 | + terminate_on_close=True, |
| 141 | + auth=auth, |
| 142 | + ) |
| 143 | + |
| 144 | + @pytest.mark.asyncio |
| 145 | + async def test_streamable_http_with_auth_and_factory(self): |
| 146 | + """StreamableHttp create_streams forwards both auth and httpx_client_factory.""" |
| 147 | + auth = httpx.BasicAuth(username="user", password="pass") |
| 148 | + |
| 149 | + def custom_factory( |
| 150 | + headers: dict[str, str] | None = None, |
| 151 | + timeout: httpx.Timeout | None = None, |
| 152 | + auth: httpx.Auth | None = None, |
| 153 | + ) -> httpx.AsyncClient: |
| 154 | + return httpx.AsyncClient(verify=False) # pragma: no cover |
| 155 | + |
| 156 | + with patch("agents.mcp.server.streamablehttp_client") as mock_client: |
| 157 | + mock_client.return_value = MagicMock() |
| 158 | + server = MCPServerStreamableHttp( |
| 159 | + params={ |
| 160 | + "url": "http://localhost:8000/mcp", |
| 161 | + "auth": auth, |
| 162 | + "httpx_client_factory": custom_factory, |
| 163 | + } |
| 164 | + ) |
| 165 | + server.create_streams() |
| 166 | + mock_client.assert_called_once_with( |
| 167 | + url="http://localhost:8000/mcp", |
| 168 | + headers=None, |
| 169 | + timeout=5, |
| 170 | + sse_read_timeout=300, |
| 171 | + terminate_on_close=True, |
| 172 | + auth=auth, |
| 173 | + httpx_client_factory=custom_factory, |
| 174 | + ) |
0 commit comments