|
14 | 14 |
|
15 | 15 | from mcp.types import ( |
16 | 16 | JSONRPCNotification, |
17 | | - JSONRPCRequest, |
18 | 17 | jsonrpc_message_adapter, |
19 | 18 | ) |
20 | 19 |
|
21 | 20 |
|
22 | | -class TestNullIdRejection: |
23 | | - """Verify that ``"id": null`` is never silently absorbed.""" |
| 21 | +def test_notification_rejects_id_field() -> None: |
| 22 | + """JSONRPCNotification must not accept messages with an 'id' field.""" |
| 23 | + with pytest.raises(ValidationError, match="must not include an 'id' field"): |
| 24 | + JSONRPCNotification.model_validate({"jsonrpc": "2.0", "method": "initialize", "id": None}) |
24 | 25 |
|
25 | | - def test_request_rejects_null_id(self) -> None: |
26 | | - """JSONRPCRequest correctly rejects null id.""" |
27 | | - with pytest.raises(ValidationError): |
28 | | - JSONRPCRequest.model_validate({"jsonrpc": "2.0", "method": "initialize", "id": None}) |
29 | 26 |
|
30 | | - def test_notification_rejects_id_field(self) -> None: |
31 | | - """JSONRPCNotification must not accept messages with an 'id' field.""" |
32 | | - with pytest.raises(ValidationError, match="must not include an 'id' field"): |
33 | | - JSONRPCNotification.model_validate({"jsonrpc": "2.0", "method": "initialize", "id": None}) |
| 27 | +@pytest.mark.parametrize("id_value", [None, 0, 1, "", "abc"]) |
| 28 | +def test_notification_rejects_any_id_value(id_value: object) -> None: |
| 29 | + """Notification rejects 'id' regardless of value — null, int, or str.""" |
| 30 | + with pytest.raises(ValidationError): |
| 31 | + JSONRPCNotification.model_validate({"jsonrpc": "2.0", "method": "test", "id": id_value}) |
34 | 32 |
|
35 | | - def test_notification_rejects_any_id_value(self) -> None: |
36 | | - """Notification rejects 'id' regardless of value — null, int, or str.""" |
37 | | - for id_value in [None, 0, 1, "", "abc"]: |
38 | | - with pytest.raises(ValidationError): |
39 | | - JSONRPCNotification.model_validate({"jsonrpc": "2.0", "method": "test", "id": id_value}) |
40 | 33 |
|
41 | | - def test_message_adapter_rejects_null_id(self) -> None: |
42 | | - """JSONRPCMessage union must not accept ``"id": null``.""" |
43 | | - raw = {"jsonrpc": "2.0", "method": "initialize", "id": None} |
44 | | - with pytest.raises(ValidationError): |
45 | | - jsonrpc_message_adapter.validate_python(raw) |
| 34 | +def test_message_adapter_rejects_null_id() -> None: |
| 35 | + """JSONRPCMessage union must not accept ``"id": null``.""" |
| 36 | + raw = {"jsonrpc": "2.0", "method": "initialize", "id": None} |
| 37 | + with pytest.raises(ValidationError): |
| 38 | + jsonrpc_message_adapter.validate_python(raw) |
46 | 39 |
|
47 | | - def test_message_adapter_rejects_null_id_json(self) -> None: |
48 | | - """Same test but via validate_json (the path used by transports).""" |
49 | | - raw_json = json.dumps({"jsonrpc": "2.0", "method": "initialize", "id": None}) |
50 | | - with pytest.raises(ValidationError): |
51 | | - jsonrpc_message_adapter.validate_json(raw_json) |
52 | 40 |
|
53 | | - def test_valid_notification_still_works(self) -> None: |
54 | | - """A valid notification (no 'id' field at all) must still parse fine.""" |
55 | | - msg = JSONRPCNotification.model_validate({"jsonrpc": "2.0", "method": "notifications/initialized"}) |
56 | | - assert msg.method == "notifications/initialized" |
57 | | - |
58 | | - def test_valid_notification_with_params(self) -> None: |
59 | | - """Notification with params but no 'id' should work.""" |
60 | | - msg = JSONRPCNotification.model_validate( |
61 | | - {"jsonrpc": "2.0", "method": "notifications/progress", "params": {"progress": 50}} |
62 | | - ) |
63 | | - assert msg.method == "notifications/progress" |
64 | | - assert msg.params == {"progress": 50} |
65 | | - |
66 | | - def test_valid_request_with_string_id(self) -> None: |
67 | | - """A valid request with a string id still works.""" |
68 | | - msg = JSONRPCRequest.model_validate({"jsonrpc": "2.0", "method": "initialize", "id": "abc-123"}) |
69 | | - assert msg.id == "abc-123" |
70 | | - |
71 | | - def test_valid_request_with_int_id(self) -> None: |
72 | | - """A valid request with an integer id still works.""" |
73 | | - msg = JSONRPCRequest.model_validate({"jsonrpc": "2.0", "method": "initialize", "id": 42}) |
74 | | - assert msg.id == 42 |
75 | | - |
76 | | - def test_message_adapter_parses_valid_request(self) -> None: |
77 | | - """The union adapter correctly identifies a valid request.""" |
78 | | - raw = {"jsonrpc": "2.0", "method": "initialize", "id": 1} |
79 | | - parsed = jsonrpc_message_adapter.validate_python(raw) |
80 | | - assert isinstance(parsed, JSONRPCRequest) |
81 | | - assert parsed.id == 1 |
82 | | - |
83 | | - def test_message_adapter_parses_valid_notification(self) -> None: |
84 | | - """The union adapter correctly identifies a valid notification.""" |
85 | | - raw = {"jsonrpc": "2.0", "method": "notifications/initialized"} |
86 | | - parsed = jsonrpc_message_adapter.validate_python(raw) |
87 | | - assert isinstance(parsed, JSONRPCNotification) |
88 | | - assert parsed.method == "notifications/initialized" |
89 | | - |
90 | | - def test_message_adapter_parses_notification_json(self) -> None: |
91 | | - """The union adapter correctly identifies a valid notification via JSON.""" |
92 | | - raw_json = json.dumps({"jsonrpc": "2.0", "method": "notifications/initialized"}) |
93 | | - parsed = jsonrpc_message_adapter.validate_json(raw_json) |
94 | | - assert isinstance(parsed, JSONRPCNotification) |
| 41 | +def test_message_adapter_rejects_null_id_json() -> None: |
| 42 | + """Same test but via validate_json (the path used by transports).""" |
| 43 | + raw_json = json.dumps({"jsonrpc": "2.0", "method": "initialize", "id": None}) |
| 44 | + with pytest.raises(ValidationError): |
| 45 | + jsonrpc_message_adapter.validate_json(raw_json) |
0 commit comments