Skip to content

Commit 04ee6bb

Browse files
committed
refactor(fetch): scope timeout to the content fetch and validate it
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.
1 parent abb03fa commit 04ee6bb

4 files changed

Lines changed: 74 additions & 18 deletions

File tree

‎src/fetch/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The fetch tool will truncate the response, but by using the `start_index` argume
1616
- `max_length` (integer, optional): Maximum number of characters to return (default: 5000)
1717
- `start_index` (integer, optional): Start content from this character index (default: 0)
1818
- `raw` (boolean, optional): Get raw content without markdown conversion (default: false)
19+
- `timeout` (number, optional): Request timeout in seconds for this fetch; overrides the server default (default: 30)
1920

2021
### Prompts
2122

@@ -170,6 +171,12 @@ This can be customized by adding the argument `--user-agent=YourUserAgent` to th
170171

171172
The server can be configured to use a proxy by using the `--proxy-url` argument.
172173

174+
### Customization - Timeout
175+
176+
By default, requests time out after 30 seconds. The default can be changed with the `--timeout` argument (in seconds) or
177+
the `FETCH_TIMEOUT` environment variable, and a single request can override it with the `timeout` tool argument
178+
(most specific wins).
179+
173180
## Windows Configuration
174181

175182
If you're experiencing timeout issues on Windows, you may need to set the `PYTHONIOENCODING` environment variable to ensure proper character encoding:

‎src/fetch/src/mcp_server_fetch/__init__.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,26 @@ def main():
1818
)
1919
parser.add_argument("--proxy-url", type=str, help="Proxy URL to use for requests")
2020

21+
def positive_timeout(value: str) -> float:
22+
seconds = float(value)
23+
if seconds <= 0:
24+
raise argparse.ArgumentTypeError(
25+
"timeout must be a positive number of seconds"
26+
)
27+
return seconds
28+
2129
env_timeout = os.environ.get("FETCH_TIMEOUT")
30+
try:
31+
default_timeout = (
32+
positive_timeout(env_timeout) if env_timeout else DEFAULT_REQUEST_TIMEOUT
33+
)
34+
except (ValueError, argparse.ArgumentTypeError) as error:
35+
parser.error(f"invalid FETCH_TIMEOUT environment variable: {error}")
36+
2237
parser.add_argument(
2338
"--timeout",
24-
type=float,
25-
default=float(env_timeout) if env_timeout else DEFAULT_REQUEST_TIMEOUT,
39+
type=positive_timeout,
40+
default=default_timeout,
2641
help="Request timeout in seconds (default: 30, or the FETCH_TIMEOUT env var)",
2742
)
2843

‎src/fetch/src/mcp_server_fetch/server.py‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
DEFAULT_USER_AGENT_AUTONOMOUS = "ModelContextProtocol/1.0 (Autonomous; +https://github.com/modelcontextprotocol/servers)"
2424
DEFAULT_USER_AGENT_MANUAL = "ModelContextProtocol/1.0 (User-Specified; +https://github.com/modelcontextprotocol/servers)"
2525

26-
# Default per-request timeout, in seconds, for outbound HTTP requests. Matches
27-
# httpx's timeout unit and the value previously hardcoded here. Overridable via
28-
# the serve() timeout argument, the --timeout CLI flag, the FETCH_TIMEOUT
29-
# environment variable, or the per-request "timeout" tool argument.
26+
# Default per-request timeout, in seconds, for the outbound content fetch.
27+
# Matches httpx's timeout unit and the value previously hardcoded here.
28+
# Overridable via the serve() timeout argument, the --timeout CLI flag, the
29+
# FETCH_TIMEOUT environment variable, or the per-request "timeout" tool argument.
3030
DEFAULT_REQUEST_TIMEOUT = 30.0
3131

3232

@@ -69,12 +69,7 @@ def get_robots_txt_url(url: str) -> str:
6969
return robots_url
7070

7171

72-
async def check_may_autonomously_fetch_url(
73-
url: str,
74-
user_agent: str,
75-
proxy_url: str | None = None,
76-
timeout: float = DEFAULT_REQUEST_TIMEOUT,
77-
) -> None:
72+
async def check_may_autonomously_fetch_url(url: str, user_agent: str, proxy_url: str | None = None) -> None:
7873
"""
7974
Check if the URL can be fetched by the user agent according to the robots.txt file.
8075
Raises a McpError if not.
@@ -89,7 +84,6 @@ async def check_may_autonomously_fetch_url(
8984
robot_txt_url,
9085
follow_redirects=True,
9186
headers={"User-Agent": user_agent},
92-
timeout=timeout,
9387
)
9488
except HTTPError:
9589
raise McpError(ErrorData(
@@ -214,8 +208,8 @@ async def serve(
214208
custom_user_agent: Optional custom User-Agent string to use for requests
215209
ignore_robots_txt: Whether to ignore robots.txt restrictions
216210
proxy_url: Optional proxy URL to use for requests
217-
timeout: Default request timeout in seconds, used when a fetch call does
218-
not specify its own timeout
211+
timeout: Default request timeout in seconds for the content fetch, used
212+
when a fetch call does not specify its own timeout
219213
"""
220214
server = Server("mcp-fetch")
221215
user_agent_autonomous = custom_user_agent or DEFAULT_USER_AGENT_AUTONOMOUS
@@ -261,9 +255,7 @@ async def call_tool(name, arguments: dict) -> list[TextContent]:
261255
request_timeout = args.timeout if args.timeout is not None else timeout
262256

263257
if not ignore_robots_txt:
264-
await check_may_autonomously_fetch_url(
265-
url, user_agent_autonomous, proxy_url, timeout=request_timeout
266-
)
258+
await check_may_autonomously_fetch_url(url, user_agent_autonomous, proxy_url)
267259

268260
content, prefix = await fetch_url(
269261
url,

‎src/fetch/tests/test_server.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"""Tests for the fetch MCP server."""
22

3+
import os
4+
import sys
5+
36
import pytest
47
from unittest.mock import AsyncMock, patch, MagicMock
58
from mcp.shared.exceptions import McpError
69

10+
from mcp_server_fetch import main
711
from mcp_server_fetch.server import (
812
extract_content_from_html,
913
get_robots_txt_url,
@@ -368,3 +372,41 @@ async def test_fetch_uses_custom_timeout(self):
368372
)
369373

370374
assert mock_client.get.call_args.kwargs["timeout"] == 60.0
375+
376+
377+
class TestServerTimeoutConfiguration:
378+
"""Tests for timeout configuration via the CLI flag and environment variable."""
379+
380+
def _serve_timeout_for(self, argv, env=None):
381+
"""Run main() with the given argv/env and return the timeout passed to serve()."""
382+
import mcp_server_fetch
383+
384+
with patch.object(mcp_server_fetch, "serve", new=AsyncMock()) as mock_serve, \
385+
patch.dict(os.environ, env or {}, clear=False), \
386+
patch.object(sys, "argv", argv):
387+
if not (env and "FETCH_TIMEOUT" in env):
388+
os.environ.pop("FETCH_TIMEOUT", None)
389+
main()
390+
391+
# serve(custom_user_agent, ignore_robots_txt, proxy_url, timeout)
392+
return mock_serve.call_args.args[3]
393+
394+
def test_default_timeout_when_unset(self):
395+
"""The default timeout is used when neither flag nor env var is set."""
396+
assert self._serve_timeout_for(["mcp-server-fetch"]) == DEFAULT_REQUEST_TIMEOUT
397+
398+
def test_timeout_from_cli_flag(self):
399+
"""The --timeout flag sets the server default."""
400+
assert self._serve_timeout_for(["mcp-server-fetch", "--timeout", "45"]) == 45.0
401+
402+
def test_timeout_from_env_var(self):
403+
"""FETCH_TIMEOUT sets the server default when no flag is given."""
404+
assert self._serve_timeout_for(
405+
["mcp-server-fetch"], {"FETCH_TIMEOUT": "50"}
406+
) == 50.0
407+
408+
def test_cli_flag_overrides_env_var(self):
409+
"""The --timeout flag takes precedence over FETCH_TIMEOUT."""
410+
assert self._serve_timeout_for(
411+
["mcp-server-fetch", "--timeout", "45"], {"FETCH_TIMEOUT": "50"}
412+
) == 45.0

0 commit comments

Comments
 (0)