Skip to content

Commit 6af618e

Browse files
committed
mcp/server: cleanup resourceupdated logic
1 parent 4894249 commit 6af618e

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

mcp/server.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"fmt"
1414
"iter"
1515
"log"
16-
"maps"
1716
"net/url"
1817
"path/filepath"
1918
"slices"
@@ -445,22 +444,18 @@ func fileResourceHandler(dir string) ResourceHandler {
445444

446445
func (s *Server) ResourceUpdated(ctx context.Context, params *ResourceUpdatedNotificationParams) error {
447446
s.mu.Lock()
448-
subscribedSessionIDs := maps.Clone(s.resourceSubscriptions[params.URI])
449-
s.mu.Unlock()
447+
subscribedSessionIDs := s.resourceSubscriptions[params.URI]
450448
if len(subscribedSessionIDs) == 0 {
449+
s.mu.Unlock()
451450
return nil
452451
}
453-
sessions := make([]*ServerSession, 0, len(subscribedSessionIDs))
454-
for sessionID, active := range subscribedSessionIDs {
455-
if !active {
456-
continue
457-
}
458-
for session := range s.Sessions() {
459-
if session.ID() == sessionID {
460-
sessions = append(sessions, session)
461-
}
452+
var sessions []*ServerSession
453+
for _, session := range s.sessions {
454+
if _, ok := subscribedSessionIDs[session.ID()]; ok {
455+
sessions = append(sessions, session)
462456
}
463457
}
458+
s.mu.Unlock()
464459
notifySessions(sessions, notificationResourceUpdated, params)
465460
return nil
466461
}
@@ -474,11 +469,10 @@ func (s *Server) subscribe(ctx context.Context, ss *ServerSession, params *Subsc
474469
}
475470
s.mu.Lock()
476471
defer s.mu.Unlock()
477-
uri := params.URI
478-
if s.resourceSubscriptions[uri] == nil {
479-
s.resourceSubscriptions[uri] = make(map[string]bool)
472+
if s.resourceSubscriptions[params.URI] == nil {
473+
s.resourceSubscriptions[params.URI] = make(map[string]bool)
480474
}
481-
s.resourceSubscriptions[uri][ss.ID()] = true
475+
s.resourceSubscriptions[params.URI][ss.ID()] = true
482476
return &emptyResult{}, nil
483477
}
484478

@@ -494,10 +488,13 @@ func (s *Server) unsubscribe(ctx context.Context, ss *ServerSession, params *Uns
494488
s.mu.Lock()
495489
defer s.mu.Unlock()
496490

497-
uri := params.URI
498-
if subscribedSessionIDs, ok := s.resourceSubscriptions[uri]; ok {
499-
subscribedSessionIDs[ss.ID()] = false
491+
if subscribedSessionIDs, ok := s.resourceSubscriptions[params.URI]; ok {
492+
delete(subscribedSessionIDs, ss.ID())
493+
if len(subscribedSessionIDs) == 0 {
494+
delete(s.resourceSubscriptions, params.URI)
495+
}
500496
}
497+
501498
return &emptyResult{}, nil
502499
}
503500

0 commit comments

Comments
 (0)