Add a minimal Model Context Protocol integration that lets configured MCP servers expose tools to the agent loop. The first version is intentionally narrow:
stdio, http, and sse.mcp__<server>__<tool>./mcp shows server status and can connect or disconnect a configured server.Prompts, resources, OAuth, server tool-list change notifications, and MCP server hosting are out of scope for this first version.
MCP crosses process and network boundaries, so it is an adapter concern.
contracts: defines MCP config, status, tool descriptors, call results, and the McpPort interface.adapters: owns the MCP SDK clients and transport implementations.core: converts McpToolDescriptor values into normal ToolEntry registrations. Core never imports the MCP SDK.bootstrap: wires config to the adapter and passes the McpPort to runtime.cli: exposes /mcp as a command-center command.contextbuilder: does not call MCP directly. It sees MCP tools only through the normal ToolRegistry and context usage accounting.Config file shape:
{
"features": {
"mcp": true
},
"mcp": {
"servers": {
"filesystem": {
"type": "stdio",
"command": "node",
"args": ["server.js"],
"cwd": ".",
"env": {
"EXAMPLE": "value"
},
"timeoutMs": 30000
},
"remote": {
"type": "http",
"url": "https://example.com/mcp",
"headers": {
"Authorization": "Bearer token"
},
"timeoutMs": 30000
}
}
}
}
Plugin-provided MCP servers are specified by
plugin-compat.md. They are normalized into the same
mcp.servers config shape before runtime startup. Manual user/project MCP
servers win on server-name collisions. Plugin server configs may use Claude
Code's .mcp.json wrapper shape and may omit type when a command is
present; ZCode infers stdio in that case.
features.mcp defaults to true, but no server is connected unless mcp.servers is configured. Individual servers may set enabled: false.
MCP client initialize 的 clientInfo.version 必须使用和 CLI 相同的 app version 来源,即仓库根 package.json 注入的版本值。源码测试或未注入 bundle 的直接运行环境可以回退到 0.0.0,但不能读取 packages/adapters/package.json 之类的 workspace 子包版本。
MCP 协议版本协商必须服从 server 的连接预算:
auto 模式允许 modern probe 失败后回退 legacy,因此 probe 继续使用 min(5000ms, timeoutMs / 2),必须给 legacy initialize 留出预算。timeoutMs,不能再套通用 5 秒上限。否则冷启动超过 5 秒的合法 server 会被静默移出工具池,即使它已声明更长的连接预算。配置模型的基本形态:
mcp 是普通 config section,server name 是 map key,后加载的 config 覆盖同名 server。ZCode 采用“config 先合并,MCP 后连接”的大方向,但 project MCP 有额外安全边界:project config 可能是仓库内容,不能让进入目录本身就静默启动本地进程或发起网络连接。
ZCode CLI/TUI/headless prompt 应自动读取 project-level config,使仓库可以声明自己的 MCP servers。典型形态:
{
"$schema": "https://zcode.dev/config.json",
"mcp": {
"servers": {
"repo-docs": {
"type": "stdio",
"command": "node",
"args": ["scripts/mcp-docs.js"],
"cwd": ".",
"timeoutMs": 30000
},
"issue-tracker": {
"type": "http",
"url": "https://example.com/mcp",
"enabled": false
}
}
}
}
The first shipped slice should keep the existing global config path unchanged:
~/.zcode/cli/config.jsonprojectConfigPath option, mostly for tests/embeddingAdd automatic project discovery for local CLI/TUI/headless sessions:
workingDirectory, find the nearest worktree root by walking upward until a .git marker.workingDirectory.Candidate project files, per directory, in increasing precedence:
<dir>/zcode.json<dir>/.zcode/config.jsonJSONC support is desirable because users expect comments in config files, but ZCode's current config adapter is JSON-only. Treat zcode.jsonc / .zcode/config.jsonc as Phase 2 unless the same implementation slice adds a JSONC parser and schema diagnostics.
Effective config order should be:
~/.zcode/cli/config.jsonZCODE_* environment configThis preserves the current rule that env and CLI/session overrides win. It also means project config can override user config for normal preference fields.
Security-sensitive project MCP behavior needs stricter handling than a plain deep merge:
features.mcp is disabled by env or CLI/session override, project config cannot re-enable it.cwd for project-origin MCP servers resolves relative to the project config base directory, not the shell's current directory. For <dir>/zcode.json, base is <dir>; for <dir>/.zcode/config.json, base is <dir>.mcp.servers entries deep-merge by source order. A project can disable an inherited server with enabled: false when the full server type is known. A later slice may add tombstones such as "server": false or { "enabled": false } without repeating type.Connecting an MCP server is itself a side effect: stdio starts a process, and http / sse can make network requests during connect and tool listing. Project config therefore needs a trust gate before auto-connect.
P0 policy:
/mcp list, but are marked untrusted and excluded from startup auto-connect./mcp connect <server> user command may connect a configured project server for the current process.This trust gate is separate from normal MCP tool-call permission. Tool calls still go through mcp permission even after a server is connected.
ZCode app-server sessions are protocol-controlled and already accept mcpServers during session setup. Keep the current ZCode app-server rule:
mcpServers is the complete server set for that ZCode app-server session.mcpServers means no MCP servers for that session.This prevents a generic ZCode protocol client from accidentally enabling repository-local MCP servers.
Config discovery
path APIs and worktree-root detection; no shell command is required for P0.projectConfigPath as an explicit override/test hook.Config provenance
cwd relative to the project config base directory.RuntimeConfig shape stable unless source metadata must cross module boundaries.MCP trust gate
trusted, untrusted, disabled, and configured./mcp list should show configured-but-untrusted project servers./mcp connect <server> as the user action; a later slice should add persistent trust decisions before connecting.Runtime wiring
connectConfiguredServers.CLI/TUI/ZCode app-server surfaces
Minimum coverage for the first implementation slice:
zcode.json and .zcode/config.json from cwd upward to worktree root, root-to-cwd.mcp.servers merges with user config by server key and later project config wins.cwd in project config resolves against the config base directory.features.mcp: false prevents project config from enabling MCP.mcpServers remains a complete override and does not merge project config.Each MCP tool is projected as a normal tool:
name: mcp__<normalized-server>__<normalized-tool>.inputSchema: MCP inputSchema, forced to an object schema if absent.outputSchema: a stable wrapper containing content, optional structuredContent, optional _meta, and optional isError.sideEffectScope: defaults to network.needsApproval: defaults to true.readOnly: follows MCP annotations.readOnlyHint.destructive: follows MCP annotations.destructiveHint.timeout: defaults to the server config timeoutMs when present, otherwise the MCP default. Long-running
local MCP servers such as simulator/build automation must not be forced into a generic short tool timeout.This keeps unknown remote behavior visible to permission review. A later version may support project-level allowlists for specific MCP tools.
MCP result content uses the same provider-neutral model content path as built-in tools:
text blocks stay model-visible text.image blocks with base64 data and mimeType are projected as model-visible image content blocks, so screenshots from MCP servers can be inspected by vision-capable models.structuredContent is appended as bounded text after content blocks./mcp and /mcp list return configured server status:
MCP servers:
- filesystem: connected (2 tools)
- remote: failed (Connection timed out)
- repo-docs: untrusted (Project MCP server requires explicit connection before use.)
/mcp connect <server> reconnects one configured server. For project-origin MCP servers in P0, this explicit user command is the connection approval for the current process; persistent project trust is a later slice.
/mcp disconnect <server> closes one connected server and removes its current tool registrations from future sessions. Dynamic unregistering from the already-built context is not required in the first version.
The TUI right sidebar shows the same configured MCP server status as /mcp list
through the app-level listMcpServers() interface:
/mcp connect <server> and /mcp disconnect <server> remain the command
surface for state changes; the sidebar only reflects the resulting status.failed; other servers may still connect.Minimum coverage:
stdio, http, and sse server shapes.mcp__server__tool names and executes through McpPort./mcp list formats status without submitting a model prompt.