MCP servers and clients in TypeScript · Chapter 12 of 16

The HTTP transport

By the end of this chapter you can serve an MCP server over streamable HTTP with node:http, connect a client to it by URL, speak the protocol by hand with curl and fetch in both eras, add plain HTTP routes 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
  • TypeScript
  • In the all labs plan

The first 2 chapters of MCP servers and clients in TypeScript 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 Node 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, because the server file already exports its factory.

What you build: 4 tasks

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

  1. Task 1

    http-server.ts: the same tools, over HTTP

    In http-server.ts, import createServer from './stock-server.ts' (do not copy its tools) and serve it over streamable HTTP with node:http on host 127.0.0.1 and port 8000: createMcpHandler, wrapped with toNodeHandler, behind the localhost Host and Origin guards from @modelcontextprotocol/node.

    Send /mcp to the MCP handler, answer GET /health yourself with the JSON {"ok":true,"stores":40}, with the number of stores counted in /workspace/data/bazaar.db, and answer any other path with a 404. The check connects at http://127.0.0.1:8000/mcp in both protocol eras and calls its tools.

  2. Task 2

    http-client.ts: a client by URL

    In http-client.ts, connect the SDK's Client (auto mode) 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. Close the client.

    Do not start the server from this file, and do not open the database. The check starts its own server on port 8000.

  3. Task 3

    by-hand.ts: MCP over HTTP, by hand

    In by-hand.ts, with fetch 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 content-type, 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.

  4. Task 4 · interview question

    Interview: fix-me.ts, a server nobody can reach

    fix-me.ts 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 never finishes answering.

    Keep its price_of tool and what it returns.

What you will learn

  • Serving over HTTP: createMcpHandler(createServer), toNodeHandler and node:http, on 127.0.0.1.
  • The guards that belong in front of a local HTTP server, and why.
  • A client by URL: StreamableHTTPClientTransport.
  • On the wire: the handshake era's event streams, and the 2026-07-28 era's plain JSON with its headers, and exactly what this server refuses.
  • Plain HTTP routes next to MCP, written in your own node:http code.
  • 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. You build it in TypeScript. 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 in TypeScript are free. This one comes with the all labs plan.