|
| 1 | +import asyncio |
1 | 2 | from typing import Any, cast
|
2 | 3 |
|
3 | 4 | import anyio
|
|
10 | 11 | from mcp.server.models import InitializationOptions
|
11 | 12 | from mcp.server.session import ServerSession
|
12 | 13 | from mcp.shared.context import RequestContext
|
| 14 | +from mcp.shared.message import SessionMessage |
13 | 15 | from mcp.shared.progress import progress
|
14 | 16 | from mcp.shared.session import (
|
15 | 17 | BaseSession,
|
16 | 18 | RequestResponder,
|
17 |
| - SessionMessage, |
18 | 19 | )
|
19 | 20 |
|
20 | 21 |
|
@@ -333,3 +334,191 @@ async def handle_client_message(
|
333 | 334 | assert server_progress_updates[3]["progress"] == 100
|
334 | 335 | assert server_progress_updates[3]["total"] == 100
|
335 | 336 | assert server_progress_updates[3]["message"] == "Processing results..."
|
| 337 | + |
| 338 | + |
| 339 | +@pytest.mark.anyio |
| 340 | +async def test_initialized_notification(): |
| 341 | + """Test that the server receives and handles InitializedNotification.""" |
| 342 | + server_to_client_send, server_to_client_receive = anyio.create_memory_object_stream[SessionMessage](1) |
| 343 | + client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[SessionMessage | Exception](1) |
| 344 | + |
| 345 | + server = Server("test") |
| 346 | + initialized_received = asyncio.Event() |
| 347 | + |
| 348 | + @server.initialized_notification() |
| 349 | + async def handle_initialized(notification: types.InitializedNotification): |
| 350 | + initialized_received.set() |
| 351 | + |
| 352 | + async def run_server(): |
| 353 | + await server.run( |
| 354 | + client_to_server_receive, |
| 355 | + server_to_client_send, |
| 356 | + server.create_initialization_options(), |
| 357 | + ) |
| 358 | + |
| 359 | + async def message_handler( |
| 360 | + message: (RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception), |
| 361 | + ) -> None: |
| 362 | + if isinstance(message, Exception): |
| 363 | + raise message |
| 364 | + |
| 365 | + async with ( |
| 366 | + ClientSession( |
| 367 | + server_to_client_receive, |
| 368 | + client_to_server_send, |
| 369 | + message_handler=message_handler, |
| 370 | + ) as client_session, |
| 371 | + anyio.create_task_group() as tg, |
| 372 | + ): |
| 373 | + tg.start_soon(run_server) |
| 374 | + await client_session.initialize() |
| 375 | + await initialized_received.wait() |
| 376 | + tg.cancel_scope.cancel() |
| 377 | + |
| 378 | + assert initialized_received.is_set() |
| 379 | + |
| 380 | + |
| 381 | +@pytest.mark.anyio |
| 382 | +async def test_roots_list_changed_notification(): |
| 383 | + """Test that the server receives and handles RootsListChangedNotification.""" |
| 384 | + server_to_client_send, server_to_client_receive = anyio.create_memory_object_stream[SessionMessage](1) |
| 385 | + client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[SessionMessage | Exception](1) |
| 386 | + |
| 387 | + server = Server("test") |
| 388 | + roots_list_changed_received = asyncio.Event() |
| 389 | + |
| 390 | + @server.roots_list_changed_notification() |
| 391 | + async def handle_roots_list_changed( |
| 392 | + notification: types.RootsListChangedNotification, |
| 393 | + ): |
| 394 | + roots_list_changed_received.set() |
| 395 | + |
| 396 | + async def run_server(): |
| 397 | + await server.run( |
| 398 | + client_to_server_receive, |
| 399 | + server_to_client_send, |
| 400 | + server.create_initialization_options(), |
| 401 | + ) |
| 402 | + |
| 403 | + async def message_handler( |
| 404 | + message: (RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception), |
| 405 | + ) -> None: |
| 406 | + if isinstance(message, Exception): |
| 407 | + raise message |
| 408 | + |
| 409 | + async with ( |
| 410 | + ClientSession( |
| 411 | + server_to_client_receive, |
| 412 | + client_to_server_send, |
| 413 | + message_handler=message_handler, |
| 414 | + ) as client_session, |
| 415 | + anyio.create_task_group() as tg, |
| 416 | + ): |
| 417 | + tg.start_soon(run_server) |
| 418 | + await client_session.initialize() |
| 419 | + await client_session.send_notification( |
| 420 | + types.ClientNotification( |
| 421 | + root=types.RootsListChangedNotification(method="notifications/roots/list_changed", params=None) |
| 422 | + ) |
| 423 | + ) |
| 424 | + await roots_list_changed_received.wait() |
| 425 | + tg.cancel_scope.cancel() |
| 426 | + |
| 427 | + assert roots_list_changed_received.is_set() |
| 428 | + |
| 429 | + |
| 430 | +@pytest.mark.anyio |
| 431 | +async def test_initialized_notification_with_session(): |
| 432 | + """Test that the server receives and handles InitializedNotification with a session.""" |
| 433 | + server_to_client_send, server_to_client_receive = anyio.create_memory_object_stream[SessionMessage](1) |
| 434 | + client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[SessionMessage | Exception](1) |
| 435 | + |
| 436 | + server = Server("test") |
| 437 | + initialized_received = asyncio.Event() |
| 438 | + received_session = None |
| 439 | + |
| 440 | + @server.initialized_notification() |
| 441 | + async def handle_initialized(notification: types.InitializedNotification, session: ServerSession): |
| 442 | + nonlocal received_session |
| 443 | + received_session = session |
| 444 | + initialized_received.set() |
| 445 | + |
| 446 | + async def run_server(): |
| 447 | + await server.run( |
| 448 | + client_to_server_receive, |
| 449 | + server_to_client_send, |
| 450 | + server.create_initialization_options(), |
| 451 | + ) |
| 452 | + |
| 453 | + async def message_handler( |
| 454 | + message: (RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception), |
| 455 | + ) -> None: |
| 456 | + if isinstance(message, Exception): |
| 457 | + raise message |
| 458 | + |
| 459 | + async with ( |
| 460 | + ClientSession( |
| 461 | + server_to_client_receive, |
| 462 | + client_to_server_send, |
| 463 | + message_handler=message_handler, |
| 464 | + ) as client_session, |
| 465 | + anyio.create_task_group() as tg, |
| 466 | + ): |
| 467 | + tg.start_soon(run_server) |
| 468 | + await client_session.initialize() |
| 469 | + await initialized_received.wait() |
| 470 | + tg.cancel_scope.cancel() |
| 471 | + |
| 472 | + assert initialized_received.is_set() |
| 473 | + assert isinstance(received_session, ServerSession) |
| 474 | + |
| 475 | + |
| 476 | +@pytest.mark.anyio |
| 477 | +async def test_roots_list_changed_notification_with_session(): |
| 478 | + """Test that the server receives and handles RootsListChangedNotification with a session.""" |
| 479 | + server_to_client_send, server_to_client_receive = anyio.create_memory_object_stream[SessionMessage](1) |
| 480 | + client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[SessionMessage | Exception](1) |
| 481 | + |
| 482 | + server = Server("test") |
| 483 | + roots_list_changed_received = asyncio.Event() |
| 484 | + received_session = None |
| 485 | + |
| 486 | + @server.roots_list_changed_notification() |
| 487 | + async def handle_roots_list_changed(notification: types.RootsListChangedNotification, session: ServerSession): |
| 488 | + nonlocal received_session |
| 489 | + received_session = session |
| 490 | + roots_list_changed_received.set() |
| 491 | + |
| 492 | + async def run_server(): |
| 493 | + await server.run( |
| 494 | + client_to_server_receive, |
| 495 | + server_to_client_send, |
| 496 | + server.create_initialization_options(), |
| 497 | + ) |
| 498 | + |
| 499 | + async def message_handler( |
| 500 | + message: (RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception), |
| 501 | + ) -> None: |
| 502 | + if isinstance(message, Exception): |
| 503 | + raise message |
| 504 | + |
| 505 | + async with ( |
| 506 | + ClientSession( |
| 507 | + server_to_client_receive, |
| 508 | + client_to_server_send, |
| 509 | + message_handler=message_handler, |
| 510 | + ) as client_session, |
| 511 | + anyio.create_task_group() as tg, |
| 512 | + ): |
| 513 | + tg.start_soon(run_server) |
| 514 | + await client_session.initialize() |
| 515 | + await client_session.send_notification( |
| 516 | + types.ClientNotification( |
| 517 | + root=types.RootsListChangedNotification(method="notifications/roots/list_changed", params=None) |
| 518 | + ) |
| 519 | + ) |
| 520 | + await roots_list_changed_received.wait() |
| 521 | + tg.cancel_scope.cancel() |
| 522 | + |
| 523 | + assert roots_list_changed_received.is_set() |
| 524 | + assert isinstance(received_session, ServerSession) |
0 commit comments