Skip to content

Commit cb17593

Browse files
update Subscribe/Unsubscribe handlers in ServerOpts (#231)
## Background Since the Server has a map of ServersSessions->subscriptions, and takes care of routing ResourceUpdate messages to ServerSessions, I think the decision was made to leave *ServerSession out of the SubscribeHandler/UnsubscribeHandler signatures. However, resource subscriptions are not specific to the a particular client and this information is kept private in the Session today. ## Proposal Align the Subscribe/Unsubscribe handlers with the other two handlers that are client specific (ProgressNotificationHandler, and RootListChangeHanlder), and include *ServerSession in the signature. It comes in practice because because a gateway server that needs to forward subscriptions still needs this information. The routing logic in Server is convenient and means that most servers won't need to know which ServerSessions are subscribed. But I think there are still use cases where the user will need to know.
1 parent 1e40ede commit cb17593

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

design/design.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,9 +776,9 @@ If a server author wants to support resource subscriptions, they must provide ha
776776
type ServerOptions struct {
777777
...
778778
// Function called when a client session subscribes to a resource.
779-
SubscribeHandler func(context.Context, *SubscribeParams) error
779+
SubscribeHandler func(context.Context, ss *ServerSession, *SubscribeParams) error
780780
// Function called when a client session unsubscribes from a resource.
781-
UnsubscribeHandler func(context.Context, *UnsubscribeParams) error
781+
UnsubscribeHandler func(context.Context, ss *ServerSession, *UnsubscribeParams) error
782782
}
783783
```
784784

mcp/mcp_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ func TestEndToEnd(t *testing.T) {
7878
ProgressNotificationHandler: func(context.Context, *ServerSession, *ProgressNotificationParams) {
7979
notificationChans["progress_server"] <- 0
8080
},
81-
SubscribeHandler: func(context.Context, *SubscribeParams) error {
81+
SubscribeHandler: func(context.Context, *ServerSession, *SubscribeParams) error {
8282
notificationChans["subscribe"] <- 0
8383
return nil
8484
},
85-
UnsubscribeHandler: func(context.Context, *UnsubscribeParams) error {
85+
UnsubscribeHandler: func(context.Context, *ServerSession, *UnsubscribeParams) error {
8686
notificationChans["unsubscribe"] <- 0
8787
return nil
8888
},

mcp/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ type ServerOptions struct {
6666
// the session is automatically closed.
6767
KeepAlive time.Duration
6868
// Function called when a client session subscribes to a resource.
69-
SubscribeHandler func(context.Context, *SubscribeParams) error
69+
SubscribeHandler func(context.Context, *ServerSession, *SubscribeParams) error
7070
// Function called when a client session unsubscribes from a resource.
71-
UnsubscribeHandler func(context.Context, *UnsubscribeParams) error
71+
UnsubscribeHandler func(context.Context, *ServerSession, *UnsubscribeParams) error
7272
// If true, advertises the prompts capability during initialization,
7373
// even if no prompts have been registered.
7474
HasPrompts bool
@@ -469,7 +469,7 @@ func (s *Server) subscribe(ctx context.Context, ss *ServerSession, params *Subsc
469469
if s.opts.SubscribeHandler == nil {
470470
return nil, fmt.Errorf("%w: server does not support resource subscriptions", jsonrpc2.ErrMethodNotFound)
471471
}
472-
if err := s.opts.SubscribeHandler(ctx, params); err != nil {
472+
if err := s.opts.SubscribeHandler(ctx, ss, params); err != nil {
473473
return nil, err
474474
}
475475

@@ -488,7 +488,7 @@ func (s *Server) unsubscribe(ctx context.Context, ss *ServerSession, params *Uns
488488
return nil, jsonrpc2.ErrMethodNotFound
489489
}
490490

491-
if err := s.opts.UnsubscribeHandler(ctx, params); err != nil {
491+
if err := s.opts.UnsubscribeHandler(ctx, ss, params); err != nil {
492492
return nil, err
493493
}
494494

mcp/server_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@ func TestServerCapabilities(t *testing.T) {
281281
s.AddResourceTemplate(&ResourceTemplate{URITemplate: "file:///rt"}, nil)
282282
},
283283
serverOpts: ServerOptions{
284-
SubscribeHandler: func(ctx context.Context, sp *SubscribeParams) error {
284+
SubscribeHandler: func(ctx context.Context, _ *ServerSession, sp *SubscribeParams) error {
285285
return nil
286286
},
287-
UnsubscribeHandler: func(ctx context.Context, up *UnsubscribeParams) error {
287+
UnsubscribeHandler: func(ctx context.Context, _ *ServerSession, up *UnsubscribeParams) error {
288288
return nil
289289
},
290290
},
@@ -325,10 +325,10 @@ func TestServerCapabilities(t *testing.T) {
325325
s.AddTool(tool, nil)
326326
},
327327
serverOpts: ServerOptions{
328-
SubscribeHandler: func(ctx context.Context, sp *SubscribeParams) error {
328+
SubscribeHandler: func(ctx context.Context, _ *ServerSession, sp *SubscribeParams) error {
329329
return nil
330330
},
331-
UnsubscribeHandler: func(ctx context.Context, up *UnsubscribeParams) error {
331+
UnsubscribeHandler: func(ctx context.Context, _ *ServerSession, up *UnsubscribeParams) error {
332332
return nil
333333
},
334334
CompletionHandler: func(ctx context.Context, ss *ServerSession, params *CompleteParams) (*CompleteResult, error) {

0 commit comments

Comments
 (0)