Skip to content

refactor(server): replace sync.Map with Sessionizer interface for session management #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ type MCPServer struct {
tools map[string]ServerTool
notificationHandlers map[string]NotificationHandlerFunc
capabilities serverCapabilities
sessions sync.Map
sessionizer Sessionizer
hooks *Hooks
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func (s *MCPServer) RegisterSession(
session ClientSession,
) error {
sessionID := session.SessionID()
if _, exists := s.sessions.LoadOrStore(sessionID, session); exists {
if _, exists := s.sessionizer.LoadOrStore(sessionID, session); exists {
return fmt.Errorf("session %s is already registered", sessionID)
}
return nil
Expand All @@ -185,7 +185,7 @@ func (s *MCPServer) RegisterSession(
func (s *MCPServer) UnregisterSession(
sessionID string,
) {
s.sessions.Delete(sessionID)
s.sessionizer.Delete(sessionID)
}

// sendNotificationToAllClients sends a notification to all the currently active clients.
Expand All @@ -203,16 +203,15 @@ func (s *MCPServer) sendNotificationToAllClients(
},
}

s.sessions.Range(func(k, v any) bool {
if session, ok := v.(ClientSession); ok && session.Initialized() {
for _, session := range s.sessionizer.All() {
if session.Initialized() {
select {
case session.NotificationChannel() <- notification:
default:
// TODO: log blocked channel in the future versions
}
}
return true
})
}
}

// SendNotificationToClient sends a notification to the current client
Expand Down Expand Up @@ -322,6 +321,12 @@ func WithInstructions(instructions string) ServerOption {
}
}

func WithSessionizer(sessionizer Sessionizer) ServerOption {
return func(s *MCPServer) {
s.sessionizer = sessionizer
}
}

// NewMCPServer creates a new MCP server instance with the given name, version and options
func NewMCPServer(
name, version string,
Expand All @@ -342,6 +347,7 @@ func NewMCPServer(
prompts: nil,
logging: false,
},
sessionizer: &SyncMapSessionizer{},
}

for _, opt := range opts {
Expand Down
38 changes: 38 additions & 0 deletions server/sessionizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package server

import "sync"

type Sessionizer interface {
LoadOrStore(sessionID string, session ClientSession) (ClientSession, bool)

Delete(sessionID string)

All() []ClientSession
}

type SyncMapSessionizer struct {
sessions sync.Map
}

var _ Sessionizer = (*SyncMapSessionizer)(nil)

func (s *SyncMapSessionizer) LoadOrStore(sessionID string, session ClientSession) (ClientSession, bool) {
actual, ok := s.sessions.LoadOrStore(sessionID, session)
if ok {
return actual.(ClientSession), true
}
return session, false
}

func (s *SyncMapSessionizer) Delete(sessionID string) {
s.sessions.Delete(sessionID)
}

func (s *SyncMapSessionizer) All() []ClientSession {
var sessions []ClientSession
s.sessions.Range(func(key, value any) bool {
sessions = append(sessions, value.(ClientSession))
return true
})
return sessions
}
Loading