-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathtest_streamable_http_client_factory.py
More file actions
442 lines (371 loc) · 15.9 KB
/
test_streamable_http_client_factory.py
File metadata and controls
442 lines (371 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"""Tests for MCPServerStreamableHttp httpx_client_factory functionality."""
from __future__ import annotations
import base64
from unittest.mock import MagicMock, patch
import httpx
import pytest
from anyio import create_memory_object_stream
from mcp.shared.message import SessionMessage
from mcp.types import JSONRPCMessage, JSONRPCNotification, JSONRPCRequest
from agents.mcp import MCPServerStreamableHttp
from agents.mcp.server import (
_create_default_streamable_http_client,
_InitializedNotificationTolerantStreamableHTTPTransport,
_streamablehttp_client_with_transport,
)
class TestMCPServerStreamableHttpClientFactory:
"""Test cases for custom httpx_client_factory parameter."""
@pytest.mark.asyncio
async def test_default_httpx_client_factory(self):
"""Test that default behavior works when no custom factory is provided."""
# Mock the streamablehttp_client to avoid actual network calls
with patch("agents.mcp.server.streamablehttp_client") as mock_client:
mock_client.return_value = MagicMock()
server = MCPServerStreamableHttp(
params={
"url": "http://localhost:8000/mcp",
"headers": {"Authorization": "Bearer token"},
"timeout": 10,
}
)
# Create streams should not pass httpx_client_factory when not provided
server.create_streams()
# Verify streamablehttp_client was called with correct parameters
mock_client.assert_called_once_with(
url="http://localhost:8000/mcp",
headers={"Authorization": "Bearer token"},
timeout=10,
sse_read_timeout=300, # Default value
terminate_on_close=True, # Default value
# httpx_client_factory should not be passed when not provided
)
@pytest.mark.asyncio
async def test_custom_httpx_client_factory(self):
"""Test that custom httpx_client_factory is passed correctly."""
# Create a custom factory function
def custom_factory(
headers: dict[str, str] | None = None,
timeout: httpx.Timeout | None = None,
auth: httpx.Auth | None = None,
) -> httpx.AsyncClient:
return httpx.AsyncClient(
verify=False, # Disable SSL verification for testing
timeout=httpx.Timeout(60.0),
headers={"X-Custom-Header": "test"},
)
# Mock the streamablehttp_client to avoid actual network calls
with patch("agents.mcp.server.streamablehttp_client") as mock_client:
mock_client.return_value = MagicMock()
server = MCPServerStreamableHttp(
params={
"url": "http://localhost:8000/mcp",
"headers": {"Authorization": "Bearer token"},
"timeout": 10,
"httpx_client_factory": custom_factory,
}
)
# Create streams should pass the custom factory
server.create_streams()
# Verify streamablehttp_client was called with the custom factory
mock_client.assert_called_once_with(
url="http://localhost:8000/mcp",
headers={"Authorization": "Bearer token"},
timeout=10,
sse_read_timeout=300, # Default value
terminate_on_close=True, # Default value
httpx_client_factory=custom_factory,
)
@pytest.mark.asyncio
async def test_custom_httpx_client_factory_with_ssl_cert(self):
"""Test custom factory with SSL certificate configuration."""
def ssl_cert_factory(
headers: dict[str, str] | None = None,
timeout: httpx.Timeout | None = None,
auth: httpx.Auth | None = None,
) -> httpx.AsyncClient:
return httpx.AsyncClient(
verify="/path/to/cert.pem", # Custom SSL certificate
timeout=httpx.Timeout(120.0),
)
with patch("agents.mcp.server.streamablehttp_client") as mock_client:
mock_client.return_value = MagicMock()
server = MCPServerStreamableHttp(
params={
"url": "https://secure-server.com/mcp",
"timeout": 30,
"httpx_client_factory": ssl_cert_factory,
}
)
server.create_streams()
mock_client.assert_called_once_with(
url="https://secure-server.com/mcp",
headers=None,
timeout=30,
sse_read_timeout=300,
terminate_on_close=True,
httpx_client_factory=ssl_cert_factory,
)
@pytest.mark.asyncio
async def test_custom_httpx_client_factory_with_proxy(self):
"""Test custom factory with proxy configuration."""
def proxy_factory(
headers: dict[str, str] | None = None,
timeout: httpx.Timeout | None = None,
auth: httpx.Auth | None = None,
) -> httpx.AsyncClient:
return httpx.AsyncClient(
proxy="http://proxy.example.com:8080",
timeout=httpx.Timeout(60.0),
)
with patch("agents.mcp.server.streamablehttp_client") as mock_client:
mock_client.return_value = MagicMock()
server = MCPServerStreamableHttp(
params={
"url": "http://localhost:8000/mcp",
"httpx_client_factory": proxy_factory,
}
)
server.create_streams()
mock_client.assert_called_once_with(
url="http://localhost:8000/mcp",
headers=None,
timeout=5, # Default value
sse_read_timeout=300,
terminate_on_close=True,
httpx_client_factory=proxy_factory,
)
@pytest.mark.asyncio
async def test_custom_httpx_client_factory_with_retry_logic(self):
"""Test custom factory with retry logic configuration."""
def retry_factory(
headers: dict[str, str] | None = None,
timeout: httpx.Timeout | None = None,
auth: httpx.Auth | None = None,
) -> httpx.AsyncClient:
return httpx.AsyncClient(
timeout=httpx.Timeout(30.0),
# Note: httpx doesn't have built-in retry, but this shows how
# a custom factory could be used to configure retry behavior
# through middleware or other mechanisms
)
with patch("agents.mcp.server.streamablehttp_client") as mock_client:
mock_client.return_value = MagicMock()
server = MCPServerStreamableHttp(
params={
"url": "http://localhost:8000/mcp",
"httpx_client_factory": retry_factory,
}
)
server.create_streams()
mock_client.assert_called_once_with(
url="http://localhost:8000/mcp",
headers=None,
timeout=5,
sse_read_timeout=300,
terminate_on_close=True,
httpx_client_factory=retry_factory,
)
def test_httpx_client_factory_type_annotation(self):
"""Test that the type annotation is correct for httpx_client_factory."""
from agents.mcp.server import MCPServerStreamableHttpParams
# This test ensures the type annotation is properly set
# We can't easily test the TypedDict at runtime, but we can verify
# that the import works and the type is available
assert hasattr(MCPServerStreamableHttpParams, "__annotations__")
# Verify that the httpx_client_factory parameter is in the annotations
annotations = MCPServerStreamableHttpParams.__annotations__
assert "httpx_client_factory" in annotations
# The annotation should contain the string representation of the type
annotation_str = str(annotations["httpx_client_factory"])
assert "HttpClientFactory" in annotation_str
@pytest.mark.asyncio
async def test_all_parameters_with_custom_factory(self):
"""Test that all parameters work together with custom factory."""
def comprehensive_factory(
headers: dict[str, str] | None = None,
timeout: httpx.Timeout | None = None,
auth: httpx.Auth | None = None,
) -> httpx.AsyncClient:
return httpx.AsyncClient(
verify=False,
timeout=httpx.Timeout(90.0),
headers={"X-Test": "value"},
)
with patch("agents.mcp.server.streamablehttp_client") as mock_client:
mock_client.return_value = MagicMock()
server = MCPServerStreamableHttp(
params={
"url": "https://api.example.com/mcp",
"headers": {"Authorization": "Bearer token"},
"timeout": 45,
"sse_read_timeout": 600,
"terminate_on_close": False,
"httpx_client_factory": comprehensive_factory,
}
)
server.create_streams()
mock_client.assert_called_once_with(
url="https://api.example.com/mcp",
headers={"Authorization": "Bearer token"},
timeout=45,
sse_read_timeout=600,
terminate_on_close=False,
httpx_client_factory=comprehensive_factory,
)
@pytest.mark.asyncio
async def test_initialized_notification_failure_returns_synthetic_success():
async def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(503, request=request)
transport = _InitializedNotificationTolerantStreamableHTTPTransport("https://example.test/mcp")
read_stream_writer, _ = create_memory_object_stream[SessionMessage | Exception](0)
client = httpx.AsyncClient(transport=httpx.MockTransport(handler))
try:
ctx = MagicMock()
ctx.client = client
ctx.read_stream_writer = read_stream_writer
ctx.session_message = SessionMessage(
JSONRPCMessage(
JSONRPCNotification(
jsonrpc="2.0",
method="notifications/initialized",
params={},
)
)
)
await transport._handle_post_request(ctx)
finally:
await client.aclose()
await read_stream_writer.aclose()
@pytest.mark.asyncio
async def test_initialized_notification_transport_exception_returns_synthetic_success():
async def handler(request: httpx.Request) -> httpx.Response:
raise httpx.ConnectError("boom", request=request)
transport = _InitializedNotificationTolerantStreamableHTTPTransport("https://example.test/mcp")
read_stream_writer, _ = create_memory_object_stream[SessionMessage | Exception](0)
client = httpx.AsyncClient(transport=httpx.MockTransport(handler))
try:
ctx = MagicMock()
ctx.client = client
ctx.read_stream_writer = read_stream_writer
ctx.session_message = SessionMessage(
JSONRPCMessage(
JSONRPCNotification(
jsonrpc="2.0",
method="notifications/initialized",
params={},
)
)
)
await transport._handle_post_request(ctx)
finally:
await client.aclose()
await read_stream_writer.aclose()
@pytest.mark.asyncio
async def test_streamable_http_server_passes_ignore_initialized_notification_failure():
with patch("agents.mcp.server._streamablehttp_client_with_transport") as mock_client:
mock_client.return_value = MagicMock()
server = MCPServerStreamableHttp(
params={
"url": "http://localhost:8000/mcp",
"ignore_initialized_notification_failure": True,
}
)
server.create_streams()
kwargs = mock_client.call_args.kwargs
assert kwargs["url"] == "http://localhost:8000/mcp"
assert kwargs["headers"] is None
assert kwargs["timeout"] == 5
assert kwargs["sse_read_timeout"] == 300
assert kwargs["terminate_on_close"] is True
assert (
kwargs["transport_factory"] is _InitializedNotificationTolerantStreamableHTTPTransport
)
@pytest.mark.asyncio
async def test_transport_preserves_non_initialized_failures():
async def handler(request: httpx.Request) -> httpx.Response:
raise httpx.ConnectError("boom", request=request)
transport = _InitializedNotificationTolerantStreamableHTTPTransport("https://example.test/mcp")
read_stream_writer, _ = create_memory_object_stream[SessionMessage | Exception](0)
client = httpx.AsyncClient(transport=httpx.MockTransport(handler))
try:
ctx = MagicMock()
ctx.client = client
ctx.read_stream_writer = read_stream_writer
ctx.session_message = SessionMessage(
JSONRPCMessage(
JSONRPCRequest(
jsonrpc="2.0",
id=1,
method="tools/list",
params={},
)
)
)
with pytest.raises(httpx.ConnectError):
await transport._handle_post_request(ctx)
finally:
await client.aclose()
await read_stream_writer.aclose()
@pytest.mark.asyncio
async def test_stream_client_preserves_custom_factory_headers_timeout_and_auth():
seen: dict[str, object] = {}
class RecordingAuth(httpx.Auth):
def auth_flow(self, request: httpx.Request):
request.headers["Authorization"] = f"Basic {base64.b64encode(b'user:pass').decode()}"
yield request
async def handler(request: httpx.Request) -> httpx.Response:
seen["request_headers"] = dict(request.headers)
return httpx.Response(200, request=request)
def base_factory(
headers: dict[str, str] | None = None,
timeout: httpx.Timeout | None = None,
auth: httpx.Auth | None = None,
) -> httpx.AsyncClient:
seen["factory_headers"] = headers
seen["factory_timeout"] = timeout
seen["factory_auth"] = auth
return httpx.AsyncClient(
headers=headers,
timeout=timeout,
auth=auth,
transport=httpx.MockTransport(handler),
)
timeout = httpx.Timeout(12.0)
auth = RecordingAuth()
async with _streamablehttp_client_with_transport(
"https://example.test/mcp",
headers={"X-Test": "value"},
timeout=12.0,
sse_read_timeout=30.0,
httpx_client_factory=base_factory,
auth=auth,
transport_factory=_InitializedNotificationTolerantStreamableHTTPTransport,
):
pass
assert seen["factory_headers"] == {"X-Test": "value"}
seen_timeout = seen["factory_timeout"]
assert isinstance(seen_timeout, httpx.Timeout)
assert seen_timeout.connect == timeout.connect
assert seen_timeout.read == 30.0
assert seen_timeout.write == timeout.write
assert seen_timeout.pool == timeout.pool
assert seen["factory_auth"] is auth
@pytest.mark.asyncio
async def test_default_streamable_http_client_matches_expected_defaults():
timeout = httpx.Timeout(12.0)
auth = httpx.BasicAuth("user", "pass")
client = _create_default_streamable_http_client(
headers={"X-Test": "value"},
timeout=timeout,
auth=auth,
)
try:
assert client.headers["X-Test"] == "value"
assert client.timeout.connect == timeout.connect
assert client.timeout.read == timeout.read
assert client.timeout.write == timeout.write
assert client.timeout.pool == timeout.pool
assert client.auth is auth
assert client.follow_redirects is True
finally:
await client.aclose()