Skip to main content
Glama

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
NameRequiredDescriptionDefault
headersNoOptional headers to include in the request
urlYesURL of the website to fetch

Implementation Reference

  • 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"],
      },
    },
    {
  • 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>;
  • 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;
    }
  • 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`);
          }
        }
      }

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/tokenizin-agency/mcp-npx-fetch'

If you have feedback or need assistance with the MCP directory API, please join our Discord server