fetch_markdown
Convert website content to Markdown format by fetching URLs, enabling structured text extraction for documentation or analysis.
Instructions
Fetch a website and return the content as Markdown
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | URL of the website to fetch | |
| headers | No | Optional headers to include in the request |
Implementation Reference
- src/Fetcher.ts:90-103 (handler)The core handler function for the 'fetch_markdown' tool. Fetches the HTML content from the provided URL and converts it to Markdown using TurndownService.static async markdown(requestPayload: RequestPayload) { try { const response = await this._fetch(requestPayload); const html = await response.text(); const turndownService = new TurndownService(); const markdown = turndownService.turndown(html); return { content: [{ type: "text", text: markdown }], isError: false }; } catch (error) { return { content: [{ type: "text", text: (error as Error).message }], isError: true, }; } }
- src/types.ts:3-8 (schema)Zod schema used to validate input parameters (url and optional headers) for the fetch_markdown tool.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:46-63 (registration)Tool registration in ListToolsRequestHandler, defining name, description, and input schema.{ name: "fetch_markdown", description: "Fetch a website and return the content as 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", }, }, required: ["url"], }, },
- src/index.ts:122-125 (registration)Dispatch logic in CallToolRequestHandler that invokes the fetch_markdown handler.if (request.params.name === "fetch_markdown") { const fetchResult = await Fetcher.markdown(validatedArgs); return fetchResult; }
- src/Fetcher.ts:6-30 (helper)Shared helper method for performing the HTTP fetch with custom headers and error handling, used by the markdown handler.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`); } } }