Skip to content

Commit d651e2d

Browse files
mcp: make NotificationSubscriptions a mandatory field (#1050)
Fixes #1037
1 parent ec564f9 commit d651e2d

4 files changed

Lines changed: 56 additions & 5 deletions

File tree

‎mcp/client.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ func (c *Client) Connect(ctx context.Context, t Transport, opts *ClientSessionOp
330330
if hc, ok := cs.mcpConn.(clientConnection); ok {
331331
hc.sessionUpdated(cs.state)
332332
}
333-
subscribeParams := &SubscriptionsListenParams{}
333+
subscribeParams := &SubscriptionsListenParams{
334+
Notifications: &NotificationSubscriptions{},
335+
}
334336
if c.opts.ToolListChangedHandler != nil {
335337
subscribeParams.Notifications.ToolsListChanged = true
336338
}
@@ -1393,7 +1395,7 @@ func (cs *ClientSession) Subscribe(ctx context.Context, params *SubscribeParams)
13931395
}
13941396

13951397
return cs.subscriptionsListen(listenCtx, &SubscriptionsListenParams{
1396-
Notifications: NotificationSubscriptions{
1398+
Notifications: &NotificationSubscriptions{
13971399
ResourceSubscriptions: []string{uri},
13981400
},
13991401
})

‎mcp/mcp_test.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,48 @@ func TestSubscriptionsListen_NoHandlersNoListen(t *testing.T) {
26522652
}
26532653
}
26542654

2655+
// TestSubscriptionsListen_MissingNotifications verifies that a
2656+
// subscriptions/listen request without the required "notifications" field
2657+
// is rejected with an invalid params error.
2658+
func TestSubscriptionsListen_MissingNotifications(t *testing.T) {
2659+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
2660+
defer cancel()
2661+
2662+
server := newSubListenServer()
2663+
_, st := NewInMemoryTransports()
2664+
ss, err := server.Connect(ctx, st, nil)
2665+
if err != nil {
2666+
t.Fatalf("server connect: %v", err)
2667+
}
2668+
defer ss.Close()
2669+
2670+
// Invoke the server handler directly with a nil Notifications field,
2671+
// simulating a request whose params omit the required field on the wire.
2672+
// The client-side subscriptionsListen does not await the RPC response
2673+
// (the call's lifetime is the notification stream), so we assert the
2674+
// server-side behavior at the handler level.
2675+
id, err := jsonrpc.MakeID("test-1")
2676+
if err != nil {
2677+
t.Fatalf("MakeID: %v", err)
2678+
}
2679+
reqCtx := context.WithValue(ctx, idContextKey{}, id)
2680+
req := &SubscriptionsListenRequest{
2681+
Session: ss,
2682+
Params: &SubscriptionsListenParams{}, // Notifications is nil
2683+
}
2684+
_, err = server.subscriptionsListen(reqCtx, req)
2685+
if err == nil {
2686+
t.Fatal("expected error for missing notifications field, got nil")
2687+
}
2688+
var jerr *jsonrpc.Error
2689+
if !errors.As(err, &jerr) {
2690+
t.Fatalf("expected *jsonrpc.Error, got %T: %v", err, err)
2691+
}
2692+
if jerr.Code != jsonrpc.CodeInvalidParams {
2693+
t.Errorf("error code = %d, want %d", jerr.Code, jsonrpc.CodeInvalidParams)
2694+
}
2695+
}
2696+
26552697
// resourceSubServer builds a server that advertises resource subscriptions
26562698
// and records every Subscribe/Unsubscribe handler invocation through chans.
26572699
func resourceSubServer(t *testing.T, subCh, unsubCh chan string) *Server {

‎mcp/protocol.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ type SubscriptionsListenParams struct {
20632063
Meta `json:"_meta,omitempty"`
20642064
// Notifications declares which notification types the client wants to
20652065
// receive on this stream.
2066-
Notifications NotificationSubscriptions `json:"notifications"`
2066+
Notifications *NotificationSubscriptions `json:"notifications"`
20672067
}
20682068

20692069
func (x *SubscriptionsListenParams) isParams() {}

‎mcp/server.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,10 @@ func (s *Server) subscriptionsListen(ctx context.Context, req *SubscriptionsList
11691169
return nil, fmt.Errorf("%w: subscriptions/listen requires a request ID", jsonrpc2.ErrInvalidRequest)
11701170
}
11711171

1172+
if req.Params.Notifications == nil {
1173+
return nil, fmt.Errorf("%w: missing required 'notifications' field", jsonrpc2.ErrInvalidParams)
1174+
}
1175+
11721176
allowed := s.allowedSubscriptions(req.Params.Notifications)
11731177
s.mu.Lock()
11741178
if allowed.ToolsListChanged {
@@ -1217,11 +1221,14 @@ func (s *Server) subscriptionsListen(ctx context.Context, req *SubscriptionsList
12171221
return nil, fmt.Errorf("sending subscriptions/acknowledged: %w", err)
12181222
}
12191223

1220-
<-ctx.Done()
1224+
// If there are any active subscriptions, we block until the context is cancelled. Otherwise, we return immediately.
1225+
if len(allowed.ResourceSubscriptions) > 0 || allowed.ToolsListChanged || allowed.PromptsListChanged || allowed.ResourcesListChanged {
1226+
<-ctx.Done()
1227+
}
12211228
return &emptyResult{}, nil
12221229
}
12231230

1224-
func (s *Server) allowedSubscriptions(want NotificationSubscriptions) NotificationSubscriptions {
1231+
func (s *Server) allowedSubscriptions(want *NotificationSubscriptions) NotificationSubscriptions {
12251232
caps := s.capabilities()
12261233
agreed := NotificationSubscriptions{}
12271234
if want.ToolsListChanged && caps.Tools != nil && caps.Tools.ListChanged {

0 commit comments

Comments
 (0)