|
| 1 | +"""Tests for tools/resources/prompts list_changed notification callbacks.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mcp import Client, types |
| 6 | +from mcp.server.mcpserver import MCPServer |
| 7 | +from mcp.shared.session import RequestResponder |
| 8 | + |
| 9 | + |
| 10 | +class ListChangedCollector: |
| 11 | + """Collects list_changed notification invocations.""" |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self.tool_changed_count = 0 |
| 15 | + self.resource_changed_count = 0 |
| 16 | + self.prompt_changed_count = 0 |
| 17 | + |
| 18 | + async def on_tool_list_changed(self) -> None: |
| 19 | + self.tool_changed_count += 1 |
| 20 | + |
| 21 | + async def on_resource_list_changed(self) -> None: |
| 22 | + self.resource_changed_count += 1 |
| 23 | + |
| 24 | + async def on_prompt_list_changed(self) -> None: |
| 25 | + self.prompt_changed_count += 1 |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.anyio |
| 29 | +async def test_tool_list_changed_callback(): |
| 30 | + """Client receives tools/list_changed notification and invokes callback.""" |
| 31 | + server = MCPServer("test") |
| 32 | + collector = ListChangedCollector() |
| 33 | + |
| 34 | + @server.tool("trigger_tool_change") |
| 35 | + async def trigger_tool_change() -> str: |
| 36 | + ctx = server.get_context() |
| 37 | + await ctx.session.send_notification(types.ToolListChangedNotification()) |
| 38 | + return "ok" |
| 39 | + |
| 40 | + async def message_handler( |
| 41 | + message: RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception, |
| 42 | + ) -> None: |
| 43 | + if isinstance(message, Exception): |
| 44 | + raise message |
| 45 | + |
| 46 | + async with Client( |
| 47 | + server, |
| 48 | + tool_list_changed_callback=collector.on_tool_list_changed, |
| 49 | + message_handler=message_handler, |
| 50 | + ) as client: |
| 51 | + result = await client.call_tool("trigger_tool_change", {}) |
| 52 | + assert result.is_error is False |
| 53 | + assert collector.tool_changed_count == 1 |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.anyio |
| 57 | +async def test_resource_list_changed_callback(): |
| 58 | + """Client receives resources/list_changed notification and invokes callback.""" |
| 59 | + server = MCPServer("test") |
| 60 | + collector = ListChangedCollector() |
| 61 | + |
| 62 | + @server.tool("trigger_resource_change") |
| 63 | + async def trigger_resource_change() -> str: |
| 64 | + ctx = server.get_context() |
| 65 | + await ctx.session.send_notification(types.ResourceListChangedNotification()) |
| 66 | + return "ok" |
| 67 | + |
| 68 | + async def message_handler( |
| 69 | + message: RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception, |
| 70 | + ) -> None: |
| 71 | + if isinstance(message, Exception): |
| 72 | + raise message |
| 73 | + |
| 74 | + async with Client( |
| 75 | + server, |
| 76 | + resource_list_changed_callback=collector.on_resource_list_changed, |
| 77 | + message_handler=message_handler, |
| 78 | + ) as client: |
| 79 | + result = await client.call_tool("trigger_resource_change", {}) |
| 80 | + assert result.is_error is False |
| 81 | + assert collector.resource_changed_count == 1 |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.anyio |
| 85 | +async def test_prompt_list_changed_callback(): |
| 86 | + """Client receives prompts/list_changed notification and invokes callback.""" |
| 87 | + server = MCPServer("test") |
| 88 | + collector = ListChangedCollector() |
| 89 | + |
| 90 | + @server.tool("trigger_prompt_change") |
| 91 | + async def trigger_prompt_change() -> str: |
| 92 | + ctx = server.get_context() |
| 93 | + await ctx.session.send_notification(types.PromptListChangedNotification()) |
| 94 | + return "ok" |
| 95 | + |
| 96 | + async def message_handler( |
| 97 | + message: RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception, |
| 98 | + ) -> None: |
| 99 | + if isinstance(message, Exception): |
| 100 | + raise message |
| 101 | + |
| 102 | + async with Client( |
| 103 | + server, |
| 104 | + prompt_list_changed_callback=collector.on_prompt_list_changed, |
| 105 | + message_handler=message_handler, |
| 106 | + ) as client: |
| 107 | + result = await client.call_tool("trigger_prompt_change", {}) |
| 108 | + assert result.is_error is False |
| 109 | + assert collector.prompt_changed_count == 1 |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.anyio |
| 113 | +async def test_list_changed_without_callback_does_not_crash(): |
| 114 | + """list_changed notifications are silently ignored when no callback is set.""" |
| 115 | + server = MCPServer("test") |
| 116 | + |
| 117 | + @server.tool("trigger_all_changes") |
| 118 | + async def trigger_all_changes() -> str: |
| 119 | + ctx = server.get_context() |
| 120 | + await ctx.session.send_notification(types.ToolListChangedNotification()) |
| 121 | + await ctx.session.send_notification(types.ResourceListChangedNotification()) |
| 122 | + await ctx.session.send_notification(types.PromptListChangedNotification()) |
| 123 | + return "ok" |
| 124 | + |
| 125 | + async def message_handler( |
| 126 | + message: RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception, |
| 127 | + ) -> None: |
| 128 | + if isinstance(message, Exception): |
| 129 | + raise message |
| 130 | + |
| 131 | + async with Client( |
| 132 | + server, |
| 133 | + message_handler=message_handler, |
| 134 | + ) as client: |
| 135 | + result = await client.call_tool("trigger_all_changes", {}) |
| 136 | + assert result.is_error is False |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.anyio |
| 140 | +async def test_multiple_list_changed_notifications(): |
| 141 | + """Multiple list_changed notifications each invoke the callback.""" |
| 142 | + server = MCPServer("test") |
| 143 | + collector = ListChangedCollector() |
| 144 | + |
| 145 | + @server.tool("trigger_double") |
| 146 | + async def trigger_double() -> str: |
| 147 | + ctx = server.get_context() |
| 148 | + await ctx.session.send_notification(types.ToolListChangedNotification()) |
| 149 | + await ctx.session.send_notification(types.ToolListChangedNotification()) |
| 150 | + return "ok" |
| 151 | + |
| 152 | + async def message_handler( |
| 153 | + message: RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception, |
| 154 | + ) -> None: |
| 155 | + if isinstance(message, Exception): |
| 156 | + raise message |
| 157 | + |
| 158 | + async with Client( |
| 159 | + server, |
| 160 | + tool_list_changed_callback=collector.on_tool_list_changed, |
| 161 | + message_handler=message_handler, |
| 162 | + ) as client: |
| 163 | + result = await client.call_tool("trigger_double", {}) |
| 164 | + assert result.is_error is False |
| 165 | + assert collector.tool_changed_count == 2 |
0 commit comments