Lesson 2 - Step 1 of 5

HTTP Fundamentals

Understand the basics of HTTP before building a server.

What You're Building

In this lesson, you'll build a simple HTTP server using Python's built-in http.server module. This server will handle JSON requests and responses — the foundation for MCP's HTTP/SSE transport.

HTTP in 60 Seconds

HTTP (HyperText Transfer Protocol) is how clients and servers communicate on the web. For MCP, you need to understand just a few key concepts:

Request Structure

Every HTTP request has:

  • Method — What action to take (GET, POST, etc.)
  • Path — The URL path (/mcp, /api/tools)
  • Headers — Metadata (Content-Type, Authorization)
  • Body — The actual data (for POST/PUT requests)
http
POST /mcp HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Content-Length: 45

{"jsonrpc": "2.0", "method": "tools/list"}

Response Structure

Every HTTP response has:

  • Status Code — Success (200), error (400, 500), etc.
  • Headers — Metadata about the response
  • Body — The actual response data
http
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 52

{"jsonrpc": "2.0", "id": 1, "result": {"tools": []}}

HTTP Methods for MCP

MCP's HTTP transport uses just a few methods:

Method MCP Usage
POST Send JSON-RPC requests to the server
GET Open SSE stream for server-initiated messages
DELETE Close a session (optional)

Important Headers

These headers matter for MCP:

text
# Request headers
Content-Type: application/json       # We're sending JSON
Accept: application/json, text/event-stream  # We accept JSON or SSE
MCP-Protocol-Version: 2025-11-25    # MCP version

# Response headers
Content-Type: application/json       # For single responses
Content-Type: text/event-stream     # For SSE streaming

Status Codes to Know

Code Meaning When to Use
200 OK Request succeeded
400 Bad Request Invalid JSON or malformed request
404 Not Found Session doesn't exist
500 Server Error Something went wrong on the server
Key Takeaway

For MCP's HTTP transport, you mainly need to handle POST requests that contain JSON bodies and respond with JSON (or SSE streams). That's it!