Neural Mastery
← Practice
JSON-RPC 2.0 Request Shape Validationmedium

JSON-RPC 2.0 Request Shape Validation

medium Agents in Production 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement JSON-RPC 2.0 request validation, the transport-level contract MCP is built directly on top of.

Task

Implement is_valid_jsonrpc_request(message). A valid request has EXACTLY: jsonrpc equal to the string '2.0', a method key whose value is a non-empty string, and (if present) a params key that is a dict or list. id is optional (its absence marks it a notification) but if present must be a str, int, or None. Return True only if all present fields satisfy these rules.

Function Signature

is_valid_jsonrpc_request(message: dict) -> bool

Examples

Example 1: valid request with id
Input: message = {'jsonrpc': '2.0', 'method': 'tools/list', 'id': 1}
Output: True
Example 2: valid notification no id
Input: message = {'jsonrpc': '2.0', 'method': 'notifications/ping'}
Output: True
Example 3: wrong jsonrpc version
Input: message = {'jsonrpc': '1.0', 'method': 'x', 'id': 1}
Output: False

Constraints

  • message is a dict
Python3Saved ✓
def is_valid_jsonrpc_request(message):
# Your implementation here
pass

Case 1: valid request with id
Input: message = {'jsonrpc': '2.0', 'method': 'tools/list', 'id': 1}
Expected: True
Case 2: valid notification no id
Input: message = {'jsonrpc': '2.0', 'method': 'notifications/ping'}
Expected: True
Case 3: wrong jsonrpc version
Input: message = {'jsonrpc': '1.0', 'method': 'x', 'id': 1}
Expected: False