|
| 1 | +"""Tests for tools/resources/prompts list_changed notification callbacks.""" |
| 2 | + |
| 3 | +import anyio |
| 4 | +import pytest |
| 5 | + |
| 6 | +from mcp import Client, types |
| 7 | +from mcp.server.mcpserver import Context, MCPServer |
| 8 | +from mcp.shared.session import RequestResponder |
| 9 | +from mcp.types import TextContent |
| 10 | + |
| 11 | +pytestmark = pytest.mark.anyio |
| 12 | + |
| 13 | + |
| 14 | +async def test_tools_list_changed_callback(): |
| 15 | + """Verify that the client invokes the tools_list_changed_callback when |
| 16 | + the server sends a notifications/tools/list_changed notification.""" |
| 17 | + server = MCPServer("test") |
| 18 | + received = anyio.Event() |
| 19 | + |
| 20 | + async def on_tools_list_changed() -> None: |
| 21 | + received.set() |
| 22 | + |
| 23 | + @server.tool("trigger_tool_change") |
| 24 | + async def trigger_tool_change(ctx: Context) -> str: |
| 25 | + await ctx.session.send_tool_list_changed() |
| 26 | + return "triggered" |
| 27 | + |
| 28 | + async def message_handler( |
| 29 | + message: RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception, |
| 30 | + ) -> None: |
| 31 | + if isinstance(message, Exception): # pragma: no cover |
| 32 | + raise message |
| 33 | + |
| 34 | + async with Client( |
| 35 | + server, |
| 36 | + tools_list_changed_callback=on_tools_list_changed, |
| 37 | + message_handler=message_handler, |
| 38 | + ) as client: |
| 39 | + result = await client.call_tool("trigger_tool_change", {}) |
| 40 | + assert result.is_error is False |
| 41 | + assert isinstance(result.content[0], TextContent) |
| 42 | + assert result.content[0].text == "triggered" |
| 43 | + |
| 44 | + with anyio.fail_after(5): |
| 45 | + await received.wait() |
| 46 | + |
| 47 | + |
| 48 | +async def test_resources_list_changed_callback(): |
| 49 | + """Verify that the client invokes the resources_list_changed_callback when |
| 50 | + the server sends a notifications/resources/list_changed notification.""" |
| 51 | + server = MCPServer("test") |
| 52 | + received = anyio.Event() |
| 53 | + |
| 54 | + async def on_resources_list_changed() -> None: |
| 55 | + received.set() |
| 56 | + |
| 57 | + @server.tool("trigger_resource_change") |
| 58 | + async def trigger_resource_change(ctx: Context) -> str: |
| 59 | + # Notify clients that the resource list has changed |
| 60 | + await ctx.session.send_resource_list_changed() |
| 61 | + return "triggered" |
| 62 | + |
| 63 | + async def message_handler( |
| 64 | + message: RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception, |
| 65 | + ) -> None: |
| 66 | + if isinstance(message, Exception): # pragma: no cover |
| 67 | + raise message |
| 68 | + |
| 69 | + async with Client( |
| 70 | + server, |
| 71 | + resources_list_changed_callback=on_resources_list_changed, |
| 72 | + message_handler=message_handler, |
| 73 | + ) as client: |
| 74 | + result = await client.call_tool("trigger_resource_change", {}) |
| 75 | + assert result.is_error is False |
| 76 | + |
| 77 | + with anyio.fail_after(5): |
| 78 | + await received.wait() |
| 79 | + |
| 80 | + |
| 81 | +async def test_prompts_list_changed_callback(): |
| 82 | + """Verify that the client invokes the prompts_list_changed_callback when |
| 83 | + the server sends a notifications/prompts/list_changed notification.""" |
| 84 | + server = MCPServer("test") |
| 85 | + received = anyio.Event() |
| 86 | + |
| 87 | + async def on_prompts_list_changed() -> None: |
| 88 | + received.set() |
| 89 | + |
| 90 | + @server.tool("trigger_prompt_change") |
| 91 | + async def trigger_prompt_change(ctx: Context) -> str: |
| 92 | + await ctx.session.send_prompt_list_changed() |
| 93 | + return "triggered" |
| 94 | + |
| 95 | + async def message_handler( |
| 96 | + message: RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception, |
| 97 | + ) -> None: |
| 98 | + if isinstance(message, Exception): # pragma: no cover |
| 99 | + raise message |
| 100 | + |
| 101 | + async with Client( |
| 102 | + server, |
| 103 | + prompts_list_changed_callback=on_prompts_list_changed, |
| 104 | + message_handler=message_handler, |
| 105 | + ) as client: |
| 106 | + result = await client.call_tool("trigger_prompt_change", {}) |
| 107 | + assert result.is_error is False |
| 108 | + |
| 109 | + with anyio.fail_after(5): |
| 110 | + await received.wait() |
| 111 | + |
| 112 | + |
| 113 | +async def test_list_changed_callbacks_not_called_without_notification(): |
| 114 | + """Verify that list_changed callbacks are NOT invoked when |
| 115 | + no list_changed notification is sent.""" |
| 116 | + server = MCPServer("test") |
| 117 | + called = False |
| 118 | + |
| 119 | + async def should_not_be_called() -> None: |
| 120 | + nonlocal called |
| 121 | + called = True # pragma: no cover |
| 122 | + |
| 123 | + @server.tool("normal_tool") |
| 124 | + async def normal_tool() -> str: |
| 125 | + return "ok" |
| 126 | + |
| 127 | + async with Client( |
| 128 | + server, |
| 129 | + tools_list_changed_callback=should_not_be_called, |
| 130 | + resources_list_changed_callback=should_not_be_called, |
| 131 | + prompts_list_changed_callback=should_not_be_called, |
| 132 | + ) as client: |
| 133 | + result = await client.call_tool("normal_tool", {}) |
| 134 | + assert result.is_error is False |
| 135 | + |
| 136 | + assert not called |
0 commit comments