How to build an MCP client

Build agents
HoneyHive Engineering

Build an MCP client in the order the spec and docs imply: one client per server, a transport, tool listing, a capped tool loop and the client-side rules.

An MCP client is the part of a host application that connects to one MCP server, lists its tools and forwards the model’s tool calls to it. To build one, install an SDK, open a transport, list tools, pass them to your model, run a capped loop that executes each call, and close the connection. The official tutorial shows this with Claude and a stdio server. This page adds the order of decisions and the client rules the 2026-07-28 spec sets. For the protocol itself, see what is the Model Context Protocol, and for the other side of the wire, how to build an MCP server.

Host, client and server

The architecture spec splits the work three ways:

Role Job
Host Creates and manages clients, controls their permissions and lifecycle, enforces consent and security policy, coordinates the LLM and aggregates context across clients
Client Talks to exactly one server, attaches protocol version and capabilities to every request, routes messages, manages subscriptions
Server Exposes tools, resources and prompts, and can ask the client for input

If your application connects to three servers, it runs three clients. The spec also says a server should not be able to read the whole conversation or see into other servers, so the full conversation stays with the host.

Build order

  1. Install an SDK. The tutorial lists Python (mcp), TypeScript (@modelcontextprotocol/client), Java (Spring AI MCP starters), Kotlin, C# (ModelContextProtocol, prerelease at the time of reading), Ruby and Rust (rmcp). It notes that the Java and Kotlin clients use protocol 2025-11-25, the version their latest stable SDKs support, so they do not connect to servers that accept only 2026-07-28.
  2. Open a transport. For a local server, the client launches it as a subprocess over stdio. In Python, Client(stdio_client(...)) opens the connection when you enter the async with block. For a remote server, use Streamable HTTP (next section).
  3. List the tools. Call list_tools() and convert each tool to your model API’s shape. The tutorial maps them to name, description and input_schema for the Messages API.
  4. Run the tool loop. Send the user’s query and the tools to the model. For each tool_use block, call call_tool(name, input) on the server, then send all results back in one user message. Repeat until the model answers without a tool call.
  5. Cap the loop. The tutorial sets MAX_TOOL_TURNS = 10 and warns that without it a query can loop indefinitely.
  6. Pass errors through. A failed tool call goes back to the model with is_error set, not raised, so the model can retry or explain. The tutorial also notes that every tool_use needs a matching tool_result in the next message, and that thinking blocks must be returned unchanged.
  7. Close the connection. In Python, leaving the async with block disconnects and shuts down the subprocess. In TypeScript, call this.mcp.close().

Expect the first response to be slow: the tutorial says it can take up to 30 seconds. In Python, raise read_timeout_seconds on Client if tool calls time out.

Streamable HTTP client rules

For remote servers, the Streamable HTTP spec sets these requirements on the client:

  • Send every JSON-RPC message as a new HTTP POST to the single MCP endpoint, with one request or notification per body.
  • Send an Accept header listing both application/json and text/event-stream, and support both response types. The server picks per request.
  • Include MCP-Protocol-Version on every POST. It must match the version in the request’s _meta, or the server answers 400 with a HeaderMismatch error.
  • Include Mcp-Method on all requests, and Mcp-Name on tools/call, resources/read and prompts/get.
  • Support x-mcp-header. If a server marks a tool parameter with it, the client must copy that value into an Mcp-Param-{Name} header, Base64-encoding values that are not plain ASCII, and must drop any tool whose annotation is invalid from the tools/list result. Stdio clients may ignore it.

The 2026-07-28 revision also removed protocol-level sessions and the GET stream endpoint, so a client no longer keeps a session ID. Closing an SSE response stream cancels that request. To receive tool-list changes or resource updates, send a subscriptions/listen request and keep its response stream open.

Clients also declare what they support on each request: the architecture page says they put capabilities in _meta.io.modelcontextprotocol/clientCapabilities, and may call server/discover first to learn what the server offers.

Older servers

A client that must reach servers on older revisions can try a modern request first. If a 400 carries a recognized modern JSON-RPC error, the server is modern: retry with one of its supported versions. If the body is empty or unrecognized, the spec says to fall back to initialize for servers on 2025-03-26 through 2025-11-25. To also reach the legacy HTTP+SSE transport from 2024-11-05, on a 400, 404 or 405 without a modern error body, issue a GET and look for an endpoint event as the first SSE event. That transport has been deprecated since 2025-03-26, and new implementations should not adopt it.

Handling requests from the server

A server cannot call a client directly over Streamable HTTP. It returns an InputRequiredResult listing what it needs, and the client gathers the input and retries the original request with inputResponses. Three request types use this path.

Feature Status in 2026-07-28 What a client must do
Elicitation Current Declare elicitation (form, url or both). Show which server is asking, offer decline and cancel, and let users review form answers
Sampling Deprecated New clients should not adopt it; call the model provider directly
Roots Deprecated New clients should not adopt it; pass directories through tool parameters, resource URIs or server configuration

Deprecated features stay in the spec for at least twelve months after the revision.

For elicitation, an empty elicitation capability means form mode only. The user’s answer is one of three actions: accept, decline or cancel. In URL mode, the client must not pre-fetch the URL, must show the full URL and get explicit consent before opening it, and must open it so that neither the client nor the LLM can inspect the page or the user’s input. Servers must not ask for passwords, API keys or tokens in form mode.

Security duties that fall on the host

The client is where consent happens. The spec puts human approval on the client side: for tools, the server spec says there should always be a human in the loop with the ability to deny tool invocations. The tools spec also says clients should show tool inputs to the user before calling the server, prompt for confirmation on sensitive operations, validate tool results before passing them to the LLM, and implement timeouts for tool calls. MCP security covers the threats in more detail, and an MCP gateway can centralize access control when many clients share many servers.

Tracing an MCP client in HoneyHive

HoneyHive’s MCP tracing guide covers Python servers and clients, so tool calls from your loop show up inside the trace of the agent run. See MCP observability for what to record, and function calling for the model-side half of the loop.

FAQ

What is an MCP client?

The component inside a host application that holds the connection to one MCP server. The spec says each client communicates with exactly one server, and the host creates and manages many clients.

What is the difference between an MCP host and an MCP client?

The host is the application. It creates clients, enforces consent and security policy, and coordinates the LLM. A client only speaks the protocol to a single server, per the architecture page.

Which SDK should I use to build an MCP client?

The client tutorial has paths for Python (mcp), TypeScript (@modelcontextprotocol/client), Java, Kotlin, C#, Ruby and Rust. Check which protocol revision your SDK supports before you pick it.

Do I need to implement sampling and roots in my client?

No. Both are deprecated as of protocol version 2026-07-28, and new implementations should not adopt them.

How do I stop an MCP client from looping on tool calls forever?

Cap the turns. The tutorial's Python client sets MAX_TOOL_TURNS = 10 and warns that without a cap a query can loop indefinitely. The sampling spec also says both parties should implement iteration limits for tool loops.

Share