Part V · Dynamic Capabilities: MCP, Skills, Plugins, and LSP
Chapter 15
Model Context Protocol (MCP): Clients, Transports, Auth, and Tool Search
Layer: Layer 5 (Dynamic Capabilities: MCP, Skills, Plugins & LSP)
Prerequisites: Chapter 02, Chapter 04, Chapter 06, Chapter 11, Chapter 14
Primary Source Files:
- services/mcp/config.ts — Multi-scope server resolution, enterprise allow/deny policy, content-signature deduplication, and atomic
.mcp.jsonwrites- services/mcp/client.ts — Transport construction, memoized connections, timeout/401/session-expiry recovery, tool/prompt/resource adaptation, output budgeting, and URL elicitation retries
- services/mcp/auth.ts —
ClaudeAuthProvider, OAuth 2.1 + PKCE + CIMD, cross-process lockfile refresh, 403 step-up interception, and XAA orchestration (services/mcp/xaa.ts)- services/mcp/useManageMCPConnections.ts — React lifecycle hook, two-phase startup, 16 ms state coalescing, backoff reconnects,
list_changed, and channel push handlers- services/mcp/elicitationHandler.ts — Server-initiated form/URL elicitations, hook interception, and completion notifications
- services/mcp/InProcessTransport.ts & services/mcp/SdkControlTransport.ts — Microtask-linked in-process transport and CLI↔SDK control-channel bridge
- services/mcp/mcpStringUtils.ts & services/mcp/normalization.ts —
mcp__server__toolnaming, character normalization, and permission-key extraction- tools/MCPTool/MCPTool.ts & tools.ts — Base MCP
Tooltemplate and cache-safeassembleToolPool()partitioning- tools/ToolSearchTool/ToolSearchTool.ts, tools/ToolSearchTool/prompt.ts, & utils/toolSearch.ts —
shouldDefer/defer_loading: true,tool_referencediscovery, keyword ranking, anddeferred_tools_deltacache stability
Why This Layer Exists
Everything built in Layers 0 through 4 operates over a closed world of compiled-in tools (Bash, Read, Edit, Grep, Agent). Those tools share a single process, deterministic schemas, static permission rules, and a byte-stable prefix that lets Anthropic's backend cache the system prompt and tool array globally across sessions.
External capabilities shatter every one of those assumptions simultaneously:
- Heterogeneous transports and lifecycles. An external server might be a local Docker container over
stdio, a remote SaaS endpoint behind OAuth 2.1 Streamable HTTP, a VS Code extension over WebSocket, a browser controller running in-process to avoid a 325 MB fork, or a JavaScript callback inside a parent SDK host process. Connections stall, drop mid-tool-call, expire sessions, and rotate tokens across competing CLI processes. - Untrusted schemas and outputs. Third-party servers emit arbitrary Unicode tool names, 60 KB OpenAPI-generated descriptions, raw base64 audio/binary blobs, and server-initiated UI prompts (elicitations) that must be routed through hooks and user consent dialogs without hanging the loop.
- Context bloat and cache destruction. Attach twenty corporate MCP servers (Slack, GitHub, Linear, Jira, Datadog,
claude.aiconnectors) and you easily inherit 200+ tools—tens of thousands of tokens of JSON Schema on turn 1. Worse, if servers finish handshaking asynchronously after turn 1—or sort alphabetically between built-in tools—every late connection invalidates the entire prompt cache.
Layer 5 solves all three problems without leaking transport mechanics into query.ts. To the turn engine, an MCP tool is just another Tool<Input, Output, P>—normalized, policy-filtered, partitioned behind built-ins, and lazily materialized via ToolSearch.
1. Configuration Resolution, Policy Gates, and Deduplication
The Problem
MCP server definitions arrive from seven distinct scopes (ConfigScopeSchema in services/mcp/types.ts:10-20): local, user, project, dynamic (CLI --mcp-config and plugins), enterprise, claudeai, and managed. Merging them by object key alone fails in three real-world ways:
- Silent duplication across namespaces. Plugin servers are keyed
plugin:<plugin>:<server>(utils/plugins/mcpPluginIntegration.ts:350) andclaude.aiorg connectors are keyedclaude.ai <DisplayName>(services/mcp/claudeai.ts:100). Neither collides by key with a user's manualslackentry in.mcp.json, so both connect tomcp.slack.com—doubling processes and burning ~600 chars/turn of duplicate tool listings. - Rewritten proxy URLs. In remote CCR sessions,
claude.aiconnectors arrive with URLs rewritten to/v2/session_ingress/shttp/mcp/...or/v2/ccr-sessions/...carrying the original vendor URL in?mcp_url=.... Naive URL comparison misses the duplicate. - Pre-trust code execution. Both
.mcp.jsonservers and dynamicheadersHelpershell commands can come from an untrusted repository cloned off the internet.
The Real Implementation
services/mcp/config.ts resolves configurations in a strict pipeline inside getClaudeCodeMcpConfigs() (config.ts:1071-1251) and getAllMcpConfigs() (config.ts:1258-1290):
- Exclusive enterprise override. If
managed-mcp.jsonexists (doesEnterpriseMcpConfigExist(),config.ts:1470-1477), it takes exclusive control: user, project, local, plugin, andclaude.aiservers are ignored completely. Even dynamic--mcp-configentries are rejected unless every server satisfiesareMcpConfigsAllowedWithEnterpriseMcpConfig()(config.ts:1494-1504), which currently whitelists onlytype === 'sdk' && name === 'claude-vscode'so enterprise policies do not break the VS Code extension. - Scope discovery and precedence. Otherwise, unless
isRestrictedToPluginOnly('mcp')locks out non-plugin scopes,getMcpConfigsByScope()loadsuser(~/.claude.json),project(.mcp.jsonwalked from filesystem root down tocwdso nearer directories override parents, filtered throughgetProjectMcpServerStatus(name) === 'approved'),local(per-project config in~/.claude.json), and enabled plugins (loadAllPluginsCacheOnly()→getPluginMcpServers()). Merge order isclaudeai < plugin (dynamic) < user < approved project < local. - Policy evaluation (
isMcpServerAllowedByPolicy(),config.ts:417-508).deniedMcpServersalways merges across all setting sources (getMcpDenylistSettings(), so users can still deny servers for themselves even under managed policy) and takes absolute precedence.allowedMcpServersreads exclusively frompolicySettingswhenallowManagedMcpServersOnly: true, else merged settings. An empty array[]blocks everything.- Matching is mode-strict: if the allowlist contains any
serverCommandentry, everystdioserver must match one by exact[command, ...args]array equality; if it contains anyserverUrlentry, every remote server must match a wildcard pattern (*→.*). Only when no command/URL entries exist does matching fall back toserverName. filterMcpServersByPolicy()(config.ts:536-551) explicitly exemptstype === 'sdk'servers because they are host-managed callbacks with neither a subprocess command nor a network URL.
- Content-signature deduplication (
config.ts:182-310).getMcpServerSignature()normalizes each server to eitherstdio:${JSON.stringify([command, ...args])}orurl:${unwrapCcrProxyUrl(url)}, deliberately ignoringenv(plugins injectCLAUDE_PLUGIN_ROOT) andheaders.dedupPluginMcpServers()anddedupClaudeAiMcpServers()drop duplicates where a manually configured server already covers the same signature, surfacing suppressed plugin duplicates as informationalmcp-server-suppressed-duplicateentries in/plugin.
// services/mcp/config.ts:202-212
export function getMcpServerSignature(config: McpServerConfig): string | null {
const cmd = getServerCommandArray(config)
if (cmd) {
return `stdio:${jsonStringify(cmd)}`
}
const url = getServerUrl(config)
if (url) {
return `url:${unwrapCcrProxyUrl(url)}`
}
return null
}
- Variable expansion & dynamic headers.
parseMcpConfig()expands${VAR}and${VAR:-default}acrosscommand,args,env,url, andheaders(services/mcp/envExpansion.ts:10-38), logging warnings for missing variables and Windowsnpxcommands lacking acmd /cwrapper. At connection time, services/mcp/headersHelper.ts runs an optionalheadersHelpershell command (10 s timeout, passingCLAUDE_CODE_MCP_SERVER_NAMEandCLAUDE_CODE_MCP_SERVER_URL) whose JSON object output overrides staticheaders.
Hidden Invariants & Failure Modes
- Disabled manual servers must never suppress twins. Both
dedupPluginMcpServers(caller atconfig.ts:1188-1193) anddedupClaudeAiMcpServers(config.ts:290) exclude disabled or policy-blocked servers from the dedup target map. Otherwise a disabled manualslackentry would suppressclaude.ai Slackor a plugin twin, leaving neither running. - Trust gate on
headersHelper. Forprojectandlocalscopes in interactive sessions,getMcpHeadersFromHelper()(headersHelper.ts:42-57) refuses to execute unlesscheckHasTrustDialogAccepted()is true, loggingtengu_mcp_headersHelper_missing_trust. - Atomic
.mcp.jsonmutation.writeMcpjsonFile()(config.ts:88-131) reads existing file mode, writes to.mcp.json.tmp.<pid>.<ts>, callshandle.datasync(), restores permissions viachmod, and performs an atomicrename()without following symlinks. - Opt-in vs. opt-out disable state. Normal servers default enabled and are recorded in
disabledMcpServers. Underfeature('CHICAGO_MCP'), the built-in Computer Use MCP server is markedDEFAULT_DISABLED_BUILTIN(config.ts:1512-1536), flipping polarity to opt-in viaenabledMcpServers. Bothclaude-in-chromeand the Computer Use server name are reserved and blocked inaddMcpConfig().
2. The Eight Transports and Connection Resilience
The Problem
Once configs are resolved, services/mcp/client.ts has to establish and sustain JSON-RPC connections over eight distinct transport shapes—while neutralizing a surprising collection of runtime and MCP SDK pitfalls:
- Passing a single
AbortSignal.timeout()into a transport'srequestInitexpires 60 seconds after startup and fails every future tool call permanently. - Using
AbortSignal.timeout()per request in Bun leaks ~2.4 KB of native memory per call until lazy GC runs after the full 60 s timer expires. - Applying any request timeout to
GETkills persistent SSE event streams after 60 s. - Runtimes spreading a
Headersinstance dropAccept: application/json, text/event-stream, triggering HTTP406from strict Streamable HTTP servers. - The MCP SDK's HTTP/SSE transports invoke
onerroron broken streams and exhausted SSE retries without callingonclose—leaving pendingcallTool()promises hung forever. StdioClientTransport.close()only aborts stdin/stdout, leaving Docker-wrapped MCP servers running as orphaned containers.
The Real Implementation
connectToServer() (services/mcp/client.ts:595-1641) constructs the transport for each ScopedMcpServerConfig['type']:
Config type |
Transport Class | Special Wiring & Auth |
|---|---|---|
stdio (or omitted) |
StdioClientTransport |
subprocessEnv() + serverRef.env; stderr: 'pipe' (capped at 64 MB); honors CLAUDE_CODE_SHELL_PREFIX |
stdio (claude-in-chrome or CHICAGO_MCP) |
InProcessTransport.ts (createLinkedTransportPair()) |
Runs server in-process via queueMicrotask() to avoid spawning a ~325 MB subprocess (client.ts:905-943) |
sse |
SSEClientTransport |
ClaudeAuthProvider; fetch wrapped with step-up + 60 s POST timeout; separate untimed eventSourceInit.fetch |
sse-ide |
SSEClientTransport |
No OAuth; proxy dispatcher if configured; sends ide_connected notification |
http |
StreamableHTTPClientTransport |
ClaudeAuthProvider; session-ingress bearer fallback only when !hasOAuthTokens (SDK merges requestInit after authProvider) |
ws / ws-ide |
utils/mcpWebSocketTransport.ts (WebSocketTransport) |
Subprotocol ['mcp']; dual runtime dispatch (globalThis.WebSocket on Bun vs. ws package on Node); ws-ide adds X-Claude-Code-Ide-Authorization |
claudeai-proxy |
StreamableHTTPClientTransport |
Routes through MCP_PROXY_URL with X-Mcp-Client-Session-Id and createClaudeAiProxyFetch() |
sdk |
SdkControlTransport.ts (SdkControlClientTransport) |
Handled separately in setupSdkMcpClients() (client.ts:3262-3348), multiplexing JSON-RPC over CLI↔SDK control messages |
Fetch Wrapping, In-Process Microtasks, and SDK Bridging
Every remote POST path passes through wrapFetchWithTimeout() (client.ts:492-550):
// services/mcp/client.ts:492-549 (condensed)
export function wrapFetchWithTimeout(baseFetch: FetchLike): FetchLike {
return async (url: string | URL, init?: RequestInit) => {
const method = (init?.method ?? 'GET').toUpperCase()
if (method === 'GET') {
return baseFetch(url, init) // long-lived SSE stream — never time out
}
const headers = new Headers(init?.headers)
if (!headers.has('accept')) {
headers.set('accept', MCP_STREAMABLE_HTTP_ACCEPT) // 'application/json, text/event-stream'
}
const controller = new AbortController()
const timer = setTimeout(
c => c.abort(new DOMException('The operation timed out.', 'TimeoutError')),
MCP_REQUEST_TIMEOUT_MS, // 60_000 ms
controller,
)
timer.unref?.()
// ... forwards parentSignal abort, clears timer in try/catch, returns baseFetch(...)
}
}
For first-party in-process servers (claude-in-chrome via @ant/claude-for-chrome-mcp, and Computer Use behind feature('CHICAGO_MCP')), services/mcp/InProcessTransport.ts:11-63 links two InProcessTransport instances where send() schedules queueMicrotask(() => this.peer?.onmessage?.(message)). The microtask hop is load-bearing: synchronous delivery would blow the JS call stack on request/response chains.
For Agent SDK hosts (type: 'sdk'), services/mcp/SdkControlTransport.ts:60-136 provides a mirror pair: SdkControlClientTransport in the CLI process invokes sendMcpMessage(serverName, message) (which sends an NDJSON control request over stdout and awaits the response on stdin), while SdkControlServerTransport in the SDK process forwards incoming JSON-RPC frames to the in-process MCP server and returns responses via callback.
For claudeai-proxy connectors, createClaudeAiProxyFetch() (client.ts:372-422) attaches the claude.ai OAuth bearer token and retries once on 401 via handleOAuth401Error(sentToken). Notice the subtle race it explicitly prevents: it returns { response, sentToken } from doRequest() rather than re-reading getClaudeAIOAuthTokens() after the 401. Under 30+ concurrent connectors hitting a stale token simultaneously, another connector's refresh clears the memoize cache; re-reading after the request would pass the new token to handleOAuth401Error(), see no diff against keychain, and skip the retry—stranding the connector in the 15-minute needs-auth cache. It also re-checks getClaudeAIOAuthTokens()?.accessToken !== sentToken when handleOAuth401Error returns false due to ELOCKED lockfile contention.
Connection Drop Detection and Session Expiry Recovery
The MCP client declares capabilities: { roots: {}, elicitation: {} } (client.ts:994-1000) and races client.connect(transport) against MCP_TIMEOUT (default 30 s). Once connected, client.ts installs a defensive wrapper around client.onerror and client.onclose:
// services/mcp/client.ts:1227-1263
let consecutiveConnectionErrors = 0
const MAX_ERRORS_BEFORE_RECONNECT = 3
let hasTriggeredClose = false
const closeTransportAndRejectPending = (reason: string) => {
if (hasTriggeredClose) return
hasTriggeredClose = true
logMCPDebug(name, `Closing transport (${reason})`)
void client.close().catch(e => {
logMCPDebug(name, `Error during close: ${errorMessage(e)}`)
})
}
Why
client.close()instead ofclient.onclose?.(): Callingclient.onclose?.()directly would clear the memoization cache, but any in-flightcallTool()promise would remain hung indefinitely. Callingclient.close()flows throughtransport.close()→ SDK_onclose(), which rejects all pending request handlers withMcpError -32000 "Connection closed"and firesclient.oncloseto evict caches (client.ts:1234-1247).
Three error signatures trigger closeTransportAndRejectPending():
- Session expiry (
isMcpSessionExpiredError,client.ts:193-206): requires both HTTP404(error.code === 404) and JSON-RPC error code-32001in the message body so generic web 404s never cause false reconnect storms. - Exhausted SDK SSE retries:
error.message.includes('Maximum reconnection attempts')(client.ts:1342). - Three consecutive terminal transport errors:
ECONNRESET,ETIMEDOUT,EPIPE,EHOSTUNREACH,ECONNREFUSED,Body Timeout Error,terminated,SSE stream disconnected, orFailed to reconnect SSE stream.
At the tool-call layer, callMCPTool() (client.ts:3217-3231) catches both a direct 404 / -32001 and the derived HTTP -32000 "Connection closed" rejection, clears the connection cache via clearServerCache(), and throws McpSessionExpiredError. The outer loop in fetchToolsForClient()'s call() wrapper (client.ts:1859-1922) retries once (MAX_SESSION_RETRIES = 1) via ensureConnectedClient().
Hidden Invariants & Failure Modes
- Stdio escalation ladder (
client.ts:1426-1562).cleanup()registers withregisterCleanup()(utils/cleanupRegistry.ts) for all transport types. Forstdio, it explicitly sendsSIGINT, pollsprocess.kill(pid, 0)every 50 ms for 100 ms, escalates toSIGTERMfor 400 ms, and finishes withSIGKILLunder a 600 ms absolute failsafe. - Work-stealing concurrency split (
client.ts:2218-2403).getMcpToolsCommandsAndResources()partitions active servers into local (stdio/sdk, concurrency 3 viaMCP_SERVER_CONNECTION_BATCH_SIZE) and remote (concurrency 20 viaMCP_REMOTE_SERVER_CONNECTION_BATCH_SIZE) and runs both pools concurrently throughpMap. A March 2026 scar note (client.ts:2212-2217) explains whypMapreplaced fixed sequential batches: one slow server in batch N previously stalled all 20 slots of batch N+1. - Coupled cache eviction.
connectToServerislodash-es/memoized by${name}-${jsonStringify(serverRef)}, whilefetchToolsForClient,fetchResourcesForClient, andfetchCommandsForClientusememoizeWithLRU(..., 20)keyed byclient.name. Bothclient.onclose(client.ts:1389-1396) andclearServerCache()(client.ts:1666-1672) must evict all four caches together—otherwise a reconnected client serves stale tools from the dead connection.reconnectMcpServerImpl()(client.ts:2152) also callsclearKeychainCache()before reconnecting so tokens updated by a sibling process (like the VS Code extension host) take effect immediately.
3. OAuth 2.1, Cross-Process Refresh Locking, Step-Up Auth, and XAA
The Problem
Remote sse and http MCP servers authenticate through OAuth 2.1, exposing five classic production traps:
- Multi-process token rotation races. A developer runs three Claude Code terminals pointing at the same Linear or Slack MCP server. All three detect an expiring token at the same minute and race to consume a single-use refresh token—two fail with
invalid_grantand wipe the third process's freshly saved credentials. - The macOS Keychain 4 KB cliff. Storing full OAuth authorization server metadata (~1.5–2 KB per server) in secure storage overflows
security -i's 4096-byte stdin line limit (~2013 bytes of JSON after hex encoding) as soon as two OAuth servers are connected, corrupting the entire credential store. - Hot-path keychain CPU burn. The MCP SDK invokes
authProvider.tokens()inside_commonHeaderson every HTTP request; invalidating the keychain cache on each call spawnssecurity find-generic-password30–40 times/sec (measured at 7.2% of total CPU). - RFC 6749 §6 step-up deadlocks. When a server returns
403 insufficient_scope, the SDK's default handler sees arefresh_token, refreshes (which RFC 6749 §6 forbids from elevating scope), retries with the same-scoped token, gets403again, and aborts—never triggering re-consent. - Non-standard providers and startup 401 storms. Slack returns HTTP
200with{"error":"invalid_refresh_token"}, bypassing the SDK's!response.okerror parser. Meanwhile, 30 unauthenticated remote servers would execute 60+ HTTP round-trips on every startup (blocking print mode) without negative caching.
The Real Implementation
services/mcp/auth.ts solves each of these inside ClaudeAuthProvider (implementing the SDK's OAuthClientProvider) and performMCPOAuthFlow().
Keying, Discovery, and CIMD
Credentials live in getSecureStorage() under mcpOAuth[serverKey] where getServerKey() (auth.ts:325-341) hashes { type, url, headers } into ${serverName}|${sha256.slice(0, 16)}, preventing tokens from leaking across two servers that share a name.
fetchAuthServerMetadata() (auth.ts:256-311) resolves OAuth metadata in three tiers:
- Explicit
oauth.authServerMetadataUrl(enforcinghttps://at both Zod schema and runtime). - RFC 9728 Protected Resource Metadata (
/.well-known/oauth-protected-resource) → RFC 8414 Authorization Server Metadata viadiscoverOAuthServerInfo(). - Legacy path-aware RFC 8414 fallback (
/.well-known/oauth-authorization-server/{path}) when the MCP URL has a non-root path.
For client registration, ClaudeAuthProvider.clientMetadataUrl (auth.ts:1445-1452) implements CIMD (SEP-991): when the authorization server advertises client_id_metadata_document_supported: true, the SDK uses MCP_CLIENT_METADATA_URL (or MCP_OAUTH_CLIENT_METADATA_URL) as client_id instead of Dynamic Client Registration.
Browser Callback Flow (performMCPOAuthFlow, auth.ts:847-1342)
Interactive auth binds a temporary HTTP server on 127.0.0.1 using findAvailablePort() (services/mcp/oauthPort.ts:36-78)—picking a random port in 49152–65535 on POSIX, 39152–49151 on Windows (where 49152+ is reserved), falling back to 3118, or honoring oauth.callbackPort / MCP_OAUTH_CALLBACK_PORT. It generates a 32-byte base64url CSRF state, passes the URL to onAuthorizationUrl before calling openBrowser(), supports manual callback URL pasting (options.onWaitingForCallback) for SSH/headless users, sanitizes error pages with xss(), and calls server.unref() plus timeoutId.unref() (5 min timeout) so an unmounted UI never keeps the process alive.
Cross-Process Lockfile Token Refresh (auth.ts:2090-2359)
When tokens() sees expiresIn <= 300s and a refreshToken exists, it coalesces concurrent in-process callers on this._refreshInProgress and enters refreshAuthorization():
// services/mcp/auth.ts:2138-2165
try {
// Re-read tokens after acquiring lock — another process may have refreshed
clearKeychainCache()
const storage = getSecureStorage()
const data = storage.read()
const tokenData = data?.mcpOAuth?.[serverKey]
if (tokenData) {
const expiresIn = (tokenData.expiresAt - Date.now()) / 1000
if (expiresIn > 300) {
logMCPDebug(this.serverName, `Another process already refreshed tokens ...`)
return {
access_token: tokenData.accessToken,
refresh_token: tokenData.refreshToken,
expires_in: expiresIn,
scope: tokenData.scope,
token_type: 'Bearer',
}
}
if (tokenData.refreshToken) {
refreshToken = tokenData.refreshToken
}
}
return await this._doRefresh(refreshToken)
} finally { ... }
The lockfile is ~/.claude/mcp-refresh-<sanitizedKey>.lock (up to 5 retries with 1–2 s jitter on ELOCKED). Inside _doRefresh(), transient errors (ServerError, TemporarilyUnavailableError, TooManyRequestsError, timeouts) retry up to 3 times (1s, 2s, 4s). Even if the AS returns InvalidGrantError, _doRefresh() calls clearKeychainCache() and checks storage one last time—adopting a sibling process's winning token if expiresIn > 300 before calling invalidateCredentials('tokens') (auth.ts:2289-2325).
Step-Up Scope Interception & Slack 200-Error Normalization
To break the RFC 6749 §6 refresh loop, both sse and http transports wrap their innermost fetch with wrapFetchWithStepUpDetection() (auth.ts:1354-1374):
// services/mcp/auth.ts:1354-1374
export function wrapFetchWithStepUpDetection(
baseFetch: FetchLike,
provider: ClaudeAuthProvider,
): FetchLike {
return async (url, init) => {
const response = await baseFetch(url, init)
if (response.status === 403) {
const wwwAuth = response.headers.get('WWW-Authenticate')
if (wwwAuth?.includes('insufficient_scope')) {
const match = wwwAuth.match(/scope=(?:"([^"]+)"|([^\s,]+))/)
const scope = match?.[1] ?? match?.[2]
if (scope) {
provider.markStepUpPending(scope)
}
}
}
return response
}
}
When this._pendingStepUpScope contains a scope missing from tokenData.scope, tokens() intentionally returns refresh_token: undefined (auth.ts:1628-1690). The SDK skips refresh, calls redirectToAuthorization(), and because the transport-attached provider has handleRedirection = false, it persists existing.stepUpScope (auth.ts:1890-1900) so the subsequent interactive performMCPOAuthFlow() requests the elevated scope immediately without an extra probe.
Meanwhile, createAuthFetch() pipes every OAuth POST response through normalizeOAuthErrorBody() (auth.ts:147-190), which peeks at HTTP 2xx bodies matching OAuthErrorResponseSchema (and not OAuthTokensSchema), rewrites non-standard error codes (invalid_refresh_token, expired_refresh_token, token_expired) to invalid_grant, and returns a synthetic HTTP 400 so the SDK's InvalidGrantError path fires reliably.
Cross-App Access (XAA / SEP-990)
Behind CLAUDE_CODE_ENABLE_XAA=1 and per-server oauth.xaa: true, services/mcp/xaa.ts and services/mcp/xaaIdpLogin.ts eliminate per-server browser consent screens for enterprise deployments:
- A single OIDC browser login against the org's IdP (
settings.xaaIdp) caches anid_tokenkeyed by issuer. performCrossAppAccess()exchangesid_token→ ID-JAG via RFC 8693 Token Exchange at the IdP, then exchanges ID-JAG →access_tokenvia RFC 7523jwt-bearergrant at the MCP server's AS.ClaudeAuthProvider.tokens()(auth.ts:1585-1615) silently triggersxaaRefresh()whenever a cachedid_tokenexists and the access token is absent or expiring.
No silent downgrade on XAA (
auth.ts:864-876). Ifoauth.xaa: trueis configured,performMCPOAuthFlow()never falls back to the standard consent flow—even ifCLAUDE_CODE_ENABLE_XAAis unset. Falling back would silently bypass the enterprise IdP's authorization posture.
Needs-Auth Caching & McpAuthTool
To stop unauthenticated remote servers from burning startup latency on every turn, client.ts:257-316 maintains a 15-minute mcp-needs-auth-cache.json (coalescing concurrent reads on authCachePromise and serializing read-modify-write updates through a module-level writeChain). getMcpToolsCommandsAndResources() (client.ts:2307-2322) skips connecting if the server is in that cache or hasMcpDiscoveryButNoToken() sees persisted OAuth discovery state with empty tokens (bypassed for XAA servers so silent re-auth stays reachable).
Rather than leaving the server invisible to the model, needs-auth servers receive a synthesized pseudo-tool from createMcpAuthTool() (tools/McpAuthTool/McpAuthTool.ts:49-215) named mcp__<server>__authenticate. When the model calls it, it launches performMCPOAuthFlow(..., { skipBrowserOpen: true }), races onAuthorizationUrl against silent completion (XAA), and returns the URL for the user to open. A .then() continuation waits in the background, calls reconnectMcpServerImpl(), and swaps the server's real tools into AppState.mcp.tools—automatically wiping the authenticate pseudo-tool via prefix replacement (mcp__<server>__*).
Hidden Invariants & Failure Modes
- Stripped discovery persistence (
auth.ts:2007-2035).saveDiscoveryState()writes only{ authorizationServerUrl, resourceMetadataUrl }—neverauthorizationServerMetadataorresourceMetadata—to stay well under macOSsecurity -i's 4096-byte stdin cap. - No
clearKeychainCache()intokens()(auth.ts:1541-1549).tokens()usesawait storage.readAsync()and relies on the keychain TTL; cache clearing is reserved for lock-protected refreshes, explicit reconnects, andInvalidGrantErrorrecovery. - Mid-session 401 demotion. If an active tool call hits HTTP 401 or
UnauthorizedError,callMCPTool()throwsMcpAuthError(client.ts:3198-3207). services/tools/toolExecution.ts:1601-1629 catchesMcpAuthErrorand transitions that entry inAppState.mcp.clientsfrom'connected'to'needs-auth'so/mcpreflects the stale token immediately.
4. Live Connection Lifecycle, Notifications, and Elicitation
The Problem
In interactive sessions, MCP connections are not a one-shot startup step. Servers connect asynchronously at wildly different speeds, drop over Wi-Fi sleep, change their tool lists at runtime, push inbound chat messages (channels), pause mid-tool-call to request human input (elicitations), and get added/removed live when the user edits .mcp.json or runs /reload-plugins.
The Real Implementation
services/mcp/MCPConnectionManager.tsx mounts useManageMCPConnections() (services/mcp/useManageMCPConnections.ts) and exposes reconnectMcpServer and toggleMcpServer via React context.
Two-Phase Startup & 16 ms State Coalescing
useManageMCPConnections() runs two coordinated effects keyed on sessionId, authVersion, dynamicMcpConfig, and mcp.pluginReconnectKey:
- Pending initialization + stale eviction (
useManageMCPConnections.ts:772-854). Seeds all local/plugin servers intoAppState.mcp.clientsas'pending'(or'disabled') immediately so the UI renders status right away. Before adding new entries, it runsexcludeStalePluginClients()(services/mcp/utils.ts:185-224), which compareshashMcpConfig()(SHA-256 of key-sorted config excludingscope) to detect edited.mcp.jsonservers across all scopes and removed servers in'dynamic'scope. - Two-phase connection (
useManageMCPConnections.ts:858-1024). Kicks offfetchClaudeAIMcpConfigsIfEligible()beforegetClaudeCodeMcpConfigs()so the network call overlaps withloadAllPluginsCacheOnly(). Phase 1 starts connecting local/plugin servers immediately without waiting for Phase 2; Phase 2 awaits the memoizedclaudeaiPromise, policy-filters, deduplicates against manual servers, and starts connectingclaude.aiproxies concurrently. - 16 ms batch window (
useManageMCPConnections.ts:207-308). Because 20+ servers complete handshakes milliseconds apart,updateServer()buffers incoming updates inpendingUpdatesRefand flushes them in onesetAppState()call everyMCP_BATCH_FLUSH_MS = 16ms (one terminal frame). Each flush replaces tools/commands/resources cleanly by server prefix (mcp__<server>__, plus<server>:for MCP skills viacommandBelongsToServer()).
Automatic Backoff Reconnects & Server Notifications
Inside onConnectionAttempt() (useManageMCPConnections.ts:310-763), every newly 'connected' server wires four classes of runtime handlers:
client.onclosebackoff loop (333-468). For remote transports (http,sse,ws,ws-ide,sse-ide,claudeai-proxy), clears server cache and retries up toMAX_RECONNECT_ATTEMPTS = 5with exponential backoff (1s → 2s → 4s → 8s → 16s, capped at30s), checkingisMcpServerDisabled(name)before every attempt. Localstdioandsdkservers transition directly to'failed'.list_changednotifications (618-751). If the server advertisescapabilities.{tools,prompts,resources}.listChanged, registers handlers forToolListChangedNotificationSchema,PromptListChangedNotificationSchema, andResourceListChangedNotificationSchemathat evict the correspondingfetch*ForClientLRU entry and push fresh snapshots throughupdateServer().- Channel push notifications (
473-614, services/mcp/channelNotification.ts). Behindfeature('KAIROS') || feature('KAIROS_CHANNELS')and runtime gatetengu_harbor(requiringclaude.aiOAuth and orgchannelsEnabled: true),gateChannelServer()decides'register'vs.'skip'. Registered servers mapnotifications/claude/channelintoenqueue()wrapped in<channel>XML tags (with strict identifier validation onmetakeys to prevent XML attribute injection), and—if the server explicitly opts in viacapabilities.experimental['claude/channel/permission']—routenotifications/claude/channel/permissionreplies toChannelPermissionCallbacks. On'skip', it explicitly callsremoveNotificationHandler()so a mid-session/logoutdemotes live handlers idempotently.
Snapshot Absences Observed at Call Sites: Two build-time-gated modules imported in services/mcp/client.ts:117-121 and services/mcp/useManageMCPConnections.ts:22-31 are absent from this checkout:
skills/mcpSkills.ts(underfeature('MCP_SKILLS'), observed callingfetchMcpSkillsForClient(client)and.cache.delete(name)whenresources/list_changedfires) andservices/skillSearch/localSearch.ts(underfeature('EXPERIMENTAL_SKILL_SEARCH'), observed callingclearSkillIndexCache()). Similarly,types/message.tsand@ant/claude-for-chrome-mcpare referenced at call sites but absent from the snapshot.
Elicitation: Form, URL, Hooks, and Error -32042 Retries
MCP Elicitation lets a server pause execution to ask the user for structured form data (mode: 'form') or out-of-band browser action (mode: 'url'). Two subtle design details hold it together:
- Empty capability object (
client.ts:996-999). The handshake advertiseselicitation: {}instead of{ form: {}, url: {} }because Spring AI's Java MCP SDK defines an emptyElicitationclass that throws on unknown JSON properties. - Bootstrap cancel handler (
client.ts:1188-1197).connectToServer()installs a placeholderElicitRequestSchemahandler returning{ action: 'cancel' }to cover the narrow window beforeonConnectionAttempt()callsregisterElicitationHandler()(services/mcp/elicitationHandler.ts:68-212).
Once registered, elicitations flow along two complementary paths:
- Explicit request (
ElicitRequestSchema): RunsrunElicitationHooks()first (hooks can programmaticallyaccept,decline, orcancelwithout touching the UI); otherwise pushes anElicitationRequestEventontoAppState.elicitation.queue, awaits the dialog'srespond()(orextra.signalabort), and filters the user's answer throughrunElicitationResultHooks()(which can mutate or block the response before returning it to the server). - Error-driven URL retry (
callMCPToolWithUrlElicitationRetry,client.ts:2813-3027): When a tool call fails withMcpErrorcodeErrorCode.UrlElicitationRequired(-32042),client.tsextractserror.data.elicitations(note: checkserror.coderather thaninstanceof UrlElicitationRequiredErrorbecause the SDK constructs plainMcpErrors) and processes up toMAX_URL_ELICITATION_RETRIES = 3rounds. In headless/SDK mode, it delegates tocontext.handleElicitation(structuredIO); in interactive REPL mode, it uses a two-phase waiting flow:respond({ action: 'accept' })is intentionally a no-op (the user just opened the browser), while eitheronWaitingDismiss('retry')or the server'sElicitationCompleteNotificationSchemasettingcompleted: trueon the queue entry (elicitationHandler.ts:175-207) resolves the promise and retries the tool call.
Hidden Invariants & Failure Modes
- Three hazards defused during stale-server cleanup (
useManageMCPConnections.ts:791-812). Because React state updaters must be synchronous, disconnecting stale servers on/reload-pluginsor.mcp.jsonedits runs fire-and-forget—after explicitly: (1) clearing any pendingreconnectTimersReftimer (which holds the old config), (2) settings.client.onclose = undefined(preventing the old closure'sreconnectWithBackofffrom racing the fresh connection), and (3) callingclearServerCache()only whens.type === 'connected'(for never-connected servers,clearServerCacheinvokes memoizedconnectToServer, which would spawn a server just to kill it). - Disk-first disable ordering (
useManageMCPConnections.ts:1093-1100).toggleMcpServer()writessetMcpServerEnabled(serverName, false)to disk before callingclearServerCache(). Theonclosecallback deliberately reads disk state viaisMcpServerDisabled()rather than a stale React state closure; reversing those two lines spawns an immediate auto-reconnect loop for a server the user just disabled.
5. Adapting MCP Tools: Normalization, Permissions, Output Budgeting, and Cache Partitioning
The Problem
Raw MCP tool definitions cannot be handed directly to the Anthropic Messages API or the permission engine:
- Server and tool names can contain dots, spaces, or slashes (
claude.ai Google Drive,org/search), violating the API's^[a-zA-Z0-9_-]{1,64}$tool-name pattern. - OpenAPI-generated MCP servers have been observed returning 15–60 KB
descriptionstrings and massive JSON payloads or raw base64 audio/PDF attachments. - In
CLAUDE_AGENT_SDK_MCP_NO_PREFIXmode, SDK MCP servers expose unprefixed tool names (likeWrite) to override built-ins—which could accidentally match built-in permission rules if rule matching inspectedtool.name. - Sorting MCP tools alongside built-ins alphabetically inserts
mcp__*betweenGrepandRead, shattering Anthropic's global tool-prefix prompt cache across every user.
The Real Implementation
Naming & Permission Key Separation
services/mcp/normalization.ts:17-23 implements normalizeNameForMCP(): replaces any character outside [a-zA-Z0-9_-] with _, and for claude.ai servers also collapses consecutive _+ and trims leading/trailing underscores so they cannot collide with the __ delimiter. (services/mcp/claudeai.ts:93-111 additionally deduplicates post-normalization collisions by appending (2), (3) suffixes.)
services/mcp/mcpStringUtils.ts:19-67 builds and parses qualified names (mcp__<server>__<tool>) and defines the permission-key invariant:
// services/mcp/mcpStringUtils.ts:60-67
export function getToolNameForPermissionCheck(tool: {
name: string
mcpInfo?: { serverName: string; toolName: string }
}): string {
return tool.mcpInfo
? buildMcpToolName(tool.mcpInfo.serverName, tool.mcpInfo.toolName)
: tool.name
}
utils/permissions/permissions.ts:240-269 uses getToolNameForPermissionCheck(tool) for every allow, deny, and ask rule match. Even when CLAUDE_AGENT_SDK_MCP_NO_PREFIX sets tool.name = 'Write', permission evaluation checks mcp__my_sdk_server__Write via tool.mcpInfo. toolMatchesRule() also supports server-wide rules: a rule targeting mcp__github or mcp__github__* matches every tool whose parsed mcpInfoFromString() shares serverName === 'github'. Crucially, filterToolsByDenyRules() (tools.ts:262-269) runs this exact matcher before tools reach the model, so blanket-denied servers disappear from the prompt entirely.
Synthesizing Tool Objects via MCPTool
tools/MCPTool/MCPTool.ts defines the shared fail-closed template (isMcp: true, inputSchema: z.object({}).passthrough(), maxResultSizeChars: 100_000, checkPermissions() returning 'passthrough', plus shared Ink UI renderers). For each connected server, fetchToolsForClient() (client.ts:1743-1998) runs tools/list, sanitizes the payload via recursivelySanitizeUnicode(), and spreads ...MCPTool with per-tool overrides:
- Description & instruction caps: Both
tool.prompt()(client.ts:1789-1794) andInitializeResult.instructions(client.ts:1160-1171) truncate atMAX_MCP_DESCRIPTION_LENGTH = 2048chars. - Safety & scheduling annotations:
isConcurrencySafe()andisReadOnly()both delegate totool.annotations?.readOnlyHint ?? false(fail-closed serial execution unless the server explicitly declares read-only);isDestructive()andisOpenWorld()readdestructiveHintandopenWorldHint;toAutoClassifierInput()encodes arguments ask=vpairs for Chapter 11's auto-mode classifier. - Discovery metadata:
searchHintreads_meta['anthropic/searchHint']and collapses\s+to a single space (preventing external servers from injecting fake tool lines via\ninto deferred-tool listings!), whilealwaysLoadchecks_meta['anthropic/alwaysLoad'] === true. - Permission suggestions:
checkPermissions()returns'passthrough'with anaddRulessuggestion targeting the exactfullyQualifiedNameinlocalSettings(client.ts:1814-1832). - Execution & progress:
call()attaches_meta: { 'claudecode/toolUseId': toolUseId }, streamsMCPProgressevents (started/progress/completed/failed), racesclient.callTool()againstMCP_TOOL_TIMEOUT(default100_000_000ms ≈ 27.8 h, guarding against broken SSE streams where the SDK's own timeout fails to fire), and mapsisError: trueintoMcpToolCallErrorwhile preserving_meta.
MCP prompts (prompts/list) are adapted in fetchCommandsForClient() (client.ts:2033-2107) into type: 'prompt' Command objects named mcp__<server>__<prompt> (using prompt.name, not prompt.title, so spaces cannot break slash-command parsing). Whenever any connected server advertises capabilities.resources, client.ts:2360-2364 injects the built-in ListMcpResourcesTool and ReadMcpResourceTool into the tool pool.
Output Transformation & Disk Spillover
transformMCPResult() and processMCPResult() (client.ts:2478-2799) protect the context window from rogue server payloads:
imageblocks and image resource blobs pass throughmaybeResizeAndDownsampleImageBuffer().audioblocks and non-image binaryresource.blobs are decoded and saved to disk viapersistBlobToTextBlock(), returning a short file-path text block instead of raw base64.- Text /
structuredContent/contentArrayoutputs exceedinggetMaxMcpOutputTokens()(utils/mcpValidation.ts:16-47: default25,000tokens, overridable viaMAX_MCP_OUTPUT_TOKENSor GrowthBooktengu_satin_quoll['mcp_tool']) are persisted viapersistToolResult()with a compact jq-friendly shape inferred byinferCompactSchema()({title: string, items: [{id: number, name: string}]}) so the model can slice the file precisely. IfENABLE_MCP_LARGE_OUTPUT_FILESis falsy or the output contains images (which would lose compression if JSON-serialized), it falls back totruncateMcpContentIfNeeded().
Partition-Sorting for Global Prompt Cache Stability
In tools.ts:345-367 (and identically in utils/toolPool.ts:55-79 for useMergedTools), built-in tools and MCP tools are never sorted in one flat array:
// tools.ts:345-367
export function assembleToolPool(
permissionContext: ToolPermissionContext,
mcpTools: Tools,
): Tools {
const builtInTools = getTools(permissionContext)
const allowedMcpTools = filterToolsByDenyRules(mcpTools, permissionContext)
const byName = (a: Tool, b: Tool) => a.name.localeCompare(b.name)
return uniqBy(
[...builtInTools].sort(byName).concat(allowedMcpTools.sort(byName)),
'name',
)
}
Why partition-sorting is load-bearing: Anthropic's server-side
claude_code_system_cache_policyplaces a global cache breakpoint immediately after the last prefix-matched built-in tool. Keeping built-ins as a contiguous sorted prefix means per-user MCP tools sit strictly after the global breakpoint, anduniqBy(..., 'name')guarantees built-ins win any name collision.
Hidden Invariants & Failure Modes
- Double-underscore parsing asymmetry (
mcpStringUtils.ts:14-31).mcpInfoFromString()splits on__, takesparts[1]asserverName, and rejoinsparts.slice(2)with__astoolName. Tool names with__round-trip intact; server names containing__do not. - IDE server tool allowlist (
client.ts:568-573).isIncludedMcpTool()drops every tool prefixedmcp__ide__exceptmcp__ide__executeCodeandmcp__ide__getDiagnostics. - Coordinator mode pruning (
utils/toolPool.ts:11-41). Underfeature('COORDINATOR_MODE'),applyCoordinatorToolFilter()strips MCP tools from the coordinator except lightweight PR activity subscription suffixes (subscribe_pr_activity,unsubscribe_pr_activity).
6. Solving Tool-Schema Bloat Without Cache Busts: ToolSearchTool and Deferred Loading
The Problem
Partition-sorting protects built-in caches, but two scaling cliffs remain when dozens of MCP servers are attached:
- Token bloat: Sending 250 full JSON schemas on every turn consumes 25k–60k tokens before the user types a word—and degrades tool-selection accuracy.
- Cache-busting announcements: Even if schemas are deferred, telling the model which deferred tools exist by prepending a synthetic
<available-deferred-tools>message at index 0—or injecting server instructions into the system prompt—invalidates the entire conversation cache whenever an MCP server connects 2 seconds late or reconnects mid-session.
The Real Implementation
Claude Code solves both problems through a four-part protocol spanning tools/ToolSearchTool/prompt.ts, tools/ToolSearchTool/ToolSearchTool.ts, utils/toolSearch.ts, utils/attachments.ts, and services/api/claude.ts.
1. Deferral Policy and Mode Selection
isDeferredTool(tool) (tools/ToolSearchTool/prompt.ts:62-108) decides per tool in strict priority order:
tool.alwaysLoad === true→ never deferred (lets MCP servers opt critical tools into turn 1 via_meta['anthropic/alwaysLoad']).tool.isMcp === true→ always deferred.ToolSearchitself (plusAgentwhenfeature('FORK_SUBAGENT')is enabled, andBrief/SendUserFileunderfeature('KAIROS')/KAIROS_BRIEF) → never deferred.- Built-ins setting
tool.shouldDefer === true→ deferred.
getToolSearchMode() (utils/toolSearch.ts:172-198) resolves ENABLE_TOOL_SEARCH:
- Unset (default),
true, orauto:0→'tst'(always defer). autoorauto:1-99→'tst-auto'(defer only when deferred tool schemas exceed N% of the model's context window—default10%—measured via memoizedcountToolDefinitionTokens()with a2.5chars/token fallback).falseorauto:100→'standard'(inline all tools).
Three runtime guards can still disable deferral for a specific call (utils/toolSearch.ts:181-435):
CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETASforces'standard'and stripsdefer_loadingattoolToAPISchema()(utils/api.ts:243) for strict third-party gateways.- Non-first-party
ANTHROPIC_BASE_URLproxies disable tool search automatically only whenENABLE_TOOL_SEARCHis unset (utils/toolSearch.ts:299-311); settingENABLE_TOOL_SEARCH=trueorautoexplicitly opts compatible proxies (LiteLLM, Cloudflare AI Gateway) back in. modelSupportsToolReference(model)rejects models matching GrowthBooktengu_tool_search_unsupported_models(default['haiku']), andisToolSearchToolAvailable(tools)disables deferral ifToolSearchwas stripped bydisallowedTools.
2. Dynamic Wire Filtering & defer_loading: true
At API request assembly in services/api/claude.ts:1120-1255, Claude Code does not send all 250 deferred tool definitions on turn 1—not even with defer_loading: true:
// services/api/claude.ts:1154-1168
if (useToolSearch) {
const discoveredToolNames = extractDiscoveredToolNames(messages)
filteredTools = tools.filter(tool => {
// Always include non-deferred tools
if (!deferredToolNames.has(tool.name)) return true
// Always include ToolSearchTool (so it can discover more tools)
if (toolMatchesName(tool, TOOL_SEARCH_TOOL_NAME)) return true
// Only include deferred tools that have been discovered
return discoveredToolNames.has(tool.name)
})
}
Every tool in filteredTools is converted by toolToAPISchema() (utils/api.ts:119-230), which session-caches the immutable base schema (name, description, input_schema, strict, eager_input_streaming) keyed by ${tool.name}:${jsonStringify(inputJSONSchema)} so mid-session GrowthBook flips (tengu_tool_pear, tengu_fgts) never mutate serialized schema bytes, and overlays defer_loading: true on every discovered deferred tool (willDefer(t) at claude.ts:1208-1209, which also defers LSP tools when shouldDeferLspTool(t) is true).
The request adds the provider-specific beta header (advanced-tool-use on 1P/Foundry; tool-search-tool on Vertex/Bedrock). On the server, Anthropic's API strips defer_loading: true definitions out of the top-of-prompt tool list and expands them inline only where a tool_reference content block appears inside a ToolSearch tool_result in message history.
That gives two simultaneous wins:
- Zero prefix mutation on discovery: Adding a newly discovered MCP tool to
filteredToolswithdefer_loading: truenever alters the cached top-of-prompt tool bytes.claude.ts:1212-1214even keeps global system-prompt caching active (needsToolBasedCacheMarker = false) whenever all MCP tools infilteredToolsare deferred, andPROMPT_CACHE_BREAK_DETECTION(claude.ts:1460-1467) excludesdefer_loading: trueschemas from its cache-break hash. - Compaction survival (
utils/toolSearch.ts:545-592): Because compaction summarizes away earliertool_resultblocks containingtool_references, services/compact/compact.ts and services/compact/sessionMemoryCompact.ts callextractDiscoveredToolNames()before summarizing and persist the set ontocompactMetadata.preCompactDiscoveredToolson thecompact_boundarysystem message.extractDiscoveredToolNames()reads that field back on subsequent turns so the model keeps calling previously discovered tools without re-searching.
3. Cache-Safe Announcements via deferred_tools_delta
How does the model know what names exist to search for?
- Legacy path (
!isDeferredToolsDeltaEnabled()): services/api/claude.ts:1330-1345 prepends an ephemeral<available-deferred-tools>user message at index 0 ofmessagesForAPI. Every time an MCP server connects or disconnects, index 0 changes and busts the entire conversation cache. - Delta attachment path (
USER_TYPE === 'ant'or GrowthBooktengu_glacier_2xr):getDeferredToolsDeltaAttachment()(utils/attachments.ts:1455-1475) callsgetDeferredToolsDelta()(utils/toolSearch.ts:646-706), which reconstructs the already-announced set by scanning priordeferred_tools_deltaattachments inmessagesand appends only newly added or disconnected tool names at the tail of the conversation as a<system-reminder>(utils/messages.ts:4178-4193). Earlier turns stay byte-identical and cache-hot.
Notice the exact twin mechanism for MCP server instructions in utils/mcpInstructionsDelta.ts (gated by USER_TYPE === 'ant', GrowthBook tengu_basalt_3kr, or CLAUDE_CODE_MCP_INSTR_DELTA): instead of rebuilding a DANGEROUS_uncachedSystemPromptSection every turn, mcp_instructions_delta appends ## <server>\n<instructions> blocks (plus client-side blocks like Chrome instructions) only when servers connect or disconnect.
4. ToolSearchTool Query Parsing, Ranking, and Pending-Server Hints
When the model invokes ToolSearch (tools/ToolSearchTool/ToolSearchTool.ts, isConcurrencySafe: true, isReadOnly: true), call() handles three query paths:
- Direct selection (
select:A,B,C, lines 363-406): Splits comma-separated names, searchingdeferredToolsfirst and falling back to already-loadedtools(selecting an already-loaded tool is a harmless no-op that avoids model retry churn). - Fast paths (
lines 199-216): An exact bare tool name (common from subagents or post-compaction) returns immediately; a query starting withmcp__performs prefix matching up tomax_results(default5). - Weighted keyword search (
lines 218-302): Splits terms into+required(pre-filtered so all required terms must match name parts,searchHint, or description) and optional terms, pre-compiles\b<term>\bregexes once per query, splits tool names (mcp__server__actionon__/_; built-ins on CamelCase/_), and scores each candidate:
// tools/ToolSearchTool/ToolSearchTool.ts:266-291 (abridged)
let score = 0
for (const term of allScoringTerms) {
const pattern = termPatterns.get(term)!
if (parsed.parts.includes(term)) {
score += parsed.isMcp ? 12 : 10 // exact name part match
} else if (parsed.parts.some(part => part.includes(term))) {
score += parsed.isMcp ? 6 : 5 // partial name part match
}
if (parsed.full.includes(term) && score === 0) {
score += 3 // full-name substring fallback
}
if (hintNormalized && pattern.test(hintNormalized)) {
score += 4 // _meta['anthropic/searchHint']
}
if (pattern.test(descNormalized)) {
score += 2 // memoized tool.prompt() match
}
}
mapToolResultToToolResultBlockParam() (ToolSearchTool.ts:444-470) returns { type: 'tool_reference', tool_name } blocks for every match—causing the Anthropic API to expand their full JSON schemas into the model's context on the very next turn. On zero matches, ToolSearchTool checks getAppState().mcp.clients for type === 'pending' servers and appends "Some MCP servers are still connecting: ..." so the model knows to retry rather than giving up.
Hidden Invariants & Failure Modes
- Mid-conversation model switch sanitization (
claude.ts:1283-1296). If the user switches from Sonnet/Opus to Haiku mid-session,useToolSearchflips tofalse;claude.tsstripstool_referenceblocks from prior user messages andcallerfields from assistanttool_useblocks so Haiku does not reject the history with HTTP 400. - Pending-server latch (
claude.ts:1139-1148). Even whendeferredToolNames.size === 0,claude.tskeepsToolSearchenabled ifoptions.hasPendingMcpServersis true—preventing turn 1 from strippingToolSearchright before a slow MCP server finishes handshaking. - Undeferral is silent (
utils/toolSearch.ts:671-675). If a previously deferred tool transitions to non-deferred while remaining in the pool,getDeferredToolsDelta()deliberately omits it fromremovedNamesso the model is never told a callable tool vanished. - Description cache invalidation (
ToolSearchTool.ts:55-100).getToolDescriptionMemoizedcachestool.prompt()per tool name and wipes itself whenever the sorted CSV of deferred tool names changes.
Putting the Layer Together
flowchart TD
Cfg["config.ts: 7 Scopes + Enterprise Policy + Signature Dedup"] --> Mgr["useManageMCPConnections.ts: 2-Phase Connect + 16ms Batch"]
Mgr --> Trans["client.ts: 8 Transports (stdio, HTTP, SSE, WS, InProcess, SDK, claude.ai)"]
Trans <--> Auth["auth.ts: ClaudeAuthProvider (PKCE, CIMD, Lockfile Refresh, 403 Step-Up, XAA)"]
Trans <--> Elicit["elicitationHandler.ts: Form/URL + Hooks + -32042 Retry"]
Trans --> Adapt["fetchToolsForClient(): MCPTool + mcp__server__tool + 2048c Cap"]
Adapt --> State["AppState.mcp (clients, tools, commands, resources)"]
State --> Pool["tools.ts: assembleToolPool() [sortedBuiltIns | sortedMcp]"]
Pool --> Gate["utils/toolSearch.ts: isDeferredTool() + extractDiscoveredToolNames()"]
Gate --> Wire["services/api/claude.ts: non-deferred + ToolSearch + discovered(defer_loading:true)"]
State --> Delta["utils/attachments.ts: deferred_tools_delta + mcp_instructions_delta"]
Delta --> Wire
At the prompt-cache boundary, every mechanism in this chapter aligns to keep the prefix byte-frozen:
+--------------------------------------------------------------------------+
| [Global Cache Prefix] Static System Prompt + Sorted Built-in Tools |
| (MCP tools excluded or marked defer_loading:true) |
+--------------------------------------------------------------------------+
| [Turn History...] User msgs + <system-reminder> deferred_tools_delta|
| + <system-reminder> mcp_instructions_delta |
| + ToolSearch tool_result [tool_reference blocks] |
+--------------------------------------------------------------------------+
^ Late MCP connect appends a tail delta -> 0% prefix cache invalidation
^ ToolSearch discovers mcp__slack__post -> defer_loading:true stays out
of top tool prefix
How This Snaps Into the Next Layer
MCP is only the first dynamic capability in Layer 5; Chapter 16 builds three sibling extension systems directly on top of the seams we just exposed:
- Plugins bundling MCP servers: utils/plugins/mcpPluginIntegration.ts (
getPluginMcpServers(),loadPluginMcpServers()) parses plugin.mcp.json, inline manifests, and.mcpbbundles, resolves${CLAUDE_PLUGIN_ROOT}and${user_config.X}variables, namespaces servers asplugin:<plugin>:<server>(scope: 'dynamic'), and feeds them directly intoservices/mcp/config.ts:1147-1154. - Zero-restart hot reload: Running
/reload-pluginscallsrefreshActivePlugins()(utils/plugins/refresh.ts:72-136), which clears plugin caches and incrementsAppState.mcp.pluginReconnectKey. That single counter re-firesuseManageMCPConnections.tsto runexcludeStalePluginClients(), tear down removed or edited plugin MCP servers cleanly, and connect newly enabled ones in the background. - Shared deferral & discovery hooks:
LSPTool(tools.ts:224, gated byENABLE_LSP_TOOL) plugs directly intoshouldDeferLspTool()in services/api/claude.ts:783 using the exact samedefer_loading: trueandToolSearchmachinery, while MCPresources/list_changednotifications already coordinate withfetchMcpSkillsForClient(feature('MCP_SKILLS')) andclearSkillIndexCache(feature('EXPERIMENTAL_SKILL_SEARCH')).
Reading Exercises & Code Checkpoints
- Trace a permission rule collision in no-prefix SDK mode.
Run
rg -n "getToolNameForPermissionCheck|CLAUDE_AGENT_SDK_MCP_NO_PREFIX" services/mcp/ utils/permissions/and trace what happens when an SDK MCP server namedcustomexposes a tool namedWritewhile the user has a deny ruleWritevs.mcp__custom. Why doestools.ts:filterToolsByDenyRules()automatically stay consistent with runtime permission checks? - Verify every MCP cache-invalidation site.
Run
rg -n "fetchToolsForClient\.cache|connectToServer\.cache|clearKeychainCache" services/mcp/and map each call site to the failure mode it prevents (broken stream, manual reconnect, cross-process OAuth refresh,tools/list_changed). - Inspect all
defer_loadingandtool_referenceescape hatches. Runrg -n "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS|defer_loading|preCompactDiscoveredTools" utils/ services/api/ services/compact/and verify how a tool discovered on turn 2 remains callable after turn 15 compaction and how the wire payload degrades safely if the user switches to Haiku or setsCLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1. - Compare
deferred_tools_deltavs.mcp_instructions_deltavs.agent_listing_delta. Read utils/toolSearch.ts:646-706, utils/mcpInstructionsDelta.ts:55-130, and utils/attachments.ts:1455-1510. Notice why all three use stateless history scanning over persisted attachment messages instead of an in-memorySet—what breaks on/resume, compaction, or subagent forks if you used a module-levelSetinstead?
Manuscript chapters/15-model-context-protocol-mcp.md from AtlantisPleb/cc at 5aa286a, synced Nov 24, 2026.