I create a mcp server by FastMCP, I can ensure that the mcp server has already finished the initialization, due to the server has already process several tool request, but I also get following error:
INFO: 192.168.182.1:2520 - "GET /sse HTTP/1.1" 200 OK
INFO: 192.168.182.1:2691 - "POST /messages/?session_id=aa9fa14634144092aed00e8a04f84ca4 HTTP/1.1" 202 Accepted
INFO: 192.168.182.1:2760 - "POST /messages/?session_id=aa9fa14634144092aed00e8a04f84ca4 HTTP/1.1" 202 Accepted
ERROR: Exception in ASGI application
+ Exception Group Traceback (most recent call last):
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
| result = await app( # type: ignore[func-returns-value]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| self.scope, self.receive, self.send
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| )
| ^
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
| return await self.app(scope, receive, send)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/applications.py", line 112, in __call__
| await self.middleware_stack(scope, receive, send)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/middleware/errors.py", line 187, in __call__
| raise exc
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/middleware/errors.py", line 165, in __call__
| await self.app(scope, receive, _send)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
| await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
| raise exc
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
| await app(scope, receive, sender)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/routing.py", line 714, in __call__
| await self.middleware_stack(scope, receive, send)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/routing.py", line 734, in app
| await route.handle(scope, receive, send)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/routing.py", line 288, in handle
| await self.app(scope, receive, send)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/routing.py", line 76, in app
| await wrap_app_handling_exceptions(app, request)(scope, receive, send)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
| raise exc
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
| await app(scope, receive, sender)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/routing.py", line 73, in app
| response = await f(request)
| ^^^^^^^^^^^^^^^^
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/fastmcp/server.py", line 485, in handle_sse
| async with sse.connect_sse(
| ~~~~~~~~~~~~~~~^
| request.scope,
| ^^^^^^^^^^^^^^
| request.receive,
| ^^^^^^^^^^^^^^^^
| request._send, # type: ignore[reportPrivateUsage]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ) as streams:
| ^
| File "/home/lewis/.local/share/uv/python/cpython-3.13.2-linux-x86_64-gnu/lib/python3.13/contextlib.py", line 235, in __aexit__
| await self.gen.athrow(value)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/sse.py", line 123, in connect_sse
| async with anyio.create_task_group() as tg:
| ~~~~~~~~~~~~~~~~~~~~~~~^^
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
| raise BaseExceptionGroup(
| "unhandled errors in a TaskGroup", self._exceptions
| ) from None
| ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last):
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/sse.py", line 131, in connect_sse
| yield (read_stream, write_stream)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/fastmcp/server.py", line 490, in handle_sse
| await self._mcp_server.run(
| ...<3 lines>...
| )
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/lowlevel/server.py", line 483, in run
| async with AsyncExitStack() as stack:
| ~~~~~~~~~~~~~~^^
| File "/home/lewis/.local/share/uv/python/cpython-3.13.2-linux-x86_64-gnu/lib/python3.13/contextlib.py", line 768, in __aexit__
| raise exc
| File "/home/lewis/.local/share/uv/python/cpython-3.13.2-linux-x86_64-gnu/lib/python3.13/contextlib.py", line 751, in __aexit__
| cb_suppress = await cb(*exc_details)
| ^^^^^^^^^^^^^^^^^^^^^^
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/shared/session.py", line 210, in __aexit__
| return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
| raise BaseExceptionGroup(
| "unhandled errors in a TaskGroup", self._exceptions
| ) from None
| ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/shared/session.py", line 324, in _receive_loop
| await self._received_request(responder)
| File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/session.py", line 163, in _received_request
| raise RuntimeError(
| "Received request before initialization was complete"
| )
| RuntimeError: Received request before initialization was complete
+------------------------------------
I also add a parameter when create object of FastMCP
# Create the MCP server with initialization timeout
# This helps prevent "Received request before initialization was complete" errors
# by automatically closing connections that don't initialize properly
mcp = FastMCP(
config.server.name,
host=config.server.host,
port=config.server.port,
settings={"initialization_timeout": 10.0} # 10 second timeout
)
mcp server run code:
mcp.run(transport="sse")
BTW: my mcp client is CherryStdio
How to resolve this?