Skip to content

Commit 2fdabde

Browse files
mcp: do not check metatada on notifications (#1215)
Fixes #1212
1 parent 59185e6 commit 2fdabde

4 files changed

Lines changed: 133 additions & 2 deletions

File tree

‎mcp/server.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1962,7 +1962,7 @@ func (ss *ServerSession) handle(ctx context.Context, req *jsonrpc.Request) (any,
19621962
}
19631963
}
19641964
default:
1965-
if !initialized && !validatedMeta.usesNewProtocol {
1965+
if !initialized && !validatedMeta.usesNewProtocol && req.IsCall() {
19661966
ss.server.opts.Logger.Error("method invalid during initialization", "method", req.Method)
19671967
return nil, fmt.Errorf("method %q is invalid during session initialization", req.Method)
19681968
}

‎mcp/shared.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,15 @@ type validatedMeta struct {
560560
// a non-nil error. clientInfo is optional; if present but invalid, an error is
561561
// returned. Otherwise, it returns usesNewProtocol set to true and the populated
562562
// initializeParams.
563+
// Notifications always report usesNewProtocol false and never error.
563564
func validateRequestMeta(req *jsonrpc.Request) (*validatedMeta, error) {
565+
// SEP-2575 defines the `_meta` triple for calls only: NotificationParams
566+
// declares `_meta` optional with no protocolVersion, so a notification
567+
// neither selects the new protocol nor is discarded for carrying an
568+
// incomplete triple.
569+
if !req.IsCall() {
570+
return &validatedMeta{usesNewProtocol: false, initializeParams: nil}, nil
571+
}
564572
meta := extractRequestMeta(req.Params)
565573
if meta == nil {
566574
return &validatedMeta{usesNewProtocol: false, initializeParams: nil}, nil

‎mcp/streamable.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1530,12 +1530,15 @@ func (c *streamableServerConn) servePOST(w http.ResponseWriter, req *http.Reques
15301530
// The new (>= 2026-07-28) protocol is supported on the HTTP transport
15311531
// only when [StreamableHTTPOptions.Stateless] is true.
15321532
//
1533+
// The `_meta` triple is defined for calls only, notifications are
1534+
// excluded.
1535+
//
15331536
// TODO: this validation can be moved within validateMcpHeaders.
15341537
var metaVersion string
15351538
if meta := extractRequestMeta(jreq.Params); meta != nil {
15361539
metaVersion, _ = meta[MetaKeyProtocolVersion].(string)
15371540
}
1538-
if protocolVersion >= protocolVersion20260728 || metaVersion != "" {
1541+
if (jreq.IsCall() && protocolVersion >= protocolVersion20260728) || metaVersion != "" {
15391542
// Extract again the protocol version from the context to see what the client
15401543
// is advertising in the Mcp-Protocol-Version HTTP header.
15411544
headerVersion := protocolVersionFromContext(req.Context())

‎mcp/streamable_test.go‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3712,6 +3712,126 @@ func TestStreamableStateless_AcceptsNewProtocol(t *testing.T) {
37123712
}
37133713
}
37143714

3715+
// TestStreamableStateless_NotificationMetaValidation checks that the SEP-2575
3716+
// per-request `_meta` triple is required of calls only. NotificationParams
3717+
// declares `_meta` optional with no protocolVersion, so a notification that
3718+
// omits it is well-formed and must be accepted with 202.
3719+
func TestStreamableStateless_NotificationMetaValidation(t *testing.T) {
3720+
newProtocolMeta := map[string]any{
3721+
MetaKeyProtocolVersion: protocolVersion20260728,
3722+
MetaKeyClientInfo: map[string]any{"name": "new-proto-client", "version": "9.9"},
3723+
MetaKeyClientCapabilities: map[string]any{},
3724+
}
3725+
3726+
tests := []struct {
3727+
name string
3728+
message map[string]any
3729+
wantStatus int
3730+
}{
3731+
{
3732+
name: "cancelled without meta",
3733+
message: map[string]any{
3734+
"jsonrpc": "2.0",
3735+
"method": notificationCancelled,
3736+
"params": map[string]any{"requestID": 1, "reason": "context canceled"},
3737+
},
3738+
wantStatus: http.StatusAccepted,
3739+
},
3740+
{
3741+
name: "progress without meta",
3742+
message: map[string]any{
3743+
"jsonrpc": "2.0",
3744+
"method": notificationProgress,
3745+
"params": map[string]any{"progressToken": "t", "progress": 1},
3746+
},
3747+
wantStatus: http.StatusAccepted,
3748+
},
3749+
{
3750+
name: "notification with matching meta",
3751+
message: map[string]any{
3752+
"jsonrpc": "2.0",
3753+
"method": notificationCancelled,
3754+
"params": map[string]any{
3755+
"_meta": newProtocolMeta,
3756+
"requestID": 1,
3757+
},
3758+
},
3759+
wantStatus: http.StatusAccepted,
3760+
},
3761+
{
3762+
// A notification that volunteers a version is still held to the
3763+
// header-match rule, so relaxing the requirement does not open a
3764+
// hole for inconsistent messages.
3765+
name: "notification with mismatched meta",
3766+
message: map[string]any{
3767+
"jsonrpc": "2.0",
3768+
"method": notificationCancelled,
3769+
"params": map[string]any{
3770+
"_meta": map[string]any{
3771+
MetaKeyProtocolVersion: "2025-06-18",
3772+
MetaKeyClientCapabilities: map[string]any{},
3773+
},
3774+
"requestID": 1,
3775+
},
3776+
},
3777+
wantStatus: http.StatusBadRequest,
3778+
},
3779+
{
3780+
// Calls remain subject to the requirement.
3781+
name: "call without meta",
3782+
message: map[string]any{
3783+
"jsonrpc": "2.0",
3784+
"id": 1,
3785+
"method": "tools/list",
3786+
"params": map[string]any{},
3787+
},
3788+
wantStatus: http.StatusBadRequest,
3789+
},
3790+
}
3791+
3792+
server := NewServer(testImpl, nil)
3793+
AddTool(server, &Tool{Name: "noop"},
3794+
func(ctx context.Context, req *CallToolRequest, args struct{}) (*CallToolResult, any, error) {
3795+
return &CallToolResult{Content: []Content{&TextContent{Text: "ok"}}}, nil, nil
3796+
})
3797+
handler := NewStreamableHTTPHandler(
3798+
func(*http.Request) *Server { return server },
3799+
&StreamableHTTPOptions{Stateless: true},
3800+
)
3801+
httpServer := httptest.NewServer(handler)
3802+
defer httpServer.Close()
3803+
3804+
for _, test := range tests {
3805+
t.Run(test.name, func(t *testing.T) {
3806+
body, err := json.Marshal(test.message)
3807+
if err != nil {
3808+
t.Fatal(err)
3809+
}
3810+
req, err := http.NewRequest(http.MethodPost, httpServer.URL, bytes.NewReader(body))
3811+
if err != nil {
3812+
t.Fatal(err)
3813+
}
3814+
req.Header.Set("Content-Type", "application/json")
3815+
req.Header.Set("Accept", "application/json, text/event-stream")
3816+
req.Header.Set(protocolVersionHeader, protocolVersion20260728)
3817+
req.Header.Set(methodHeader, test.message["method"].(string))
3818+
3819+
resp, err := http.DefaultClient.Do(req)
3820+
if err != nil {
3821+
t.Fatal(err)
3822+
}
3823+
defer resp.Body.Close()
3824+
respBody, _ := io.ReadAll(resp.Body)
3825+
if resp.StatusCode != test.wantStatus {
3826+
t.Fatalf("status = %d, want %d; body = %s", resp.StatusCode, test.wantStatus, respBody)
3827+
}
3828+
if test.wantStatus == http.StatusAccepted && len(respBody) > 0 {
3829+
t.Errorf("accepted notification returned body %q, want empty", respBody)
3830+
}
3831+
})
3832+
}
3833+
}
3834+
37153835
// TestStreamableClientUnsupportedVersionFallback exercises the full
37163836
// SEP-2575 fallback. The client requests protocolVersion20260728, which the
37173837
// server is configured to NOT advertise in supportedProtocolVersions for the

0 commit comments

Comments
 (0)