feat(fetch): make the request timeout configurable#4496
Open
ameyypawar wants to merge 2 commits into
Open
Conversation
The fetch server hardcoded a 30s httpx timeout, which is too short for large downloads and slow endpoints and too long for quick health checks, with no way to override it (modelcontextprotocol#4448). Make the timeout configurable, most-specific wins: - per-request: an optional `timeout` argument on the fetch tool; - server default: `--timeout` CLI flag or the `FETCH_TIMEOUT` env var; - falls back to the existing 30s default. The timeout is in seconds (float), matching httpx and the previous hardcoded value rather than the milliseconds suggested in the issue. The robots.txt preflight honors the same timeout so it does not become the bottleneck on slow hosts. Addresses modelcontextprotocol#4448.
Address review feedback on the configurable-timeout change: - Revert the robots.txt preflight to its previous (httpx-default) timeout; only the content fetch honors the configurable timeout, keeping the change scoped to what modelcontextprotocol#4448 describes and avoiding a behavior change to the preflight. - Reject non-positive --timeout / FETCH_TIMEOUT values (matching the per-request field's gt=0), with a clean error instead of a traceback. - Add tests for the --timeout flag, the FETCH_TIMEOUT env var, and their precedence. - Document --timeout / FETCH_TIMEOUT and the per-request timeout argument in the README.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
fetchserver hardcoded a 30s httpx timeout with no way to override it — too short for large downloads and slow endpoints, too long for quick health checks (#4448).The content-fetch timeout is now configurable, most-specific wins:
timeoutargument on thefetchtool--timeoutCLI flag or theFETCH_TIMEOUTenvironment variableChange
server.py: thread atimeoutparameter throughfetch_urlandserve, and add an optionaltimeoutfield (gt=0) to theFetchtool-argument model.call_toolresolves the effective timeout (per-request overrides the server default).__init__.py: add the--timeoutflag andFETCH_TIMEOUTenv var; both reject non-positive values (matching the tool field'sgt=0) with a clean error.fetch_urlapplies the default/explicit timeout;--timeout,FETCH_TIMEOUT, and their precedence.Notes
timeout=30, and is idiomatic for a Python/httpx server. Happy to switch to milliseconds if maintainers prefer the issue's original convention.robots.txtpreflight keeps its existing default timeout, so its behavior is unchanged.Verified locally:
uv run pytest→ 26 passed,uv run pyright→ 0 errors,uv run ruff check→ clean,uv build→ ok.Addresses #4448.