Skip to content

feat(mcp): reuse warm connections for tool execution and discovery#5760

Merged
waleedlatif1 merged 13 commits into
stagingfrom
mcp-connection-reuse
Jul 19, 2026
Merged

feat(mcp): reuse warm connections for tool execution and discovery#5760
waleedlatif1 merged 13 commits into
stagingfrom
mcp-connection-reuse

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Adds a warm MCP connection pool so tool execution and discovery reuse a live connection per server instead of connecting, initializing, and tearing down on every call (the connect-per-operation pattern behind the systemic -32001 request timeouts)
  • One warm connection per server, keyed by server id; concurrent requests multiplex over the single transport (verified against the MCP SDK, LibreChat, Cline, VS Code)
  • Single-flight creation (concurrent misses share one connect), 60s liveness ping-cache, and eviction on config change, max age (re-resolves the pinned SSRF IP), idle timeout, transport close, and session error
  • Wires all four connect-per-op sites through one withServerClient seam; calls carrying per-request headers bypass the pool (can't share a connection under different headers)
  • Reconnect stays demand-driven via the callers' existing retry — no background reconnect loop. On by default, no flag

Type of Change

  • New feature

Testing

  • Added connection-pool.test.ts (12) and service-pool.test.ts (3); full MCP suite green (347 tests), tsc clean, boundary audit at baseline
  • Not yet exercised against a live server under load — will watch pool-hit / timeout metrics after deploy

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Jul 18, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Jul 19, 2026 1:50am

Request Review

@cursor

cursor Bot commented Jul 18, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes core MCP hot paths (long-lived sockets, shared transports, credential-scoped pooling) and lifecycle eviction; behavior is heavily tested but not yet validated under production load.

Overview
Replaces connect-per-operation for MCP tool execution and tools/list discovery with a warm connection pool keyed by server, workspace, and user so credentials from env resolution are never shared across users.

McpConnectionPool leases ref-counted clients with single-flight creation, 60s liveness ping cache (5s ping timeout via updated McpClient.ping), max age, idle eviction, LRU cap, and safe retirement while borrowers are active. Failed ops poison the lease; config changes use evictServerConnections (wired from server update/delete lifecycle), separate from tool cache clearCache.

McpService centralizes access in withServerClient: pool hits skip reconnect and env/SSRF work; per-request extraHeaders bypass the pool; dead-connection and auth errors retire connections and trigger existing retries (including one-shot credential rotation for headers auth on discovery/execute).

Broad unit/integration tests cover pool races and service wiring.

Reviewed by Cursor Bugbot for commit d74d48d. Configure here.

Comment thread apps/sim/lib/mcp/service.ts
Comment thread apps/sim/lib/mcp/service.ts
Comment thread apps/sim/lib/mcp/connection-pool.ts Outdated
@greptile-apps

greptile-apps Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds reusable warm connections for MCP tool execution and discovery. The main changes are:

  • Adds an auth-scoped connection pool with single-flight creation and liveness checks.
  • Defers connection shutdown until active borrowers finish.
  • Rebuilds connections after config, session, transport, or credential failures.
  • Routes tool execution and discovery through a shared client lease.
  • Keeps per-request-header calls on isolated connections.
  • Adds pool, lifecycle, retry, and credential-rotation tests.

Confidence Score: 5/5

This looks safe to merge.

  • The updated lifecycle guards keep active requests alive during retirement.
  • Stale close events cannot evict replacement connections.
  • Pool keys separate workspace and user credential contexts.
  • Authentication failures retire stale connections and retry with freshly resolved credentials.
  • No blocking issues were found in the changed code.

Important Files Changed

Filename Overview
apps/sim/lib/mcp/connection-pool.ts Adds pooled connection creation, reuse, liveness checks, retirement, eviction, and deferred shutdown.
apps/sim/lib/mcp/service.ts Uses pooled clients for execution and discovery, scopes connections by auth context, and retries stale credentials.
apps/sim/lib/mcp/orchestration/server-lifecycle.ts Evicts warm connections after server configuration changes and deletion.
apps/sim/lib/mcp/client.ts Adds an optional timeout for MCP liveness pings.

Reviews (13): Last reviewed commit: "chore(mcp): add debug logging for pool h..." | Re-trigger Greptile

Comment thread apps/sim/lib/mcp/connection-pool.ts Outdated
Comment thread apps/sim/lib/mcp/service.ts Outdated
Comment thread apps/sim/lib/mcp/connection-pool.ts Outdated
Comment thread apps/sim/lib/mcp/service.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/mcp/service.ts
Comment thread apps/sim/lib/mcp/service.ts
Comment thread apps/sim/lib/mcp/connection-pool.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/mcp/service.ts
Comment thread apps/sim/lib/mcp/service.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/mcp/service.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/mcp/connection-pool.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/mcp/connection-pool.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/mcp/service.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 0edd043. Configure here.

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/mcp/service.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/mcp/service.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 65bd1f3. Configure here.

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit d74d48d. Configure here.

@waleedlatif1
waleedlatif1 merged commit bd636d4 into staging Jul 19, 2026
20 checks passed
@waleedlatif1
waleedlatif1 deleted the mcp-connection-reuse branch July 19, 2026 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant