fix(mcp): handle null JSON-RPC request payloads safely#987
Conversation
|
Good catch on the 1. Missing Other error branches in return {"jsonrpc": "2.0", "id": None, "error": {"code": -32600, "message": "Invalid Request"}}2. No regression test A parametrised test covering @pytest.mark.parametrize("payload", [None, [], "plain", 42, True])
def test_handle_request_invalid_payload_returns_jsonrpc_error(payload):
resp = handle_request(payload)
assert resp == {"jsonrpc": "2.0", "id": None, "error": {"code": -32600, "message": "Invalid Request"}}Otherwise a narrow fix on a real bug. |
|
@mvalentsev faced the issue when agent just refused to send a proper request. Mitigated this by adding
|
When the MCP client sends a malformed or null top-level request, prevent the AttributeError on request.get() by explicitly validating that the request is a dictionary. Returns standard JSON-RPC Error -32600 (Invalid Request) instead of crashing the server.
…add validation tests
fcb9709 to
869ab38
Compare
What does this PR do?
When an MCP client sends a malformed or
nulltop-level request,handle_request(request)crashes with anAttributeErrorbecause it attempts to call.get()on a non-dictionary object.This PR adds explicit dictionary type validation at the top of the handler. Instead of crashing the server and disconnecting the client, it now gracefully returns a standard JSON-RPC Error
-32600(Invalid Request), adhering to robust null-handling practices for JSON-RPC parameters.How to test
null,[], or a plain string) to the standard input.{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}}Checklist
python -m pytest tests/ -v)ruff check .)