跪拜 Guibai
← Back to the summary

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

This assistant is not an island. It needs to connect to different companies' AI models, prove your identity, rescue itself when the network fails, connect to external tools and services, be commanded by editors, and even be remotely controlled by a phone. This chapter covers these six things.


7.1 Swapping Brains: Why It Can Use Models from Other Companies

Chapter 1 explained that the model doing the actual thinking runs on a remote server. The problem is: multiple companies offer models, and their "ways of speaking" (request formats, response formats) are all different. Why can this assistant switch to another company's model and still work?

Each Model Company Speaks a Different "Dialect"

The interfaces of different companies' model services vary considerably:

If the core loop were directly tied to one company's format, switching models would require rewriting the core loop—a disaster.

The Solution: Deploy Translators at the Boundary

The project's approach (in the independent small package @ant/model-provider) is: the core loop only recognizes one "common language" (the unified events discussed in Chapter 3: message_start / content blocks / deltas / message_stop), and then a translator (adapter) is written for each model company, responsible for translating the "common language" into that company's "dialect" when sending, and translating the returned "dialect" back into the "common language".

Core Loop ──speaks common language──► Translator ──speaks OpenAI dialect──► OpenAI's model
Core Loop ──speaks common language──► Translator ──speaks Gemini dialect──► Gemini's model
Core Loop ──speaks common language──► Translator ──speaks … dialect──► Other models

The translator does three specific things:

  1. Translate Input: Converts the internal message list and tool specifications into the format required by that model;
  2. Translate Output Stream: "Interprets" the streaming data returned by that model into the unified event stream—one says "choices[0].delta", another says "parts[].text", and after translation both become "content block deltas";
  3. Translate the Bill: Converts the different usage fields from each company into a unified calculation of "input tokens, output tokens, cache tokens".

The Power of This Pattern

This is the full realization of the design principle emphasized in Chapter 3: translate at the system boundary, keep the interior unified. The tool system, interface, and compression logic are all built on the foundation of "unified event format", and the foundation can be unified precisely because of this adapter layer.


7.2 The Login Matter: How to Prove "You Are You"

Using a model service requires proving your identity first (otherwise, who pays for this call?). The identity verification methods for different services are varied; this chapter sorts them out.

The Simplest: A Key

The most basic method is an API key—a long string of password-like characters. You apply for it on the service provider's website, and the program carries it with every request, equivalent to "saying the password to enter". It's simple and direct, but the downside is that the key must be kept safe; if leaked, others can spend your money.

More Convenient: Account Login (OAuth)

Another method is logging in with your account, similar to "logging into another app with your WeChat account":

1. The program says "login required", opens a browser and jumps to the service provider's login page
2. You log in in the browser and click "Authorize"
3. The service provider sends a "temporary pass" back to the program
   (via a small receiving port temporarily opened locally)
4. The program accesses the service with the pass from then on, without you needing to enter your password again
5. When the pass is about to expire, the program automatically exchanges it for a new one using a "renewal credential", without requiring you to log in again

The pass (access token) has a short lifespan (limited loss if leaked), while the renewal credential (refresh token) has a long lifespan and is stored securely. This is both safe and hassle-free.

Cloud Provider Verification Methods

If the model is deployed on major cloud platforms, the verification methods differ again: some use the cloud platform's own account system (automatically obtaining identity after logging into the cloud platform's command-line tool), others use an access key plus a complex signature algorithm (each request calculates a signature using the key, and the key itself is never sent out). These differences are similarly contained within their respective integration layers; users just log in following the cloud platform's usual method.

Where Credentials Are Stored

The storage of credentials (keys, passes) prioritizes security:

  1. First, store in the operating system's keychain (macOS Keychain, Windows Credential Manager)—this is system-level encrypted storage;
  2. If the system keychain is unavailable, fall back to encrypted file storage;
  3. Passes are automatically refreshed before expiry, transparent to the user.

Unified Login State

Regardless of the underlying method, the program internally maintains a unified "authentication state": who you are, which service you are using, whether the login is valid. Commands like /login and /logout operate on this state. Before any external request is sent, the integration layer automatically attaches the corresponding identity credential; the core loop does not need to care about these details.


7.3 What Happens When Errors Occur: Self-Rescue from Network Jitter, Rate Limiting, and Asking Too Much

Network requests will inevitably fail. A mature assistant cannot just crash on you at the first error; it has a whole set of self-rescue strategies.

First, Classify the Error Type

When the program receives an error, it first classifies it; different errors get different handling:

Error Plain English Response
Rate Limit (429) "You're asking too fast/too much, wait a moment" Back off and retry according to the wait time specified by the server
Server Busy (529/503) "I'm overwhelmed over here" Back off and retry
Network Timeout/Disconnect The network flickered Retry
Content Too Long "The data you stuffed into this question exceeds my capacity" Compress first, then retry
Authentication Failure "Who are you? Not recognized" Prompt to re-login
Model Persistent Errors This model isn't working right now Try switching to another model/tier
Illegal Parameters The request itself is problematic Don't retry, report the error directly to the user

Backoff Retry: Don't Hammer Relentlessly

When encountering "overwhelmed", you can't retry immediately (that adds fuel to the fire), nor can you wait foolishly. The strategy is exponential backoff: wait 1 second the first time, 2 seconds if it fails, 4 seconds if it fails again… gradually lengthening the interval. Moreover, the server usually explicitly tells you "please wait N seconds" (Retry-After) in the error, and the program strictly complies. There is an upper limit on retries; if exceeded, it gives up and informs the user.

Content Too Long: Auto-Compress Then Retry

If the error is "data exceeds capacity", the program does not fail directly but triggers the compression discussed in Chapter 5 (highlighting key points), freeing up space and then automatically re-asking the question. The user often only feels "it paused for a moment, then continued."

Model Fallback: If This One Doesn't Work, Switch to Another

There is a type of error indicating "the current model has a problem" (for example, the model is temporarily unavailable). The program can automatically switch to a backup model (fallback) to retry—for instance, if the primary model is busy, temporarily switch to another tier model to complete this round of requests. The switch will be indicated on the interface, and you can switch back afterward.

Tool Failure ≠ Conversation Failure

It's especially important to distinguish between two types of failures. Tool execution failure (e.g., command error, file not found) does not count as a system error—the failure information is returned normally to the model as a "tool result". Seeing the error, the model often adjusts itself ("Command doesn't exist? Then I'll try another command"). This is a normal part of the Agent loop and does not need to interrupt the user. Only when "the conversation itself cannot proceed" (network, authentication, model crash) are the above error handling procedures triggered.

Not Concealing Problems

Retries and fallbacks are silent, but when a final failure occurs, it is not hidden: the interface clearly displays the error reason, error code (for easier troubleshooting), and tries to give actionable suggestions ("Please re-login", "Please try again later"). A complete record is also left in the logs.


7.4 External Capabilities: How to Connect to GitHub, Databases, Internal Company Systems

Core tools (reading/writing files, running commands) are built-in, but a vast amount of capability lies externally: operating GitHub, querying databases, sending Slack messages, calling internal company systems… How are these connected? The answer is a standard called MCP (Model Context Protocol).

First, Understand the Problem: Why a Standard is Needed

There are hundreds or thousands of external services. If connecting each one required writing specialized code and specialized adaptations, both sides would suffer—the service side would need to integrate individually for each AI assistant, and the assistant side would need to adapt individually for each service.

MCP is a set of "socket standards" (this analogy was made in Chapter 1): it specifies a unified format for the conversation between "external capability providers" and "AI assistants". Any service that provides its capabilities according to this standard (called an MCP server), and any AI assistant that supports this standard (called an MCP client), can plug together and work, without挑剔挑剔 each other.

GitHub Server ┐
Database Server ├─ All speak MCP, this "common language" ──► Assistant (MCP Client)
Internal Company Service ┘                               Incorporates the tools they provide
                                                        into its own toolbox

What External Capabilities Can Provide

Not just tools; an external service can provide three things:

  1. Tools: Actions that can be executed ("create an issue", "execute an SQL query");
  2. Resources: Data that can be read ("this document", "the structure of this table");
  3. Prompt Templates: Preset common operation workflows.

The assistant incorporates external tools into the toolbox discussed in Chapter 4 (going through the same permission checks) and treats external resources as readable materials.

Three Connection Methods

Where Does Configuration Come From: Five Levels

Configuration for which external services to connect can come from five levels, stacked from lowest to highest priority:

Company admin unified push (read-only, employees cannot change)  ← Lowest
Machine-level configuration
Your personal configuration (global, effective for all your projects)
Project-level configuration (stored in the project, shared by the team, committed to git)
Project-level personal override (not committed to git, only effective for your local machine)  ← Highest

This allows an enterprise to control "employees can only use these services", a team to share "our project has connected these services", and an individual to temporarily add their own services, without conflicts.

External Services Also Need Login: Authorization Flow

Many external services require authorization (accessing your GitHub naturally requires your consent). MCP has a built-in standard authorization flow: when the assistant discovers a service requires login, it automatically opens a browser for you to authorize on that service's website. Once authorized, the pass is automatically stored and automatically renewed upon expiry—the same approach as the login mechanism in 7.2.

Security Checks

External tools go through the same permission checks as built-in tools (Chapter 4): if an external tool wants to delete something or send a message, it still asks you. Moreover, external tools are tagged with their source ("This tool is provided by the GitHub service"), so you can clearly distinguish which actions come from built-in capabilities and which from external ones. Admins can also set a "whitelist", only allowing specified services to start.

The Assistant Itself Can Also Be a "Server"

Conversely, the assistant can also package its own capabilities as an MCP server to provide externally, allowing other AI tools to call it. Architecturally, this means "the same set of capabilities can both consume external services as a client and be consumed by others as a server."


7.5 Letting the Editor Command It: How Other Software Talks to It

Chapter 1 mentioned the third form: editors (Zed, Cursor, etc.) can drive this assistant as a backend. This chapter covers the communication rules between them.

The Problem Scenario

Editor vendors want to integrate AI assistant capabilities but don't want to build their own; the assistant wants to be used by editors, but there are many editors. Both sides need a set of communication protocols—similar to how MCP is the standard for "connecting external capabilities", this set is the standard for "Editor ↔ AI Assistant", called ACP (Agent Client Protocol).

How the Conversation Proceeds

The assistant starts in a special mode (no interface, no terminal UI displayed), and then communicates with the editor via "message passing". Messages have a fixed format, similar to this question-and-answer pattern:

Editor: Initialize—I am an editor, I can read/write files, I can open a terminal
Assistant: OK, I am AI Assistant, version X, supporting these capabilities

Editor: New session, working directory is /path/to/project
Assistant: Session created

Editor: The user said this: "Help me refactor this function"
Assistant: (Starts working, continuously reporting progress)
  ├─ I'm thinking… (thought content, streaming)
  ├─ I want to invoke tool: read file xxx
  ├─ I updated the plan: Step 1 in progress…
  └─ I want to execute "run command npm install", need your approval ← Permission request
Editor: (Pops up a dialog asking the user) User approved
Assistant: (Continues) … Done, changes are as follows

Key Point: The Assistant No Longer Draws an Interface, It "Reports Status" Instead

In terminal mode, the assistant draws the interface itself using the techniques from Chapter 6. In ACP mode, the assistant does not draw any interface but reports "what is happening now" to the editor via messages: thinking, what tool is being invoked, where the plan progress is, what needs user approval. The editor takes these statuses and draws them in its own interface style (each editor looks different).

This is why Chapter 1 said "the editor is responsible for the interface, the assistant is responsible for thinking and doing the work."

How Permission Requests Are Handled

When the assistant needs to perform a dangerous operation, it doesn't pop up its own dialog (it has no interface to pop up) but sends a "permission request" message to the editor: "I want to do X, options are: Allow this time / Always allow / Deny this time / Always deny". The editor is responsible for popping up a dialog to the user and then passing the user's choice back. The assistant continues after receiving the choice—the permission model is completely consistent with terminal mode, only the action of "asking the user" is performed by the editor on its behalf.

Visualization of Plan Progress

The assistant's todo plan (TodoWrite from Chapter 4) is also reported via messages: "Plan has 5 steps, Step 2 in progress". The editor renders its own progress bar based on this. The same plan data is drawn in the terminal in terminal mode, and handed to the editor to draw in ACP mode.

An Independent "Relay" Small Program

Sometimes there is a relay program (acp-link) between the editor and the assistant: the editor connects to the relay program, and the relay program is responsible for starting and managing individual assistant processes and forwarding messages. The advantage is that the editor doesn't need to care about how the assistant is installed or started, and the relay program can manage multiple sessions simultaneously. This is also why the assistant needs to support a "no-interface, pure message" mode—it can be driven by any relay layer, any editor.

Difference from the SDK Form

Both run the same engine (the core loop) underneath.


7.6 Remote Control by Phone: How to Make the Assistant on Your Home Computer Work When You're Out

The final form: a person is outside, using their phone to command the assistant running on their home/office computer.

Why You Can't "Just Run One Directly on the Phone"

The assistant works relying on the environment on your computer: your code, your files, your terminal, your login state. Installing an assistant on a phone, it can't touch the things on your computer. So the correct architecture is: the assistant runs on your computer, and the phone is just a "remote control + display".

Phone (Remote Control/Display)
   │   Network
   ▼
Relay Service (helps connect, because your computer usually doesn't have a public network address)
   │
   ▼
Assistant on Your Computer (the place where the real work happens)

How to Establish the Connection: Scan to Pair

After you start the "remote control" mode at home, a QR code appears on the computer screen. The phone app scans the code to complete pairing. The QR code contains information like the connection address, session identifier, and a temporary public key; the phone and computer establish trust through an encrypted handshake—preventing impersonation.

After successful pairing, you can see the same conversation interface on the phone as in the terminal. You send a message, the message travels all the way back to the assistant at home for execution, and the results are pushed back to the phone in real time.

What You Can Do on the Phone

Multiple Devices Watching Simultaneously

Supports multiple devices connected to one session at the same time (phone + tablet + computer screen). State is synchronized across devices: if the model is switched on the computer, the phone also displays the switch synchronously. Permission dialogs are intelligently pushed to the most appropriate device.

A Few Engineering Details

Commonality with Editor Mode

Note the architectural commonality with 7.5: the assistant kernel only cares about "doing work + reporting status"; the terminal interface, editor interface, and phone interface are all "frontends" connected externally. The kernel doesn't care who the results are displayed to or who the permission dialogs are popped up for—this is precisely the fundamental reason why the same engine can drive so many forms.


Chapter Summary

The next chapter covers data: how state is stored in the program, how conversations are retrieved, how to roll back when something goes wrong, and how billing is calculated.