Skip to content

Commit 56734ed

Browse files
authored
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
1 parent 9f48a04 commit 56734ed

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mcp/shared.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ func checkRequest(req *jsonrpc.Request, infos map[string]methodInfo) (methodInfo
155155
return methodInfo{}, fmt.Errorf("%w: %q unsupported", jsonrpc2.ErrNotHandled, req.Method)
156156
}
157157
if info.isRequest && !req.ID.IsValid() {
158-
return methodInfo{}, fmt.Errorf("%w: %q missing ID", jsonrpc2.ErrInvalidRequest, req.Method)
158+
return methodInfo{}, fmt.Errorf("%w: missing ID, %q", jsonrpc2.ErrInvalidRequest, req.Method)
159+
}
160+
if !info.isRequest && req.ID.IsValid() {
161+
return methodInfo{}, fmt.Errorf("%w: unexpected id for %q", jsonrpc2.ErrInvalidRequest, req.Method)
159162
}
160163
return info, nil
161164
}

mcp/testdata/conformance/server/missing_fields.txtar renamed to mcp/testdata/conformance/server/bad_requests.txtar

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ bad requests.
44
Fixed bugs:
55
- No id in 'initialize' should not panic (#197).
66
- No id in 'ping' should not panic (#194).
7+
- Notifications with IDs should not be treated like requests.
78

89
TODO:
910
- No params in 'initialize' should not panic (#195).
@@ -31,6 +32,7 @@ code_review
3132
"clientInfo": { "name": "ExampleClient", "version": "1.0.0" }
3233
}
3334
}
35+
{"jsonrpc":"2.0", "id": 3, "method":"notifications/initialized"}
3436
{"jsonrpc":"2.0", "method":"ping"}
3537

3638
-- server --
@@ -52,3 +54,11 @@ code_review
5254
}
5355
}
5456
}
57+
{
58+
"jsonrpc": "2.0",
59+
"id": 3,
60+
"error": {
61+
"code": -32600,
62+
"message": "JSON RPC invalid request: unexpected id for \"notifications/initialized\""
63+
}
64+
}

0 commit comments

Comments
 (0)