Runnable example

MCP tool server (2026-07-28 specification)

Build a stateless MCP tool server with JSON-RPC 2.0 dispatch, schema validation, error isolation, and the input_required round-trip — with no initialize handshake.

Intermediate15–20 minAI agentsPython 3.12+ · Standard library only · No API key

The problem

The Model Context Protocol connects AI models and agents to external tools and data. A naive server crashes on unknown tools, skips argument validation, and still assumes the initialize handshake that the 2026-07-28 specification removed.

The contract

  1. Answer tools/list and tools/call directly; the 2026-07-28 specification is stateless and has no initialize handshake or sessions.
  2. Validate _meta.protocolVersion on every request and reject unsupported versions as invalid requests.
  3. Return JSON-RPC error codes -32600, -32601, and -32602 for invalid requests, removed or unknown methods, and invalid params.
  4. Validate required tool arguments against the declared input schema before execution.
  5. Isolate handler failures as structured isError results instead of breaking the transport.
  6. Complete the input_required round-trip: return the pending requests, then finish on the retry carrying params.inputResponses.

Verify it locally

From the root of the FlyPython repository, reproduce the broken starter first, then verify the reviewed solution:

python examples/mcp-server/verify.py starter --expect-failure
python examples/mcp-server/verify.py solution

Hand it to a coding agent

  1. Clone the repository and reproduce the starter failure with the command above.
  2. Give the task contract ↗ to your coding agent and allow changes only in starter/mcp_server.py.
  3. Run python examples/mcp-server/verify.py starter and treat any failure as unfinished work.
  4. Compare the patch with the reference solution ↗ — the goal is the same contract with a small, readable diff, not identical syntax.

Keep going

Where this example sits in the roadmap.

Reviewed 2026-09-06 · Repository content version 2. Continue with the related learning path, the repository directory, or the MCP migration guide for the specification background.