Stream modern request notifications and honor the envelope logLevel per SEP-2575

## Motivation and Context

The modern (stateless) lifecycle of MCP 2026-07-28 serves each request as a self-contained exchange,
and two delivery requirements of that model were unimplemented:

- Notifications a handler emits while serving a modern request (progress, log messages)
  had nowhere to go: the modern path answered with a single JSON object,
  and the ephemeral per-request session degraded every notification to non-delivery.
  The conformance requirement expects them as frames on the request's own response stream
  (`tools-call-with-progress` fails with "No progress notifications received" at 2026-07-28).
- The envelope's `logLevel` member, which replaces the removed `logging/setLevel` RPC,
  was parsed but never applied: log delivery fell back to the server-wide level, violating the requirement
  that no `notifications/message` is sent for requests that did not set `_meta` `logLevel`.

The changes wire both up:

- `StreamableHTTPTransport#handle_modern` registers a per-request notification sink for its ephemeral session;
  `send_notification` routes into the sink ahead of the stateless guard.
  A request whose handler emitted notifications answers as an SSE stream carrying them ahead of the final response;
  a request that emitted none keeps the single JSON exchange and its HTTP status ladder.
- `Server#handle_request` applies the lifted envelope's `logLevel` to the session via the existing `configure_logging`,
  and on modern-era sessions `ServerSession#notify_log_message` no longer falls back to the server-wide level:
  without a per-request `logLevel` it stays silent, on every transport. An unrecognized level reads as absent,
  the safe direction, matching the Python SDK's `allowed_log_levels`.

The `server-stateless` scenario of the 2026-07-28 conformance requirements probes two stream-discipline
rules through diagnostic fixture tools the Ruby fixture did not define, so both checks failed as
not testable: `sep-2575-http-server-no-independent-requests-on-stream` calls `test_streaming_elicitation`
and requires that every frame on the request's response stream is a notification or the final response,
and `sep-2575-server-no-log-without-loglevel` calls `test_logging_tool` without an envelope `logLevel`
and requires that no `notifications/message` reaches the stream.

The fixture now defines both tools. `test_streaming_elicitation` reports progress and completes;
`test_logging_tool` calls `notify_log_message` unconditionally and relies on the SDK's per-request gate,
which drops the notification for modern requests that did not authorize logging through
`io.modelcontextprotocol/logLevel`. Both lean on the notification streaming this change adds:
with a `progressToken` or an authorizing `logLevel`, the notification is delivered as an SSE frame
ahead of the final response rather than being dropped.

One known limitation relative to the TypeScript and Python SDKs, which stream notifications live while the handler runs:
delivery here is buffered, flushing the frames after the handler returns. Ordering is preserved
and the conformance checks are satisfied, but long-running handlers get no real-time interleaving.
The sink is bounded (`MAX_MODERN_REQUEST_NOTIFICATIONS`, 1000): a handler emitting notifications
in proportion to client-supplied input cannot grow one request's memory without bound,
and past the cap the notify helpers report `false`, the same non-delivery degradation
they have on every other undeliverable path. Live streaming can follow without changing the wire shape.

## How Has This Been Tested?

`bundle exec rake test` passes with zero failures and RuboCop reports no offenses. New tests cover:
a modern `tools/call` streaming `notifications/progress` frames ahead of the result, the single JSON exchange staying
in place without notifications, log suppression without the envelope `logLevel`, log delivery with it,
the modern-era session ignoring the server-wide level while honoring a per-request one,
and the notification cap bounding one request's buffered frames while the notify helpers report `false` past it.

Against the conformance fixture server, the `tools-call-with-progress` progress check passes
at `--spec-version 2026-07-28` (its remaining wire-schema check belongs to the separate `resultType` change),
the stream-discipline checks `sep-2575-http-server-no-independent-requests-on-stream` and
`sep-2575-server-no-log-without-loglevel` report SUCCESS, and manual probes confirm the positive
directions the suite leaves untested: with `io.modelcontextprotocol/logLevel` in `_meta`,
`test_logging_tool` streams its `notifications/message` frame ahead of the result, and
with a `progressToken`, `test_streaming_elicitation` streams its `notifications/progress` frame.
The `--requirements 2025-11-25` server leg passes 78/78, unchanged.

## Breaking Changes

None for stable protocol versions: legacy requests and their notification delivery are unchanged.
Within the modern lifecycle, notifications become deliverable where they were previously dropped,
and log messages without a per-request `logLevel` stop leaking through the server-wide level,
as the specification requires.
This commit is contained in:
Koichi ITO 2026-08-08 05:05:10 +09:00
commit da2a7f80d7
3 changed files with 286 additions and 8 deletions

View file

@ -584,6 +584,16 @@ module MCP
exception_already_reported: ->(e) { reported_exception.equal?(e) },
) do
envelope = lift_request_envelope(params, method: method, session: session)
# The envelope's `logLevel` member replaces `logging/setLevel` in the modern lifecycle:
# it authorizes `notifications/message` for this request only (SEP-2575). Without it,
# `ServerSession#notify_log_message` stays silent on modern-era sessions. An unrecognized level
# reads as absent, so delivery stays off (the safe direction), matching the Python SDK.
if envelope&.log_level && session.respond_to?(:configure_logging)
request_logging = LoggingMessageNotification.new(level: envelope.log_level)
session.configure_logging(request_logging) if request_logging.valid_level?
end
result = case method
when Methods::INITIALIZE
init(params, session: session)

View file

@ -51,6 +51,13 @@ module MCP
# the stack or amplify parse cost (complements the byte cap).
MAX_JSON_NESTING = 64
# Cap on the notifications buffered for one modern request (SEP-2575). The sink holds them
# in memory until the handler returns, so a handler that emits notifications in proportion to
# client-supplied input would otherwise let one request grow memory without bound.
# Notifications past the cap are not delivered and the notify helpers report `false`,
# the same non-delivery degradation they have on every other undeliverable path.
MAX_MODERN_REQUEST_NOTIFICATIONS = 1_000
# Creates a Streamable HTTP transport that can be mounted as a Rack app.
#
# @param server [MCP::Server] the server whose requests this transport dispatches.
@ -107,6 +114,10 @@ module MCP
@allowed_origins = Array(allowed_origins).map(&:downcase).freeze
@pending_responses = {}
# Maps a modern request's ephemeral session id to the Array collecting the notifications its handler emits;
# `handle_modern` registers the sink and flushes it as SSE frames ahead of the final response (SEP-2575).
@modern_request_sinks = {}
# Resolve the idle timeout: an explicit value (including `nil` to opt out) wins; otherwise apply the secure default,
# which does not apply to stateless mode since it retains no sessions.
@session_idle_timeout = if session_idle_timeout.equal?(UNSET_IDLE_TIMEOUT)
@ -235,17 +246,30 @@ module MCP
end
def send_notification(method, params = nil, session_id: nil, related_request_id: nil)
# Stateless mode has no streams to deliver notifications on. Report non-delivery instead of raising
# so the ephemeral per-request session's notify_* helpers (e.g. progress or log notifications from
# a tool handler) degrade gracefully rather than spamming the exception reporter on every call.
return false if @stateless
notification = {
jsonrpc: "2.0",
method: method,
}
notification[:params] = params if params
# A modern request's notifications ride its own response stream (SEP-2575): `handle_modern` registers
# a per-request sink for its ephemeral session and flushes it as SSE frames ahead of the final response.
# Checked before the stateless guard, since modern requests are served in stateless deployments too.
# The sink is bounded; past `MAX_MODERN_REQUEST_NOTIFICATIONS` the notification is dropped as
# non-delivery (`false`), never handed to the legacy paths below.
sink = @mutex.synchronize { session_id && @modern_request_sinks[session_id] }
if sink
return false if sink.size >= MAX_MODERN_REQUEST_NOTIFICATIONS
sink << notification
return true
end
# Stateless mode has no streams to deliver notifications on. Report non-delivery instead of raising
# so the ephemeral per-request session's notify_* helpers (e.g. progress or log notifications from
# a tool handler) degrade gracefully rather than spamming the exception reporter on every call.
return false if @stateless
if session_id
deliver_targeted_notification(notification, session_id, related_request_id)
else
@ -564,12 +588,29 @@ module MCP
mismatch_error = validate_modern_headers(request, body, header_version)
return mismatch_error if mismatch_error
response = @server.handle(body, session: modern_session)
session = modern_session
notifications = @mutex.synchronize { @modern_request_sinks[session.session_id] = [] }
begin
response = @server.handle(body, session: session)
ensure
@mutex.synchronize { @modern_request_sinks.delete(session.session_id) }
end
# `nil` covers notifications and cancellation-suppressed responses; ack with 202 like the legacy notification path.
return handle_accepted if response.nil?
[modern_http_status(response), { "content-type" => "application/json" }, [response.to_json]]
# Notifications a handler emitted during the request ride the request's own response stream as SSE frames ahead of
# the final response (SEP-2575); a request that emitted none keeps the single JSON exchange and its HTTP status ladder.
# Delivery is buffered: frames flush after the handler returns, preserving order but not real-time interleaving
# (the TypeScript and Python SDKs stream live), and the sink grows with the handler's notification count.
# Live streaming can follow without changing the wire shape.
if notifications.empty?
[modern_http_status(response), { "content-type" => "application/json" }, [response.to_json]]
else
frames = notifications + [response]
sse_body = frames.map { |frame| "event: message\ndata: #{frame.to_json}\n\n" }.join
[200, SSE_HEADERS.dup, [sse_body]]
end
rescue StandardError => e
MCP.configuration.exception_reporter.call(e, { request: body_string })
json_rpc_error_response(

View file

@ -252,7 +252,15 @@ module MCP
def notify_log_message(data:, level:, logger: nil, related_request_id: nil)
@server.send(:warn_if_deprecated_protocol_feature, :logging, session: self, uplevel: 2)
effective_logging = @logging_message_notification || @server.logging_message_notification
# In the modern lifecycle, log delivery is authorized per request through the `_meta` envelope's `logLevel` member
# (applied via `configure_logging`); without it no `notifications/message` is sent, and the server-wide level
# does not apply (SEP-2575).
effective_logging = if @era == :modern
@logging_message_notification
else
@logging_message_notification || @server.logging_message_notification
end
return unless effective_logging&.should_notify?(level)
params = { "data" => data, "level" => level }