Skip to content

fix: unseal requestState tokens sealed with a fractional ttl - #714

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/request-state-security-float-ttl
Nov 28, 2026
Merged

koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/request-state-security-float-ttl

Conversation

@yorzi

@yorzi yorzi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

A RequestStateSecurity built with a fractional ttl: now unseals its own tokens, and a ttl that is not finite is refused at construction.

Fixes #713

Motivation and Context

RequestStateSecurity.new accepts any positive Numeric as ttl: (lib/mcp/server/request_state_security.rb:41), but seal writes exp as Time.now.to_i + ttl (:54) and unseal only accepts an Integer exp (:110). With a ttl such as 30.5, 60.0 or Rational(5, 2), exp becomes a Float (or a Rational, which JSON writes as a string), so every token is rejected as expired the moment it comes back, and every input_required round that uses requestState fails with an invalid-state error. ttl: Float::INFINITY passes the constructor check and then makes seal raise JSON::GeneratorError.

seal now writes exp as (Time.now + ttl).to_i, so exp stays a whole number of seconds and unseal is unchanged: an Integer ttl gives exactly the previous exp, and a fractional one ends within the second it falls in, the same one-second granularity an Integer ttl has. Tokens already issued keep unsealing. The constructor also requires a finite ttl, so Float::INFINITY and Float::NAN raise ArgumentError there instead of failing at the first seal.

How Has This Been Tested?

New tests in test/mcp/server/request_state_security_test.rb:

  • Tokens sealed with a ttl of 30.5, 60.0 and Rational(5, 2) unseal right away and are rejected once the ttl has passed.
  • Float::INFINITY and Float::NAN are refused by the constructor.

Both fail against the previous library (InvalidStateError: expired, and no ArgumentError). Reverting either half of the change fails one of them. bundle exec rake test, bundle exec rake rubocop, bundle exec yard --no-output and bundle exec rake conformance:test (baseline) pass locally on Ruby 4.0.9.

Breaking Changes

None. A non-finite ttl now raises ArgumentError at construction; it could not seal a token before.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed (none needed: the MRTR page does not describe the ttl's type)

## Motivation and Context

`RequestStateSecurity.new` accepts any positive `Numeric` as `ttl:`, but `seal` writes `exp` as `Time.now.to_i + ttl`
and `unseal` only accepts an `Integer` `exp`. With a fractional ttl such as `30.5`, `60.0` or `Rational(5, 2)`, `exp`
becomes a `Float` (or a `Rational`, which JSON writes as a string), so every token is rejected as `expired` the moment
it comes back, and every `input_required` round that uses `requestState` fails with an invalid-state error.
`ttl: Float::INFINITY` passes the constructor check and then makes `seal` raise `JSON::GeneratorError`.

`seal` now writes `exp` as `(Time.now + ttl).to_i`, so `exp` stays a whole number of seconds and `unseal` is unchanged:
an `Integer` ttl gives exactly the previous `exp`, and a fractional one ends within the second it falls in, the same
one-second granularity an `Integer` ttl has. Tokens already issued keep unsealing. The constructor also requires a
finite ttl, so `Float::INFINITY` and `Float::NAN` raise `ArgumentError` there instead of failing at the first `seal`.

## How Has This Been Tested?

New tests in `test/mcp/server/request_state_security_test.rb`: tokens sealed with a ttl of `30.5`, `60.0` and
`Rational(5, 2)` unseal right away and are rejected once the ttl has passed; `Float::INFINITY` and `Float::NAN` are
refused by the constructor. Both fail against the previous library (`expired`, and no `ArgumentError`), and reverting
either half of the change fails one of them.

## Breaking Changes

None. A non-finite ttl now raises `ArgumentError` at construction; it could not seal a token before.

Fixes modelcontextprotocol#713.
@koic
koic merged commit 1050f84 into modelcontextprotocol:main Nov 28, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RequestStateSecurity rejects every token sealed with a fractional ttl

2 participants