Sub topic 2
Hosts, clients, and servers: who does what
This chapter assumes you can read basic Python, know what JSON looks like, and understand how an LLM application calls tools. You should also be comfortable with client-server communication. The wire format and the handshake come in later chapters. Here the question is who owns which responsibility.
Three roles, one connection
The Model Context Protocol (MCP) defines three roles.
- Host: the application the user interacts with. Examples are a desktop chat app, an IDE assistant, or a command-line agent. The host runs the language model and decides what the user sees.
- Client: a protocol component inside the host. Each client holds exactly one connection to one server.
- Server: a separate program that exposes capabilities such as tools, resources, and prompts to a client.
The word "client" is the one that causes confusion. People often call a chat app a "client", and that is reasonable in everyday speech. In MCP vocabulary, though, the chat app is the host. The client is a small connector object that the host creates for each server it wants to talk to.
Three facts follow from this picture:
- The relationship between a client and a server is one to one. A client never multiplexes several servers.
- The relationship between a host and its servers is one to many. The host creates as many clients as it has servers.
- The language model sits in the host, not in the server and not in the client. Servers never talk to the model directly.
The host
The host is the most powerful role, and most of the policy lives there. In practice the host does the following:
- Starts and stops clients, and decides which servers to connect to.
- Gathers what each server offers and presents it to the model and to the user.
- Runs the model, sends it the available tools, and receives its requests to call them.
- Asks the user for consent before sensitive actions.
- Handles failures such as a server crashing or a connection dropping.
Because the host mediates everything, a server cannot act on its own. A server that exposes a delete_file tool does nothing until a host connects to it, shows the tool to a model, and the model chooses to call it. The host sits between the model's intention and the server's action, and it can refuse.
The client
The client is the protocol machinery inside the host. It has a narrow, mechanical job:
- Open the connection to one server, using whatever transport the host configured.
- Run the initialization handshake and remember what the server said it supports.
- Send requests (such as listing tools or calling one) and match responses to them.
- Receive notifications from the server.
- Answer requests that the server sends back, such as asking the host's model for a completion. These are client features, covered in a later chapter.
A client holds state. It knows its negotiated protocol version, the server's declared capabilities, and which requests are still waiting for a reply. This is why the session is described as stateful, and why each client belongs to exactly one server.
Most developers never write a client by hand. SDKs supply the class, and the host wires it in. You write one directly when you build your own host, or when you want a script that exercises a server.
The server
A server is any program that speaks MCP and offers capabilities. It has three properties worth remembering.
It is focused. A good server does one job: file access, a database, a ticketing system, a calendar. Hosts combine many small servers instead of relying on one large one.
It is isolated. A server sees only the requests its own client sends. It does not see the whole conversation between the user and the model. It cannot see other servers or what they return. If you want a server to know something, the host has to pass that information to it as a tool argument or through a defined mechanism.
It can run anywhere. A server may be a local subprocess started by the host, or a remote service reached over HTTP. The protocol messages are the same in both cases. Only the transport changes, and a later chapter covers that choice.
What each role controls
MCP separates the features a server offers by who decides when they are used. This table previews the idea.
| Feature | Offered by | Typically triggered by | Example |
|---|---|---|---|
| Tool | Server | The model, through the host | Run a database query |
| Resource | Server | The application or the user | Read a file's contents |
| Prompt | Server | The user | A "review this code" template |
| Sampling | Client | The server asks, the host decides | Server requests a model completion |
| Roots | Client | The server asks | Which directories the server may work in |
| Elicitation | Client | The server asks | Ask the user for a missing value |
Notice that the flow goes both ways. The server offers capabilities to the client, and the client offers some back to the server. The roles describe who owns a connection endpoint. They do not mean that all requests flow in one direction.
A host in code
The following script is a tiny host. It starts one server, creates a client session for it, and builds a combined catalog of tools. It assumes the server.py from the server-building chapter sits in the same directory.
import asyncio
from contextlib import AsyncExitStack
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
SERVERS = {
"demo": StdioServerParameters(command="python", args=["server.py"]),
}
async def main():
async with AsyncExitStack() as stack:
sessions = {}
for name, params in SERVERS.items():
read, write = await stack.enter_async_context(stdio_client(params))
session = await stack.enter_async_context(ClientSession(read, write))
await session.initialize()
sessions[name] = session
catalog = {}
for name, session in sessions.items():
result = await session.list_tools()
for tool in result.tools:
catalog[f"{name}__{tool.name}"] = (name, tool.name)
print(catalog)
asyncio.run(main())With the demo server, the output is {'demo__add': ('demo', 'add')}. Several things here map directly to the roles:
- The script as a whole is the host. It owns the list of servers and the lifetime of every connection.
- Each
ClientSessionis a client. The loop creates one per server entry. - The
AsyncExitStackmakes the host responsible for cleanup. When the block ends, every session closes and every subprocess stops. - The catalog keys carry a server-name prefix. Two servers may both define a tool called
search. The host, not the protocol, must resolve that collision, and prefixing is one common way to do it.
To add a second server, add another entry to SERVERS. The rest of the code does not change, which shows the benefit of the one-client-per-server design.
Who decides what happens
Follow a single user request through the roles. Suppose the user asks, "How much is 2 plus 3?" and the demo server is connected.
- The host sends the user's message to the model, along with the tool descriptions it collected from its clients.
- The model replies that it wants to call
addwitha=2andb=3. - The host checks its policy. It may ask the user to approve, or it may allow the call automatically.
- The host routes the call to the client that owns the
demoserver. - The client sends a
tools/callrequest and waits for the matching response. - The server runs the function and returns the result.
- The host gives the result to the model, which writes the final answer.
The model never contacts the server, and the server never contacts the model. Every step passes through the host. This is the design's main safety property, and later chapters on security build on it.
Common errors
Mixing up "host" and "client". If documentation says "the client decides which tools to show the model", check whether it means the whole application. Strictly, that decision belongs to the host.
Expecting one client to serve many servers. A single session talks to one server. If you need five servers, you need five sessions. Pooling them is host code.
Assuming servers share context. A server cannot read another server's results. If a workflow needs data from the files server inside the database server, the host or the model must carry it across.
Trusting a server because it is local. A local subprocess runs with the user's permissions. The host still needs to decide what it may do and what the user must confirm.
Ignoring name collisions. Two servers may expose identically named tools. Without prefixing or another rule in the host, one tool can shadow the other.
Treating the server as the security boundary. A server can validate its own inputs, but it cannot know whether the user approved an action. Consent is enforced in the host.
Summary of responsibilities
| Concern | Owner |
|---|---|
| Which servers to connect to | Host |
| Opening and closing connections | Host, using clients |
| Protocol session state | Client |
| Capabilities such as tools and resources | Server |
| Model access and selection | Host |
| User consent and permission policy | Host |
| Input validation for its own operations | Server |
Exercise
A chat application connects to a files server, a calendar server, and an issue tracker server. How many clients does the host create, and what would break if a single client tried to handle all three servers?
Show answer
The host creates three clients, one per server. MCP defines a client as a one-to-one stateful session with a single server, and the client stores that server's negotiated version and capabilities. A single client handling three servers could not keep each server's state separate or match responses to the right connection. The host is the component that manages the group.
Exercise
A server author wants the server to read the user's earlier chat messages so its tool can give better results. Why can't the server do this by itself, and how could the information reach it?
Show answer
A server only sees requests that its own client sends, and the conversation lives in the host. The host (or model) has to pass the needed information explicitly, for example as arguments in a tool call. The host can also grant limited access through a defined client feature, but only if the user and host allow it.