fastmcp icon indicating copy to clipboard operation
fastmcp copied to clipboard

After a while it constantly gives an error: "RuntimeError: Received request before initialization was complete"

Open itinance opened this issue 6 months ago • 3 comments
trafficstars

Description

After some sleeping ours, our mcp-server on the production node falls into a status where every new request causes such error: RuntimeError: Received request before initialization was complete

Here is the log:

news-api-1  |   |   File "/usr/local/lib/python3.11/site-packages/mcp/server/fastmcp/server.py", line 741, in sse_endpoint
news-api-1  |   |     return await handle_sse(request.scope, request.receive, request._send)  # type: ignore[reportPrivateUsage]
news-api-1  |   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
news-api-1  |   |   File "/usr/local/lib/python3.11/site-packages/mcp/server/fastmcp/server.py", line 674, in handle_sse
news-api-1  |   |     async with sse.connect_sse(
news-api-1  |   |   File "/usr/local/lib/python3.11/contextlib.py", line 231, in __aexit__
news-api-1  |   |     await self.gen.athrow(typ, value, traceback)
news-api-1  |   |   File "/usr/local/lib/python3.11/site-packages/mcp/server/sse.py", line 128, in connect_sse
news-api-1  |   |     async with anyio.create_task_group() as tg:
news-api-1  |   |   File "/usr/local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
news-api-1  |   |     raise BaseExceptionGroup(
news-api-1  |   | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
news-api-1  |   +-+---------------- 1 ----------------
news-api-1  |     | Exception Group Traceback (most recent call last):
news-api-1  |     |   File "/usr/local/lib/python3.11/site-packages/mcp/server/sse.py", line 147, in connect_sse
news-api-1  |     |     yield (read_stream, write_stream)
news-api-1  |     |   File "/usr/local/lib/python3.11/site-packages/mcp/server/fastmcp/server.py", line 679, in handle_sse
news-api-1  |     |     await self._mcp_server.run(
news-api-1  |     |   File "/usr/local/lib/python3.11/site-packages/mcp/server/lowlevel/server.py", line 489, in run
news-api-1  |     |     async with AsyncExitStack() as stack:
news-api-1  |     |   File "/usr/local/lib/python3.11/contextlib.py", line 745, in __aexit__
news-api-1  |     |     raise exc_details[1]
news-api-1  |     |   File "/usr/local/lib/python3.11/contextlib.py", line 728, in __aexit__
news-api-1  |     |     cb_suppress = await cb(*exc_details)
news-api-1  |     |                   ^^^^^^^^^^^^^^^^^^^^^^
news-api-1  |     |   File "/usr/local/lib/python3.11/site-packages/mcp/shared/session.py", line 209, in __aexit__
news-api-1  |     |     return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
news-api-1  |     |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
news-api-1  |     |   File "/usr/local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
news-api-1  |     |     raise BaseExceptionGroup(
news-api-1  |     | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
news-api-1  |     +-+---------------- 1 ----------------
news-api-1  |       | Traceback (most recent call last):
news-api-1  |       |   File "/usr/local/lib/python3.11/site-packages/mcp/server/session.py", line 146, in _receive_loop
news-api-1  |       |     await super()._receive_loop()
news-api-1  |       |   File "/usr/local/lib/python3.11/site-packages/mcp/shared/session.py", line 349, in _receive_loop
news-api-1  |       |     await self._received_request(responder)
news-api-1  |       |   File "/usr/local/lib/python3.11/site-packages/mcp/server/session.py", line 171, in _received_request
news-api-1  |       |     raise RuntimeError(
news-api-1  |       | RuntimeError: Received request before initialization was complete

At that time, the CursorAI settings page gives also an unknown error on our SSE-URL. When I click on the refresh-button, it seems to send a command that make the MCP-server somehow awaking.

After that, any request from Cursor to the MCP server just works. Hours later without any traffic , it again prints the error obove.

I can't explain more as this is what I see in the logs. I don't know what CursorAI is doing to refresh the service configuration. But I want to understand, what is happening there on the protocol level.

Would it make sense, to create a cronjob on the server, that tries to keep the MCP server alive every 5 minutes or so?

Has someone experienced the same issue and can tell more about it?

We use fastmcp 2.3.2 (latest version AFAIK) inside FastAPI, running in a docker-container, orchestrated via docker-compose, on Ubuntu 22.

mcp = FastMCP("news-api")

@mcp.tool()
# .... functions

# setup:
app = fastapi.FastAPI(
    title="MCP API Server",
    description="API-Server with FastMCP-Integration",
    lifespan=fastapi_lifespan,
    redirect_slashes=True,
    routes=[
        routing.APIRoute('/', endpoints.index.index),
        routing.Mount('/api', endpoints.api_router),
#        routing.APIRoute('/status', endpoints.status.get_status),
        routing.Mount(
            '/static',
            staticfiles.StaticFiles(directory=config.BASE_PATH / 'static'),
            name='static',
        ),
        routing.Mount('/', app=mcp.sse_app()),
    ],
    version=version,
)

# Hauptausführung
if __name__ == "__main__":
    uvicorn.run(
        app,
        host='0.0.0.0',
        port=8000,
        proxy_headers=True,
        date_header=True,
        server_header=False,
        ws='none',
    )

Example Code


Version Information

2.3.2

Additional Context

No response

itinance avatar May 15 '25 08:05 itinance