Why our MCP server felt slow until we stopped spawning a fresh subprocess on every tool call
Last week a few folks on our team complained that our internal MCP server felt sluggish compared to plain CLI scripts. A simple lookup that should take 15ms was taking almost 2 seconds.
We timed the database query first—12ms. Then we timed the MCP handler itself—4ms. The missing 1.8 seconds was happening before our handler even ran.
Our client wrapper was spinning up a fresh stdio Python subprocess per task, and at the top of server.py we had eager imports for pydantic, sqlalchemy, httpx, and an embedding tokenizer we only used in one rare tool. Every cold start was paying a full import tax.
Two fixes that dropped tool latency to ~25ms
- Keep one long-lived session or Streamable HTTP daemon instead of spawning a new
stdiochild process for each agent turn. - Lazy-import heavy modules inside the specific tool function that actually needs them, so startup stays under 80ms even when a client does cold-start.
python# Before: 1.6s import tax even if the agent only calls ping() or get_status() # import sentence_transformers @mcp.tool() def semantic_lookup(query: str) -> list[str]: # Loaded only once, and only if this specific tool is actually invoked from sentence_transformers import SentenceTransformer model = get_cached_model(SentenceTransformer) return search_index(model.encode(query))
Nothing fancy here, just standard Python startup hygiene—but it made our coding agent feel noticeably snappier overnight.