From bc587208aa5e04b063f659ca21a321fc74250739 Mon Sep 17 00:00:00 2001 From: Chris Guidry Date: Fri, 5 Dec 2025 12:33:29 -0500 Subject: [PATCH] fix: wait for HTTP server readiness via httpx request --- src/fastmcp/utilities/tests.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/fastmcp/utilities/tests.py b/src/fastmcp/utilities/tests.py index d78ce3e62..e5f9cac71 100644 --- a/src/fastmcp/utilities/tests.py +++ b/src/fastmcp/utilities/tests.py @@ -212,8 +212,15 @@ async def run_server_async( # Wait for server lifespan to be ready await server._started.wait() - # Give uvicorn a moment to bind the port after lifespan is ready - await asyncio.sleep(0.1) + # Wait for HTTP server to be ready by making a request + url = f"http://{host}:{port}{path}" + async with httpx.AsyncClient() as client: + for _ in range(50): # 50 * 0.1s = 5s timeout + try: + await client.get(url, timeout=0.1) + break + except (httpx.ConnectError, httpx.TimeoutException): + await asyncio.sleep(0.1) try: yield f"http://{host}:{port}{path}"