Repository navigation
Description
Describe the bug
Seven duration options and MCP::Client.new(input_required_max_rounds:) let through values that only fail later, or that do nothing useful:
server_to_client_request_timeout,listen_keepalive_interval,stream_write_timeoutandmax_stream_lifetimeofStreamableHTTPTransport, andmax_reconnection_waitofMCP::Client::HTTP, acceptFloat::INFINITY.session_idle_timeoutalso acceptsNaN.read_timeoutofMCP::Client::Stdiois not checked at all, soFloat::INFINITY, negative numbers and strings are accepted.input_required_max_roundsis not checked either, sonilandNaNare accepted.
An infinite wait is not an unbounded one: ConditionVariable#wait and IO#wait_readable raise RangeError. With stream_write_timeout: Float::INFINITY, Timeout.timeout(Float::INFINITY) ends the process-wide timeout thread with the same error (timeout 0.6.1). OAuth::TokenVerifier and RequestStateSecurity already refuse non-finite durations.
To Reproduce
Save as repro.rb in the repository root and run bundle exec ruby -Ilib repro.rb on main (bafdca4):
require "mcp"
require "mcp/client"
require "mcp/client/http"
require "mcp/client/stdio"
def attempt(label)
yield
puts "#{label}: accepted"
rescue => e
puts "#{label}: #{e.class}: #{e.message}"
end
Transport = MCP::Server::Transports::StreamableHTTPTransport
server = MCP::Server.new(name: "repro", version: "1.0")
# 1. Accepted by the constructor, although the wait each of them feeds cannot use it.
attempt("server_to_client_request_timeout: Infinity") { Transport.new(server, server_to_client_request_timeout: Float::INFINITY).close }
attempt("listen_keepalive_interval: Infinity") { Transport.new(server, listen_keepalive_interval: Float::INFINITY).close }
attempt("stream_write_timeout: Infinity") { Transport.new(server, stream_write_timeout: Float::INFINITY).close }
attempt("max_reconnection_wait: Infinity") { MCP::Client::HTTP.new(url: "http://localhost", max_reconnection_wait: Float::INFINITY) }
attempt("read_timeout: Infinity") { MCP::Client::Stdio.new(command: "ruby", read_timeout: Float::INFINITY) }
# 2. Values that are not durations or round counts.
attempt("session_idle_timeout: NaN") { Transport.new(server, session_idle_timeout: Float::NAN).close }
attempt("read_timeout: -1") { MCP::Client::Stdio.new(command: "ruby", read_timeout: -1) }
attempt("input_required_max_rounds: nil") { MCP::Client.new(transport: Object.new, input_required_max_rounds: nil) }
# 3. What an infinite wait does at run time.
attempt("ConditionVariable#wait(Infinity)") { Mutex.new.tap { |m| m.synchronize { ConditionVariable.new.wait(m, Float::INFINITY) } } }
attempt("IO#wait_readable(Infinity)") { IO.pipe.first.wait_readable(Float::INFINITY) }Expected behavior
The constructors raise ArgumentError. A duration is a positive finite number, or nil where the option documents it. input_required_max_rounds is a non-negative Integer: 0 works today and leaves every input_required result to the caller.
Logs
server_to_client_request_timeout: Infinity: accepted
listen_keepalive_interval: Infinity: accepted
stream_write_timeout: Infinity: accepted
max_reconnection_wait: Infinity: accepted
read_timeout: Infinity: accepted
session_idle_timeout: NaN: accepted
read_timeout: -1: accepted
input_required_max_rounds: nil: accepted
ConditionVariable#wait(Infinity): RangeError: Inf out of Time range
IO#wait_readable(Infinity): RangeError: Inf out of Time range
Ruby 4.0.9.
Additional context
Some of these values do have an effect today:
session_idle_timeout:Float::INFINITY,Float::NANandfalsedisable expiry.nilis the documented way to ask for that.max_stream_lifetimeandstream_write_timeout:Float::INFINITYmeans no limit, as the documentednildoes.read_timeout:falsemeans no timeout, asnildoes.listen_keepalive_interval:Float::INFINITYworks on streams with a deadline. A large finite number does the same;nildoes not, since a quiet stream then stays open past its deadline.input_required_max_rounds: a Float or Rational is rounded down,Float::INFINITYandFloat::NANmean no cap, and a negative number behaves like0. An Integer replaces each of them;nilis not accepted.
I suggest refusing them like the others.
The per-call timeout: of server-to-client requests is not checked either. It is left for a separate change.
I have a fix with tests and will open a PR for it.
Activity
fix: validate transport timeouts and input round limits