stateless-mcp-scale-demo

stateless-mcp-scale-demo

Demonstrates a stateless MCP server workflow for purchase requests, using signed request handles to maintain state across load-balanced replicas without session stickiness.

Category
访问服务器

README

Stateless MCP Scale Demo

A small, production-minded demonstration of the MCP 2026-07-28 stateless protocol core.

The same multi-step purchase workflow is served by two independent MCP replicas behind an ordinary Nginx round-robin load balancer. There are:

  • no protocol sessions;
  • no sticky routing;
  • no shared session database;
  • no hidden in-memory workflow state.

Instead, every mutating tool returns an explicit, HMAC-signed request_handle. The client passes that handle into the next tool call, and any replica with the shared signing key can validate it and continue the workflow.

This demo is intentionally small. It illustrates architecture, not a complete authorization or purchasing system.

Why this exists

The 2026-07-28 MCP specification retired the required initialize handshake and Mcp-Session-Id for modern requests. A request is self-describing, so it can land on any healthy replica. Application state is still allowed; the important change is that it should be explicit rather than hidden in the transport.

This repository makes that behavior visible:

  1. Nginx sends calls to mcp-a and mcp-b in round-robin order.
  2. Each response includes served_by.
  3. The workflow survives replica changes because its state travels in request_handle.
  4. Nginx logs Mcp-Method, Mcp-Name, and Mcp-Protocol-Version, showing how a gateway can observe and govern tool traffic without parsing the JSON body.

Architecture

sequenceDiagram
    participant C as MCP client
    participant G as Nginx gateway
    participant A as MCP replica A
    participant B as MCP replica B

    C->>G: create_purchase_request
    G->>A: tools/call
    A-->>C: signed request_handle + served_by=mcp-a

    C->>G: add_line_item(request_handle)
    G->>B: tools/call
    B->>B: verify HMAC and decode state
    B-->>C: updated request_handle + served_by=mcp-b

    C->>G: review_purchase_request(request_handle)
    G->>A: tools/call
    A->>A: verify the same explicit state
    A-->>C: summary + served_by=mcp-a

Tools

Tool Purpose
create_purchase_request Creates the workflow and returns its first signed handle.
add_line_item Accepts a handle and returns a new handle with an immutable state revision.
review_purchase_request Validates and summarizes the handle without changing it.
submit_purchase_request Demonstrates a policy boundary: requests above €500 require explicit human approval.

Run with two replicas

Requirements: Docker and Docker Compose.

cp .env.example .env
# Replace STATE_SIGNING_KEY in .env. For example:
# openssl rand -hex 32

docker compose up --build

The MCP endpoint is:

http://localhost:8080/mcp

In a second terminal, install the project and run the client:

uv sync --dev
uv run python scripts/demo_client.py

Representative output—the exact replica order can vary:

create_purchase_request     replica=mcp-a, total=€0.00
add_line_item (GPU)         replica=mcp-b, total=€450.00
add_line_item (storage)     replica=mcp-a, total=€570.00
review_purchase_request     replica=mcp-b, total=€570.00
submit (without approval)   replica=mcp-a, total=€570.00, status=approval_required
submit (approved)           replica=mcp-b, total=€570.00, status=accepted

Now inspect the gateway logs:

docker compose logs gateway

You should see fields such as:

mcp_method=tools/call mcp_name=add_line_item protocol=2026-07-28 upstream=...

That is the practical value of header-based routing: an API gateway, WAF, or rate limiter can identify the MCP method and tool name directly from headers.

Test with MCP Inspector

The reference Inspector supports modern remote MCP servers:

npx @modelcontextprotocol/inspector --server-url http://localhost:8080/mcp --transport http

Run tests

uv sync --dev
uv run ruff check .
uv run pytest -q

The tests prove that a handle minted by one replica can be verified by another replica with the same key, while tampered, expired, or differently signed handles are rejected.

Important security notes

  • The handle is signed, not encrypted. Its contents can be decoded by the client. Never put credentials, secrets, or sensitive personal data in it.
  • A real application can return an opaque ID and store state in PostgreSQL, Redis, or another durable system. The MCP transport still remains stateless.
  • Tool annotations are behavioral hints, not authorization controls.
  • The demo disables SDK DNS-rebinding protection because Nginx controls the inbound Host header locally. A public deployment should configure explicit allowed_hosts and allowed_origins.
  • The human_approved field demonstrates a policy checkpoint, not cryptographic proof of a human decision. Production approval should be tied to authenticated identity and authorization.
  • The submission is simulated and idempotent; no payment or external order is created.

What this demonstrates

Stateless MCP does not mean “no state.” It means protocol state is not hidden inside a transport session. Once the workflow handle is explicit, the system becomes easier to scale, test, observe, and reason about.

References

  • Model Context Protocol 2026-07-28 specification announcement
  • Official MCP Python SDK v2 documentation
  • Simon Willison, “Stateless MCP has recaptured my interest”

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选
mcp-server-qdrant

mcp-server-qdrant

这个仓库展示了如何为向量搜索引擎 Qdrant 创建一个 MCP (Managed Control Plane) 服务器的示例。

官方
精选
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选