fetch_html
Retrieve website content in HTML format directly by specifying a URL, optional headers, and character limits.
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 | |
| max_length | No | Maximum number of characters to return (default: 5000) | |
| start_index | No | Start content from this character index (default: 0) | |
| url | Yes | URL of the website to fetch |
Implementation Reference
- src/Fetcher.ts:46-65 (handler)The static `html()` method in the `Fetcher` class executes the core logic for the `fetch_html` tool. It fetches the HTML content from the provided URL, applies length limits based on parameters, and returns the content in the expected MCP format or an error.static async html(requestPayload: RequestPayload) { try { const response = await this._fetch(requestPayload); let html = await response.text(); // Apply length limits html = this.applyLengthLimits( html, requestPayload.max_length ?? 5000, requestPayload.start_index ?? 0 ); return { content: [{ type: "text", text: html }], isError: false }; } catch (error) { return { content: [{ type: "text", text: (error as Error).message }], isError: true, }; } }
- src/types.ts:5-10 (schema)Zod schema `RequestPayloadSchema` used to validate the input arguments for the `fetch_html` tool (and others). Defines the expected structure: url (required), optional headers, max_length, start_index.export const RequestPayloadSchema = z.object({ url: z.string().url(), headers: z.record(z.string()).optional(), max_length: z.number().int().min(0).optional().default(downloadLimit), start_index: z.number().int().min(0).optional().default(0), });
- src/index.ts:30-54 (schema)The inputSchema definition for the `fetch_html` tool, declared in the ListTools response. Matches the Zod schema used for validation.{ name: "fetch_html", description: "Fetch a website and return its unmodified contents 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", }, max_length: { type: "number", description: `Maximum number of characters to return (default: ${downloadLimit})`, }, start_index: { type: "number", description: "Start content from this character index (default: 0)", }, }, required: ["url"], },
- src/index.ts:27-137 (registration)Registration of the `fetch_html` tool via the ListToolsRequestSchema handler, which returns the tool list including name, description, and inputSchema.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: "fetch_html", description: "Fetch a website and return its unmodified contents 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", }, max_length: { type: "number", description: `Maximum number of characters to return (default: ${downloadLimit})`, }, start_index: { type: "number", description: "Start content from this character index (default: 0)", }, }, required: ["url"], }, }, { name: "fetch_markdown", description: "Fetch a website and return its contents converted content to Markdown", 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", }, max_length: { type: "number", description: `Maximum number of characters to return (default: ${downloadLimit}})`, }, start_index: { type: "number", description: "Start content from this character index (default: 0)", }, }, required: ["url"], }, }, { name: "fetch_txt", description: "Fetch a website, convert the content to plain text (no 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", }, max_length: { type: "number", description: `Maximum number of characters to return (default: ${downloadLimit})`, }, start_index: { type: "number", description: "Start content from this character index (default: 0)", }, }, required: ["url"], }, }, { name: "fetch_json", description: "Fetch a JSON file from a URL", inputSchema: { type: "object", properties: { url: { type: "string", description: "URL of the JSON to fetch", }, headers: { type: "object", description: "Optional headers to include in the request", }, max_length: { type: "number", description: `Maximum number of characters to return (default: ${downloadLimit})`, }, start_index: { type: "number", description: "Start content from this character index (default: 0)", }, }, required: ["url"], }, }, ], }; });
- src/index.ts:139-161 (registration)Tool dispatch/registration in the CallToolRequestSchema handler: validates args and calls Fetcher.html if name is 'fetch_html'.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; const validatedArgs = RequestPayloadSchema.parse(args); if (request.params.name === "fetch_html") { const fetchResult = await Fetcher.html(validatedArgs); return fetchResult; } if (request.params.name === "fetch_json") { const fetchResult = await Fetcher.json(validatedArgs); return fetchResult; } if (request.params.name === "fetch_txt") { const fetchResult = await Fetcher.txt(validatedArgs); return fetchResult; } if (request.params.name === "fetch_markdown") { const fetchResult = await Fetcher.markdown(validatedArgs); return fetchResult; } throw new Error("Tool not found"); });