Skip to content

Commit 3d67e72

Browse files
ihrprKludex
andauthored
Fix async resource functions not being awaited (#1092)
Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent 3fa7bc7 commit 3d67e72

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/mcp/server/fastmcp/resources/types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ class FunctionResource(Resource):
5454
async def read(self) -> str | bytes:
5555
"""Read the resource by calling the wrapped function."""
5656
try:
57-
result = await self.fn() if inspect.iscoroutinefunction(self.fn) else self.fn()
57+
# Call the function first to see if it returns a coroutine
58+
result = self.fn()
59+
# If it's a coroutine, await it
60+
if inspect.iscoroutine(result):
61+
result = await result
62+
5863
if isinstance(result, Resource):
5964
return await result.read()
6065
elif isinstance(result, bytes):

tests/issues/test_188_concurrency.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import anyio
22
import pytest
3+
from pydantic import AnyUrl
34

45
from mcp.server.fastmcp import FastMCP
56
from mcp.shared.memory import create_connected_server_and_client_session as create_session
67

78

89
@pytest.mark.anyio
9-
async def test_messages_are_executed_concurrently():
10+
async def test_messages_are_executed_concurrently_tools():
1011
server = FastMCP("test")
1112
event = anyio.Event()
1213
tool_started = anyio.Event()
@@ -44,3 +45,42 @@ async def trigger():
4445
"trigger_end",
4546
"tool_end",
4647
], 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

Comments
 (0)