Repository navigation
fix: validate transport timeouts and input round limits #736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module MCP | ||
| # @api private | ||
| module Duration | ||
| class << self | ||
| def positive_finite?(value) | ||
| value.is_a?(Numeric) && value.real? && value.finite? && value.positive? | ||
| end | ||
| end | ||
| end | ||
| private_constant :Duration | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| require "json" | ||
| require "set" | ||
| require "timeout" | ||
| require_relative "../../duration" | ||
| require_relative "../../result_type" | ||
| require_relative "../../transport" | ||
|
|
||
|
|
@@ -135,6 +136,7 @@ class InvalidJsonError < StandardError; end | |
| 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. | ||
| # | ||
| # @param server [MCP::Server] the server whose requests this transport dispatches. | ||
| # @param stateless [Boolean] when `true`, no session is issued and each POST is self-contained. | ||
|
|
@@ -312,12 +314,13 @@ def initialize( | |
| session_idle_timeout | ||
| end | ||
|
|
||
| if @session_idle_timeout | ||
| if @stateless | ||
| raise ArgumentError, "session_idle_timeout is not supported in stateless mode." | ||
| elsif @session_idle_timeout <= 0 | ||
| raise ArgumentError, "session_idle_timeout must be a positive number." | ||
| # The value is checked ahead of the mode, so that an invalid one such as `false` is reported as what it is in stateless mode too. | ||
| unless @session_idle_timeout.nil? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, refusing |
||
| unless Duration.positive_finite?(@session_idle_timeout) | ||
| raise ArgumentError, "session_idle_timeout must be a positive finite number, or nil to disable the timeout." | ||
| end | ||
|
|
||
| raise ArgumentError, "session_idle_timeout is not supported in stateless mode." if @stateless | ||
| end | ||
|
|
||
| unless max_sessions.nil? || (max_sessions.is_a?(Integer) && max_sessions > 0) | ||
|
|
@@ -352,8 +355,8 @@ def initialize( | |
|
|
||
| @max_resource_subscription_bytes = max_resource_subscription_bytes | ||
|
|
||
| if !listen_keepalive_interval.nil? && !(listen_keepalive_interval.is_a?(Numeric) && listen_keepalive_interval > 0) | ||
| raise ArgumentError, "listen_keepalive_interval must be a positive number or nil" | ||
| unless listen_keepalive_interval.nil? || Duration.positive_finite?(listen_keepalive_interval) | ||
| raise ArgumentError, "listen_keepalive_interval must be a positive finite number or nil" | ||
| end | ||
|
|
||
| @listen_keepalive_interval = listen_keepalive_interval | ||
|
|
@@ -368,14 +371,14 @@ def initialize( | |
|
|
||
| @stream_request_notifications = stream_request_notifications | ||
|
|
||
| unless server_to_client_request_timeout.is_a?(Numeric) && server_to_client_request_timeout.positive? | ||
| raise ArgumentError, "server_to_client_request_timeout must be a positive number" | ||
| unless Duration.positive_finite?(server_to_client_request_timeout) | ||
| raise ArgumentError, "server_to_client_request_timeout must be a positive finite number" | ||
| end | ||
|
|
||
| @server_to_client_request_timeout = server_to_client_request_timeout | ||
|
|
||
| unless stream_write_timeout.nil? || (stream_write_timeout.is_a?(Numeric) && stream_write_timeout.positive?) | ||
| raise ArgumentError, "stream_write_timeout must be a positive number or nil" | ||
| unless stream_write_timeout.nil? || Duration.positive_finite?(stream_write_timeout) | ||
| raise ArgumentError, "stream_write_timeout must be a positive finite number or nil" | ||
| end | ||
|
|
||
| @stream_write_timeout = stream_write_timeout | ||
|
|
@@ -399,8 +402,8 @@ def initialize( | |
| @oauth_authenticator = nil | ||
| end | ||
|
|
||
| unless max_stream_lifetime.nil? || (max_stream_lifetime.is_a?(Numeric) && max_stream_lifetime.positive?) | ||
| raise ArgumentError, "max_stream_lifetime must be a positive number or nil" | ||
| unless max_stream_lifetime.nil? || Duration.positive_finite?(max_stream_lifetime) | ||
| raise ArgumentError, "max_stream_lifetime must be a positive finite number or nil" | ||
| end | ||
|
|
||
| @max_stream_lifetime = max_stream_lifetime | ||
|
|
||
There was a problem hiding this comment.
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::INFINITYforsession_idle_timeoutandmax_stream_lifetimemeant "never expire" (the issue notes this),falseforsession_idle_timeoutdisabled it, andinput_required_max_roundsaccepted a Float such as10.0. Please state these in the commit message, withnilas the way to ask for no limit, so that the release notes can carry it.There was a problem hiding this comment.
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
mainshowed that my list, and the "two infinite values" in #735, were incomplete.stream_write_timeout: Float::INFINITYandsession_idle_timeout: Float::NANalso meant no limit, andnilreplaces both.nilis not the replacement in two cases.input_required_max_roundsdoes not accept it, soFloat::INFINITYthere becomes a large Integer.listen_keepalive_interval: Float::INFINITYclosed an authenticated stream at its deadline, whichnildoes not do while the stream is quiet, so the message suggests a large finite number. I corrected #735 and the PR description too.