Sub topic 2
Hosts, clients and servers
Prerequisites and scope
This chapter assumes you can read Python with async/await, know what JSON Schema is, and have called an LLM API with tool (function) calling. It covers the three roles in the Model Context Protocol (MCP) architecture and how they map onto code you write. Message formats, transports and the handshake get their own chapters, so they appear here only as far as the roles need them.
The three roles
MCP names three participants. People often blur them together, and that confusion causes design mistakes later, so keep them separate.
- A host is the application the user runs. It owns the conversation with the LLM, decides which servers to connect to, and decides what the model is allowed to do. A chat app, an IDE assistant, or the agent script you are building in this tutorial is a host.
- A client is a component inside the host that holds one connection to one server. It speaks the protocol: it sends requests, receives responses and notifications, and tracks the state of that connection.
- A server is a separate program that exposes capabilities (tools, resources and prompts) to clients. It might wrap a database, a file system, a ticketing API or a calculator.
The relationship is one host, many clients, and each client talks to exactly one server. If your application connects to three servers, it runs three client instances.
Where the model fits
The LLM is not part of the protocol. MCP defines nothing about how the model is called. The host calls the model through whatever API it uses, and the model never talks to a server directly. The flow looks like this:
- The host asks each client to list the tools its server offers.
- The host converts those tool definitions into the model API's tool format and sends them with the conversation.
- The model replies with a request to call a tool, using a name and arguments.
- The host decides whether to allow the call, then asks the right client to send it to the right server.
- The server runs the tool and returns a result to the client. The host passes that result back to the model.
The host is the only place where the model's intent turns into an action. This makes the host the natural place for approvals, logging, limits and routing. A server cannot see the conversation and cannot call the model unless the host explicitly offers that ability.
Why the split exists
Separating clients from servers is what lets one server work with many hosts. A server written once can be used by a desktop assistant, a command line agent and your own application, as long as each speaks the protocol. Without a shared protocol, every host would need custom glue for every tool source. With it, adding a tool source means pointing a client at a new server.
Separating the host from the client has a different purpose. It keeps connection details (transport, handshake, request IDs) out of the code that manages the conversation. The host asks "list tools" or "call this tool", and the client handles the rest.
Mapping the roles to the Python SDK
In the official Python SDK, a ClientSession plays the role of the client. Your own program is the host. The server is a separate process or service. The two code samples below, from this tutorial, show both sides.
The server side declares one tool. The SDK derives the input schema from the type hints:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calc")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
if __name__ == "__main__":
mcp.run(transport="stdio")The host side starts that server as a subprocess, opens a client session, and calls the tool:
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
server_params = StdioServerParameters(command="python", args=["server.py"])
async def main():
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
listed = await session.list_tools()
for tool in listed.tools:
print(tool.name, "-", tool.description)
print(tool.inputSchema)
result = await session.call_tool("add", {"a": 2, "b": 3})
print(result.isError, [c.text for c in result.content if c.type == "text"])
asyncio.run(main())Notice what each part is responsible for. The main function is host code: it decides which server to launch and when to call a tool. The ClientSession is the client: it owns the connection and the protocol exchange. The add function lives in the server and knows nothing about who calls it.
What each role controls
Each role has different authority. Knowing the split tells you where to put each safeguard.
| Role | Controls | Does not control |
|---|---|---|
| Host | Which servers to connect to, which tools the model sees, whether a call runs, how results are shown to the model | What a server does internally |
| Client | The connection to one server, protocol state, request and response matching | Conversation, model choice, user approval policy |
| Server | What capabilities it offers and how each one runs | Who connects, what the model is told, what happens to its results |
The host is trusted by the user, because it is the application they chose to run. Servers are a separate matter. A server may be written by a third party, and its tool descriptions and outputs are text that ends up in the model's context. That is why the host, not the server, has to enforce policy. The security chapter returns to this point.
Common mistakes
- Treating the server as part of the model. A server is ordinary software. It can fail, hang, return errors or return misleading text. Your host must handle each case.
- Sharing one client across servers. A client holds one connection. To use two servers you need two sessions, and you must keep track of which session owns which tool. The multi-server chapter covers naming and routing.
- Putting approval logic in a server. A server cannot reliably decide whether the user wants an action, because it does not see the conversation. Approval belongs in the host.
- Assuming the server sees the conversation. It sees only the requests the client sends. If a tool needs context, the model must pass it as arguments.
- Using "client" for the whole application. In MCP vocabulary, the application is the host. Documentation and error messages use "client" for the connection component, so mixing the terms makes them hard to read.
Recap
The host is your application and owns the model conversation and the policy. Each client is a connection object inside the host, tied to one server. Each server is an independent program that offers capabilities. The model only ever talks to the host.
Exercise
Your application connects to a file server and a database server. How many clients does it hold, and which role decides whether the model's request to delete a file is allowed to run?
Show answer
Two clients, one per server. The host decides, because it is the only component that sees both the conversation and the model's request, and it controls whether the call is forwarded to the client.
Exercise
In the host code above, which object is the client and which part is the server? Where would you add a user confirmation prompt before call_tool?
Show answer
The ClientSession is the client, and the server.py process that stdio_client launches is the server. The confirmation prompt goes in the host code, immediately before the session.call_tool(...) line, because the host owns the decision to run the call.