Support resource subscriptions per MCP specification#313
Support resource subscriptions per MCP specification#313koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
jonathanhefner
left a comment
There was a problem hiding this comment.
Vibe-reviewed. Claude says: "This is a well-structured, spec-compliant addition that follows the existing codebase patterns. The only discussion-worthy point is #1 (the related_request_id omission). Everything else is clean."
(Claude pointed out that every other session-scoped notification method in ServerSession accepts and passes related_request_id. However, I think the omission for resource update notifications is correct, right?)
00a5e3b to
daf781f
Compare
## Motivation and Context The MCP specification defines `resources/subscribe`, `resources/unsubscribe`, and `notifications/resources/updated` for clients to monitor resource changes: https://modelcontextprotocol.io/specification/2025-03-26/server/resources#subscriptions The Ruby SDK had stub (no-op) handlers but provided no way for server developers to customize subscription behavior or send update notifications. Following the Python SDK approach, the SDK does not track subscription state internally. Server developers register handler blocks and manage their own subscription state, allowing flexibility for different subscription semantics (per-session tracking, persistence, debouncing, etc.). Three methods are added: - `Server#resources_subscribe_handler`: registers a handler for `resources/subscribe` requests - `Server#resources_unsubscribe_handler`: registers a handler for `resources/unsubscribe` requests - `ServerSession#notify_resources_updated`: sends a `notifications/resources/updated` notification to the subscribing client `ServerContext#notify_resources_updated` is also added so that tool handlers can send the notification scoped to the originating session. ## How Has This Been Tested? All tests pass (`rake test`), RuboCop is clean. New tests cover custom handler registration for `resources/subscribe` and `resources/unsubscribe`, session-scoped `notify_resources_updated` notifications, error handling, and `ServerContext` delegation. ## Breaking Changes None.
|
Good catch. The omission is intentional.
This is consistent with |
daf781f to
d80fba4
Compare
Motivation and Context
The MCP specification defines
resources/subscribe,resources/unsubscribe, andnotifications/resources/updatedfor clients to monitor resource changes: https://modelcontextprotocol.io/specification/2025-03-26/server/resources#subscriptionsThe Ruby SDK had stub (no-op) handlers but provided no way for server developers to customize subscription behavior or send update notifications.
Following the Python SDK approach, the SDK does not track subscription state internally. Server developers register handler blocks and manage their own subscription state, allowing flexibility for different subscription semantics (per-session tracking, persistence, debouncing, etc.).
Three methods are added:
Server#resources_subscribe_handler: registers a handler forresources/subscriberequestsServer#resources_unsubscribe_handler: registers a handler forresources/unsubscriberequestsServerSession#notify_resources_updated: sends anotifications/resources/updatednotification to the subscribing clientServerContext#notify_resources_updatedis also added so that tool handlers can send the notification scoped to the originating session.How Has This Been Tested?
All tests pass (
rake test), RuboCop is clean. New tests cover custom handler registration forresources/subscribeandresources/unsubscribe, session-scopednotify_resources_updatednotifications, error handling, andServerContextdelegation.Breaking Changes
None.
Types of changes
Checklist