|
| 1 | +import asyncio |
1 | 2 | from unittest.mock import AsyncMock, call, patch |
2 | 3 |
|
3 | 4 | import pytest |
4 | | -from mcp.types import PaginatedRequestParams |
| 5 | +from mcp.types import CallToolResult, PaginatedRequestParams, TextContent |
5 | 6 |
|
6 | 7 | from agents import Agent |
| 8 | +from agents.exceptions import UserError |
7 | 9 | from agents.mcp import MCPServerStdio |
8 | 10 | from agents.run_context import RunContextWrapper |
9 | 11 |
|
@@ -64,6 +66,228 @@ async def test_server_caching_works( |
64 | 66 | assert result_tools == tools |
65 | 67 |
|
66 | 68 |
|
| 69 | +@pytest.mark.asyncio |
| 70 | +@patch("mcp.client.stdio.stdio_client", return_value=DummyStreamsContextManager()) |
| 71 | +@patch("mcp.client.session.ClientSession.initialize", new_callable=AsyncMock, return_value=None) |
| 72 | +@patch("mcp.client.session.ClientSession.call_tool", new_callable=AsyncMock) |
| 73 | +@patch("mcp.client.session.ClientSession.list_tools") |
| 74 | +async def test_cache_invalidation_during_refresh_is_preserved( |
| 75 | + mock_list_tools: AsyncMock, |
| 76 | + mock_call_tool: AsyncMock, |
| 77 | + mock_initialize: AsyncMock, |
| 78 | + mock_stdio_client, |
| 79 | +): |
| 80 | + refresh_started = asyncio.Event() |
| 81 | + release_refresh = asyncio.Event() |
| 82 | + request_count = 0 |
| 83 | + responses = [ |
| 84 | + ListToolsResult( |
| 85 | + tools=[ |
| 86 | + MCPTool( |
| 87 | + name="tool1", |
| 88 | + description="initial", |
| 89 | + inputSchema={"required": ["q"]}, |
| 90 | + ), |
| 91 | + ], |
| 92 | + ), |
| 93 | + ListToolsResult( |
| 94 | + tools=[ |
| 95 | + MCPTool( |
| 96 | + name="tool1", |
| 97 | + description="before-second-invalidation", |
| 98 | + inputSchema={}, |
| 99 | + ), |
| 100 | + ], |
| 101 | + ), |
| 102 | + ListToolsResult( |
| 103 | + tools=[ |
| 104 | + MCPTool( |
| 105 | + name="tool1", |
| 106 | + description="after-second-invalidation", |
| 107 | + inputSchema={"required": ["latest"]}, |
| 108 | + ), |
| 109 | + ], |
| 110 | + ), |
| 111 | + ] |
| 112 | + |
| 113 | + async def list_tools(): |
| 114 | + nonlocal request_count |
| 115 | + request_count += 1 |
| 116 | + if request_count == 2: |
| 117 | + refresh_started.set() |
| 118 | + await release_refresh.wait() |
| 119 | + return responses[request_count - 1] |
| 120 | + |
| 121 | + mock_list_tools.side_effect = list_tools |
| 122 | + mock_call_tool.return_value = CallToolResult( |
| 123 | + content=[TextContent(type="text", text="ok")], |
| 124 | + ) |
| 125 | + server = MCPServerStdio( |
| 126 | + params={"command": tee}, |
| 127 | + cache_tools_list=True, |
| 128 | + ) |
| 129 | + |
| 130 | + async with server: |
| 131 | + initial = await server.list_tools() |
| 132 | + assert initial[0].description == "initial" |
| 133 | + |
| 134 | + server.invalidate_tools_cache() |
| 135 | + refresh_task = asyncio.create_task(server.list_tools()) |
| 136 | + try: |
| 137 | + await asyncio.wait_for(refresh_started.wait(), timeout=1) |
| 138 | + |
| 139 | + server.invalidate_tools_cache() |
| 140 | + release_refresh.set() |
| 141 | + refreshed = await asyncio.wait_for(refresh_task, timeout=1) |
| 142 | + finally: |
| 143 | + release_refresh.set() |
| 144 | + if not refresh_task.done(): |
| 145 | + refresh_task.cancel() |
| 146 | + await asyncio.gather(refresh_task, return_exceptions=True) |
| 147 | + |
| 148 | + assert refreshed[0].description == "before-second-invalidation" |
| 149 | + assert (server.cached_tools or [])[0].description == "initial" |
| 150 | + |
| 151 | + await server.call_tool("tool1", {}) |
| 152 | + assert mock_call_tool.call_count == 1 |
| 153 | + |
| 154 | + latest = await server.list_tools() |
| 155 | + assert latest[0].description == "after-second-invalidation" |
| 156 | + assert (server.cached_tools or [])[0].description == "after-second-invalidation" |
| 157 | + assert request_count == 3 |
| 158 | + |
| 159 | + with pytest.raises(UserError, match="missing required parameters: latest"): |
| 160 | + await server.call_tool("tool1", {}) |
| 161 | + assert mock_call_tool.call_count == 1 |
| 162 | + |
| 163 | + |
| 164 | +@pytest.mark.asyncio |
| 165 | +@patch("mcp.client.stdio.stdio_client", return_value=DummyStreamsContextManager()) |
| 166 | +@patch("mcp.client.session.ClientSession.initialize", new_callable=AsyncMock, return_value=None) |
| 167 | +@patch("mcp.client.session.ClientSession.list_tools") |
| 168 | +async def test_older_concurrent_refresh_does_not_overwrite_newer_cache( |
| 169 | + mock_list_tools: AsyncMock, |
| 170 | + mock_initialize: AsyncMock, |
| 171 | + mock_stdio_client, |
| 172 | +): |
| 173 | + first_refresh_started = asyncio.Event() |
| 174 | + release_first_refresh = asyncio.Event() |
| 175 | + request_count = 0 |
| 176 | + |
| 177 | + async def list_tools(): |
| 178 | + nonlocal request_count |
| 179 | + request_count += 1 |
| 180 | + if request_count == 1: |
| 181 | + first_refresh_started.set() |
| 182 | + await release_first_refresh.wait() |
| 183 | + return ListToolsResult( |
| 184 | + tools=[ |
| 185 | + MCPTool( |
| 186 | + name="tool1", |
| 187 | + description="first-started", |
| 188 | + inputSchema={"required": ["old"]}, |
| 189 | + ), |
| 190 | + ], |
| 191 | + ) |
| 192 | + return ListToolsResult( |
| 193 | + tools=[ |
| 194 | + MCPTool( |
| 195 | + name="tool1", |
| 196 | + description="second-started", |
| 197 | + inputSchema={"required": ["latest"]}, |
| 198 | + ), |
| 199 | + ], |
| 200 | + ) |
| 201 | + |
| 202 | + mock_list_tools.side_effect = list_tools |
| 203 | + server = MCPServerStdio( |
| 204 | + params={"command": tee}, |
| 205 | + cache_tools_list=True, |
| 206 | + ) |
| 207 | + |
| 208 | + async with server: |
| 209 | + first_refresh = asyncio.create_task(server.list_tools()) |
| 210 | + try: |
| 211 | + await asyncio.wait_for(first_refresh_started.wait(), timeout=1) |
| 212 | + second_result = await asyncio.wait_for(server.list_tools(), timeout=1) |
| 213 | + assert second_result[0].description == "second-started" |
| 214 | + assert (server.cached_tools or [])[0].description == "second-started" |
| 215 | + |
| 216 | + release_first_refresh.set() |
| 217 | + first_result = await asyncio.wait_for(first_refresh, timeout=1) |
| 218 | + finally: |
| 219 | + release_first_refresh.set() |
| 220 | + if not first_refresh.done(): |
| 221 | + first_refresh.cancel() |
| 222 | + await asyncio.gather(first_refresh, return_exceptions=True) |
| 223 | + |
| 224 | + assert first_result[0].description == "first-started" |
| 225 | + assert (server.cached_tools or [])[0].description == "second-started" |
| 226 | + assert (server.cached_tools or [])[0].input_schema == {"required": ["latest"]} |
| 227 | + assert request_count == 2 |
| 228 | + |
| 229 | + |
| 230 | +@pytest.mark.asyncio |
| 231 | +@patch("mcp.client.stdio.stdio_client", return_value=DummyStreamsContextManager()) |
| 232 | +@patch("mcp.client.session.ClientSession.initialize", new_callable=AsyncMock, return_value=None) |
| 233 | +@patch("mcp.client.session.ClientSession.list_tools") |
| 234 | +async def test_older_refresh_publishes_when_newer_refresh_fails( |
| 235 | + mock_list_tools: AsyncMock, |
| 236 | + mock_initialize: AsyncMock, |
| 237 | + mock_stdio_client, |
| 238 | +): |
| 239 | + first_refresh_started = asyncio.Event() |
| 240 | + release_first_refresh = asyncio.Event() |
| 241 | + request_count = 0 |
| 242 | + |
| 243 | + async def list_tools(): |
| 244 | + nonlocal request_count |
| 245 | + request_count += 1 |
| 246 | + if request_count == 1: |
| 247 | + first_refresh_started.set() |
| 248 | + await release_first_refresh.wait() |
| 249 | + return ListToolsResult( |
| 250 | + tools=[ |
| 251 | + MCPTool( |
| 252 | + name="tool1", |
| 253 | + description="first-started", |
| 254 | + inputSchema={"required": ["q"]}, |
| 255 | + ), |
| 256 | + ], |
| 257 | + ) |
| 258 | + raise RuntimeError("second refresh failed") |
| 259 | + |
| 260 | + mock_list_tools.side_effect = list_tools |
| 261 | + server = MCPServerStdio( |
| 262 | + params={"command": tee}, |
| 263 | + cache_tools_list=True, |
| 264 | + ) |
| 265 | + |
| 266 | + async with server: |
| 267 | + first_refresh = asyncio.create_task(server.list_tools()) |
| 268 | + try: |
| 269 | + await asyncio.wait_for(first_refresh_started.wait(), timeout=1) |
| 270 | + with pytest.raises(RuntimeError, match="second refresh failed"): |
| 271 | + await asyncio.wait_for(server.list_tools(), timeout=1) |
| 272 | + |
| 273 | + release_first_refresh.set() |
| 274 | + first_result = await asyncio.wait_for(first_refresh, timeout=1) |
| 275 | + finally: |
| 276 | + release_first_refresh.set() |
| 277 | + if not first_refresh.done(): |
| 278 | + first_refresh.cancel() |
| 279 | + await asyncio.gather(first_refresh, return_exceptions=True) |
| 280 | + |
| 281 | + assert first_result[0].description == "first-started" |
| 282 | + cached_tools = server.cached_tools |
| 283 | + assert cached_tools is not None |
| 284 | + assert cached_tools[0].description == "first-started" |
| 285 | + |
| 286 | + cached_result = await server.list_tools() |
| 287 | + assert cached_result[0].description == "first-started" |
| 288 | + assert request_count == 2 |
| 289 | + |
| 290 | + |
67 | 291 | @pytest.mark.asyncio |
68 | 292 | @patch("mcp.client.stdio.stdio_client", return_value=DummyStreamsContextManager()) |
69 | 293 | @patch("mcp.client.session.ClientSession.initialize", new_callable=AsyncMock, return_value=None) |
|
0 commit comments