MCP Integration Plane
Overview
Treat MCP as an external capability bus, not as a thin RPC wrapper. It has to manage connection types, auth, discovery, timeouts, progress, oversized output, session expiry, and user elicitation without destabilizing the main agent loop.
Source Anchors
src/services/mcp/client.tssrc/services/mcp/config.tssrc/services/mcp/elicitationHandler.ts
Workflow
- Normalize server configuration first and distinguish stdio, SSE, HTTP, WebSocket, IDE, and proxy-style transports.
- Build the correct auth provider, headers, proxy behavior, and timeout strategy for each transport type.
- Convert remote auth failures into explicit
needs-authstate instead of generic connection errors. - Memoize connections and cache tool, resource, and command discovery with bounded LRU behavior.
- Sanitize Unicode, trim oversized descriptions, and preserve tool annotations such as read-only and destructive hints during discovery.
- Run MCP tool calls with progress reporting, hard timeouts, preserved
_meta, and large-result persistence. - Let hooks or users resolve URL elicitations and retry the tool call only after the elicitation has completed.
- Handle 401, 404, and session-expired paths with targeted recovery such as cache clearing, reconnection, or token refresh.
- In cleanup, close transports, stop process monitoring, and unregister all cleanup handlers.
Design Rules
- Create a fresh timeout for each request instead of reusing one stale abort signal.
- Do not apply normal request timeouts to long-lived SSE subscription streams.
- Log only redacted and query-stripped base URLs.
- Turn oversized output into persisted references or truncated guidance, not raw prompt inflation.
- Distinguish auth failure, transport closure, and session expiry because the recovery path differs for each.
- Treat MCP tool metadata as a shared input for permissions, UI, context collapse, and search behavior.
Failure Modes
- One expired token causes a wave of 401s and then gets cached as prolonged unavailability.
- A connection-scoped timeout is reused until every later request fails instantly.
- Session-expired 404 responses are treated as generic network failures.
- Generated tool descriptions flood the context window because no cap exists.
- Large MCP responses are injected into the transcript without persistence or truncation.
Output
- Produce a transport matrix covering auth, timeout, and cleanup behavior.
- Produce a recovery policy for needs-auth, session expiry, timeout, and connection loss.
- Produce an output policy that defines when to truncate, when to persist, and when to preserve
_meta.