|
1 | 1 | import anyio
|
2 | 2 | import pytest
|
| 3 | +from pydantic import AnyUrl |
3 | 4 |
|
4 | 5 | from mcp.server.fastmcp import FastMCP
|
5 | 6 | from mcp.shared.memory import create_connected_server_and_client_session as create_session
|
6 | 7 |
|
7 | 8 |
|
8 | 9 | @pytest.mark.anyio
|
9 |
| -async def test_messages_are_executed_concurrently(): |
| 10 | +async def test_messages_are_executed_concurrently_tools(): |
10 | 11 | server = FastMCP("test")
|
11 | 12 | event = anyio.Event()
|
12 | 13 | tool_started = anyio.Event()
|
@@ -44,3 +45,42 @@ async def trigger():
|
44 | 45 | "trigger_end",
|
45 | 46 | "tool_end",
|
46 | 47 | ], f"Expected concurrent execution, but got: {call_order}"
|
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.anyio |
| 51 | +async def test_messages_are_executed_concurrently_tools_and_resources(): |
| 52 | + server = FastMCP("test") |
| 53 | + event = anyio.Event() |
| 54 | + tool_started = anyio.Event() |
| 55 | + call_order = [] |
| 56 | + |
| 57 | + @server.tool("sleep") |
| 58 | + async def sleep_tool(): |
| 59 | + call_order.append("waiting_for_event") |
| 60 | + tool_started.set() |
| 61 | + await event.wait() |
| 62 | + call_order.append("tool_end") |
| 63 | + return "done" |
| 64 | + |
| 65 | + @server.resource("slow://slow_resource") |
| 66 | + async def slow_resource(): |
| 67 | + # Wait for tool to start before setting the event |
| 68 | + await tool_started.wait() |
| 69 | + event.set() |
| 70 | + call_order.append("resource_end") |
| 71 | + return "slow" |
| 72 | + |
| 73 | + async with create_session(server._mcp_server) as client_session: |
| 74 | + # First tool will wait on event, second will set it |
| 75 | + async with anyio.create_task_group() as tg: |
| 76 | + # Start the tool first (it will wait on event) |
| 77 | + tg.start_soon(client_session.call_tool, "sleep") |
| 78 | + # Then the resource (it will set the event) |
| 79 | + tg.start_soon(client_session.read_resource, AnyUrl("slow://slow_resource")) |
| 80 | + |
| 81 | + # Verify that both ran concurrently |
| 82 | + assert call_order == [ |
| 83 | + "waiting_for_event", |
| 84 | + "resource_end", |
| 85 | + "tool_end", |
| 86 | + ], f"Expected concurrent execution, but got: {call_order}" |
0 commit comments