Skip to content

fix(server): reject non-string protocolVersion - #579

Merged
chris-dare-dev merged 1 commit into
mainfrom
fix/473-initialize-protocol-version
Nov 21, 2026
Merged

chris-dare-dev merged 1 commit into
mainfrom
fix/473-initialize-protocol-version

Conversation

@chris-dare-agent

Copy link
Copy Markdown
Contributor

What changed

  • server/middleware.py: new pure-ASGI InitializeParamsMiddleware. On POST /mcp, a single JSON-RPC initialize whose params.protocolVersion is present but not a string (int, float, bool, null, array, object) gets HTTP 400 with a JSON-RPC error -32602 ("Invalid params: initialize.protocolVersion must be a string"). The request id is echoed when it is a valid JSON-RPC id. This is the same status and code the mcp library uses for its own validation failures. Every other request, including bodies that do not parse, is replayed to FastMCP unchanged.
  • server/main.py: the middleware is mounted just outside SessionCapMiddleware, inside the 1 MB request-body limit, so the body it buffers is already bounded.
  • Why a middleware: the mcp library types protocolVersion as str | int and maps any unsupported value to LATEST_PROTOCOL_VERSION. SessionCapMiddleware already parses bodies, but it skips any request without a session id, and initialize never has one. The installed library is not patched.
  • An unknown string version (e.g. "9999-99-99") still returns 200 and negotiates a supported version, as the MCP spec allows.

Testing

  • tests/test_security.py::TestInitializeProtocolVersion:
    • regression test, parametrized over 12345, 1.5, True, None, a list and an object; checks for 400, -32602, the echoed id, no result and no session id
    • unknown string "9999-99-99" still gets 200 and a version in SUPPORTED_PROTOCOL_VERSIONS
    • supported "2025-06-18" is echoed back
  • With the server/ change stashed, all 6 rejection cases fail (they get 200). With it applied, they pass.
  • .venv/bin/python -m ruff check .: All checks passed!
  • .venv/bin/python -m pytest tests/test_tools_all.py tests/test_security.py tests/test_session_caps.py tests/test_server_startup.py tests/test_server_tool_schema.py tests/test_mcp_instructions.py: 270 passed
  • The full suite was not run, because torch cannot import in this environment.

tools/list is unchanged, so the tool-schema hash does not need re-pinning.

Closes #473

The mcp library models initialize.protocolVersion as str | int and
downgrades any unsupported value to its latest version, so an integer
version negotiated with HTTP 200. A pure-ASGI middleware in front of
FastMCP now answers a non-string protocolVersion with HTTP 400 and
JSON-RPC -32602. An unknown string version still passes through and is
negotiated to a supported version, as the MCP spec allows.

Closes #473
@chris-dare-dev
chris-dare-dev merged commit 42c44bf into main Nov 21, 2026
4 checks passed
@chris-dare-dev
chris-dare-dev deleted the fix/473-initialize-protocol-version branch November 21, 2026 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chaos-api-06: MCP initialize accepts garbage/wrong-type protocolVersion (int 12345, '9999-99-99') with HTTP 200

1 participant