ruby-sdk/test/mcp
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Koichi ITO 27044b0047 Add a resources_list_handler for context-dependent resource lists
## Motivation and Context

`resources/read` can be taken over with `resources_read_handler`, but `resources/list` had no equivalent:
it was bound to the internal `list_resources`, which paginates the constructor-provided `resources` array,
and `define_custom_method` refuses to rebind a method the server already handles.
An application whose visible resources depend on the request - authenticated versus anonymous sessions,
or an OAuth-scoped connector that must see a scope-filtered set - had to build a fresh `Server` per request
or override a private method.

Both reference SDKs let the list vary per request. The TypeScript SDK exposes `resources/list` through
`setRequestHandler` and gives resource templates a per-context `listCallback(ctx)`; the Python SDK takes
an `on_list_resources` handler. This adds the Ruby equivalent.

`resources_list_handler(&block)` registers a block that returns the resource collection to serve.
It feeds `list_resources` rather than replacing it, so the framework still paginates the returned array
and stamps the SEP-2549 cache hints; the block returns only the array, matching how `resources_read_handler` returns
only the contents and the framework wraps them. A block that declares a `server_context:` keyword receives
an `MCP::ServerContext`, reusing the same opt-in rule the read handler uses, so the list can be filtered by
the authenticated principal or granted scope. The block is invoked once per page and must return
a stable ordering across the pages of one query, since the cursor is a positional offset. When no handler is set,
the constructor-provided resources are served unchanged.

The `resources` capability is advertised whether or not the constructor array is populated, so a server that
provides only a `resources_list_handler` still answers `resources/list`.

Fixes #507.

## How Has This Been Tested?

New tests in `test/mcp/server_test.rb` cover a handler replacing the served collection, a handler receiving `server_context:`
and returning different sets for authenticated and anonymous requests, pagination and cache hints still applying to
the handler-provided array, and a server with no constructor-provided resources served entirely by the handler.
The existing `resources/list` test, which exercises the no-handler default path, is unchanged.

## Breaking Changes

None. When no `resources_list_handler` is set, `resources/list` serves the constructor-provided resources exactly as before.
2026-08-13 00:21:41 +09:00
..
client Mirror x-mcp-header tool parameters into Mcp-Param-* headers per SEP-2243 2026-08-08 20:58:35 +09:00
elicitation Add MCP::Elicitation::EnumSchema builders for SEP-1330 2026-08-05 12:26:13 +09:00
prompt Add _meta field to resource, content, and result classes 2026-04-11 15:08:55 +09:00
resource Add _meta field to resource, content, and result classes 2026-04-11 15:08:55 +09:00
server Merge pull request #496 from koic/request_state_security 2026-08-10 12:19:56 +09:00
tool fix: preserve explicit tool response content 2026-07-23 03:11:48 +08:00
annotations_test.rb Add audience role validation for MCP::Annotations per MCP specification 2026-06-22 01:13:17 +09:00
apps_test.rb Add Server-Side MCP Apps Helpers per SEP-1865 2026-07-07 01:16:02 +09:00
cancellation_test.rb Support notifications/cancelled per MCP specification 2026-05-03 04:02:22 +09:00
client_test.rb Mirror x-mcp-header tool parameters into Mcp-Param-* headers per SEP-2243 2026-08-08 20:58:35 +09:00
configuration_test.rb Expose the user-defined server_context in instrumentation data 2026-08-08 08:33:07 +09:00
content_test.rb Add _meta field to resource, content, and result classes 2026-04-11 15:08:55 +09:00
error_codes_test.rb Add Stateless Lifecycle Foundations per SEP-2575 2026-07-26 20:03:57 +09:00
icon_test.rb Replace assert_empty with assert_equal using explicit literals 2026-02-24 16:09:34 +09:00
instrumentation_test.rb Expose the user-defined server_context in instrumentation data 2026-08-08 08:33:07 +09:00
logging_message_notification_test.rb Add logging support 2026-01-26 22:23:46 +09:00
methods_test.rb Add server/discover and stateless lifecycle error codes per SEP-2575 2026-07-04 20:24:37 +09:00
progress_test.rb Auto-set server.transport in Transport#initialize 2026-04-09 12:08:55 +09:00
prompt_test.rb Fix Prompt#validate_arguments! crash when arguments are nil 2026-03-01 17:21:41 +09:00
protocol_deprecations_test.rb feat: deprecate roots sampling and logging 2026-08-02 13:13:10 -04:00
request_envelope_test.rb Align modern envelope validation with the finalized spec 2026-08-08 16:00:29 +09:00
resource_template_test.rb Support class-based Resource and ResourceTemplate definitions 2026-07-10 22:39:42 +09:00
resource_test.rb Support class-based Resource and ResourceTemplate definitions 2026-07-10 22:39:42 +09:00
server_cancellation_test.rb Fix an incorrect result: null response to an id-bearing notification message 2026-07-01 01:55:15 +09:00
server_context_test.rb fix: stop leaking exception messages to clients via JSON-RPC error data 2026-08-06 18:28:17 +02:00
server_elicitation_test.rb Associate server-to-client requests with the originating client request per SEP-2260 2026-07-12 02:10:16 +09:00
server_notification_test.rb Stream modern request notifications and honor the envelope logLevel per SEP-2575 2026-08-08 15:22:58 +09:00
server_progress_test.rb Auto-set server.transport in Transport#initialize 2026-04-09 12:08:55 +09:00
server_roots_test.rb feat: deprecate roots sampling and logging 2026-08-02 13:13:10 -04:00
server_sampling_test.rb feat: deprecate roots sampling and logging 2026-08-02 13:13:10 -04:00
server_session_ping_test.rb Support server-to-client ping per MCP specification 2026-05-22 11:28:51 +09:00
server_test.rb Add a resources_list_handler for context-dependent resource lists 2026-08-13 00:21:41 +09:00
string_utils_test.rb Fix ReDoS vulnerability in StringUtils#underscore 2026-01-09 01:14:52 +09:00
tool_test.rb Speed Up Tool::Schema Validation by 5x to 100x 2026-06-01 21:47:30 +09:00
trace_context_test.rb Support W3C Trace Context Propagation via _meta per SEP-414 2026-06-12 14:36:24 +09:00
transport_test.rb Remove redundant require statements 2025-08-02 00:57:31 +09:00