Skip to content

feat(fetch): make the request timeout configurable#4496

Open
ameyypawar wants to merge 2 commits into
modelcontextprotocol:mainfrom
ameyypawar:fix/fetch-configurable-timeout
Open

feat(fetch): make the request timeout configurable#4496
ameyypawar wants to merge 2 commits into
modelcontextprotocol:mainfrom
ameyypawar:fix/fetch-configurable-timeout

Conversation

@ameyypawar

Copy link
Copy Markdown

Summary

The fetch server 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:

  • per-request: an optional timeout argument on the fetch tool
  • server default: the --timeout CLI flag or the FETCH_TIMEOUT environment variable
  • fallback: the existing 30s default

Change

  • server.py: thread a timeout parameter through fetch_url and serve, and add an optional timeout field (gt=0) to the Fetch tool-argument model. call_tool resolves the effective timeout (per-request overrides the server default).
  • __init__.py: add the --timeout flag and FETCH_TIMEOUT env var; both reject non-positive values (matching the tool field's gt=0) with a clean error.
  • Tests: fetch_url applies the default/explicit timeout; --timeout, FETCH_TIMEOUT, and their precedence.
  • README: document the flag, the env var, and the per-request argument.

Notes

  • Unit is seconds (float), not the milliseconds suggested in the issue — this matches httpx's timeout unit and the previous hardcoded timeout=30, and is idiomatic for a Python/httpx server. Happy to switch to milliseconds if maintainers prefer the issue's original convention.
  • Scope: only the content fetch is affected. The robots.txt preflight 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.

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

1 participant