ruby-sdk/test/mcp
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Koichi ITO 719e5e7fee Introduce ServerSession for per-connection state
## Motivation and Context

The Ruby SDK uses a 1-Server-to-many-sessions model where session-specific state
(`@client`, `@logging_message_notification`) is stored on the shared `Server` instance.
This causes the last client to connect to overwrite state for all sessions,
and requires `session_id` to be threaded through method signatures for session-scoped notifications.

The Python SDK avoids this by creating a `ServerSession` per connection that wraps a shared `Server`.
Each session holds its own state and naturally scopes notifications through its own stream.

### Changes

- Added `MCP::ServerSession` class holding per-session state: client info,
  logging configuration, and a `transport` reference for session-scoped
  notification delivery.
- `StreamableHTTPTransport#handle_initialization` creates a `ServerSession`
  per `initialize` request and stores it in the session hash.
- `handle_regular_request` routes requests through the session's
  `ServerSession` instead of the shared `Server` directly.
- `Server#handle` and `Server#handle_json` accept an optional `session:`
  keyword (`ServerSession` instance).
- `Server#init` stores client info on the session when available.
- `Server#configure_logging_level` stores logging config on the session.
- `ServerContext` and `Progress` accept a `notification_target:` which
  can be either a `ServerSession` (session-scoped) or a `Server` (broadcast).
  No `session_id:` parameter needed.
- `ServerContext#notify_progress` and `#notify_log_message` delegate to
  the `notification_target` without `session_id` threading.
- `StdioTransport` creates a single `ServerSession` on `open`,
  making the session model transparent across both transports.

### Design Decision

This follows the Python SDK's "shared `Server` + per-connection `Session`" pattern.
The `Server` holds configuration (tools, prompts, resources, handlers).
Each `ServerSession` holds per-connection state (client info, logging level, stream writer).
Notifications from `ServerSession` go only to that session's stream.

## How Has This Been Tested?

All existing tests pass. All conformance tests pass. Added tests verifying:

- Session-scoped log notification is sent only to the originating session.
- Session-scoped progress notification is sent only to the originating session.
- Each session stores its own client info independently.
- Each session stores its own logging level independently.
- `StdioTransport#open` creates a `ServerSession` and stores client info on it.

## Breaking Change

None for end users.

The public API (`Server.new`, `define_tool`,`server_context.report_progress`,
`server_context.notify_log_message`, etc.) is unchanged. The following internal API
changes affect only SDK internals:

- `ServerContext.new` now requires `notification_target:` instead of just`progress:`.
- `Progress.new` now takes `notification_target:` instead of `server:`.
- `Server#handle` and `Server#handle_json` accept an optional `session:` keyword.
2026-03-29 08:04:56 +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 Introduce ServerSession for per-connection state 2026-03-29 08:04:56 +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