@@ -147,7 +147,7 @@ type MCPServer struct {
147147 tools map [string ]ServerTool
148148 notificationHandlers map [string ]NotificationHandlerFunc
149149 capabilities serverCapabilities
150- sessions sync. Map
150+ sessionizer Sessionizer
151151 hooks * Hooks
152152}
153153
@@ -175,7 +175,7 @@ func (s *MCPServer) RegisterSession(
175175 session ClientSession ,
176176) error {
177177 sessionID := session .SessionID ()
178- if _ , exists := s .sessions .LoadOrStore (sessionID , session ); exists {
178+ if _ , exists := s .sessionizer .LoadOrStore (sessionID , session ); exists {
179179 return fmt .Errorf ("session %s is already registered" , sessionID )
180180 }
181181 return nil
@@ -185,7 +185,7 @@ func (s *MCPServer) RegisterSession(
185185func (s * MCPServer ) UnregisterSession (
186186 sessionID string ,
187187) {
188- s .sessions .Delete (sessionID )
188+ s .sessionizer .Delete (sessionID )
189189}
190190
191191// sendNotificationToAllClients sends a notification to all the currently active clients.
@@ -203,16 +203,15 @@ func (s *MCPServer) sendNotificationToAllClients(
203203 },
204204 }
205205
206- s . sessions . Range ( func ( k , v any ) bool {
207- if session , ok := v .( ClientSession ); ok && session .Initialized () {
206+ for _ , session := range s . sessionizer . All () {
207+ if session .Initialized () {
208208 select {
209209 case session .NotificationChannel () <- notification :
210210 default :
211211 // TODO: log blocked channel in the future versions
212212 }
213213 }
214- return true
215- })
214+ }
216215}
217216
218217// SendNotificationToClient sends a notification to the current client
@@ -322,6 +321,12 @@ func WithInstructions(instructions string) ServerOption {
322321 }
323322}
324323
324+ func WithSessionizer (sessionizer Sessionizer ) ServerOption {
325+ return func (s * MCPServer ) {
326+ s .sessionizer = sessionizer
327+ }
328+ }
329+
325330// NewMCPServer creates a new MCP server instance with the given name, version and options
326331func NewMCPServer (
327332 name , version string ,
@@ -342,6 +347,7 @@ func NewMCPServer(
342347 prompts : nil ,
343348 logging : false ,
344349 },
350+ sessionizer : & SyncMapSessionizer {},
345351 }
346352
347353 for _ , opt := range opts {
@@ -410,7 +416,7 @@ func (s *MCPServer) AddTools(tools ...ServerTool) {
410416 }
411417 s .mu .Unlock ()
412418
413- // Send notification to all initialized sessions
419+ // Send notification to all initialized sessionizer
414420 s .sendNotificationToAllClients ("notifications/tools/list_changed" , nil )
415421}
416422
@@ -430,7 +436,7 @@ func (s *MCPServer) DeleteTools(names ...string) {
430436 }
431437 s .mu .Unlock ()
432438
433- // Send notification to all initialized sessions
439+ // Send notification to all initialized sessionizer
434440 s .sendNotificationToAllClients ("notifications/tools/list_changed" , nil )
435441}
436442
0 commit comments