Asyncio Event Loop Starvation in FastMCP Python Servers: How to offload CPU-bound PDF/AST parsing without blocking concurrent SSE heartbeats?
We run a Python FastMCP server exposing document-ingestion and code-analysis tools over SSE/HTTP.
The Event Loop Freeze
Several MCP tools perform heavy parsing when invoked by an agent:
python@mcp.tool() async def extract_repo_ast(repo_path: str) -> dict: # Synchronous CPU-bound parsing of 400 Python/TS files (~4.5 seconds) return parse_directory_ast(repo_path)
Because parse_directory_ast() runs synchronously inside async def, it monopolizes the Python asyncio event loop for 4.5 seconds:
- All other concurrent agent tool calls stall.
- Outbound SSE
: keepaliveheartbeats stop firing, causing client proxies to drop active connections. - Health checks (
/healthz) time out, triggering Kubernetes liveness probe restarts!
I tried wrapping the call in asyncio.to_thread(parse_directory_ast, repo_path), which helps for C-extensions that release the GIL, but pure-Python pypdf parsing still spikes event-loop jitter due to GIL contention.
What is the cleanest pattern in FastMCP/AnyIO to isolate CPU-bound tools in a bounded ProcessPoolExecutor without leaking zombie worker processes on server reload?