|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from unittest.mock import patch |
| 5 | +from unittest.mock import AsyncMock, MagicMock, patch |
6 | 6 |
|
7 | 7 | import anyio |
8 | 8 | import pytest |
@@ -307,13 +307,103 @@ async def test_complete_with_prompt_reference(simple_server: Server): |
307 | 307 | def test_client_with_url_initializes_streamable_http_transport(): |
308 | 308 | with patch("mcp.client.client.streamable_http_client") as mock: |
309 | 309 | _ = Client("http://localhost:8000/mcp") |
310 | | - mock.assert_called_once_with("http://localhost:8000/mcp", session_id=None) |
| 310 | + mock.assert_called_once_with("http://localhost:8000/mcp", session_id=None, terminate_on_close=True) |
311 | 311 |
|
312 | 312 |
|
313 | 313 | def test_client_with_url_and_session_id_initializes_streamable_http_transport(): |
314 | 314 | with patch("mcp.client.client.streamable_http_client") as mock: |
315 | 315 | _ = Client("http://localhost:8000/mcp", streamable_http_session_id="resume-session-id") |
316 | | - mock.assert_called_once_with("http://localhost:8000/mcp", session_id="resume-session-id") |
| 316 | + mock.assert_called_once_with( |
| 317 | + "http://localhost:8000/mcp", |
| 318 | + session_id="resume-session-id", |
| 319 | + terminate_on_close=True, |
| 320 | + ) |
| 321 | + |
| 322 | + |
| 323 | +def test_client_with_url_and_terminate_on_close_false_initializes_streamable_http_transport(): |
| 324 | + with patch("mcp.client.client.streamable_http_client") as mock: |
| 325 | + _ = Client("http://localhost:8000/mcp", streamable_http_terminate_on_close=False) |
| 326 | + mock.assert_called_once_with("http://localhost:8000/mcp", session_id=None, terminate_on_close=False) |
| 327 | + |
| 328 | + |
| 329 | +def test_client_resume_session_builder_initializes_streamable_http_transport(): |
| 330 | + initialize_result = types.InitializeResult( |
| 331 | + protocol_version="2025-03-26", |
| 332 | + capabilities=types.ServerCapabilities(), |
| 333 | + server_info=types.Implementation(name="server", version="1.0"), |
| 334 | + ) |
| 335 | + with patch("mcp.client.client.streamable_http_client") as mock: |
| 336 | + _ = Client.resume_session( |
| 337 | + "http://localhost:8000/mcp", |
| 338 | + session_id="resume-session-id", |
| 339 | + initialize_result=initialize_result, |
| 340 | + ) |
| 341 | + mock.assert_called_once_with( |
| 342 | + "http://localhost:8000/mcp", |
| 343 | + session_id="resume-session-id", |
| 344 | + terminate_on_close=True, |
| 345 | + ) |
| 346 | + |
| 347 | + |
| 348 | +async def test_client_resume_session_skips_initialize(): |
| 349 | + initialize_result = types.InitializeResult( |
| 350 | + protocol_version="2025-03-26", |
| 351 | + capabilities=types.ServerCapabilities(), |
| 352 | + server_info=types.Implementation(name="server", version="1.0"), |
| 353 | + ) |
| 354 | + |
| 355 | + transport_cm = AsyncMock() |
| 356 | + transport_cm.__aenter__.return_value = (MagicMock(), MagicMock()) |
| 357 | + transport_cm.__aexit__.return_value = None |
| 358 | + |
| 359 | + session = MagicMock() |
| 360 | + session.initialize = AsyncMock() |
| 361 | + session.resume = MagicMock() |
| 362 | + session.initialize_result = initialize_result |
| 363 | + session_cm = AsyncMock() |
| 364 | + session_cm.__aenter__.return_value = session |
| 365 | + session_cm.__aexit__.return_value = None |
| 366 | + |
| 367 | + with ( |
| 368 | + patch("mcp.client.client.streamable_http_client", return_value=transport_cm), |
| 369 | + patch("mcp.client.client.ClientSession", return_value=session_cm), |
| 370 | + ): |
| 371 | + async with Client.resume_session( |
| 372 | + "http://localhost:8000/mcp", |
| 373 | + session_id="resume-session-id", |
| 374 | + initialize_result=initialize_result, |
| 375 | + ) as client: |
| 376 | + assert client.initialize_result == initialize_result |
| 377 | + |
| 378 | + session.initialize.assert_not_awaited() |
| 379 | + session.resume.assert_called_once_with(initialize_result) |
| 380 | + |
| 381 | + |
| 382 | +async def test_client_streamable_initialize_result_requires_session_id(): |
| 383 | + initialize_result = types.InitializeResult( |
| 384 | + protocol_version="2025-03-26", |
| 385 | + capabilities=types.ServerCapabilities(), |
| 386 | + server_info=types.Implementation(name="server", version="1.0"), |
| 387 | + ) |
| 388 | + |
| 389 | + transport_cm = AsyncMock() |
| 390 | + transport_cm.__aenter__.return_value = (MagicMock(), MagicMock()) |
| 391 | + transport_cm.__aexit__.return_value = None |
| 392 | + |
| 393 | + session_cm = AsyncMock() |
| 394 | + session_cm.__aenter__.return_value = AsyncMock() |
| 395 | + session_cm.__aexit__.return_value = None |
| 396 | + |
| 397 | + with ( |
| 398 | + patch("mcp.client.client.streamable_http_client", return_value=transport_cm), |
| 399 | + patch("mcp.client.client.ClientSession", return_value=session_cm), |
| 400 | + ): |
| 401 | + client = Client( |
| 402 | + "http://localhost:8000/mcp", |
| 403 | + streamable_http_initialize_result=initialize_result, |
| 404 | + ) |
| 405 | + with pytest.raises(RuntimeError, match="requires streamable_http_session_id"): |
| 406 | + await client.__aenter__() |
317 | 407 |
|
318 | 408 |
|
319 | 409 | async def test_client_uses_transport_directly(app: MCPServer): |
|
0 commit comments