ruby-sdk/lib/mcp/methods.rb
Koichi ITO eeb130171d Add roots/list and notifications/roots/list_changed support
## Motivation and Context

MCP spec (2025-11-25) defines Roots as a mechanism for clients to expose filesystem boundaries
(directories and files) to servers:
https://modelcontextprotocol.io/specification/2025-11-25/client/roots

This enables servers to understand which parts of the filesystem they can operate on.

This adds support for the two roots protocol interactions:

- `roots/list` -- server-to-client request to retrieve the client's filesystem roots
- `notifications/roots/list_changed` -- client-to-server notification when roots change

`roots/list` is a one-to-one request between a server and a specific client,
so `list_roots` is exposed only at the session level
(`ServerSession#list_roots`), matching the Python SDK approach and aligning with PR #311 for sampling.

`Server#on_roots_list_changed` registers a callback for the client notification,
which is server-wide and does not require session scoping.

Also fixes `notifications/roots/list_changed` capability checking: as a client-originated notification,
it should not require server-side capability declaration.

## How Has This Been Tested?

New test file `test/mcp/server_roots_test.rb` with 8 test cases covering session-scoped `list_roots`,
capability validation and fallback, `on_roots_list_changed` callback registration, and client capability storage.
Updated `test/mcp/methods_test.rb` to reflect the corrected capability checks. All tests pass (`rake test`),
RuboCop is clean.

## Breaking Changes

None.
2026-04-15 02:39:14 +09:00

97 lines
3.9 KiB
Ruby

# frozen_string_literal: true
module MCP
module Methods
INITIALIZE = "initialize"
PING = "ping"
LOGGING_SET_LEVEL = "logging/setLevel"
PROMPTS_GET = "prompts/get"
PROMPTS_LIST = "prompts/list"
COMPLETION_COMPLETE = "completion/complete"
RESOURCES_LIST = "resources/list"
RESOURCES_READ = "resources/read"
RESOURCES_TEMPLATES_LIST = "resources/templates/list"
RESOURCES_SUBSCRIBE = "resources/subscribe"
RESOURCES_UNSUBSCRIBE = "resources/unsubscribe"
TOOLS_CALL = "tools/call"
TOOLS_LIST = "tools/list"
ROOTS_LIST = "roots/list"
SAMPLING_CREATE_MESSAGE = "sampling/createMessage"
ELICITATION_CREATE = "elicitation/create"
# Notification methods
NOTIFICATIONS_INITIALIZED = "notifications/initialized"
NOTIFICATIONS_TOOLS_LIST_CHANGED = "notifications/tools/list_changed"
NOTIFICATIONS_PROMPTS_LIST_CHANGED = "notifications/prompts/list_changed"
NOTIFICATIONS_RESOURCES_LIST_CHANGED = "notifications/resources/list_changed"
NOTIFICATIONS_RESOURCES_UPDATED = "notifications/resources/updated"
NOTIFICATIONS_ROOTS_LIST_CHANGED = "notifications/roots/list_changed"
NOTIFICATIONS_MESSAGE = "notifications/message"
NOTIFICATIONS_PROGRESS = "notifications/progress"
NOTIFICATIONS_CANCELLED = "notifications/cancelled"
NOTIFICATIONS_ELICITATION_COMPLETE = "notifications/elicitation/complete"
class MissingRequiredCapabilityError < StandardError
attr_reader :method
attr_reader :capability
def initialize(method, capability)
super("Server does not support #{capability} (required for #{method})")
@method = method
@capability = capability
end
end
class << self
def ensure_capability!(method, capabilities)
case method
when PROMPTS_GET, PROMPTS_LIST
require_capability!(method, capabilities, :prompts)
when NOTIFICATIONS_PROMPTS_LIST_CHANGED
require_capability!(method, capabilities, :prompts)
require_capability!(method, capabilities, :prompts, :listChanged)
when RESOURCES_LIST, RESOURCES_TEMPLATES_LIST, RESOURCES_READ
require_capability!(method, capabilities, :resources)
when NOTIFICATIONS_RESOURCES_LIST_CHANGED
require_capability!(method, capabilities, :resources)
require_capability!(method, capabilities, :resources, :listChanged)
when RESOURCES_SUBSCRIBE, RESOURCES_UNSUBSCRIBE, NOTIFICATIONS_RESOURCES_UPDATED
require_capability!(method, capabilities, :resources)
require_capability!(method, capabilities, :resources, :subscribe)
when TOOLS_CALL, TOOLS_LIST
require_capability!(method, capabilities, :tools)
when NOTIFICATIONS_TOOLS_LIST_CHANGED
require_capability!(method, capabilities, :tools)
require_capability!(method, capabilities, :tools, :listChanged)
when LOGGING_SET_LEVEL, NOTIFICATIONS_MESSAGE
require_capability!(method, capabilities, :logging)
when COMPLETION_COMPLETE
require_capability!(method, capabilities, :completions)
when ROOTS_LIST
require_capability!(method, capabilities, :roots)
when SAMPLING_CREATE_MESSAGE
require_capability!(method, capabilities, :sampling)
when ELICITATION_CREATE
require_capability!(method, capabilities, :elicitation)
when INITIALIZE, PING, NOTIFICATIONS_INITIALIZED, NOTIFICATIONS_ROOTS_LIST_CHANGED,
NOTIFICATIONS_PROGRESS, NOTIFICATIONS_CANCELLED, NOTIFICATIONS_ELICITATION_COMPLETE
# No specific capability required.
end
end
private
def require_capability!(method, capabilities, *keys)
name = keys.join(".") # :resources, :subscribe -> "resources.subscribe"
has_capability = capabilities.dig(*keys)
return if has_capability
raise MissingRequiredCapabilityError.new(method, name)
end
end
end
end