ruby-sdk/test/mcp
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Koichi ITO 49fc501abc Support session expiry controls for StreamableHTTPTransport
## Motivation and Context

The MCP specification recommends expiring session IDs to reduce session hijacking risks:
https://modelcontextprotocol.io/specification/latest/basic/security_best_practices#session-hijacking

Several other SDKs (Go, C#, PHP, Python) already implement session expiry controls, but
the Ruby SDK did not. Sessions persisted indefinitely unless explicitly deleted or
a stream error occurred, leaving abandoned sessions to accumulate in memory.

This adds a `session_idle_timeout:` option to `StreamableHTTPTransport#initialize`. When
set, sessions that receive no HTTP requests for the specified duration (in seconds) are
automatically expired. Expired sessions return 404 on subsequent requests (GET and POST),
matching the MCP specification's behavior for terminated sessions. Each request resets
the idle timer, so actively used sessions are not interrupted.

A background reaper thread periodically cleans up expired sessions to handle orphaned
sessions that receive no further requests. The reaper only starts when
`session_idle_timeout` is configured.

The default is `nil` (no expiry) for backward compatibility, consistent with the Python
SDK's approach. The Python SDK recommends 1800 seconds (30 minutes) for most deployments:
https://github.com/modelcontextprotocol/python-sdk/pull/2022

Resolves #265.

## How Has This Been Tested?

Added tests for session expiry, idle timeout reset, reaper thread lifecycle, input
validation, and default behavior in `streamable_http_transport_test.rb`. All existing
tests continue to pass.

## Breaking Change

None. The default value of `session_idle_timeout` is `nil`, which preserves the existing
behavior of sessions never expiring. The new `last_active_at` field in the internal
session hash is not part of the public API. Existing code that instantiates
`StreamableHTTPTransport.new(server)` or
`StreamableHTTPTransport.new(server, stateless: true)` continues to work without changes.
2026-03-29 12:34:58 +09:00
..
client Add MCP::Client::Stdio transport 2026-03-19 12:41:27 +09:00
resource Fix Resource::Contents#to_h to use correct property names per MCP spec 2026-02-19 13:50:10 +09:00
server/transports Support session expiry controls for StreamableHTTPTransport 2026-03-29 12:34:58 +09:00
tool Merge pull request #237 from koic/replace_assert_empty_with_assert_equal_using_explicit_literals 2026-03-03 01:05:16 +09:00
annotations_test.rb Replace assert_empty with assert_equal using explicit literals 2026-02-24 16:09:34 +09:00
client_test.rb Allow Client#call_tool to accept a tool name 2026-03-23 00:43:59 +09:00
configuration_test.rb Merge pull request #192 from koic/add_missing_assertion 2025-12-25 00:33:12 +09:00
content_test.rb Add Content::EmbeddedResource class for embedded resource content type 2026-03-02 01:33:05 +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 Use autoload to defer loading of unused subsystems 2026-03-12 14:08:27 +09:00
logging_message_notification_test.rb Add logging support 2026-01-26 22:23:46 +09:00
methods_test.rb Use autoload to defer loading of unused subsystems 2026-03-12 14:08:27 +09:00
progress_test.rb Introduce ServerSession for per-connection state 2026-03-29 08:04:56 +09:00
prompt_test.rb Fix Prompt#validate_arguments! crash when arguments are nil 2026-03-01 17:21:41 +09:00
resource_template_test.rb Omit icons from responses when empty or nil 2026-01-12 09:46:45 +09:00
resource_test.rb Omit icons from responses when empty or nil 2026-01-12 09:46:45 +09:00
server_context_test.rb Introduce ServerSession for per-connection state 2026-03-29 08:04:56 +09:00
server_notification_test.rb Introduce ServerSession for per-connection state 2026-03-29 08:04:56 +09:00
server_progress_test.rb Support progress notifications per MCP specification 2026-03-10 01:49:09 +09:00
server_test.rb Introduce ServerSession for per-connection state 2026-03-29 08:04:56 +09:00
string_utils_test.rb Fix ReDoS vulnerability in StringUtils#underscore 2026-01-09 01:14:52 +09:00
tool_test.rb Allow $ref in Tool::Schema for protocol version 2025-11-25 2026-02-27 14:33:36 +09:00
transport_test.rb Remove redundant require statements 2025-08-02 00:57:31 +09:00