mcp: reject notifications with unexpected ID field (#204)
Use the new 'isRequest' field on methodInfo to also reject notifications with an unexpected ID field. For #196
This commit is contained in:
parent
9f48a04971
commit
56734edf46
2 changed files with 14 additions and 1 deletions
|
|
@ -155,7 +155,10 @@ func checkRequest(req *jsonrpc.Request, infos map[string]methodInfo) (methodInfo
|
||||||
return methodInfo{}, fmt.Errorf("%w: %q unsupported", jsonrpc2.ErrNotHandled, req.Method)
|
return methodInfo{}, fmt.Errorf("%w: %q unsupported", jsonrpc2.ErrNotHandled, req.Method)
|
||||||
}
|
}
|
||||||
if info.isRequest && !req.ID.IsValid() {
|
if info.isRequest && !req.ID.IsValid() {
|
||||||
return methodInfo{}, fmt.Errorf("%w: %q missing ID", jsonrpc2.ErrInvalidRequest, req.Method)
|
return methodInfo{}, fmt.Errorf("%w: missing ID, %q", jsonrpc2.ErrInvalidRequest, req.Method)
|
||||||
|
}
|
||||||
|
if !info.isRequest && req.ID.IsValid() {
|
||||||
|
return methodInfo{}, fmt.Errorf("%w: unexpected id for %q", jsonrpc2.ErrInvalidRequest, req.Method)
|
||||||
}
|
}
|
||||||
return info, nil
|
return info, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
64
mcp/testdata/conformance/server/bad_requests.txtar
vendored
Normal file
64
mcp/testdata/conformance/server/bad_requests.txtar
vendored
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
Check robustness to missing fields: servers should reject and otherwise ignore
|
||||||
|
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).
|
||||||
|
|
||||||
|
-- prompts --
|
||||||
|
code_review
|
||||||
|
|
||||||
|
-- client --
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "initialize",
|
||||||
|
"params": {
|
||||||
|
"protocolVersion": "2024-11-05",
|
||||||
|
"capabilities": {},
|
||||||
|
"clientInfo": { "name": "ExampleClient", "version": "1.0.0" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": 2,
|
||||||
|
"method": "initialize",
|
||||||
|
"params": {
|
||||||
|
"protocolVersion": "2024-11-05",
|
||||||
|
"capabilities": {},
|
||||||
|
"clientInfo": { "name": "ExampleClient", "version": "1.0.0" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{"jsonrpc":"2.0", "id": 3, "method":"notifications/initialized"}
|
||||||
|
{"jsonrpc":"2.0", "method":"ping"}
|
||||||
|
|
||||||
|
-- server --
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": 2,
|
||||||
|
"result": {
|
||||||
|
"capabilities": {
|
||||||
|
"completions": {},
|
||||||
|
"logging": {},
|
||||||
|
"prompts": {
|
||||||
|
"listChanged": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"protocolVersion": "2024-11-05",
|
||||||
|
"serverInfo": {
|
||||||
|
"name": "testServer",
|
||||||
|
"version": "v1.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": 3,
|
||||||
|
"error": {
|
||||||
|
"code": -32600,
|
||||||
|
"message": "JSON RPC invalid request: unexpected id for \"notifications/initialized\""
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue