Add opt-in requestState sealing via RequestStateSecurity

## Motivation and Context

The SEP-2322 `requestState` continuation string leaves the server, sits in the client's hands,
and comes back as client-controlled input. Without protection a client can read the server's continuation state,
tamper with it (for example forging an already-answered elicitation), or replay a state issued for one call against
another tool, other arguments, or another server. The multi-round-trip documentation of the Python SDK is explicit
that the echo must be treated as untrusted input; its high-level server seals the state by default via `RequestStateBoundary`.

New `MCP::Server::RequestStateSecurity` (OpenSSL standard library only) brings that protection to this SDK as an opt-in:

- `seal` encrypts the plaintext state with AES-256-GCM (clients cannot read it, not merely verify it) inside a claims
  envelope binding an expiry window (`ttl:`, default 300 seconds, re-sealed each round), the originating method and target
  (tool/prompt name or resource URI), a digest of the originating arguments (stringified and sorted recursively,
  so symbol/string parses of identical JSON digest identically), and an optional `audience:`. The token format is
  `v1.<base64url(iv || ciphertext || tag)>` with the version prefix bound as GCM associated data,
  following the Python SDK's `AESGCMRequestStateCodec`.
- `unseal` verifies every claim fail-closed and raises `InvalidStateError` on tampering, expiry, or any mismatch.

Passing an instance via `Server.new(request_state_security:)` makes the seal/unseal transparent: issuance seals
the `requestState` of an outgoing `input_required` result, and dispatch unseals the echoed token for `tools/call`,
`prompts/get`, and `resources/read` before any handler runs, so `server_context.request_state` always reads
the plaintext the handler wrote. A tampered, expired, or cross-call echo is rejected as `-32602`
with "Invalid or expired requestState", matching the Python SDK's frozen error.

Without the option the state crosses the wire exactly as the handler wrote it (the Python low-level Server behavior);
the README documents that this is then the handler author's responsibility, and that multi-process deployments
must share the key across workers.

The conformance fixture opts in with a random per-boot key, both to dogfood the feature and because
the tampered-state scenario of the 2026-07-28 conformance requirements retries an MRTR request with
a corrupted `requestState` and requires a JSON-RPC error; without integrity-checked state the fixture
cannot detect the corruption. Every MRTR round trip completes within one server process,
so no key persistence is needed. Sealing is transparent to the fixture tools, whose `requestState` plaintext
round-trips unchanged, and the new `test_input_required_result_tampered_state` tool is therefore
a plain MRTR elicitation flow; the tampered echo is rejected with `-32602` by the server-level unsealing
before dispatch ever reaches it.

Refs #382.

## How Has This Been Tested?

New `test/mcp/server/request_state_security_test.rb` covers constructor validation,
the seal/unseal round trip (opaque `v1.` token, plaintext not visible), expiry via time travel,
fail-closed rejection of method/target/digest/audience mismatches, tampered and malformed tokens,
and tokens sealed under a different key.

New tests in `test/mcp/server_test.rb` drive `Server#handle` with the modern envelope:
transparent sealing on issuance and plaintext restoration on the retry leg, `-32602` for
tampered echoes and for echoes replayed against different arguments, and the pass-through
default when `request_state_security:` is not set.

Against the fixture server at `--spec-version 2026-07-28` (run with `@modelcontextprotocol/conformance@alpha`;
the 2026-07-28 scenarios are not yet in a stable conformance release), `sep-2322-reject-tampered-state` reports SUCCESS,
and the other 13 `input-required-result-*` scenarios stay green with sealing active,
confirming that sealed states round-trip through the suite's retries without false rejections
(the seal binds `method`, target, and an arguments digest, and the suite echoes all three unchanged).
The `--requirements 2025-11-25` server leg still passes.

## Breaking Changes

None. The feature is opt-in via a new keyword argument that defaults to `nil`, in which case behavior is byte-identical to before.
This commit is contained in:
Koichi ITO 2026-07-02 14:40:20 +09:00
commit e6b5a9b5e3

View file

@ -12,6 +12,7 @@ require_relative "server_context"
require_relative "server/capabilities"
require_relative "server/input_required_result"
require_relative "server/pagination"
require_relative "server/request_state_security"
require_relative "server/transports"
module MCP
@ -145,7 +146,7 @@ module MCP
CACHE_SCOPES = ["public", "private"].freeze
attr_accessor :description, :icons, :name, :title, :version, :website_url, :instructions, :tools, :prompts, :resource_templates, :server_context, :configuration, :capabilities, :transport, :logging_message_notification
attr_reader :resources, :page_size, :client_capabilities, :ttl_ms, :cache_scope
attr_reader :resources, :page_size, :client_capabilities, :ttl_ms, :cache_scope, :request_state_security
def initialize(
description: nil,
@ -165,6 +166,7 @@ module MCP
page_size: nil,
ttl_ms: nil,
cache_scope: nil,
request_state_security: nil,
transport: nil
)
@description = description
@ -184,6 +186,7 @@ module MCP
self.page_size = page_size
self.ttl_ms = ttl_ms
self.cache_scope = cache_scope
@request_state_security = request_state_security
@configuration = MCP.configuration.merge(configuration)
@client = nil
@client_protocol_version = nil
@ -594,6 +597,8 @@ module MCP
session.configure_logging(request_logging) if request_logging.valid_level?
end
params = unseal_request_state(params, method: method) if @request_state_security
result = case method
when Methods::INITIALIZE
init(params, session: session)
@ -628,7 +633,7 @@ module MCP
# Runs after the cancellation check so a cancelled request stays suppressed
# instead of turning into a gate error response.
if result.is_a?(InputRequiredResult)
result = serialize_input_required_result(result, envelope: envelope, request: params)
result = serialize_input_required_result(result, envelope: envelope, request: params, method: method)
end
# SEP-2322 makes `resultType` REQUIRED on every result a 2026-07-28 server returns;
@ -707,7 +712,7 @@ module MCP
# a final result. The capability gate enforces the SEP-2575 rule that servers MUST NOT rely on
# (or embed requests for) capabilities the client did not declare, and reports every missing capability at
# once so the client sees the full set.
def serialize_input_required_result(result, envelope:, request:)
def serialize_input_required_result(result, envelope:, request:, method:)
if envelope.nil?
raise RequestHandlerError.new(
"input_required results require the 2026-07-28 stateless lifecycle (SEP-2322)",
@ -720,7 +725,76 @@ module MCP
raise MissingRequiredClientCapabilityError.new(missing, request) unless missing.empty?
add_instrumentation_data(input_required: true)
result.to_h
serialized = result.to_h
if @request_state_security && serialized[:requestState]
serialized = serialized.merge(requestState: @request_state_security.seal(
serialized[:requestState],
method: method,
target: mrtr_target(request),
arguments_digest: mrtr_arguments_digest(request),
))
end
serialized
end
# Methods whose results may be `input_required` and whose retried requests carry
# `inputResponses`/`requestState` (SEP-2322).
MRTR_METHODS = [Methods::TOOLS_CALL, Methods::PROMPTS_GET, Methods::RESOURCES_READ].freeze
# Replaces a sealed client-echoed `requestState` with its verified plaintext before dispatch,
# so handlers always read the state they wrote. A tampered, expired, or cross-request token is
# rejected as invalid params, matching the Python SDK's "Invalid or expired requestState" behavior.
def unseal_request_state(params, method:)
return params unless MRTR_METHODS.include?(method)
return params unless params.is_a?(Hash)
sealed = params[:requestState] || params["requestState"]
return params unless sealed
plaintext = @request_state_security.unseal(
sealed,
method: method,
target: mrtr_target(params),
arguments_digest: mrtr_arguments_digest(params),
)
key = params.key?("requestState") ? "requestState" : :requestState
params.merge(key => plaintext)
rescue RequestStateSecurity::InvalidStateError => e
raise RequestHandlerError.new(
"Invalid or expired requestState",
params,
error_type: :invalid_params,
error_code: JsonRpcHandler::ErrorCode::INVALID_PARAMS,
original_error: e,
)
end
def mrtr_target(params)
return "" unless params.is_a?(Hash)
params[:name] || params["name"] || params[:uri] || params["uri"] || ""
end
# Digest of the originating arguments, binding a sealed state to retries of
# the same call with the same inputs. Keys are stringified and sorted recursively
# so symbol/string parses of identical JSON digest identically.
def mrtr_arguments_digest(params)
arguments = params.is_a?(Hash) ? params[:arguments] || params["arguments"] : nil
OpenSSL::Digest::SHA256.hexdigest(canonical_json(arguments || {}))
end
def canonical_json(value)
case value
when Hash
pairs = value.map { |key, nested| [key.to_s, nested] }.sort_by(&:first)
"{#{pairs.map { |key, nested| "#{key.to_json}:#{canonical_json(nested)}" }.join(",")}}"
when Array
"[#{value.map { |element| canonical_json(element) }.join(",")}]"
else
value.to_json
end
end
# Extracts the SEP-2322 retry fields a client sends when re-issuing a request: