Audience: developers and AI agents working on Book’s MCP client implementation or extending it. Covers module structure, the McpClientFacade API, namespacing, purity hints, and the startup/registration pipeline.

Just want to configure MCP servers? See the user-facing guide: MCP Configuration (~/.bk/mcp.json, --mode mcp, troubleshooting).


Diagrams

  • 03-seq-startup-acp-mcp-mode.puml — Startup Flow (ACP + MCP mode)

  • 05-seq-mcp-tool-registration.puml — Tool Registration

Full catalogue: docs/diagrams/README.md (repository file; diagram pages are not published to the site).

Module Structure

The MCP client lives in the :features:mcp Gradle module. The sole public interface is McpClientFacade — :cli and :features:acp may only import this interface and the types it references. Everything else in the module is internal.

Internal component Role

transport/IntegerIdTransport

JSON-RPC framing over stdio with integer request ids (some servers reject the SDK’s default string ids)

tools/McpToolAdapter

Wraps an MCP tool as a domain Tool; records calls with ToolSource.MCP

RepeatCallDetector integration

MCP tools pass through the same loop guards as built-ins (see Agent Loop — Guards)

McpClientFacade API

interface McpClientFacade {
    /** Connect to all configured external tool servers and register their tools. */
    suspend fun connect(servers: List<ExternalToolServer>, toolRegistry: ToolRegistry)

    /** Disconnect all active MCP clients and deregister their tools. */
    suspend fun disconnect(toolRegistry: ToolRegistry)

    /** Snapshot of currently registered MCP-backed tool names. */
    fun registeredTools(): List<Tool>
}

Key design points (ADR §4.3):

  • toolRegistry is a call-time parameter, not constructor state — registries are thread-safe and session-contextual, so connect/disconnect never hold a stale reference.

  • connect merges per-server tool lists into one registry; duplicate tool names are resolved by namespacing (below), not by last-write-wins.

  • disconnect is symmetric: every tool registered by connect is deregistered, leaving built-in and extension tools untouched.

Tool Namespacing

MCP tools are registered under mcp<server><tool>:

  • Prevents collisions between servers exposing the same tool name and between MCP and built-in tools (ToolSource is MCP, so guards treat them distinctly)

  • The agent sees the namespaced name in its tool list, so model-facing prompts stay unambiguous

  • Session JSONL logs record the namespaced name (ADR-20261002 verbatim-string rules apply to tool names as well)

Purity Hints

McpToolAdapter reads the MCP tool’s annotations and maps them onto loop-guard behaviour:

MCP annotation Effect in Book

readOnlyHint

Tool is treated as pure-eligible: results may be served from PureToolResultCache

idempotentHint

Relaxes RepeatCallDetector sensitivity for identical argument re-calls

Hints are advisory: a server that lies about purity only degrades cache quality, never correctness (results are still validated by the guard layers — see Agent Loop for the four cooperative guard layers).

Startup & Registration Flow

  1. Config load: ~/.bk/mcp.json merged with project .bk/mcp.json (user config wins; full merge semantics in the user guide)

  2. PromptExecutorFactory/DI builds McpClientFacade (@SingleIn(AppScope) — see DI & Wiring)

  3. connect(servers, registry) walks servers in file order, performing the initialize handshake per server

  4. Each discovered tool is adapted (McpToolAdapter) and registered namespaced

  5. Registration completes before the first agent turn in every mode (TUI, headless, ACP)

Failure isolation: one broken server logs an error and is skipped — remaining servers still connect. A server that connects but exposes zero tools is reported at INFO.

Disconnecting

  • --mode mcp: disconnect happens automatically on process exit — there is no --disconnect flag; disconnect(toolRegistry) runs in a finally on the shutdown path

  • ACP mode: MCP clients are torn down with the owning session (session/end), since registries are per-session

  • Config changes (mcp.json edits) require a restart; Book watches nothing (see troubleshooting)

Ring Boundary

:features:mcp sits in the outer ring (module dependencies):

  • May import from: core/domain (only Tool, ToolRegistry, ExternalToolServer)

  • Must never be imported by: core/*; :jvm:cli and :features:acp depend on the McpClientFacade interface only

  • The MCP SDK is confined to this module — no other module may add an MCP import