MCP servers and clients · Chapter 12 of 16
The HTTP transport
By the end of this chapter you can serve an MCP server over streamable HTTP, connect a client to it by URL, speak the protocol by hand with curl and httpx in both eras, add a plain HTTP route next to it, and choose between stdio and HTTP for a given app. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Clients and production
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of MCP servers and clients are free. This one comes with the all labs plan.
Why you need this
Over stdio, every client starts its own copy of the server. That was fine on Rajat's laptop. Now the managers' WhatsApp bot runs on a cloud machine, the supplier portal on the agency's, and Rajat's assistant on his phone: none of them can start a Python process on Bazaar Bhai's server, and forty store managers would mean forty copies anyway.
Simran wants one copy of the stock server, running as a service, that every app reaches over the network. Gopal's ops team wants a URL they can poll to know it is alive. MCP has a transport for exactly this: streamable HTTP. The tools do not change at all.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
http_server.py: the same tools, over HTTP
In http_server.py, import the server object from stock_server.py (do not copy its tools) and add a plain HTTP route, GET /health, answering the JSON {"ok": true, "stores": 40}, with the number of stores counted in /workspace/data/bazaar.db.
When the file is run, serve it with the streamable HTTP transport on host 127.0.0.1 and port 8000, so clients reach it at http://127.0.0.1:8000/mcp. The check connects to it in both protocol eras and calls its tools.
- Task 2
http_client.py: a client by URL
In http_client.py, connect the SDK's Client to http://127.0.0.1:8000/mcp and print the instructions the client received on the first line, then one line per product that low_stock lists for BB19, in that order, with its units from stock_level, like P003: 3 units.
Do not start the server from this file, and do not open the database. The check starts its own server on port 8000.
- Task 3
by_hand.py: MCP over HTTP, by hand
In by_hand.py, with httpx and no mcp package, POST one 2026-07-28 tools/call of stock_level for BB26 and P051 to http://127.0.0.1:8000/mcp: the _meta envelope (protocolVersion 2026-07-28, clientInfo, clientCapabilities) in its params, and the headers accept (application/json, text/event-stream), mcp-protocol-version, mcp-method and mcp-name.
Print BB26 P051: <units> units from the result's structured content. Then send the same request without the mcp-name header and print Without mcp-name: followed by the status code and the error's message from the response body.
- Task 4 · interview question
Interview: fix_me.py, a server nobody can reach
fix_me.py is the price checker's server. It should serve MCP at http://127.0.0.1:8000/mcp and answer GET /health with {"ok": true}. Fix its three bugs, changing as little as you can:
it serves MCP on the wrong path;
it listens on the wrong port;
its health check answers 500.
Keep its price_of tool and what it returns.
What you will learn
- Serving over HTTP: one line in mcp.run, and the /mcp endpoint.
- A client by URL, with the same Client.
- On the wire: the handshake era's sessions and server-sent events, and the 2026-07-28 era's stateless requests with their headers.
- Plain HTTP routes next to MCP, and running the app with uvicorn.
- stdio or HTTP: which, when.
More in Clients and production
Build The HTTP transport yourself
Bazaar Bhai, a chain of 40 kirana stores in Jaipur, exposes its stock and orders once, to every AI tool. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of MCP servers and clients are free. This one comes with the all labs plan.