mcp: do not exclude Notifications from carrying InitializeParams in Meta (#1049)
Fixes #1043
This commit is contained in:
parent
a901f97b6d
commit
224a240875
2 changed files with 8 additions and 31 deletions
|
|
@ -515,11 +515,10 @@ type validatedMeta struct {
|
||||||
// the >= 2026-07-28 protocol via the `_meta` field.
|
// the >= 2026-07-28 protocol via the `_meta` field.
|
||||||
// If the request has no _meta, or no protocolVersion in _meta, it returns a non-nil
|
// If the request has no _meta, or no protocolVersion in _meta, it returns a non-nil
|
||||||
// validatedMeta with usesNewProtocol set to false, and a nil error.
|
// validatedMeta with usesNewProtocol set to false, and a nil error.
|
||||||
// If the request has a protocolVersion in _meta:
|
// If the request has a protocolVersion in _meta it validates the presence of clientInfo
|
||||||
// - For notifications, it returns usesNewProtocol set to true and a nil initializeParams.
|
// and clientCapabilities in _meta. If either is missing or invalid, it returns nil and
|
||||||
// - For call requests, it validates the presence of clientInfo and clientCapabilities in _meta.
|
// a non-nil error. Otherwise, it returns usesNewProtocol set to true and the populated
|
||||||
// If either is missing or invalid, it returns nil and a non-nil error. Otherwise, it returns
|
// initializeParams.
|
||||||
// usesNewProtocol set to true and the populated initializeParams.
|
|
||||||
func validateRequestMeta(req *jsonrpc.Request) (*validatedMeta, error) {
|
func validateRequestMeta(req *jsonrpc.Request) (*validatedMeta, error) {
|
||||||
meta := extractRequestMeta(req.Params)
|
meta := extractRequestMeta(req.Params)
|
||||||
if meta == nil {
|
if meta == nil {
|
||||||
|
|
@ -529,10 +528,6 @@ func validateRequestMeta(req *jsonrpc.Request) (*validatedMeta, error) {
|
||||||
if !ok || protocolVersion < protocolVersion20260728 {
|
if !ok || protocolVersion < protocolVersion20260728 {
|
||||||
return &validatedMeta{usesNewProtocol: false, initializeParams: nil}, nil
|
return &validatedMeta{usesNewProtocol: false, initializeParams: nil}, nil
|
||||||
}
|
}
|
||||||
// Notifications do not carry full client identity.
|
|
||||||
if !req.IsCall() {
|
|
||||||
return &validatedMeta{usesNewProtocol: true, initializeParams: nil}, nil
|
|
||||||
}
|
|
||||||
clientInfo, ok := decodeMetaValue[*Implementation](meta, MetaKeyClientInfo)
|
clientInfo, ok := decodeMetaValue[*Implementation](meta, MetaKeyClientInfo)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, &jsonrpc.Error{
|
return nil, &jsonrpc.Error{
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ func TestValidateRequestMeta(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
method string
|
method string
|
||||||
isNotification bool
|
|
||||||
params any
|
params any
|
||||||
wantUsesNew bool
|
wantUsesNew bool
|
||||||
wantLogLevel LoggingLevel
|
wantLogLevel LoggingLevel
|
||||||
|
|
@ -84,18 +83,6 @@ func TestValidateRequestMeta(t *testing.T) {
|
||||||
wantUsesNew: false,
|
wantUsesNew: false,
|
||||||
wantErrContains: MetaKeyClientCapabilities,
|
wantErrContains: MetaKeyClientCapabilities,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "notifications exempt from required fields",
|
|
||||||
method: notificationCancelled,
|
|
||||||
isNotification: true,
|
|
||||||
params: map[string]any{
|
|
||||||
"_meta": map[string]any{
|
|
||||||
MetaKeyProtocolVersion: protocolVersion20260728,
|
|
||||||
},
|
|
||||||
"requestId": "r1",
|
|
||||||
},
|
|
||||||
wantUsesNew: true,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "malformed _meta is ignored",
|
name: "malformed _meta is ignored",
|
||||||
method: methodCallTool,
|
method: methodCallTool,
|
||||||
|
|
@ -141,16 +128,11 @@ func TestValidateRequestMeta(t *testing.T) {
|
||||||
default:
|
default:
|
||||||
raw = mustMarshal(tc.params)
|
raw = mustMarshal(tc.params)
|
||||||
}
|
}
|
||||||
req := &jsonrpc.Request{Method: tc.method, Params: raw}
|
id, err := jsonrpc.MakeID("test")
|
||||||
if !tc.isNotification {
|
if err != nil {
|
||||||
req.ID = jsonrpc.ID{}
|
t.Fatal(err)
|
||||||
// Give the request an ID by parsing one.
|
|
||||||
id, err := jsonrpc.MakeID("test")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
req.ID = id
|
|
||||||
}
|
}
|
||||||
|
req := &jsonrpc.Request{Method: tc.method, Params: raw, ID: id}
|
||||||
|
|
||||||
vmeta, err := validateRequestMeta(req)
|
vmeta, err := validateRequestMeta(req)
|
||||||
usesNew := vmeta != nil && vmeta.usesNewProtocol
|
usesNew := vmeta != nil && vmeta.usesNewProtocol
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue