Repository navigation
fix(server): reject non-string protocolVersion - #579
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
server/middleware.py: new pure-ASGIInitializeParamsMiddleware. OnPOST /mcp, a single JSON-RPCinitializewhoseparams.protocolVersionis 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 requestidis echoed when it is a valid JSON-RPC id. This is the same status and code themcplibrary 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 outsideSessionCapMiddleware, inside the 1 MB request-body limit, so the body it buffers is already bounded.mcplibrary typesprotocolVersionasstr | intand maps any unsupported value toLATEST_PROTOCOL_VERSION.SessionCapMiddlewarealready parses bodies, but it skips any request without a session id, andinitializenever has one. The installed library is not patched."9999-99-99") still returns 200 and negotiates a supported version, as the MCP spec allows.Testing
tests/test_security.py::TestInitializeProtocolVersion:12345,1.5,True,None, a list and an object; checks for 400,-32602, the echoed id, noresultand no session id"9999-99-99"still gets 200 and a version inSUPPORTED_PROTOCOL_VERSIONS"2025-06-18"is echoed backserver/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 passedtools/listis unchanged, so the tool-schema hash does not need re-pinning.Closes #473