Sub topic 2
Hosts, clients, and servers
This chapter assumes you can read Python, know what JSON-RPC-style request and response messages look like, and have a rough idea of how an LLM calls a tool. It covers the three roles in an MCP system, how they connect, and where responsibility sits. Later chapters build on this model, so errors here tend to cause confusing bugs later.
The three roles
MCP names three participants. People often blur them, because in casual speech "the client" can mean the whole app.
| Role | What it is | Example | Who writes it |
|---|---|---|---|
| Host | The AI application the user interacts with. It runs the model loop and owns the user interface. | Claude Desktop, an IDE assistant, your team's chat bot | The application vendor or your team |
| Client | A connector object inside the host. It holds one connection to one server and speaks the protocol. | An SDK `ClientSession` | Usually the SDK, wired in by the host |
| Server | A program that exposes tools, resources, and prompts over the protocol. | The `team-tools` server you are building | You |
The host is the only participant that talks to the model. The server never calls the model directly, and the model never calls the server directly. The model produces a request to use a tool, the host decides whether to honor it, and a client forwards it to the right server.
One client per server
The relationship between clients and servers is one-to-one. If a host connects to three servers, it creates three clients. Each client has its own connection, its own initialization handshake, and its own negotiated capabilities.
This has practical consequences:
- Servers are isolated from each other. Your
team-toolsserver cannot see the tools of another server connected to the same host, and it cannot see the conversation. - The host aggregates. It collects tool lists from every client, presents the combined list to the model, and routes each call back to the client that owns the tool.
- Name collisions are the host's problem. Two servers can each define a tool called
search. How the host disambiguates is host-specific, so use descriptive names in your own server (for examplesearch_runbooksinstead ofsearch). - Capabilities differ per connection. One server may support resources while another supports only tools, and each client tracks that separately.
What the host is responsible for
The host is where policy lives. A well-behaved host does the following:
- Starts or connects to servers, based on its configuration.
- Lists the tools from each server and decides which ones to offer to the model.
- Receives the model's tool-call request and, depending on its policy, asks the user to approve it.
- Sends the call through the right client and passes the result back to the model.
- Enforces limits such as timeouts, output size, and which servers are trusted.
Because the host mediates everything, you cannot rely on a particular host to confirm destructive actions. Different hosts behave differently. Build your own safeguards into the server. The security chapter covers this in detail.
What the client is responsible for
The client is the protocol endpoint on the host's side. It:
- Opens the connection (spawns a subprocess for stdio, or opens an HTTP connection).
- Runs the initialization handshake and records what the server supports.
- Sends requests such as
tools/listandtools/call, and matches responses to requests. - Receives notifications from the server, for example that the tool list has changed.
- Offers its own features back to the server, if the host enables them.
Most of the time you do not write a client. You write a server and let a host supply the client. You still need to know what the client does, because it explains the behavior you see when testing. For example, the server cannot start talking first. The client opens the session and sends the first message.
The following script is a minimal client built with the official Python SDK. It launches the server from the previous chapters as a subprocess, runs the handshake, and lists the tools. It is useful for scripted checks and shows the client role directly.
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
params = StdioServerParameters(command="python", args=["server.py"])
async def main() -> None:
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
result = await session.list_tools()
for tool in result.tools:
print(f"{tool.name}: {tool.description}")
asyncio.run(main())Notice that the script, not the server, decides what to run: stdio_client starts server.py as a child process and connects to its standard input and output. The async with blocks also shut the subprocess down when the script exits.
What the server is responsible for
The server is a provider of capabilities. It declares three kinds of primitive, each with a different intended controller:
| Primitive | Intended controller | Typical use |
|---|---|---|
| Tools | The model decides when to call them | Actions and lookups, such as `get_service_owner` |
| Resources | The application decides when to read them | Read-only data addressed by URI, such as `runbook://billing` |
| Prompts | The user chooses them | Reusable instruction templates, such as an incident summary |
Servers can also use features that the client offers. The three most common are:
- Sampling: the server asks the host to run a model completion on its behalf. The host stays in control of the model and can show the request to the user.
- Roots: the client tells the server which filesystem locations or other boundaries are relevant.
- Elicitation: the server asks the client to collect extra input from the user.
These client features are optional. A server must check what the client declared during initialization before using them. Many hosts support only a subset. A server that assumes sampling is available will fail on hosts that do not implement it.
Local and remote servers
The role split does not depend on where a server runs.
A local server runs as a subprocess of the host on the same machine. The host starts it, talks to it over standard input and output, and stops it when finished. This is the simplest setup, and it is what the earlier FastMCP examples use. Each host process normally gets its own server process, so in-memory state is not shared between users.
A remote server runs somewhere else, such as a shared service on your internal network. Clients connect to it over HTTP. Many users and hosts can connect to one running instance, so you have to think about authentication, concurrency, and per-user state. The transports chapter compares the two options.
The roles are identical in both cases. Only the connection method and the trust assumptions change.
Where trust boundaries sit
Draw the boundaries explicitly, because security decisions follow them.
- The model is untrusted for argument values. It can produce wrong or manipulated arguments, for example after reading hostile text in a document. Your server should validate everything it receives.
- Tool descriptions and results are data that flows into the model's context. A tool result that contains instructions can influence the model. Return plain facts, and do not relay unfiltered text from untrusted sources without thinking about it.
- The host and user decide which servers to trust. A server you install runs with the permissions of the account that launched it, in the local case.
- The server does not see the user's conversation unless the host sends it something. Do not design a tool that needs the whole chat history. Pass explicit arguments instead.
Common mistakes
- Treating the server as the agent. The server does not decide what to do next. It answers requests. The model, through the host, plans.
- Expecting the server to push work to the model. Without sampling, a server can only respond to client requests and send notifications.
- Assuming one shared process. With stdio, each host launches its own server process. Anything stored in a Python dictionary is per process.
- Writing to standard output in a stdio server. Standard output carries protocol messages, so a stray
printcorrupts the stream. Log to standard error. - Hard-coding host behavior. Confirmation prompts, tool-list refresh, and approval policies differ across hosts. Check the host's documentation, and test in the Inspector, which you will meet in a later chapter.
- Using generic tool names. Since the host merges tools from many servers, a name like
runorsearchgives the model little to go on and invites collisions.
Summary of the model
Hold this picture as you move on to the protocol details. A host runs the model and the user interface. For every server it connects, it creates one client. Each client keeps one session with one server. The server exposes tools, resources, and prompts, and may use client-offered features such as sampling if the client declared support. All policy about what reaches the model, and what gets approved, belongs to the host, so the server has to protect itself.
Exercise
Your host application is connected to three MCP servers. How many clients does it hold, and why can your team-tools server not read tool results produced by the other two servers?
Show answer
The host holds three clients, one per server. Each client keeps its own isolated session with its own server, and only the host sees the combined set of tools and results. The servers never talk to each other, so team-tools cannot see what the other servers return unless the host passes that data in as arguments.
Exercise
A teammate says, "The server should ask the model whether to retry the failed deployment." Which role normally controls the model, and what optional client feature would let a server request a completion? What must the server check before using it?
Show answer
The host controls the model. The optional feature is sampling, where the server asks the client to run a completion through the host. Before using it, the server must check that the client declared the sampling capability during initialization, because many hosts do not support it.