> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo No More Sessions

> Demo showing streamable HTTP with MCP, contrasting legacy sticky session using server-issued session IDs and modern stateless protocol using per-request _meta, with example server and commands.

Welcome back. This lesson demonstrates streamable HTTP: a pattern that lets a remote server communicate with a client over HTTP using long-lived connections and event streams. For local processes you typically use STDIO, but when the server runs on a different host or region you use HTTP-based transports such as streamable HTTP.

This demo contrasts two MCP protocol eras:

* Legacy (sticky session): the server issues a session ID which the client must include in headers.
* Modern (stateless): the client includes a `_meta` object inside every request body so the server has all client context without session affinity.

Below are the exact files and commands used in the demo so you can reproduce it locally.

## Catalog — mcp.json

The inspector catalog exposes two server entries: one that represents the legacy protocol era, and another that uses the modern era. Both entries point to the same local HTTP endpoint for the demo server.

mcp.json (catalog)

```json theme={null}
{
  "mcpServers": {
    "shipment-tracker-legacy": {
      "type": "streamable-http",
      "url": "http://127.0.0.1:8000/mcp",
      "metadata": {
        "progressToken": "demo-2"
      }
    },
    "shipment-tracker-modern": {
      "type": "streamable-http",
      "url": "http://127.0.0.1:8000/mcp",
      "protocolEra": "modern"
    }
  }
}
```

Note: Both entries intentionally point to the same `http://127.0.0.1:8000/mcp` endpoint. Running a local HTTP server makes it easy to simulate a remote service while preserving realistic network behavior.

## Server implementation — server.py

This demo server exposes a single tool (`get_shipment_status`) that:

* validates the `ref` input via regex,
* reports progress with `ctx.report_progress(...)`,
* writes logs to stderr so you can see them in the server terminal,
* returns a `Shipment` object or raises a `ToolError` if not found.

Server implementation (server.py)

```python theme={null}
import sys
from typing import Annotated

from pydantic import Field
from mcp.server import MCPServer
from mcp.server.mcpserver import Context
from mcp.server.mcpserver.exceptions import ToolError
from mcp_demo import Shipment, find_shipment

server = MCPServer(name="shipment-tracker", version="2.0.0")


@server.tool("get_shipment_status")
async def get_shipment_status(
    ref: Annotated[
        str,
        Field(
            pattern=r"^PL-\d{4}$",
            description=(
                "The shipment reference to look up, for example PL-4417. "
                "Case-insensitive. This is the only thing the tool needs."
            ),
        ),
    ],
    ctx: Context,
) -> Shipment:
    # Report progress (two-step progress example)
    await ctx.report_progress(1, total=2, message=f"Looking up {ref}")
    print(f"[shipment-tracker] lookup {ref}", file=sys.stderr)

    shipment = find_shipment(ref)

    await ctx.report_progress(2, total=2, message="Found" if shipment else "Not found")

    if shipment is None:
        raise ToolError(
            f"No shipment found with reference {ref}. "
            f"This demo knows about PL-4417, PL-4418 and PL-9002."
        )
    return shipment


if __name__ == "__main__":
    print("shipment-tracker starting on http://127.0.0.1:8000/mcp", file=sys.stderr)
    server.run("streamable-http", host="127.0.0.1", port=8000, json_response=True)
```

## Running the demo

1. Start the server (terminal 1):

```bash theme={null}
cd stage-2a-two-eras
../.venv/bin/python server.py
```

Example server startup output (cleaned):

```bash theme={null}
shipment-tracker starting on http://127.0.0.1:8000/mcp
INFO:     Started server process [57882]
INFO:     Waiting for application startup.
StreamableHTTP session manager started
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```

2. Start the MCP Inspector (terminal 2):

```bash theme={null}
cd stage-2a-two-eras
npx @modelcontextprotocol/inspector --catalog ./mcp.json
```

Inspector startup (cleaned):

```bash theme={null}
Need to install the following packages:
@modelcontextprotocol/inspector@2.13.0
Ok to proceed? (y) y

Starting MCP inspector...

MCP Inspector Web is up and running at:
    http://127.0.0.1:6274?MCP_INSPECTOR_API_TOKEN=c8833f5f...
    Sandbox (MCP Apps): http://127.0.0.1:6275/sandbox

Auth token: c8833f5f...
Secrets: OS keychain

Opening browser...
```

## Legacy era (sticky session) — behavior and examples

* Protocol era: `2025-11-25`.
* The client sends an `initialize` handshake request with client info and protocol version.
* The server responds and issues a session ID via response headers (`MCP-Session-ID`).
* All subsequent requests from the client include the `MCP-Session-ID` header so the server can identify the session (sticky session).

Client initialize (example request body):

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 0,
  "method": "initialize",
  "params": {
    "protocolVersion": "2025-11-25",
    "clientInfo": {
      "name": "mcp-inspector",
      "version": "2.13.0"
    },
    "capabilities": {
      "sampling": {},
      "elicitation": {
        "form": {},
        "url": {}
      }
    },
    "roots": {
      "listChanged": true
    },
    "tasks": {
      "list": {},
      "cancel": {}
    }
  }
}
```

Server initialize response (example body):

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 0,
  "result": {
    "capabilities": {
      "experimental": {},
      "prompts": { "listChanged": false },
      "resources": { "listChanged": false, "subscribe": false },
      "tools": { "listChanged": false }
    },
    "protocolVersion": "2025-11-25",
    "serverInfo": {
      "name": "shipment-tracker",
      "version": "2.0.0"
    }
  }
}
```

Later request headers (legacy):

```http theme={null}
POST /mcp HTTP/1.1
Host: 127.0.0.1:8000
Accept: application/json, text/event-stream
Content-Type: application/json
MCP-Protocol-Version: 2025-11-25
MCP-Session-ID: f2d8c11974564e3a97cdcbbad04499be
```

Example notification body (legacy):

```json theme={null}
{
  "jsonrpc": "2.0",
  "method": "notifications/initialized",
  "id": 1
}
```

This shows the sticky session requirement: the server-supplied session ID header must be included for the server to associate subsequent requests with the same session.

## Modern era (stateless) — behavior and examples

* Protocol era: `2026-07-28`.
* The modern protocol is stateless: every request includes a `_meta` object in the request body carrying protocol version, client identity, and capabilities.
* The server does not return or require a session ID header; it reads the `_meta` content from each request to obtain client context.

Example `server/discover` request headers (modern):

```http theme={null}
POST /mcp HTTP/1.1
Host: 127.0.0.1:8000
Accept: application/json, text/event-stream
Content-Type: application/json
MCP-Method: server/discover
MCP-Protocol-Version: 2026-07-28
```

Request body (modern) — `_meta` carries the client's identity and capabilities:

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": "server-discover-probe-1",
  "method": "server/discover",
  "params": {
    "_meta": {
      "io.modelcontextprotocol/protocolVersion": "2026-07-28",
      "io.modelcontextprotocol/clientInfo": {
        "name": "mcp-inspector",
        "version": "2.13.0"
      },
      "io.modelcontextprotocol/clientCapabilities": {
        "sampling": {},
        "elicitation": {
          "form": {},
          "url": {}
        }
      }
    },
    "roots": {
      "listChanged": true
    },
    "tasks": {
      "list": {},
      "cancel": {}
    }
  }
}
```

Modern response (excerpt) — there is no `MCP-Session-ID` header:

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": "server-discover-probe-1",
  "result": {
    "cacheScope": "private",
    "resultType": "complete",
    "tools": [
      {
        "description": "Look up the current status of a shipment",
        "inputSchema": {
          "type": "object",
          "properties": {
            "ref": {
              "description": "The shipment reference to look up, for example PL-4417.",
              "pattern": "^PL-\\d{4}$",
              "title": "Ref",
              "type": "string"
            }
          },
          "required": ["ref"]
        }
      }
    ]
  }
}
```

The key difference: legacy servers rely on an out-of-band session ID returned by the server (sticky session). Modern servers are stateless — the client sends `_meta` in every request, so the server has all client context it needs without session affinity.

## Quick comparison

| Aspect | Legacy (sticky session) | Modern (stateless) |
| - | - | - |
| Protocol era example | `2025-11-25` | `2026-07-28` |
| Client identity sent | During initialize; server issues session ID | In every request body as `_meta` |
| Session affinity | Required — `MCP-Session-ID` header | Not required |
| Server-issued header | `MCP-Session-ID` | None |
| Server state management | Session state keyed by session ID | Server reads `_meta` from request for client context |

## Practical notes and tips

* A single inspector instance can only connect to one server entry at a time when two entries point at the same hostname but represent different protocol eras — each entry encodes a distinct behavior the inspector must use.
* Using `localhost` in the demo simulates a remote HTTP service while keeping the setup minimal and local.
* The server logs to stderr. For streamable HTTP, servers maintain long-lived connections and event streams; this demo uses `ctx.report_progress` to demonstrate progress events on the stream.

References and further reading:

* MCP Inspector: `https://www.npmjs.com/package/@modelcontextprotocol/inspector`
* For general guidance on long-lived HTTP connections and server-sent events, see your platform's docs for Uvicorn / ASGI servers.

That covers how streamable HTTP differs between the legacy (sticky session) and modern (stateless `_meta`) MCP protocol eras, and how this demo server and inspector illustrate both paradigms.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/model-context-protocol-associate-mcpa-certification/module/282d97e2-04f7-4beb-8301-695ecb3f3f99/lesson/6db69d5f-1223-42d0-ba6a-b2bb77f49a00)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.