fetch_url
Retrieve web content from any URL and convert it into text format. Ideal for accessing HTML, plain text, and other text-based resources directly for processing or analysis.
Instructions
Fetch content from a URL and return it as text.
This tool allows Claude to retrieve content from any accessible web URL.
The content is returned as text, making it suitable for HTML, plain text,
and other text-based content types.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| headers | No | Additional headers to send with the request | |
| timeout | No | Request timeout in seconds | |
| url | Yes | The URL to fetch |
Implementation Reference
- src/url_fetch_mcp/main.py:20-62 (handler)The core handler function for the 'fetch_url' tool. It performs an HTTP GET request using httpx, handles errors, logs via context, and returns the response text. The @app.tool() decorator registers it with the FastMCP server. Input schema is defined inline via Annotated types and Field descriptions.@app.tool() async def fetch_url( url: Annotated[AnyUrl, Field(description="The URL to fetch")], headers: Annotated[ Optional[Dict[str, str]], Field(description="Additional headers to send with the request") ] = None, timeout: Annotated[int, Field(description="Request timeout in seconds")] = 10, ctx: Context = None, ) -> str: """Fetch content from a URL and return it as text. This tool allows Claude to retrieve content from any accessible web URL. The content is returned as text, making it suitable for HTML, plain text, and other text-based content types. """ if ctx: await ctx.info(f"Fetching content from URL: {url}") request_headers = { "User-Agent": "URL-Fetch-MCP/0.1.0", } if headers: request_headers.update(headers) async with httpx.AsyncClient(follow_redirects=True, timeout=timeout) as client: try: response = await client.get(str(url), headers=request_headers) response.raise_for_status() content_type = response.headers.get("content-type", "text/plain") if ctx: await ctx.info(f"Successfully fetched content ({len(response.text)} bytes, type: {content_type})") return response.text except Exception as e: error_message = f"Error fetching URL {url}: {str(e)}" if ctx: await ctx.error(error_message) return error_message