fetch_html
Retrieve HTML content from a specified URL using optional headers for web scraping or content extraction.
Instructions
Fetch a website and return the content as HTML
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| headers | No | Optional headers to include in the request | |
| url | Yes | URL of the website to fetch |
Implementation Reference
- src/Fetcher.ts:32-43 (handler)The primary handler function for the 'fetch_html' tool. It fetches the URL, retrieves the HTML content, and returns it in the expected MCP format. Handles errors gracefully.static async html(requestPayload: RequestPayload) { try { const response = await this._fetch(requestPayload); const html = await response.text(); return { content: [{ type: "text", text: html }], isError: false }; } catch (error) { return { content: [{ type: "text", text: (error as Error).message }], isError: true, }; } }
- src/index.ts:50-68 (registration)Registration of the 'fetch_html' tool in the ListTools response, including name, description, and input schema.{ name: "fetch_html", description: "Fetch a website and return the content as HTML", inputSchema: { type: "object", properties: { url: { type: "string", description: "URL of the website to fetch", }, headers: { type: "object", description: "Optional headers to include in the request", }, }, required: ["url"], }, }, {
- src/types.ts:1-8 (schema)Zod schema for validating the tool input parameters (url and optional headers), used before calling the handler.import { z } from "zod"; export const RequestPayloadSchema = z.object({ url: z.string().url(), headers: z.record(z.string()).optional(), }); export type RequestPayload = z.infer<typeof RequestPayloadSchema>;
- src/index.ts:132-135 (handler)Dispatch logic in the CallToolRequest handler that routes 'fetch_html' calls to Fetcher.html.if (request.params.name === "fetch_html") { const fetchResult = await Fetcher.html(validatedArgs); return fetchResult; }
- src/Fetcher.ts:5-30 (helper)Shared helper method for performing the HTTP fetch with custom headers and User-Agent, used by all fetch tools including fetch_html.export class Fetcher { private static async _fetch({ url, headers, }: RequestPayload): Promise<Response> { try { const response = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", ...headers, }, }); if (!response.ok) { throw new Error(`HTTP error: ${response.status}`); } return response; } catch (e: unknown) { if (e instanceof Error) { throw new Error(`Failed to fetch ${url}: ${e.message}`); } else { throw new Error(`Failed to fetch ${url}: Unknown error`); } } }