MCP Architecture: Clients, Servers, Transports
On this page 14 sections
- Where the messages flow
- Every message is JSON-RPC
- The conversation, message by message
- _meta: every request stands alone
- The old way: the initialize handshake
- Transports: how the messages travel
- The life of a connection
- Try it yourself
- Common beginner mistakes
- Questions you will face in production
- Check your understanding
- What to remember
- What to study next
- Further reading
The Kitebase server from article 01: What Is MCP? works on your machine. Then two reports come in:
- A teammate’s host lists the server as failed. The error is a JSON parse error pointing at the text
Kitebase server starting.... - Another teammate, on an older host, gets an error about
initialize. That word appears nowhere in your code.
Both problems live below your tools, in the messages the host and server exchange. To fix them, you need to see what’s on the wire. That means three things:
- the exact messages
- how the two sides agree on what they support
- how the bytes travel
What you’ll build: a hand-rolled MCP client of about 100 lines. It launches the Kitebase server and prints every message that crosses the pipe. You’ll see the whole run, from discovery to looking up ticket KITE-142 to shutdown. It runs offline, with no API keys.
Where the messages flow
Article 01 introduced the roles. On the wire, what matters is how they pair up. The host (Claude Desktop, Claude Code, your own app) creates one client per configured server. Each client holds one connection.
That keeps servers apart. A crash in one server can’t take down another. So when a server goes missing, check that one connection first.
Servers come in two kinds, and only the transport differs between them:
- A local server runs on your machine as a subprocess of the host. It usually serves one client.
- A remote server runs somewhere else and serves many clients.
Every message is JSON-RPC
Clients and servers talk in JSON-RPC 2.0. It’s a small standard for calling functions by sending JSON. Nothing about it is specific to AI, so any language with a JSON library can speak it.
There are three kinds of message:
- A request has an
idand amethod, and expects exactly one reply. - A response echoes that
idwith either aresultor anerror. - A notification has a
methodbut noid. Nobody replies to it.
Here’s a real request from the companion example, asking the Kitebase server to run get_ticket:
{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "get_ticket",
"arguments": { "ticket_id": "KITE-142" },
"_meta": {
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
"io.modelcontextprotocol/clientInfo": { "name": "kitebase-demo-client", "version": "0.1.0" },
"io.modelcontextprotocol/clientCapabilities": {}
}
}
}
And here’s the reply, trimmed. The ticket comes back as JSON text inside a content block:
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"content": [{ "type": "text", "text": "{\n \"id\": \"KITE-142\",\n \"title\": \"Customer locked out after SSO change\", ..." }],
"isError": false,
"resultType": "complete"
}
}
The id is how the client pairs a reply with its request. A client can have several requests in flight at once. A slow one can finish after a fast one. So replies are matched by id, never by the order they arrive in.
MCP adds one rule to plain JSON-RPC: the id must be a string or an integer, never null. And resultType: "complete" marks a final answer.
Sometimes the request itself is wrong. Then the reply has an error instead of a result. The error holds an integer code, a message, and sometimes data. These are the codes worth knowing:
| Code | Meaning |
|---|---|
-32602 | Invalid params, including a request with no _meta |
-32601 | Method not found |
-32022 | Unsupported protocol version; data.supported lists the ones the server speaks |
-32021 | The request needs a client capability the client didn’t declare |
-32020 | HTTP only: a header doesn’t match the body |
A tool that runs and fails is not a protocol error. Say you ask for ticket KITE-999, which doesn’t exist. As article 01 showed, you get a normal result with isError: true. That way the model can read the message and try again.
Why JSON-RPC and not REST?
Because MCP has to work over more than HTTP. REST maps operations onto URLs and HTTP verbs, so it only works over HTTP. MCP also has to work over a pipe to a subprocess, where there are only lines of text.
A JSON-RPC message carries everything inside itself: method, arguments and id. So the same tools/call goes down a pipe or into an HTTP POST body unchanged. JSON-RPC also has notifications built in, for one-way messages like “cancel that request”.
The conversation, message by message
A client that knows nothing about a server can learn everything it needs in three requests. The companion example sends exactly these:
server/discoverasks “which protocol versions do you speak, and what can you do?” Every server on the current revision must answer it. Clients may skip it and go straight to other requests.tools/listasks “what tools do you have?” The reply has each tool’s name, description and input schema. The input schema is the JSON Schema for the tool’s arguments.tools/callsays “runget_ticketwith these arguments.”
The reply to server/discover looks like this (trimmed):
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"supportedVersions": ["2026-07-28"],
"capabilities": {
"prompts": { "listChanged": true },
"resources": { "listChanged": true, "subscribe": true },
"tools": { "listChanged": true }
},
"ttlMs": 0,
"cacheScope": "private",
"resultType": "complete",
"_meta": { "io.modelcontextprotocol/serverInfo": { "name": "kitebase", "version": "" } }
}
}
ttlMs and cacheScope are caching hints:
ttlMssays how long the client may reuse this answer.0means “ask again whenever you need it”.cacheScopesays whether the client may share the answer across users.
You never write any of this in a server. The SDK generates it from the functions in server.py.
_meta: every request stands alone
Every request the client sent carried the same three _meta fields. That’s because MCP is stateless. The server keeps no memory of earlier requests, even ones from the same pipe a millisecond ago. So each request has to say which protocol version it’s written in and what the client supports.
The version and capabilities are required. Send a tools/call without them, and the Kitebase server replies -32602, naming the two missing keys. clientInfo is optional and only there for logs.
The companion builds the block with key names from the SDK:
from mcp.types import CLIENT_CAPABILITIES_META_KEY, CLIENT_INFO_META_KEY, PROTOCOL_VERSION_META_KEY
from mcp.types.version import LATEST_MODERN_VERSION # "2026-07-28"
def envelope(version: str = LATEST_MODERN_VERSION) -> dict:
return {
PROTOCOL_VERSION_META_KEY: version,
CLIENT_INFO_META_KEY: CLIENT_INFO,
CLIENT_CAPABILITIES_META_KEY: {},
}
Why go stateless? Because it makes scaling easy. A server that remembers nothing can run as ten copies behind a load balancer. A load balancer is a proxy that spreads requests across copies of a service. Any copy can answer any request.
A server that remembers a session is harder. Every request from one client has to be routed to the same copy. And if that copy restarts, it forgets everything.
Capabilities: what each side can do
A capability is an optional feature that one side says it supports. There are two sets, one for each side.
The server’s capabilities come back from server/discover. Kitebase has tools, resources and prompts, and it can report when those lists change. The client’s rule is simple: don’t call what wasn’t advertised. A server with no prompts key won’t answer prompts/list.
The client’s capabilities travel in _meta on every request. {} means “nothing optional”. For example, a client that can ask the user a follow-up question on the server’s behalf declares "elicitation": {}.
The server must not rely on anything the client didn’t declare on that request. If it needs something that’s missing, it replies -32021 and names it.
Versions: agreed per request
With no handshake, the two sides agree on a version on every request. Run python main.py --version 2099-01-01 and each request comes back as:
{"jsonrpc":"2.0","id":3,"error":{"code":-32022,"message":"Unsupported protocol version","data":{"supported":["2026-07-28"],"requested":"2099-01-01"}}}
The client picks a version from supported and retries. That’s the whole negotiation.
The old way: the initialize handshake
The initialize error from the opening comes from the previous design. Before revision 2026-07-28, every connection opened with a handshake. Hosts and servers written before then still speak the last of those revisions, 2025-11-25. Run python main.py --legacy to see it:
client -> server
{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-11-25", "capabilities": {}, "clientInfo": {...}}}
server -> client
{"jsonrpc":"2.0","id":1,"result":{"capabilities":{...},"protocolVersion":"2025-11-25","serverInfo":{"name":"kitebase","version":""}}}
client -> server
{"jsonrpc": "2.0", "method": "notifications/initialized"}
... tools/list and its reply ...
client -> server
{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "get_ticket", "arguments": {"ticket_id": "KITE-142"}}}
The handshake has three steps:
- The client proposes a version and its capabilities.
- The server answers with its own.
- The client sends
notifications/initializedto say it’s ready. It has noid, so there’s no reply.
After that, requests carry no _meta. The connection remembers what was agreed.
The Kitebase server answers both ways, because SDK v2 serves both. If the first thing it sees is initialize, it behaves like a 2025-11-25 server.
Clients that must work with either kind of server probe first. The official Python client sends server/discover. If the reply isn’t a recognisable modern answer or error, it falls back to initialize.
So the teammate’s initialize error means their host speaks only the old revision, and the handshake failed. What happens next depends on the server:
- A server built with SDK v2 accepts the handshake.
- A server that only speaks
2026-07-28rejects it. That error should name the versions it does support, since an old host has no other way to find out.
Transports: how the messages travel
A transport is the channel the JSON-RPC messages move over. MCP defines two, and the message itself is identical on both.
stdio: a subprocess and a pipe
With stdio, the host launches the server as a child process. It talks to it through the process’s standard streams:
- Requests go to the server’s stdin.
- Replies come from its stdout.
- Each message is one line of JSON, with no newlines inside.
That’s why the companion’s send is so short:
def send(self, message: dict) -> str:
line = json.dumps(message) # one message per line, no newlines inside
self.proc.stdin.write(line + "\n")
self.proc.stdin.flush()
return line
Three rules follow:
- stdout carries only MCP messages. The spec says a server must not write anything else there.
- stderr is for logs. The host may show it, save it or ignore it, and it never parses it as protocol. The companion’s copy of
server.pylogsget_ticket called with 'KITE-142'there. - Shutdown is closing stdin. The client closes the server’s input and waits for it to exit. It kills the server only if it hangs. The companion ends every run with
closed stdin, server exited with code 0.
Now the first teammate’s error makes sense. Run python main.py --noisy, which makes the server print one line to stdout at startup:
Connection broken: server wrote something that isn't JSON-RPC to stdout: 'Kitebase server starting...'
When the Python SDK’s client meets a line like that, it logs a parse error and skips it. The teammate’s host dropped the connection instead.
The culprit isn’t always your own print. A library can print a banner or warning when it’s imported. To find it, run the server by hand, the way the host does, and look at the first thing on stdout.
Stdio also keeps security simple. Only the host can reach the pipe, so there are no ports and no login. Secrets like a Kitebase API token come from environment variables that the host sets when it launches the process.
Streamable HTTP: one endpoint, one POST per message
With Streamable HTTP, the server is a long-running web service with a single endpoint, usually /mcp. Every request is its own HTTP POST, with the JSON-RPC message as the body. Start the server with python server.py --http and call it with curl:
curl -X POST http://127.0.0.1:8765/mcp \
-H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-H 'MCP-Protocol-Version: 2026-07-28' \
-H 'Mcp-Method: tools/call' \
-H 'Mcp-Name: get_ticket' \
-d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get_ticket","arguments":{"ticket_id":"KITE-142"},"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}}}'
HTTP/1.1 200 OK (trimmed)
content-type: application/json
{"jsonrpc":"2.0","id":3,"result":{"content":[{"text":"{\n \"id\": \"KITE-142\", ...","type":"text"}],"isError":false,...}}
The JSON is the same as over stdio. HTTP adds three things.
1. Headers that repeat the body. MCP-Protocol-Version, Mcp-Method and, for tool calls, Mcp-Name copy fields from the JSON. That lets load balancers and gateways route requests without parsing the body. The headers must match the body. Leave out Mcp-Name, and the Kitebase server returns 400 Bad Request with -32020, “mcp-name header does not match the request body’s ‘name’ parameter”.
2. Two reply shapes. The server answers with either one JSON object or a stream of server-sent events (SSE). SSE is a standard way to send several messages down one HTTP response. The stream carries notifications about that request, such as progress, and then the final reply. The client’s Accept header has to allow both shapes.
3. Security is your job. Anyone who can reach the URL can call it. So the server needs three protections:
- It must check the
Originheader, so a web page in someone’s browser can’t call a server on their laptop. - It should bind to
127.0.0.1when it runs locally. - It should require auth. MCP’s authorization spec is built on OAuth, the standard behind “Sign in with Google” buttons.
You may also see an Mcp-Session-Id header. It belongs to the 2025-11-25 version of this transport. The current one has no sessions, for the same reason as _meta.
Default to stdio for a server on your own machine. There’s nothing to deploy, and you debug your tool logic instead of a web server. Switch to HTTP when many people need one shared server. An example is Kitebase hosting a single server at https://mcp.kitebase.example/mcp for all its customers.
The life of a connection
Here’s the Kitebase server from launch to shutdown, as a host runs it. (The model’s side of a tool call, where the host translates tools and results for Claude, is in article 01.)
-
Launch. Claude Desktop runs the command from its config, say
python server.py. It holds on to the child’s stdin and stdout. -
Discover. The client sends
server/discover. A modern server answers. An old one errors or stays silent, and the client falls back toinitialize. -
List.
tools/listreturnssearch_helpandget_ticket, which the host offers to the model. -
Call. You ask about KITE-142, and the model asks for
get_ticket. The client sendstools/callwithid3. If the client asked for progress updates, the server can send notifications while it works. -
Cancel, sometimes. If you press stop first, the client sends a notification and ignores any late reply:
{"jsonrpc": "2.0", "method": "notifications/cancelled", "params": {"requestId": 3, "reason": "user pressed stop"}}On HTTP there’s no such message. Closing that request’s SSE stream is the cancellation.
-
Reply. The server returns one of three things for
id3: aresult, a result withisError: trueif the tool failed, or a JSON-RPCerrorif the request was malformed. -
Shut down. When you quit, the client closes stdin and waits. If the server crashes instead, the client restarts it. Nothing was stored in the connection, so the client can simply resend whatever was in flight.
In a real host you write none of this protocol code. The official client does steps 2 to 7:
from mcp import Client, StdioServerParameters
params = StdioServerParameters(command=sys.executable, args=[str(SERVER)])
async with Client(params) as client: # launches the server and probes it
print("negotiated protocol version:", client.protocol_version)
tools = await client.list_tools()
print("tools:", [tool.name for tool in tools.tools])
result = await client.call_tool("get_ticket", {"ticket_id": TICKET})
print("is_error:", result.is_error)
print("result:", result.content[0].text)
python main.py --sdk prints:
get_ticket called with 'KITE-142'
negotiated protocol version: 2026-07-28
tools: ['search_help', 'get_ticket']
is_error: False
result: {
"id": "KITE-142",
"title": "Customer locked out after SSO change",
...
The first line is the server’s stderr log. It’s passed through to your terminal and never mixed into the protocol.
Try it yourself
The companion example has two parts:
- a copy of the article 01 Kitebase server, with two extra switches
- the hand-rolled client that printed every message in this article
Download the runnable example (zip)
cd 02-mcp-architecture
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
python main.py
Then try these:
- Run
python main.py --legacyand compare it with the default run. You’ll see an extra round trip at the start, a notification with no reply, and no_metaon later requests. - Run
python main.py --noisy. The client stops with “isn’t JSON-RPC”. Find theprintinserver.py, addfile=sys.stderr, and run it again. - Run
python main.py KITE-999. The protocol succeeds and the tool fails, so you get aresultwithisError: true, not anerror. Then runpython main.py --version 2099-01-01for the opposite: a protocol error on every request.
pip install pytest && pytest -q runs the tests. They start the server as a local subprocess and need no keys and no network.
Common beginner mistakes
- Printing to stdout in a stdio server. One stray line corrupts the stream. Log to stderr.
- Matching replies by order. Replies to concurrent requests can arrive in any order. Match on
id. - Assuming the server remembers you. On
2026-07-28, every request needs its own_meta. Code written for the handshake skips it and gets-32602. - Following an old tutorial with the new SDK.
mcpv2 replacedFastMCPwithMCPServer(from mcp.server import MCPServer). An error importingmcp.server.fastmcpmeans v1 code met v2. - Putting an HTTP server online with no auth. A stdio server can only be reached by its host. An HTTP one can be reached by anyone who finds the URL.
Questions you will face in production
“The server runs fine in my terminal, but the host says it failed to connect.” Run it with the exact command and interpreter from the host’s config. Look at the first line on stdout. If it isn’t JSON, that’s the bug.
If stdout is clean, check the host’s log for initialize. An old host fails there against a server that only speaks the new revision.
“stdio or HTTP for a tool the whole team uses?” HTTP. With stdio, everyone runs and updates their own copy, with their own credentials. A shared HTTP server is one deployment, with real auth in front of it.
Check your understanding
A reply comes back as {"jsonrpc":"2.0","id":7,"error":{"code":-32602,...}} complaining about _meta. What went wrong, and who fixes it?
The client sent a request without the required protocol version and capabilities in _meta. It was probably written for the handshake era.
It’s a client bug, so the client fixes it. Attach _meta to every request, or use the official client, which does that for you.
A tools/call for KITE-999 returns a result with isError: true. A tools/call with protocolVersion "2099-01-01" returns an error with code -32022. Which one does the model see?
The first one.
A tool failure is a normal result. The host passes its message to the model, so the model can fix the input and retry.
A protocol error means the request itself was wrong. The client handles it, here by retrying with a supported version, and the model never sees it.
You want three copies of the Kitebase HTTP server behind a load balancer. Why is that easy on 2026-07-28 and awkward on 2025-11-25?
On 2026-07-28, every request carries its own version and capabilities. So any copy can answer it.
On 2025-11-25, the handshake created a session in one copy’s memory, tracked by the Mcp-Session-Id header. So every later request from that client had to reach that same copy.
What to remember
- A host runs one client per server, each on its own connection. When a server goes missing, check that one connection first.
- Every message is JSON-RPC 2.0. Requests have an
id, replies echo it, and notifications have none. Match replies byid. - On
2026-07-28there’s no handshake. Every request carries its version and capabilities in_meta.server/discovertells you what a server supports. - Older hosts and servers use
initializeandnotifications/initialized. The official client probes and falls back, and SDK v2 servers answer both. - Use stdio for local servers. Stdout is the protocol, logs go to stderr, and closing stdin shuts the server down.
- Use Streamable HTTP for shared servers, with headers that match the body and real auth.
- A tool failure is a result with
isError: true. A broken request is a JSON-RPCerror.
What to study next
You’ve watched the protocol from the client’s side. Article 03: Setting Up Your First MCP Server switches to the server’s side. You build the Kitebase server from scratch and add search_tickets and create_ticket. You test it without a model. Then you connect it to Claude Desktop and Claude Code, which send the same messages you printed here.
Further reading
- MCP: Architecture overview. The official walkthrough of hosts, clients, servers and the discovery sequence, with example messages.
- MCP spec: Overview. The JSON-RPC rules, error codes, the
_metafields and what stateless means, with exact field names. - MCP spec: Versioning and Compatibility. Per-request version negotiation and how to work with handshake-era peers.
- MCP spec: stdio and Streamable HTTP. The two transports, defined precisely.
- MCP spec 2025-11-25: Lifecycle. The
initializehandshake, for the hosts and servers that still use it. - JSON-RPC 2.0 specification. The message format MCP is built on. Short and readable.
Where this article comes from. This is a synthesis of the MCP specification and common practice as of 2026, not a citation of any single paper. The sources above are where the mechanics come from. If you find an error or have a better source for a claim, send me a note and the article gets fixed within a day.