`RuntimeError: Task group is not initialized` in the simplest possible MCP server with default config
The bug
RuntimeError: Task group is not initialized. Make sure to use run().
Response code: 500 (Internal Server Error); Time: 9ms (9 ms); Content length: 1677 bytes (1.68 kB)
Reproduces in each subsequent HTTP request (the first one works fine).
It's very counterintuitive if the default configuration doesn't work out of the box.
To Reproduce
from mcp.server.fastmcp import FastMCP
mcp = FastMCP(port=8010, debug=True, host='0.0.0.0')
@mcp.tool()
def ping(): return "pong"
mcp.run(transport="streamable-http")
###
POST http://localhost:8010/mcp/session
Content-Type: application/json
Accept: application/json, text/event-stream
{
"method": "initialize",
"jsonrpc": "2.0",
"id": 1
}
Stack trace
HTTP/1.1 500 Internal Server Error
date: Thu, 29 May 2025 14:19:00 GMT
server: uvicorn
content-length: 1677
content-type: text/plain; charset=utf-8
Traceback (most recent call last):
File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\middleware\errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
raise exc
File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\routing.py", line 758, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\routing.py", line 778, in app
await route.handle(scope, receive, send)
File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\routing.py", line 487, in handle
await self.app(scope, receive, send)
File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\mcp\server\fastmcp\server.py", line 786, in handle_streamable_http
await self.session_manager.handle_request(scope, receive, send)
File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\mcp\server\streamable_http_manager.py", line 137, in handle_request
raise RuntimeError("Task group is not initialized. Make sure to use run().")
RuntimeError: Task group is not initialized. Make sure to use run().
Related issues
https://github.com/modelcontextprotocol/python-sdk/issues/713
IMHO,
- The FastMCP class should be able to handle its intended functionality without additional setup, or it should fail early with a clear error message if additional configuration is needed.
- Incorrect client behavior should not produce Error 500 on the MCP server side.
The behavior where every following request dies after incorrect client behavior is what this PR addresses https://github.com/modelcontextprotocol/python-sdk/pull/841
The server-side session manager is being killed during unhandled exceptions, leaving the whole server unusable.
@sorby @ihrpr I confirm, for code attached in the description, PR #841 fixes the issue
#841 just merged, thank you @soby!