From 1373a6d29675fcc5a41b3ef58cff8d96a0b4c0c8 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 17:50:49 +0000 Subject: [PATCH] refactor(mcp): centralize session broadcast iteration --- pkg/mcp/notify.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/mcp/notify.go b/pkg/mcp/notify.go index 986dd06..0865489 100644 --- a/pkg/mcp/notify.go +++ b/pkg/mcp/notify.go @@ -147,9 +147,9 @@ func (s *Service) SendNotificationToAllClients(ctx context.Context, level mcp.Lo return } ctx = normalizeNotificationContext(ctx) - for _, session := range snapshotSessions(s.server) { + s.broadcastToSessions(func(session *mcp.ServerSession) { s.sendLoggingNotificationToSession(ctx, session, level, logger, data) - } + }) } // SendNotificationToSession sends a log-level notification to one connected @@ -245,10 +245,19 @@ func (s *Service) sendChannelNotificationToAllClients(ctx context.Context, paylo return } ctx = normalizeNotificationContext(ctx) - for _, session := range snapshotSessions(s.server) { + s.broadcastToSessions(func(session *mcp.ServerSession) { if err := sendSessionNotification(ctx, session, ChannelNotificationMethod, payload); err != nil { s.debugNotify("channel: failed to send to session", "session", session.ID(), "error", err) } + }) +} + +func (s *Service) broadcastToSessions(fn func(*mcp.ServerSession)) { + if s == nil || s.server == nil || fn == nil { + return + } + for _, session := range snapshotSessions(s.server) { + fn(session) } }