MCP server project · Chapter 6 of 12

Streamable HTTP

By the end of this chapter DabbaDrop's /mcp answers every request on its own: no session to start, plain JSON replies, the same answer from any machine and after any restart. It refuses requests meant for another host or sent by a web page from another site, it lives inside the ordering API with its middleware and its lifespan in the right order, and the lab's inspector talks to it over HTTP with the SDK's client. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: One member at a time
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of MCP server project are free. This one comes with the all labs plan.

Why you need this

On Friday 30 October Kunal rehearsed launch week. DabbaDrop runs the API on 2 machines behind a load balancer, which sends each request to whichever machine is free, and it deploys about 6 times a day. With the MCP server as it is, about half of the assistants' requests came back 404 Session not found: the session had been started on the other machine. And every deploy restarted both machines, so every connected assistant lost its session at once.

The cause is the SDK's defaults, which /mcp has used since chapter 1. initialize starts a session that lives in one process's memory. Another process has never heard of it, and a restarted one has forgotten it. This chapter takes the session out.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    transport.py: the MCP app, stateless

    Write mcp_app(server) in mcp_server/transport.py. It returns server.streamable_http_app with the path /mcp, stateless_http=True, json_response=True and a TransportSecuritySettings with DNS rebinding protection on and the allowed hosts and origins from mcp_settings.

    The Check starts your MCP app on its own and sends it a tools/call with no initialize before it (it must answer 200 with plain JSON and no Mcp-Session-Id), the same call with the Host header evil.example (421) and with the Origin header http://evil.example (403), and one from the origin http://localhost:3000 (allowed).

  2. Task 2

    transport.py: inside the API

    Write mount(app, server) in mcp_server/transport.py. It adds mcp_app(server)'s routes to the FastAPI app, adds its middleware (sub.user_middleware, last first, with app.add_middleware), and replaces the app's lifespan with one that runs server.session_manager.run() around the lifespan the app had.

    The Check mounts a server that needs a bearer token into a FastAPI app with a lifespan of its own: a request without the token must get 401 and one with it 200, and the app's own lifespan must still run. Then it starts two separate whole apps, as two machines would be: initialize on the first, tools/call on the second.

  3. Task 3 · interview question

    Interview: http_client.py, a client over HTTP

    Write open_http(url, token=None, http=None) in mcp_server/http_client.py. It returns Client(streamable_http_client(url, http_client=http)), where http is the given httpx2.AsyncClient or a new one (timeout 30); with a token, the client's requests carry the header authorization: Bearer <token>.

    The Check runs the whole app on a real port and lists its tools with your open_http, then calls find_slots with a token through an httpx2 client of its own and reads the header on the requests it sent.

    Kunal will ask: DabbaDrop turned sessions off. What did a session give, and why can DabbaDrop do without one?

What you will learn

  • Streamable HTTP: one POST per message, and a reply as JSON or as server-sent events.
  • Sessions and Mcp-Session-Id: what the defaults gave, and the real 404 after a restart.
  • Stateless: why DabbaDrop needs no session, and the 202 for a notification.
  • DNS rebinding protection: the Host check (421) and the Origin check (403), plainly.
  • Mounting an ASGI app in FastAPI: its routes, its middleware in the right order, its lifespan.
  • The SDK's client over HTTP, with a header on every request.

More in One member at a time

Build "Streamable HTTP" yourself

DabbaDrop wants AI assistants like Claude Desktop to order for its members, safely. You build the MCP server that lets them, and only as the member who allowed it. 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 server project are free. This one comes with the all labs plan.