|
| 1 | +"""Tests for enabling server notifications.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import AsyncIterator |
| 6 | +from contextlib import asynccontextmanager |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +import pytest |
| 10 | +from starlette.applications import Starlette |
| 11 | + |
| 12 | +from mcp.server.fastmcp import FastMCP |
| 13 | +from mcp.server.lowlevel.server import NotificationOptions |
| 14 | +from mcp.server.models import InitializationOptions |
| 15 | +from mcp.server.streamable_http_manager import StreamableHTTPSessionManager |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.anyio |
| 19 | +async def test_fastmcp_sets_notification_options_affects_initialization(): |
| 20 | + """Test that set_notification_options() correctly affects server initialization.""" |
| 21 | + server = FastMCP("notification-test") |
| 22 | + |
| 23 | + # By default there should be no configuration |
| 24 | + assert server._notification_options is None |
| 25 | + |
| 26 | + # Configure notifications |
| 27 | + server.set_notification_options( |
| 28 | + prompts_changed=True, |
| 29 | + resources_changed=True, |
| 30 | + tools_changed=False, |
| 31 | + ) |
| 32 | + |
| 33 | + # Verify internal NotificationOptions created correctly |
| 34 | + assert isinstance(server._notification_options, NotificationOptions) |
| 35 | + assert server._notification_options.prompts_changed is True |
| 36 | + assert server._notification_options.resources_changed is True |
| 37 | + assert server._notification_options.tools_changed is False |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.anyio |
| 41 | +async def test_streamable_http_session_manager_uses_notification_options() -> None: |
| 42 | + # Create the FastMCP server and configure notifications |
| 43 | + server = FastMCP("notification-test") |
| 44 | + server.set_notification_options( |
| 45 | + prompts_changed=True, |
| 46 | + resources_changed=False, |
| 47 | + tools_changed=True, |
| 48 | + ) |
| 49 | + |
| 50 | + # Force creation of the StreamableHTTP session manager without starting uvicorn |
| 51 | + app = server.streamable_http_app() |
| 52 | + |
| 53 | + assert isinstance(app, Starlette) |
| 54 | + |
| 55 | + # Get the StreamableHTTPSessionManager |
| 56 | + assert server._session_manager is not None |
| 57 | + session_manager: StreamableHTTPSessionManager = server._session_manager |
| 58 | + |
| 59 | + # Verify internal NotificationOptions created correctly |
| 60 | + assert isinstance(session_manager._notification_options, NotificationOptions) |
| 61 | + assert session_manager._notification_options.prompts_changed is True |
| 62 | + assert session_manager._notification_options.resources_changed is False |
| 63 | + assert session_manager._notification_options.tools_changed is True |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.anyio |
| 67 | +async def test_run_stdio_uses_configured_notification_options(monkeypatch: pytest.MonkeyPatch) -> None: |
| 68 | + """Verify FastMCP passes NotificationOptions to the low-level server.run call.""" |
| 69 | + called_with: dict[str, InitializationOptions] = {} |
| 70 | + |
| 71 | + async def fake_run( |
| 72 | + read_stream: Any, |
| 73 | + write_stream: Any, |
| 74 | + initialization_options: InitializationOptions, |
| 75 | + **kwargs: Any, |
| 76 | + ) -> None: |
| 77 | + """Fake run method capturing the initialization options.""" |
| 78 | + called_with["init_opts"] = initialization_options |
| 79 | + |
| 80 | + # Create the FastMCP server instance |
| 81 | + server = FastMCP("test-server") |
| 82 | + |
| 83 | + # Patch the low-level server.run method to our fake |
| 84 | + monkeypatch.setattr(server._mcp_server, "run", fake_run) |
| 85 | + |
| 86 | + # Patch stdio_server to avoid touching real stdin/stdout |
| 87 | + @asynccontextmanager |
| 88 | + async def fake_stdio_server() -> AsyncIterator[tuple[str, str]]: |
| 89 | + yield ("fake_read", "fake_write") |
| 90 | + |
| 91 | + monkeypatch.setattr("mcp.server.fastmcp.server.stdio_server", fake_stdio_server) |
| 92 | + |
| 93 | + # Configure notification options |
| 94 | + server.set_notification_options( |
| 95 | + prompts_changed=True, |
| 96 | + resources_changed=True, |
| 97 | + tools_changed=False, |
| 98 | + ) |
| 99 | + |
| 100 | + # Execute run_stdio_async (uses patched run + stdio_server) |
| 101 | + await server.run_stdio_async() |
| 102 | + |
| 103 | + # Verify our fake_run was actually called |
| 104 | + assert "init_opts" in called_with, "Expected _mcp_server.run to be called with InitializationOptions" |
| 105 | + |
| 106 | + init_opts: InitializationOptions = called_with["init_opts"] |
| 107 | + assert isinstance(init_opts, InitializationOptions) |
| 108 | + |
| 109 | + # Verify the NotificationOptions are reflected correctly in capabilities |
| 110 | + caps = init_opts.capabilities |
| 111 | + assert caps.prompts is not None and caps.prompts.listChanged is True |
| 112 | + assert caps.resources is not None and caps.resources.listChanged is True |
| 113 | + assert caps.tools is not None and caps.tools.listChanged is False |
0 commit comments