Skip to main content

Build an MCP client

Build your own client that can integrate with MCP servers — the foundation of a custom MCP host.

Adapted from the official Build an MCP client tutorial (multi-language). Use the upstream page for complete sample apps.

Client connected to MCP tools — Source: MCP docs

Hosts expose discovered tools to the model loop. Source: modelcontextprotocol/docs.

Hammer / tools affordance in Claude Desktop — Source: MCP docs

Source: modelcontextprotocol/docs (quickstart-hammer.png).

Responsibilities of a client

  1. Transport — spawn/connect to the server (stdio command or HTTP URL).
  2. Discoveryserver/discover (or compatible handshake on older revisions).
  3. Capability usetools/list / tools/call, resources, prompts.
  4. Host UX — approval prompts, elicitation forms, error surfaces.
  5. Lifecycle — restart crashed stdio processes; refresh tool lists on notification.

Typical agent loop (simplified)

User message
→ Host adds system + tool schemas from MCP tools/list
→ LLM may request tool_use
→ Client tools/call on the matching server
→ Tool result returned to LLM
→ Repeat until final answer

Implementation tips

TopicGuidance
Process managementKeep stdio child stderr for logs; never parse stdout as logs
TimeoutsBound every tools/call; cancel where supported
Multi-serverOne client instance per server; aggregate tools with server-qualified names
SecurityDo not auto-approve destructive tools; scope env vars per server
VersioningSend protocolVersion in _meta; handle unsupported-version errors

Negative cases

  • Aggregating duplicate tool names from two servers without namespacing → ambiguous calls.
  • Blocking the UI thread on long tool calls → use async + progress/cancellation.
  • Swallowing transport errors → show “server disconnected” and offer reconnect.
  • Passing the entire conversation history into every tool → over-share; send only needed args.

See also Client best practices for scaling hosts across many servers.

Developer settings for MCP — Source: MCP docs

Example developer configuration surface. Source: modelcontextprotocol/docs.

Discussion

Comments

Share feedback or questions about this page. No account required.

Loading comments…