MCP Client — Architecture & Internals
Audience: developers and AI agents working on Book’s MCP client implementation or extending it. Covers module structure, the
McpClientFacadeAPI, 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 |
|---|---|
|
JSON-RPC framing over stdio with integer request ids (some servers reject the SDK’s default string ids) |
|
Wraps an MCP tool as a domain |
|
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):
-
toolRegistryis a call-time parameter, not constructor state — registries are thread-safe and session-contextual, so connect/disconnect never hold a stale reference. -
connectmerges per-server tool lists into one registry; duplicate tool names are resolved by namespacing (below), not by last-write-wins. -
disconnectis symmetric: every tool registered byconnectis 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 (
ToolSourceisMCP, 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 |
|---|---|
|
Tool is treated as pure-eligible: results may be served from |
|
Relaxes |
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
-
Config load:
~/.bk/mcp.jsonmerged with project.bk/mcp.json(user config wins; full merge semantics in the user guide) -
PromptExecutorFactory/DI buildsMcpClientFacade(@SingleIn(AppScope)— see DI & Wiring) -
connect(servers, registry)walks servers in file order, performing the initialize handshake per server -
Each discovered tool is adapted (
McpToolAdapter) and registered namespaced -
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--disconnectflag;disconnect(toolRegistry)runs in afinallyon the shutdown path -
ACP mode: MCP clients are torn down with the owning session (
session/end), since registries are per-session -
Config changes (
mcp.jsonedits) require a restart; Book watches nothing (see troubleshooting)
Ring Boundary
:features:mcp sits in the outer ring (module dependencies):
-
May import from:
core/domain(onlyTool,ToolRegistry,ExternalToolServer) -
Must never be imported by:
core/*;:jvm:cliand:features:acpdepend on theMcpClientFacadeinterface only -
The MCP SDK is confined to this module — no other module may add an MCP import