From d8805cb93b6c3fe18c403338becb2dfe61b139e6 Mon Sep 17 00:00:00 2001 From: saidsurucu Date: Mon, 16 Feb 2026 01:29:17 +0300 Subject: [PATCH] fix: Reject null JSON-RPC IDs per MCP spec 2025-11-25 Monkey-patch JSONRPCNotification to use extra="forbid" so that requests with "id": null are no longer misclassified as notifications (202 Accepted). They now correctly fail validation and return a -32600 Invalid Request error. --- mcp_server_main.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mcp_server_main.py b/mcp_server_main.py index 5f94348..265f1f5 100644 --- a/mcp_server_main.py +++ b/mcp_server_main.py @@ -1,4 +1,18 @@ # mcp_server_main.py + +# --- MCP Spec Compliance: Reject null JSON-RPC IDs --- +# The mcp SDK's JSONRPCNotification uses extra="allow", which causes +# {"id": null} to be misclassified as a notification (202 Accepted). +# Per MCP 2025-11-25, null IDs must be rejected with -32600 Invalid Request. +# Changing to extra="forbid" makes validation fail for null IDs, +# returning a proper JSON-RPC error response. +from mcp.types import JSONRPCNotification as _McpJSONRPCNotification, JSONRPCMessage as _McpJSONRPCMessage +from pydantic import ConfigDict as _ConfigDict +_McpJSONRPCNotification.model_config = _ConfigDict(extra="forbid") +_McpJSONRPCNotification.model_rebuild(force=True) +_McpJSONRPCMessage.model_rebuild(force=True) +# --- End MCP Spec Compliance --- + import asyncio import atexit import logging