mcp: make NotificationSubscriptions a mandatory field (#1050)

Fixes #1037
This commit is contained in:
Guglielmo Colombo 2026-07-07 11:39:18 +02:00 • committed by GitHub
commit d651e2d2e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1169,6 +1169,10 @@ func (s *Server) subscriptionsListen(ctx context.Context, req *SubscriptionsList
return nil, fmt.Errorf("%w: subscriptions/listen requires a request ID", jsonrpc2.ErrInvalidRequest)
}
if req.Params.Notifications == nil {
return nil, fmt.Errorf("%w: missing required 'notifications' field", jsonrpc2.ErrInvalidParams)
}
allowed := s.allowedSubscriptions(req.Params.Notifications)
s.mu.Lock()
if allowed.ToolsListChanged {
@ -1217,11 +1221,14 @@ func (s *Server) subscriptionsListen(ctx context.Context, req *SubscriptionsList
return nil, fmt.Errorf("sending subscriptions/acknowledged: %w", err)
}
<-ctx.Done()
// If there are any active subscriptions, we block until the context is cancelled. Otherwise, we return immediately.
if len(allowed.ResourceSubscriptions) > 0 || allowed.ToolsListChanged || allowed.PromptsListChanged || allowed.ResourcesListChanged {
<-ctx.Done()
}
return &emptyResult{}, nil
}
func (s *Server) allowedSubscriptions(want NotificationSubscriptions) NotificationSubscriptions {
func (s *Server) allowedSubscriptions(want *NotificationSubscriptions) NotificationSubscriptions {
caps := s.capabilities()
agreed := NotificationSubscriptions{}
if want.ToolsListChanged && caps.Tools != nil && caps.Tools.ListChanged {