跪拜 Guibai
← All articles
JavaScript · AI Programming

How Claude Code Connects to the Outside World: Models, Auth, MCP, Editors, and Remote Control

By 小磊哥er ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

The adapter pattern means a team can switch model providers or add a new one without touching application logic—critical when costs, latency, or regional availability change. MCP and ACP turn the assistant into a composable component that plugs into existing editor workflows and internal toolchains, not a standalone app that demands its own silo.

Summary

The core agent loop only understands one internal event format. Model-provider adapters translate that common language into each vendor's proprietary API dialect—OpenAI, Gemini, or otherwise—and translate streaming responses, tool calls, and billing data back. Adding a new model means writing one adapter; the core, UI, and tool system never change. Authentication spans API keys, OAuth flows with automatic token refresh, and cloud-provider signatures, all unified behind a single auth state that the core loop never sees. Credentials land in the OS keychain first, falling back to encrypted files.

External tools, databases, and services connect through MCP, a socket standard that lets any compliant server offer tools, resources, and prompt templates to any compliant client. Configuration stacks across five levels—from company-wide admin policy down to per-project personal overrides—and external tools pass through the same permission gate as built-in ones. Editors drive the assistant via ACP, where the agent runs headless, reporting thinking streams, plan progress, and permission requests as structured messages; the editor owns all UI rendering. Remote control works the same way: the phone is a display and remote, the computer does the work, connected through a relay with QR-code pairing, encrypted channels, message throttling, and sleep prevention.

Takeaways
Model-provider adapters translate a single internal event format into each vendor's API dialect, so the core loop never changes when models are swapped.
Adapters handle three translations: input message formatting, streaming output normalization, and usage-billing unification.
Authentication supports API keys, OAuth with automatic token refresh via refresh tokens, and cloud-provider signature schemes.
Credentials are stored in the OS keychain first, with encrypted file fallback; tokens refresh transparently before expiry.
Error handling classifies failures first: rate limits and server overload trigger exponential backoff with Retry-After compliance, content-too-long errors trigger automatic context compression and retry, and persistent model errors trigger fallback to an alternative model.
Tool execution failures are not system errors—they are returned as normal tool results so the model can self-correct.
MCP standardizes external capability integration: servers provide tools, resources, and prompt templates; clients consume them through local processes, persistent remote connections, or web-style requests.
MCP configuration merges five priority levels: company admin policy, machine-level, personal global, project-level (committed), and project-level personal override (not committed).
External tools pass through the same permission gate as built-in tools and carry source labels so users can distinguish them.
ACP lets editors drive the assistant headlessly: the agent reports thinking streams, tool invocations, plan progress, and permission requests as structured messages; the editor renders all UI.
Remote control pairs via QR code with an encrypted handshake, streams throttled output to the phone, pushes permission dialogs to mobile, and prevents the host computer from sleeping during sessions.
Conclusions

The adapter pattern is not just about model portability—it quarantines compatibility quirks so they never leak into the core loop, tool system, or UI.

Treating tool failures as normal agent-loop feedback rather than system errors is a design choice that keeps conversations resilient; the model often self-corrects without user intervention.

MCP and ACP share the same architectural instinct: the assistant kernel is a headless engine that reports state, and every frontend—terminal, editor, phone—is an interchangeable rendering layer.

The five-level MCP config stack solves a real enterprise tension: IT can lock down which services are allowed, teams can share project defaults, and individuals can still add personal tools without breaking policy.

Concepts & terms
Model-Provider Adapter
A translator module that converts the assistant's single internal event format (message_start, content blocks, deltas, message_stop) into a specific model vendor's proprietary API format for requests, and normalizes the vendor's streaming response back into the internal format.
MCP (Model Context Protocol)
A socket-like standard that defines how external capability providers (servers) and AI assistants (clients) communicate. Servers expose tools, resources, and prompt templates; clients consume them through local processes, remote connections, or web requests.
ACP (Agent Client Protocol)
A message-based protocol that lets editors drive an AI assistant as a headless backend. The assistant reports thinking streams, tool invocations, plan progress, and permission requests as structured messages; the editor owns all UI rendering and user interaction.
Exponential Backoff
A retry strategy where wait times double after each failure (1s, 2s, 4s, 8s…), often combined with a server-supplied Retry-After header, to avoid overwhelming an already overloaded service.
OAuth Token Refresh
A flow where a short-lived access token is used for API calls, and a long-lived refresh token is stored securely to obtain new access tokens automatically before expiry, eliminating repeated manual logins.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗