mcp: handle bad or missing params (#210)

Audit all cases where params must not be null, and enforce this using
methodInfo via a new methodFlags bitfield that parameterizes method
requirements. Write extensive conformance tests catching all the
(server-side) crashes that were possible.

We should go further and validate schema against the spec, but that is
more indirect, more complicated, and potentially slower. For now we
adopt this more explicit approach.

Still TODO in a subsequent CL: verify the client side of this with
client conformance tests.

Additionally, improve some error messages that were redundant or leaked
internal implementation details.

Fixes #195
This commit is contained in:
Robert Findley 2025-07-31 10:57:59 -04:00 • committed by GitHub
commit b34ba2149d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 170 additions and 52 deletions

View file

@ -4,10 +4,11 @@ bad requests.
Fixed bugs:
- No id in 'initialize' should not panic (#197).
- No id in 'ping' should not panic (#194).
- Notifications with IDs should not be treated like requests.
TODO:
- No params in 'initialize' should not panic (#195).
- Notifications with IDs should not be treated like requests. (#196)
- No params in 'logging/setLevel' should not panic.
- No params in 'completion/complete' should not panic.
- JSON null params should also not panic in these cases.
-- prompts --
code_review
@ -22,6 +23,11 @@ code_review
"clientInfo": { "name": "ExampleClient", "version": "1.0.0" }
}
}
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize"
}
{
"jsonrpc": "2.0",
"id": 2,
@ -32,10 +38,21 @@ code_review
"clientInfo": { "name": "ExampleClient", "version": "1.0.0" }
}
}
{"jsonrpc":"2.0", "id": 3, "method":"notifications/initialized"}
{"jsonrpc":"2.0", "id": 3, "method": "notifications/initialized"}
{"jsonrpc":"2.0", "method":"ping"}
{"jsonrpc":"2.0", "id": 4, "method": "logging/setLevel"}
{"jsonrpc":"2.0", "id": 5, "method": "completion/complete"}
{"jsonrpc":"2.0", "id": 4, "method": "logging/setLevel", "params": null}
-- server --
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32600,
"message": "invalid request: missing required \"params\""
}
}
{
"jsonrpc": "2.0",
"id": 2,
@ -59,6 +76,30 @@ code_review
"id": 3,
"error": {
"code": -32600,
"message": "JSON RPC invalid request: unexpected id for \"notifications/initialized\""
"message": "invalid request: unexpected id for \"notifications/initialized\""
}
}
{
"jsonrpc": "2.0",
"id": 4,
"error": {
"code": -32600,
"message": "invalid request: missing required \"params\""
}
}
{
"jsonrpc": "2.0",
"id": 5,
"error": {
"code": -32600,
"message": "invalid request: missing required \"params\""
}
}
{
"jsonrpc": "2.0",
"id": 4,
"error": {
"code": -32600,
"message": "handling 'logging/setLevel': invalid request: missing required \"params\""
}
}

View file

@ -1,7 +1,8 @@
Check behavior of a server with just prompts.
Fixed bugs:
- empty tools lists should not be returned as 'null'
- Empty tools lists should not be returned as 'null'.
- No params in 'prompts/get' should not panic.
-- prompts --
code_review
@ -19,6 +20,7 @@ code_review
}
{ "jsonrpc": "2.0", "id": 2, "method": "tools/list" }
{ "jsonrpc": "2.0", "id": 4, "method": "prompts/list" }
{ "jsonrpc": "2.0", "id": 5, "method": "prompts/get" }
-- server --
{
"jsonrpc": "2.0",
@ -63,3 +65,11 @@ code_review
]
}
}
{
"jsonrpc": "2.0",
"id": 5,
"error": {
"code": -32600,
"message": "invalid request: missing required \"params\""
}
}

View file

@ -2,6 +2,9 @@ Check behavior of a server with just resources.
Fixed bugs:
- A resource result holds a slice of contents, not just one.
- No params in 'resource/read' should not panic.
- No params in 'resources/subscribe' should not panic.
- No params in 'resources/unsubscribe' should not panic.
-- resources --
info
@ -39,6 +42,8 @@ info.txt
"roots": []
}
}
{ "jsonrpc": "2.0", "id": 4, "method": "resources/read" }
{ "jsonrpc": "2.0", "id": 5, "method": "resources/subscribe" }
-- server --
{
"jsonrpc": "2.0",
@ -107,3 +112,19 @@ info.txt
]
}
}
{
"jsonrpc": "2.0",
"id": 4,
"error": {
"code": -32600,
"message": "invalid request: missing required \"params\""
}
}
{
"jsonrpc": "2.0",
"id": 5,
"error": {
"code": -32600,
"message": "invalid request: missing required \"params\""
}
}

View file

@ -4,6 +4,7 @@ Fixed bugs:
- "tools/list" can have missing params
- "_meta" should not be nil
- empty resource or prompts should not be returned as 'null'
- the server should not crash when params are passed to tools/call
-- tools --
greet
@ -22,6 +23,7 @@ greet
{ "jsonrpc": "2.0", "id": 2, "method": "tools/list" }
{ "jsonrpc": "2.0", "id": 3, "method": "resources/list" }
{ "jsonrpc": "2.0", "id": 4, "method": "prompts/list" }
{ "jsonrpc": "2.0", "id": 5, "method": "tools/call" }
-- server --
{
"jsonrpc": "2.0",
@ -81,3 +83,11 @@ greet
"prompts": []
}
}
{
"jsonrpc": "2.0",
"id": 5,
"error": {
"code": -32600,
"message": "invalid request: missing required \"params\""
}
}