Skip to content

Commit 5f422e7

Browse files
committed
Fix async test assertion
1 parent 428e7a4 commit 5f422e7

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,16 @@ async def process_text(text: str, ctx: Context[ServerSession, None]) -> str:
535535
return f"Processed: {text.upper()}"
536536

537537

538+
@mcp.tool()
539+
async def process_text_sync(text: str, ctx: Context[ServerSession, None]) -> str:
540+
"""Process text in sync mode only."""
541+
542+
await ctx.info(f"Processing text: {text[:20]}...")
543+
await anyio.sleep(0.3)
544+
545+
return f"Processed: {text.upper()}"
546+
547+
538548
if __name__ == "__main__":
539549
mcp.run()
540550
```

examples/snippets/servers/async_tool_basic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,15 @@ async def process_text(text: str, ctx: Context[ServerSession, None]) -> str:
3838
return f"Processed: {text.upper()}"
3939

4040

41+
@mcp.tool()
42+
async def process_text_sync(text: str, ctx: Context[ServerSession, None]) -> str:
43+
"""Process text in sync mode only."""
44+
45+
await ctx.info(f"Processing text: {text[:20]}...")
46+
await anyio.sleep(0.3)
47+
48+
return f"Processed: {text.upper()}"
49+
50+
4151
if __name__ == "__main__":
4252
mcp.run()

tests/server/fastmcp/test_integration.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ async def test_async_tool_basic(server_transport: str, server_url: str) -> None:
714714
assert result.serverInfo.name == "Async Tool Basic"
715715

716716
# Test sync tool (should work normally)
717-
sync_result = await session.call_tool("process_text", {"text": "hello"})
717+
sync_result = await session.call_tool("process_text_sync", {"text": "hello"})
718718
assert len(sync_result.content) == 1
719719
assert isinstance(sync_result.content[0], TextContent)
720720
assert sync_result.content[0].text == "Processed: HELLO"
@@ -745,11 +745,30 @@ async def test_async_tool_basic(server_transport: str, server_url: str) -> None:
745745
else:
746746
pytest.fail("Async operation timed out")
747747

748-
# Test hybrid tool (process_text can work in sync or async mode)
748+
# Test hybrid tool (process_text should only run in async mode in this version)
749749
hybrid_result = await session.call_tool("process_text", {"text": "world"})
750-
assert len(hybrid_result.content) == 1
751-
assert isinstance(hybrid_result.content[0], TextContent)
752-
assert "Processed: WORLD" in hybrid_result.content[0].text
750+
assert hybrid_result.operation is not None
751+
token = hybrid_result.operation.token
752+
753+
# Poll for completion with timeout
754+
max_attempts = 20
755+
attempt = 0
756+
while attempt < max_attempts:
757+
status = await session.get_operation_status(token)
758+
if status.status == "completed":
759+
final_hybrid_result = await session.get_operation_result(token)
760+
assert not final_hybrid_result.result.isError
761+
assert len(final_hybrid_result.result.content) == 1
762+
assert isinstance(final_hybrid_result.result.content[0], TextContent)
763+
assert "Processed: WORLD" in final_hybrid_result.result.content[0].text
764+
break
765+
elif status.status == "failed":
766+
pytest.fail(f"Async operation failed: {status.error}")
767+
768+
attempt += 1
769+
await anyio.sleep(0.5)
770+
else:
771+
pytest.fail("Async operation timed out")
753772

754773

755774
# Test async tools example with legacy protocol

0 commit comments

Comments
 (0)