From 49bf7171b934fb5e5414894663a99059dd3c1a8d Mon Sep 17 00:00:00 2001 From: John Ritsema Date: Tue, 13 May 2025 23:19:31 -0400 Subject: [PATCH] fetch - max_length customization - Updated server implementation to utilize `MAX_LENGTH` environment variable - Improved README.md with detailed customization instructions for `max_length` --- src/fetch/README.md | 35 +++++++++++++++++++++++- src/fetch/src/mcp_server_fetch/server.py | 4 ++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/fetch/README.md b/src/fetch/README.md index 0b02fcf9b6..740deb8493 100644 --- a/src/fetch/README.md +++ b/src/fetch/README.md @@ -8,7 +8,7 @@ The fetch tool will truncate the response, but by using the `start_index` argume - `fetch` - Fetches a URL from the internet and extracts its contents as markdown. - `url` (string, required): URL to fetch - - `max_length` (integer, optional): Maximum number of characters to return (default: 5000) + - `max_length` (integer, optional): Maximum number of characters to return (default: 5000). This can be overridden (see customization below) - `start_index` (integer, optional): Start content from this character index (default: 0) - `raw` (boolean, optional): Get raw content without markdown conversion (default: false) @@ -159,6 +159,39 @@ This can be customized by adding the argument `--user-agent=YourUserAgent` to th The server can be configured to use a proxy by using the `--proxy-url` argument. +### Customization - Max Length + +By default, the maximum length of content returned is 5000 characters. This can be customized by setting the `MAX_LENGTH` environment variable. + +
+Using uvx + +```json +"mcpServers": { + "fetch": { + "command": "uvx", + "args": ["mcp-server-fetch"], + "env": { + "MAX_LENGTH": "10000" + } + } +} +``` +
+ +
+Using Docker + +```json +"mcpServers": { + "fetch": { + "command": "docker", + "args": ["run", "-i", "--rm", "-e", "MAX_LENGTH=10000", "mcp/fetch"] + } +} +``` +
+ ## Debugging You can use the MCP inspector to debug the server. For uvx installations: diff --git a/src/fetch/src/mcp_server_fetch/server.py b/src/fetch/src/mcp_server_fetch/server.py index 2df9d3b604..627e5e1d33 100644 --- a/src/fetch/src/mcp_server_fetch/server.py +++ b/src/fetch/src/mcp_server_fetch/server.py @@ -1,5 +1,6 @@ from typing import Annotated, Tuple from urllib.parse import urlparse, urlunparse +import os import markdownify import readabilipy.simple_json @@ -22,6 +23,7 @@ DEFAULT_USER_AGENT_AUTONOMOUS = "ModelContextProtocol/1.0 (Autonomous; +https://github.com/modelcontextprotocol/servers)" DEFAULT_USER_AGENT_MANUAL = "ModelContextProtocol/1.0 (User-Specified; +https://github.com/modelcontextprotocol/servers)" +MAX_LENGTH = os.environ.get("MAX_LENGTH", 5000) def extract_content_from_html(html: str) -> str: @@ -155,7 +157,7 @@ class Fetch(BaseModel): max_length: Annotated[ int, Field( - default=5000, + default=MAX_LENGTH, description="Maximum number of characters to return.", gt=0, lt=1000000,