ruby-sdk/lib/mcp/methods.rb
Koichi ITO 985fd03ecb Support elicitation/create per MCP specification
## Motivation and Context

The MCP specification (2025-11-25) defines elicitation as a server-to-client
request mechanism that allows servers to request user input during tool execution:
https://modelcontextprotocol.io/specification/2025-11-25/client/elicitation

This aligns with the Python SDK, which also exposes separate methods for form and URL mode elicitation.
Three methods are added following the Python SDK's API design:

- `create_form_elicitation`: collects structured data via JSON Schema
- `create_url_elicitation`: directs users to external URLs for sensitive
  interactions (OAuth, credentials, etc.)
- `notify_elicitation_complete`: notifies the client that a URL mode
  interaction has completed

`URLElicitationRequiredError` (error code `-32042`) is defined as a subclass of `RequestHandlerError`
with its own `error_code` and `error_data` attributes. `JsonRpcHandler#handle_request_error` is
extended to check for these attributes so that MCP-specific error codes can be propagated without
embedding MCP knowledge in the generic JSON-RPC layer.

## How Has This Been Tested?

- Unit tests for `ServerSession` form/URL mode, capability checks, nil
  capabilities, notification, and `URLElicitationRequiredError`
- `ServerContext` delegation tests for all three elicitation methods
- `JsonRpcHandler` tests for custom error code and fallback behavior
- `Methods` capability test for `NOTIFICATIONS_ELICITATION_COMPLETE`
- Conformance tests pass (40/40), including `tools-call-elicitation`,
  `elicitation-sep1034-defaults`, and `elicitation-sep1330-enums`
- All tests pass (`rake test`), RuboCop is clean
- Verified form mode elicitation end-to-end with a sample MCP server
  running in Claude Code

## Breaking Changes

None. This is a new feature addition.
2026-04-13 15:21:45 +09:00

99 lines
4 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 NOTIFICATIONS_ROOTS_LIST_CHANGED
require_capability!(method, capabilities, :roots)
require_capability!(method, capabilities, :roots, :listChanged)
when SAMPLING_CREATE_MESSAGE
require_capability!(method, capabilities, :sampling)
when ELICITATION_CREATE
require_capability!(method, capabilities, :elicitation)
when INITIALIZE, PING, NOTIFICATIONS_INITIALIZED, 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