Skip to content

fix: validate transport timeouts and input round limits - #736

Open
yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/reject-non-finite-timeouts-and-invalid-max-rounds
Open

yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/reject-non-finite-timeouts-and-invalid-max-rounds

Conversation

@yorzi

@yorzi yorzi commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Transport constructors refuse durations that are not positive and finite, and MCP::Client.new refuses an input_required_max_rounds that is not a non-negative Integer.

Fixes #735

Motivation and Context

Float::INFINITY passes the checks of the Streamable HTTP server options and of max_reconnection_wait, and session_idle_timeout also takes NaN. read_timeout of MCP::Client::Stdio and input_required_max_rounds are not checked. Most of the waits these values feed raise RangeError later, away from the configuration mistake.

A private Duration.positive_finite? now backs all seven duration options. It also refuses Complex, on which positive? is not defined. nil stays valid where it is documented. input_required_max_rounds: 0 stays valid: no handler runs, and InputRequiredError is raised for manual driving.

How Has This Been Tested?

  • Automated unit / integration tests

  • Tested manually

  • Tested in a real application

  • test/mcp/server/transports/streamable_http_transport_test.rb: each of the five server options refuses infinite, NaN, complex, false and string values.

  • The same file: an invalid session_idle_timeout, false included, is reported as such with and without stateless: true.

  • test/mcp/client/http_test.rb and test/mcp/client/stdio_test.rb: max_reconnection_wait and read_timeout refuse infinite, NaN, complex, string and non-positive values, and read_timeout keeps nil.

  • test/mcp/client_test.rb: input_required_max_rounds refuses nil, negative, fractional and infinite values, and 0 raises InputRequiredError without running a handler.

Nine of these ten tests fail on main; the one for 0 guards existing behavior. Reverting any one check, dropping a documented nil, or dropping any one condition of the predicate fails a test. bundle exec rake test (2,838 tests, 10,447 assertions), 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

Invalid values now raise ArgumentError from the constructor. Some of them have an effect today and need a replacement:

  • session_idle_timeout: Float::INFINITY, Float::NAN and false disable expiry. Pass nil. false was also accepted with stateless: true.
  • max_stream_lifetime and stream_write_timeout: Float::INFINITY means no limit. Pass nil.
  • read_timeout of MCP::Client::Stdio: false means no timeout. Pass nil, the default.
  • listen_keepalive_interval: Float::INFINITY works on streams with a deadline, which every authenticated stream has by default. Pass a large finite number. nil would also stop closing a quiet stream at its deadline.
  • input_required_max_rounds: a Float or Rational is rounded down. Pass an Integer. Float::INFINITY and Float::NAN mean no cap. Pass a large Integer, as nil is not accepted. A negative number behaves like 0. Pass 0.

The messages of the duration options now say "positive finite number", and that of session_idle_timeout says that nil disables the timeout. An invalid session_idle_timeout is reported ahead of stateless mode not supporting the option.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Test update (non-breaking change which adds or updates tests)
  • Refactoring (non-breaking change which makes things better)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • CI/build script update (update CI workflow or build scripts)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • I have added automated tests for my change
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed (docs/_client/mrtr.md, docs/_client/transports.md)

end

if @session_idle_timeout
unless @session_idle_timeout.nil?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

session_idle_timeout: false disabled the timeout until now, since the old check tested truthiness. With this change it raises, and together with stateless: true the error is "session_idle_timeout is not supported in stateless mode.", which does not describe the problem. Is refusing false intended? If so, the message for it should say that nil disables the timeout.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, refusing false is intended: the documented type is [Numeric, nil], and the other duration options of this transport already refuse it. You are right about the message. The value is now checked before the mode, so false raises "session_idle_timeout must be a positive finite number, or nil to disable the timeout." with and without stateless: true, and a test covers both. I also changed the other duration messages to say "positive finite number", since "positive number" does not explain why Float::INFINITY is refused.

DEFAULT_MAX_STREAM_LIFETIME = DEFAULT_SESSION_IDLE_TIMEOUT

# Creates a Streamable HTTP transport that can be mounted as a Rack app.
# Timeouts and keepalive intervals take positive, finite durations in seconds. Optional bounds accept nil.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message says that non-finite timeouts are rejected. It does not say that some values that work today stop working: Float::INFINITY for session_idle_timeout and max_stream_lifetime meant "never expire" (the issue notes this), false for session_idle_timeout disabled it, and input_required_max_rounds accepted a Float such as 10.0. Please state these in the commit message, with nil as the way to ask for no limit, so that the release notes can carry it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, the commit message now has a Breaking Changes section. Checking each value on main showed that my list, and the "two infinite values" in #735, were incomplete. stream_write_timeout: Float::INFINITY and session_idle_timeout: Float::NAN also meant no limit, and nil replaces both.

nil is not the replacement in two cases. input_required_max_rounds does not accept it, so Float::INFINITY there becomes a large Integer. listen_keepalive_interval: Float::INFINITY closed an authenticated stream at its deadline, which nil does not do while the stream is quiet, so the message suggests a large finite number. I corrected #735 and the PR description too.

## Motivation and Context

Durations that are not finite were accepted by the transport constructors, and most of them raised `RangeError`
later, in the wait that used them. The constructors now require a positive finite number, or `nil` where the option
documents it. `input_required_max_rounds` must be a non-negative Integer; `0` still leaves the input to the caller.

## Breaking Changes

These values had an effect and now raise `ArgumentError`:

- `session_idle_timeout`: `Float::INFINITY`, `Float::NAN` and `false` disabled expiry. Pass `nil`.
- `max_stream_lifetime` and `stream_write_timeout`: `Float::INFINITY` meant no limit. Pass `nil`.
- `read_timeout` of `MCP::Client::Stdio`: `false` meant no timeout. Pass `nil`.
- `listen_keepalive_interval`: `Float::INFINITY` worked on streams with a deadline. Pass a large finite number;
  `nil` would also stop closing a quiet stream at its deadline.
- `input_required_max_rounds`: a Float or Rational was rounded down; pass an Integer. `Float::INFINITY` and
  `Float::NAN` meant no cap; pass a large Integer, as `nil` is not accepted. A negative number acted as `0`; pass `0`.

Fixes modelcontextprotocol#735.
@yorzi
yorzi force-pushed the fix/reject-non-finite-timeouts-and-invalid-max-rounds branch from 02f6142 to bcc5ea4 Compare December 1, 2026 10:20
@yorzi
yorzi requested a review from koic December 1, 2026 10:25

This branch has not been deployed

No deployments
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.

Transport constructors accept invalid timeouts and round limits

2 participants