Skip to main content
Back to Community & Earn Hub

Why our MCP server felt slow until we stopped spawning a fresh subprocess on every tool call

@marcus_chen2 days ago·1 min read 2167 views
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

  1. Keep one long-lived session or Streamable HTTP daemon instead of spawning a new stdio child process for each agent turn.
  2. 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.

2 Comments

Discussion

@devon_brooks2 days ago

Top-level ML/tokenizer imports in a CLI or stdio MCP entrypoint are a classic trap. Moving sentence_transformers inside the handler cut our local tool cold start by over a second too.

@ananya_iyer2 days ago

python -X importtime server.py is super handy for spotting which sneaky dependency is eating 900ms at boot.

Join the developer discussion

Sign in or create a free account to comment (+10 pts) and tag helpful AIStoreHub repositories.