|
| 1 | +# todos-server — the reference MCP server, in Python |
| 2 | + |
| 3 | +A small project todo board where **every server-side MCP feature has a real job**: tools that mutate state, resources that expose it, prompts that seed conversations, sampling that borrows the connected host's model, elicitation that asks the user, progress and logs while it works, and per-resource subscriptions that announce every change. It is a faithful port of the TypeScript SDK's [`examples/todos-server`](https://github.com/modelcontextprotocol/typescript-sdk/tree/main/examples/todos-server) — think of it as the "polls app" of MCP servers: small enough to read in one sitting, real enough that nothing in it is contrived. |
| 4 | + |
| 5 | +It serves **both protocol revisions at once** — 2026-07-28 and 2025-11-25 are negotiated per connection, from the same handlers — and **both transports**: stdio and Streamable HTTP. |
| 6 | + |
| 7 | +## Run it |
| 8 | + |
| 9 | +From this directory: |
| 10 | + |
| 11 | +```bash |
| 12 | +# stdio — for hosts that spawn their servers as child processes |
| 13 | +uv run python -m mcp_todos_server |
| 14 | + |
| 15 | +# Streamable HTTP — for remote-style connections (default port 3000; --port or $PORT to change) |
| 16 | +uv run python -m mcp_todos_server --transport streamable-http |
| 17 | +``` |
| 18 | + |
| 19 | +Over stdio the server speaks on stdin/stdout (its own diagnostics go to stderr). Over HTTP it serves `http://127.0.0.1:3000/mcp`. |
| 20 | + |
| 21 | +There is no era flag: both entries detect each connection's revision during the handshake, so a 2025-era client and a 2026-era client can talk to the same process — simultaneously, over HTTP. |
| 22 | + |
| 23 | +Any `mcpServers`-style host can spawn it too: |
| 24 | + |
| 25 | +```jsonc |
| 26 | +{ |
| 27 | + "mcpServers": { |
| 28 | + "todos": { "command": "uv", "args": ["run", "--directory", "/absolute/path/to/examples/servers/todos-server", "python", "-m", "mcp_todos_server"] } |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +The TypeScript SDK's reference host, [`cli-client`](https://github.com/modelcontextprotocol/typescript-sdk/tree/main/examples/cli-client), connects to the HTTP entry out of the box: |
| 34 | + |
| 35 | +```bash |
| 36 | +uv run python -m mcp_todos_server --transport streamable-http # terminal A, this repo |
| 37 | +pnpm --filter @mcp-examples/cli-client start -- --server http://127.0.0.1:3000/mcp # terminal B, typescript-sdk repo |
| 38 | +``` |
| 39 | + |
| 40 | +## What demonstrates what |
| 41 | + |
| 42 | +| Server feature | Where it lives | Notes | |
| 43 | +| -------------------------- | ------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 44 | +| Tools | `add_task`, `add_tasks`, `list_tasks`, `complete_task` | plain CRUD; `add_task` also returns `structuredContent` against an `outputSchema` | |
| 45 | +| Sampling | `prioritize`, `brainstorm_tasks` | the server borrows the _host's_ model; the host shows the request for approval first | |
| 46 | +| Elicitation (form) | `clear_done`, `brainstorm_tasks` | schema-driven forms; accept / decline / cancel all handled | |
| 47 | +| Multi-round input_required | `brainstorm_tasks` | theme+count form → optional custom-amount round → sampling round; state rides `request_state` as a step-discriminated JSON object, sealed by the SDK | |
| 48 | +| Progress | `work_through_tasks`, `add_tasks` | paced per-task progress notifications via `ctx.report_progress` | |
| 49 | +| Logging | every mutating tool, via `log_info` | honours `logging/setLevel` on 2025 connections and the per-request log-level `_meta` opt-in on 2026-07-28 | |
| 50 | +| Resources | `todos://board`, `todos://tasks/{id}` | one concrete resource + a URI template; every task also appears in `resources/list` | |
| 51 | +| Subscriptions | the board | `resources/subscribe`/`unsubscribe` handlers for 2025-era clients; `subscriptions/listen` streams (over HTTP) for 2026-07-28; every mutation notifies | |
| 52 | +| list_changed | every mutation | resource list + resource updated notifications on both eras | |
| 53 | +| Prompts + completions | `plan-my-day`, `seed-board` | argument completion (project names, themes, task ids) wired to `completion/complete` via `@mcp.completion()` | |
| 54 | + |
| 55 | +The two protocol eras differ in how interactive conversations travel: on 2025-era connections the wire carries _pushed_ `elicitation/create` / `sampling/createMessage` requests; on 2026-07-28 the server returns `input_required` results and the client retries the call with the answers. The interactive tools (`brainstorm_tasks`, `clear_done`, `prioritize`) are written **once**, as state machines over `input_required` rounds — on 2025-era connections the example's small `run_interactive` driver fulfils the same rounds as real push-style requests (the job the TypeScript SDK's built-in legacy shim does), so there is no era branch in any handler. For single-question preconditions, the SDK's own era-agnostic form is a `Resolve(...)` dependency that returns `Elicit(...)` — see the [Dependencies tutorial](https://py.sdk.modelcontextprotocol.io/v2/tutorial/dependencies/); this example hand-rolls the rounds instead so the multi-round flow, the sampling rounds, and the carried state are all visible in one place. |
| 56 | + |
| 57 | +## Configuration |
| 58 | + |
| 59 | +| Env var | Effect | |
| 60 | +| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 61 | +| `REQUEST_STATE_SECRET` | Key for the sealed `request_state` (≥ 32 bytes). Unset, the SDK generates a per-process key — fine whenever a single process serves the whole flow. | |
| 62 | +| `PORT` | HTTP port when `--port` isn't passed (default 3000). | |
| 63 | + |
| 64 | +## Layout |
| 65 | + |
| 66 | +```text |
| 67 | +mcp_todos_server/ |
| 68 | + server.py transport entry: stdio by default, streamable HTTP behind --transport |
| 69 | + todos.py the application: state, tools, resources, prompts, subscriptions — every feature above |
| 70 | +``` |
| 71 | + |
| 72 | +## Fidelity to the TypeScript reference |
| 73 | + |
| 74 | +This port is verified against the TypeScript `todos-server` by driving both over stdio and HTTP, on both protocol eras, through an identical scripted scenario (same tool calls, elicitation answers, and sampling replies): every tool result text, structured output, elicitation form, sampling request, progress sequence, and log line matches. Known, deliberate differences: |
| 75 | + |
| 76 | +- **JSON Schema style.** Input schemas come from pydantic here and zod there, so cosmetics differ (pydantic emits `title`s and `$defs` refs for the nested `add_tasks` items). The schemas are semantically identical. |
| 77 | +- **`resources/list` composition.** The TypeScript `ResourceTemplate` has a `list` callback; `MCPServer` doesn't, so this example overrides the low-level `resources/list` handler to append one entry per task (the same private-API pattern the everything-server uses for `resources/subscribe` and `logging/setLevel`). |
| 78 | +- **`subscriptions/listen` over stdio.** The Python SDK serves 2026-era listen streams on streamable HTTP only; over stdio a listen request is rejected. Board-change notifications over stdio therefore reach 2025-era subscribers only. |
| 79 | +- **Legacy HTTP interactivity.** The TypeScript server's per-request HTTP posture refuses push-style sampling/elicitation for 2025-era HTTP clients; the Python server's default Streamable HTTP mode is stateful, so those tools work on that leg here. |
| 80 | +- **Legacy HTTP fan-out.** Pre-2026 board-change notifications go to the session that made the mutating call. Over stdio that is every subscriber; with several concurrent 2025-era HTTP sessions, the others don't hear about it (the TypeScript entry broadcasts via its handler notifier). Pre-2026 HTTP handshakes also advertise `listChanged: false` — the SDK exposes no seam to change that on the HTTP path (stdio is patched, see `serve_stdio`). |
| 81 | +- **Cancellation granularity.** When a 2025-era client cancels `work_through_tasks`, this SDK interrupts the handler at its next `await` (the in-flight pretend task stays open); the TypeScript server checks between tasks and finishes the in-flight one. |
0 commit comments