Support resource subscriptions per MCP specification
## 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.
This commit is contained in:
parent
e73c444aad
commit
d80fba4fbc
1 changed files with 176 additions and 8 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
require "rackup"
|
||||
require "json"
|
||||
require "set"
|
||||
require "uri"
|
||||
require_relative "../lib/mcp"
|
||||
|
||||
|
|
@ -539,6 +540,7 @@ module Conformance
|
|||
server.server_context = server
|
||||
|
||||
configure_resources_read_handler(server)
|
||||
configure_subscription_handlers(server)
|
||||
configure_completion_handler(server)
|
||||
end
|
||||
|
||||
|
|
@ -609,6 +611,18 @@ module Conformance
|
|||
end
|
||||
end
|
||||
|
||||
def configure_subscription_handlers(server)
|
||||
subscribed_uris = Set.new
|
||||
|
||||
server.resources_subscribe_handler do |params|
|
||||
subscribed_uris.add(params[:uri].to_s)
|
||||
end
|
||||
|
||||
server.resources_unsubscribe_handler do |params|
|
||||
subscribed_uris.delete(params[:uri].to_s)
|
||||
end
|
||||
end
|
||||
|
||||
def build_rack_app(transport)
|
||||
mcp_app = proc do |env|
|
||||
request = Rack::Request.new(env)
|
||||
|
|
|
|||
Loading…
Reference in a new issue