Skip to content

proposal: ClientOptions.ResourceSubscriptionEndedHandler for observing resource-subscription termination #1284

Open

Description

@jeremy

Motivation

#1283 fixes a bug where a resource subscription whose subscriptions/listen stream ends (server teardown, revoked resource, dropped connection) leaves a stale internal entry, so a bare re-Subscribe no-ops. That PR clears the subscription silently so re-Subscribe re-opens it, with no new exported API.

Silent cleanup, though, gives the application no way to observe that a subscription ended or to react to it — reconnect with its own backoff, surface a UI state, stop retrying. Today a client gets no signal at all when a live subscription dies underneath it.

Proposed API

type ClientOptions struct {
    // ...
    // ResourceSubscriptionEndedHandler is called (under SEP-2575) when the
    // subscriptions/listen stream backing a Subscribe ends for a reason other
    // than a client-initiated Unsubscribe or Close.
    ResourceSubscriptionEndedHandler func(context.Context, *ResourceSubscriptionEndedRequest)
}

// ResourceSubscriptionEndedRequest describes a resource subscription whose
// SEP-2575 subscriptions/listen stream has ended without a client-initiated
// Unsubscribe or Close.
type ResourceSubscriptionEndedRequest struct {
    URI      string // the resource whose subscription ended
    Err      error  // the terminating error, or nil on a graceful listen result
    Graceful bool   // whether it ended with a normal result rather than an error
}

Semantics

  • Opt-in. A nil handler keeps the mcp: clear a resource subscription when its listen stream ends #1283 behavior exactly: the subscription is cleared silently and a bare re-Subscribe re-opens it.
  • The SDK does not auto-resubscribe — a permanently revoked URI would hot-loop. The application owns retry/backoff and may call Subscribe again for the URI from within the handler. This is safe: the entry is cleared before the handler fires, and a per-subscription generation guard makes re-subscribe-from-callback (and an Unsubscribe→Subscribe race) well-defined.
  • Fires only for non-client-initiated ends; Unsubscribe and Close do not invoke it.

Cross-SDK context

The python and typescript SDKs already surface subscription-termination signals to callers (python raises on subscription loss / ends the async iteration; typescript reports a close reason distinguishing local vs graceful vs remote). This proposal gives Go clients an equivalent, idiomatic callback.

Process

This adds exported API (ResourceSubscriptionEndedHandler, ResourceSubscriptionEndedRequest), so per CONTRIBUTING it goes through the proposal process — filing ahead of a PR. A working implementation already exists (it was the original form of #1283, before the behavior fix was split out to land without new API); happy to open the PR once the shape is approved.

Related: #1283 (behavior-only fix, no new exported API).

Activity

  1. jmrplens commented on Oct 19, 2026

    @jmrplens
    Contributor

    I would use this from the client side of an MCP server I maintain, which refuses a subscription when the caller's token cannot read the resource. On 2026-07-28 a Go client gets nil from Subscribe and never learns about the refusal, so this handler is what would let it tell the user.

    Some input on the shape, from writing it against current main:

    • Session on the request. ClientOptions is shared by every session a Client connects, so a handler that wants to subscribe again needs to know which session to call. I would add Session *ClientSession to the request.
    • Graceful. As proposed, Graceful is always Err == nil: the listen ends either with a result or with an error. Is the field worth keeping, or is Err == nil enough?
    • The name. Every other argument a ClientOptions handler receives is a ClientRequest[*XParams] and implements Request. This one describes a local event rather than a message from the server, so it would be a plain struct that does not. Keep ResourceSubscriptionEndedRequest to match the other handlers, or use a name that does not claim to be a request, such as ResourceSubscriptionEnded?
    • The list-changed listen. When mcp: Client.Connect succeeds after subscriptions/listen is rejected #1169 was closed, the note said this handler could be extended to the listen Connect opens for list-changed notifications. That listen has no URI, so it would need either a second handler or a request whose URI is empty. Which would you prefer?
    • Close. Close does not report the streams it ends. A Subscribe made after Close still returns nil today (ClientSession.Subscribe can silently succeed while the session is closing #1171), and its listen then fails with ErrConnectionClosed, so with the handler set that failure is reported. I would document that rather than change Subscribe in the same PR, since mcp: reject resource subscriptions during close #1172 and mcp: reject Subscribe on a closed session and roll back failed registrations #1198 already address it.

    I have a tested branch in this shape, Session included. @jeremy, you mentioned an implementation of your own from the original #1283; if you would rather open yours, please go ahead and I will leave it. Otherwise I am happy to open mine once the proposal is accepted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions