fetch_html
Retrieve website content in HTML format by specifying the URL and optional headers using this MCP server tool. Ideal for extracting web data for analysis or integration.
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 core handler function for the 'fetch_html' tool. It fetches the HTML content from the provided URL using the shared _fetch method and returns it wrapped in the expected MCP response format.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:110-112 (registration)The 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/index.ts:31-44 (schema)The input schema definition for the 'fetch_html' tool, registered in the ListTools response.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:3-6 (schema)Zod schema used for validating the input arguments to all fetch tools, including 'fetch_html'.export const RequestPayloadSchema = z.object({ url: z.string().url(), headers: z.record(z.string()).optional(), });
- src/Fetcher.ts:5-30 (helper)Shared helper method for performing the HTTP fetch with custom User-Agent and error handling, used by the 'fetch_html' handler.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`); } } }