MCP Connection, Discovery & Recovery
Confirming the client role; deconstructing OAuth, tool naming, capability discovery, state merging, and reconnection
THE QUESTION THIS PAGE ANSWERS
ANSWER FIRSTWhat is the key idea behind “MCP Connection, Discovery & Recovery”?
Confirming the client role; deconstructing OAuth, tool naming, capability discovery, state merging, and reconnection
Make the claim earn its place. Use this page as a decision aid, not a definition to memorize. Connect the idea to one real task, one observable result, and one failure that would change your mind.
Write one question you could answer with evidence after trying this idea.
A conclusion that sounds complete but leaves the key assumption untested.
MCP: Connection Is Just the Beginning
A production-grade client also handles config merging, OAuth, capability discovery, namespace isolation, model visibility control, state push, and connection recovery. The source code distributes these responsibilities across the MCP crate and Session Actor.
Learning Objectives
Clarify Protocol Roles
Determine client vs. server from the call direction, and avoid conflating the internal Hub Server with an MCP Server.
Trace Visibility
Explain how tools flow from tools/list into the snapshot, search index, and model registry.
Design a Recovery State Machine
Place OAuth, state coalescing, client identity, and restart back-off within a single connection lifecycle.
From External Server to Model Tool
Client vs. Server: Pinpointing Roles in the Source Code
McpClient initiates a stdio or Streamable HTTP connection and performs initialization, list_tools, and call_tool. The Computer Hub MCP Adapter is also described as bridging MCP Server tools into the Hub routing layer.
The Hub Server in xai-grok-workspace belongs to the xAI Computer Hub protocol. The current snapshot contains no entry point exposing Grok Build itself to arbitrary MCP clients via MCP transport, so this lesson confirms only the client role.
OAuth & Where Credentials Actually Live
Config Fields
oauth_client_id
oauth_client_secret_env_var
oauth_scopescrates/codegen/xai-grok-config-types/src/mcp.rsLocal JSON File
let path = grok_home
.join("mcp_credentials.json");
// lock + load + insert + atomic saveThe source stores credentials in this file, handling concurrent writes with file locks and atomic saves.
crates/codegen/xai-grok-mcp/src/credentials.rs · oauth.rsHow Tools Become Visible to the Model
server__tool
The registration name consists of the server name, the reserved separator __, and the original tool name. The source requires exactly one separator occurrence in the full name to avoid parsing ambiguity and ensures that same-named tools from two servers have distinct ToolIds.
Model Tools vs. App Tools
Disabled tools are stored in disabled_tool_registrations; only tools where model_visible is true enter the model-side Tool Bridge; tools with ui.resourceUri can be routed to UI notifications independently.
Large MCP Tool Sets Don't Need to Live in the Prompt Permanently
ToolMetadataSnapshot stores tool and server metadata. The BM25 index supports exact hits by qualified name or bare tool name before returning search results. mcp_initialized signals the search layer when capability discovery is complete.
pub struct ToolMetadataSnapshot {
pub tools: Vec<ToolMetadata>,
pub servers: Vec<ServerMetadata>,
pub mcp_initialized: bool,
}crates/codegen/xai-grok-shell/src/session/tool_index.rsState Coalescing & Restart Protection
Last-Write-Wins Per Key
mcp_dispatcher keys events on (server_name, event_kind) and applies last-write-wins within a 50 ms tumbling window. High-frequency tools/list_changed events ultimately push only a single ACP state update.
Stale Disconnects Can't Kill the New Connection
Before removing a dead client, the code compares client_id. If the disconnect event belongs to an already-replaced old client, the current client is preserved and the stale state is discarded.
Different Transports Use Different Recovery Actions
stdio auto-restart uses a fixed back-off of 1s → 4s → 16s and checks guards for shutting down, disabled, and config-removed states. HTTP first attempts in-client recovery with its own back-off. After a successful reconnect, tools are re-discovered and re-registered, then the snapshot is refreshed.
Lab Exercise: Design a Recoverable Client
Deliverables
State diagram + 6 test cases
- Draw a state diagram covering config loading, connection, OAuth, capability discovery, registration, search, and invocation.
- Add tool paths for disabled, app-only, and model-visible tools.
- Design two tools with identical names; verify that qualified names resolve the conflict.
- Simulate 100
tools/list_changedevents and write out the expected notification count after 50 ms coalescing. - Simulate a stale disconnect event arriving late; explain how the
client_idguard protects the new connection. - Write one recoverable test and one stop-retry condition each for stdio and HTTP.
The engineering effort in MCP integration concentrates at the protocol periphery. Naming, visibility, identity, state coalescing, and recovery strategy together determine whether a connection stays reliably operational over time.
Source Snapshot Note: This page is compiled from the local grok-build-main source code covering MCP, config-types, shell session, and computer-hub adapter. Code excerpts are for educational purposes. Conclusions about the MCP server role are stated conservatively; the internal Hub Server is not treated as evidence of a general-purpose MCP server.
How “MCP: Connection Is Just the Beginning” changes an answer
“A production-grade client also handles config merging, OAuth, capability discovery, namespace isolation, model visibility control, state push, and connection recovery.” shows that a model does not process the “word count” we see. It processes Token pieces. Tokenization affects input length, how much context fits, and how much computation a request consumes.
Length, information, and context are different
As “Determine client vs.” grows, separate three questions: how many Tokens the text becomes, which pieces can change the current decision, and whether older material has fallen outside the context window. Removing repetition is often more useful than simply making the window larger.
- Draw a state diagram covering config loading, connection, OAuth, capability discovery, registration, search, and invocation
- Add tool paths for disabled, app-only, and model-visible tools
- Design two tools with identical names; verify that qualified names resolve the conflict
Keep what can change the decision
Use “The engineering effort in MCP integration concentrates at the protocol periphery.” as an A/B test: keep the same question while removing repeated background, compressing format, and trimming irrelevant history. Compare answer quality, latency, and Token count.
From “MCP: Connection Is Just the Beginning” to “Clarify Protocol Roles”
“MCP: Connection Is Just the Beginning” grounds the problem in “A production-grade client also handles config merging, OAuth, capability discovery, namespace isolation, model visibility control, state push, and connection recovery. The source code distributes these responsi…”. “Clarify Protocol Roles” then moves it toward “Determine client vs. server from the call direction, and avoid conflating the internal Hub Server with an MCP Server”. Together, they show that the lesson is not just a conclusion to remember, but a claim with conditions.
Carry the judgment into the next situation
For long text, keep what can change the conclusion before compressing format and history. A larger context is worth its cost only when the added information is useful.
- “MCP: Connection Is Just the Beginning”: A production-grade client also handles config merging, OAuth, capability discovery, namespace isolation, model visibility control, state push, and connection recovery. The source code distributes these responsi…
- “Clarify Protocol Roles”: Determine client vs. server from the call direction, and avoid conflating the internal Hub Server with an MCP Server
- “The closing point”: Simulate a stale disconnect event arriving late; explain how the client_id guard protects the new connection
The final “The closing point” brings the discussion to “Simulate a stale disconnect event arriving late; explain how the client_id guard protects the new connection”. The useful thing to carry forward is knowing which judgments must be revisited when input, scale, or risk changes.
I turned one judgment from this article into a small experiment I could run today. Knowing what to observe next is more useful than simply remembering the conclusion.
After reading this, I first looked for the conditions behind the idea instead of copying the method into a project. That order made the later trade-offs much clearer.
When this judgment reaches real work, which constraint should be added first? I am curious which step matters most between reading and the first practical attempt.
No discussion on this article yet.