什麼是 MCP?
MCP (Model Context Protocol) is a standard protocol for connecting AI agents to external tools, data sources, and services. Rather than hard-coding integrations, MCP provides a uniform interface that allows any compliant server to expose tools that the claw code agent can discover and invoke at runtime. This makes the agent's capabilities extensible without modifying the core codebase.
6 種傳輸類型
The claw code MCP integration supports six transport types, defined in the McpTransport enum in config.rs. Each transport maps to a specific McpServerConfig variant:
- Stdio — launches a local process and communicates via stdin/stdout. Configured with
McpStdioServerConfig:command(the executable),args(command-line arguments), andenv(environment variables). - SSE (Server-Sent Events) — connects to a remote HTTP endpoint using server-sent events for streaming. Configured with
McpRemoteServerConfig:url,headers,headers_helper, and optionaloauth. - HTTP — standard HTTP request/response communication. Uses the same
McpRemoteServerConfigas SSE. - WebSocket (Ws) — bidirectional communication over WebSocket. Uses
McpRemoteServerConfig. - SDK — in-process integration using the MCP SDK directly, without network transport.
- ClaudeAiProxy — a proxy transport for claude.ai-hosted MCP servers.
The McpStdioServerConfig is the most common transport for local tools — it spawns a subprocess and pipes JSON messages through stdin/stdout. The remote transports (Sse, Http, Ws) share the McpRemoteServerConfig structure, which includes URL, custom headers, and optional OAuth configuration.
OAuth 配置
Remote MCP servers can require OAuth authentication. The McpOAuthConfig struct captures the necessary parameters:
The client transport layer wraps this in McpClientAuth, which is either None or OAuth(McpOAuthConfig). The requires_user_auth() method checks whether the OAuth configuration requires an interactive browser-based authorization flow.
工具命名慣例
MCP tools follow strict naming conventions to avoid collisions with built-in tools and between different MCP servers:
- normalize_name_for_mcp — replaces all non-alphanumeric characters with underscores and collapses consecutive underscores. This produces clean, identifier-safe names.
- mcp_tool_prefix — returns
mcp__{normalized}__wherenormalizedis the server name after normalization. The double underscores create a clear namespace boundary. - mcp_tool_name — returns the fully qualified tool name by combining the prefix with the tool's own name.
For claude.ai-hosted servers, the prefix constant CLAUDEAI_SERVER_PREFIX is set to "claude.ai " (note the trailing space). This distinguishes claude.ai MCP servers from user-configured ones in tool listings and logs.
命名範例
A server named "my-database" with a tool called "query" would produce the normalized name my_database, the prefix mcp__my_database__, and the fully qualified tool name mcp__my_database__query.
伺服器簽名和雜湊
Each MCP server is assigned a deterministic signature via mcp_server_signature. This signature is used for deduplication — if the same server is configured in multiple settings files, the signature ensures it is only connected once.
Configuration hashing uses the scoped_mcp_config_hash function, which implements FNV-1a (Fowler-Noll-Vo) hashing with the standard 64-bit offset basis of 0xcbf29ce484222325. FNV-1a was chosen for its simplicity, speed, and good distribution properties — it produces consistent hashes without requiring a cryptographic hash function.
McpClientBootstrap
When the claw code harness starts, each configured MCP server is bootstrapped through the McpClientBootstrap struct:
The from_scoped_config constructor builds the bootstrap from a settings entry, normalizing the name, computing the prefix and signature, and resolving the transport type. The McpClientTransport enum mirrors the server-side McpTransport with six variants: Stdio, Sse, Http, WebSocket, Sdk, and ClaudeAiProxy.
CCR 代理處理
The claw code MCP integration includes special handling for CCR (Claude Code Runtime) proxy URLs. The unwrap_ccr_proxy_url function extracts the real URL from proxy wrappers, using CCR_PROXY_PATH_MARKERS to identify and strip proxy path segments. This allows the system to connect to the actual MCP server endpoint even when the URL has been wrapped by an intermediary proxy layer.
配置探索
MCP server configurations are discovered from three sources, merged in order of increasing specificity:
- User config —
~/.claude/settings.json— global MCP servers available in all projects - Project config —
.claude/settings.json— project-specific servers checked into version control - Local config —
.claude/settings.local.json— local overrides not checked into version control
The three sources are merged using a deep merge strategy. Local config overrides project config, which overrides user config. This allows teams to define shared MCP servers in the project config while individual developers can add personal servers or override connection parameters in the local config.
配置優先順序
The deep merge means that if a server with the same name appears in multiple config sources, the most specific one wins. Server signatures ensure that duplicate entries (same server configured at multiple levels) are connected only once.