Skip to content

Commit 8baa4cc

Browse files
mcp: add return type to subscriptions/listen (#1088)
This PR adds the expected return type to `subscriptions/listen` rpc
1 parent 9576afc commit 8baa4cc

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

mcp/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ func (cs *ClientSession) cancelAllResourceSubscriptions() {
14431443
// usual handlers registered in [ClientOptions].
14441444
func (cs *ClientSession) subscriptionsListen(ctx context.Context, params *SubscriptionsListenParams) error {
14451445
params = injectRequestMeta(cs, params)
1446-
_, err := handleSend[*emptyResult](ctx, methodSubscriptionsListen, newClientRequest(cs, orZero[Params](params)))
1446+
_, err := handleSend[*SubscriptionsListenResult](ctx, methodSubscriptionsListen, newClientRequest(cs, orZero[Params](params)))
14471447
return err
14481448
}
14491449

mcp/protocol.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,18 @@ type SubscriptionsAcknowledgedParams struct {
21092109
func (x *SubscriptionsAcknowledgedParams) isParams() {}
21102110
func (x *SubscriptionsAcknowledgedParams) isNil() bool { return x == nil }
21112111

2112+
// SubscriptionsListenResult is the response to a "subscriptions/listen"
2113+
// request, signalling that the subscription has ended gracefully (for example,
2114+
// during server shutdown). Because the listen stream is long-lived, this
2115+
// result is sent only when the server tears the subscription down; an abrupt
2116+
// transport close carries no response.
2117+
type SubscriptionsListenResult struct {
2118+
completeResultWithType
2119+
Meta `json:"_meta"`
2120+
}
2121+
2122+
func (*SubscriptionsListenResult) isResult() {}
2123+
21122124
// TODO(jba): add CompleteRequest and related types.
21132125

21142126
// A request from the server to elicit additional information from the user via the client.

mcp/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ func (s *Server) unsubscribe(ctx context.Context, req *UnsubscribeRequest) (*emp
11801180
return &emptyResult{}, nil
11811181
}
11821182

1183-
func (s *Server) subscriptionsListen(ctx context.Context, req *SubscriptionsListenRequest) (*emptyResult, error) {
1183+
func (s *Server) subscriptionsListen(ctx context.Context, req *SubscriptionsListenRequest) (*SubscriptionsListenResult, error) {
11841184
requestID, ok := ctx.Value(idContextKey{}).(jsonrpc.ID)
11851185
if !ok || !requestID.IsValid() {
11861186
return nil, fmt.Errorf("%w: subscriptions/listen requires a request ID", jsonrpc2.ErrInvalidRequest)
@@ -1242,7 +1242,9 @@ func (s *Server) subscriptionsListen(ctx context.Context, req *SubscriptionsList
12421242
if len(allowed.ResourceSubscriptions) > 0 || allowed.ToolsListChanged || allowed.PromptsListChanged || allowed.ResourcesListChanged {
12431243
<-ctx.Done()
12441244
}
1245-
return &emptyResult{}, nil
1245+
return &SubscriptionsListenResult{
1246+
Meta: Meta{MetaKeySubscriptionID: requestID.Raw()},
1247+
}, nil
12461248
}
12471249

12481250
func (s *Server) allowedSubscriptions(want *NotificationSubscriptions) NotificationSubscriptions {

0 commit comments

Comments
 (0)