Skip to main content
Back to Community & Earn Hub
Q&A Questionmcp +150 pt Bounty

Debugging silent Postgres connection pool exhaustion when 15 parallel MCP tool calls fire at once

@hannah_clark2 days ago 1618 views

Had a fun production surprise yesterday. A user asked our assistant to compare metrics across 15 different customer workspaces, so the model emitted 15 parallel MCP tool calls in a single turn.

Each MCP call hit our FastAPI backend, which uses SQLAlchemy with pool_size=5, max_overflow=10. By the time the 16th call arrived (plus a couple of normal web requests), connections started timing out with QueuePool limit of size 5 overflow 10 reached.

Worse: the agent saw the 500 error, assumed it was a transient hiccup, and fired all 15 calls again.

Apart from just cranking up max_overflow until Postgres complains about max_connections, how are people throttling parallel tool fan-out on the MCP server or API side?

1 Answers

Community Answers

@sarah_jenkins Accepted Answer2 days ago

Two fixes solved this for us without blowing up max_connections on Postgres:

  1. Put an asyncio.Semaphore inside the MCP server tool runner so a single agent turn can execute at most 3–4 concurrent tool calls against your backend, queueing the rest cleanly in memory instead of slamming your DB pool all at once.
  2. Return a non-retryable hint on 429/503 (or use PgBouncer in transaction mode in front of Postgres) so the LLM doesn't immediately re-fire all 15 calls in a panic loop.
python
import asyncio

# Cap concurrent DB-backed MCP tool executions per worker
TOOL_SEMAPHORE = asyncio.Semaphore(4)

async def run_workspace_query(workspace_id: str):
    async with TOOL_SEMAPHORE:
        return await fetch_metrics_from_db(workspace_id)

Once we added a Semaphore(4) on the MCP side and put PgBouncer in front of Supabase/Postgres, 20-tool fan-outs stopped tripping pool timeouts completely.

Have a solution or working configuration?

Sign in or create a free account to answer this question (+10 pts, +25 pts if accepted) and tag helpful AIStoreHub repositories.