Skip to main content
Glama
nathanonn
by nathanonn

fetch

Fetch and process web content from any URL with automatic format detection, supporting HTML, JSON, Markdown, and text for efficient data extraction and integration.

Instructions

Fetch content from a URL with automatic content type detection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
formatNoFormat to convert to (default: auto)
urlYesURL to fetch content from

Implementation Reference

  • Core handler logic for the 'fetch' tool: fetches the URL, buffers content, detects type, converts to specified format using helper converters, records the fetch, and returns formatted content or error response.
    async ({ url, format = "auto" }) => {
      try {
        const response = await fetchUrl(url);
        const contentBuffer = await response.buffer();
        const contentText = contentBuffer.toString();
        const detectedType = detectContentType(response, url);
    
        // If format is auto, use the detected type or default to text
        let outputFormat = format;
        if (format === "auto") {
          outputFormat = detectedType;
        }
    
        let processedContent;
    
        // Convert to the desired output format
        switch (outputFormat) {
          case "json":
            processedContent = await convertToJson(contentText, detectedType, url);
            break;
          case "markdown":
            processedContent = await convertToMarkdown(contentText, detectedType, url);
            break;
          case "html":
            processedContent = await convertToHtml(contentText, detectedType, url);
            break;
          case "text":
          default:
            processedContent = await convertToText(contentText, detectedType, url);
            outputFormat = "text";
            break;
        }
    
        // Record this fetch
        recordUrlFetch(url, outputFormat);
    
        return {
          content: [
            {
              type: "text",
              text: `# Content from ${url} converted to ${outputFormat}:\n\n${processedContent}`,
            },
          ],
        };
      } catch (error) {
        return {
          isError: true,
          content: [
            {
              type: "text",
              text: `Error fetching content from URL: ${error instanceof Error ? error.message : String(error)}`,
            },
          ],
        };
      }
    }
  • Zod input schema for the 'fetch' tool, validating 'url' as a string URL and optional 'format' enum.
    {
      url: z.string().url().describe("URL to fetch content from"),
      format: z.enum(["auto", "html", "json", "markdown", "text"]).optional().describe("Format to convert to (default: auto)"),
    },
  • src/index.ts:92-94 (registration)
    Registration of the 'fetch' tool on the MCP server with name and description.
    server.tool(
      "fetch",
      "Fetch content from a URL with automatic content type detection",
  • Helper function that performs the actual HTTP fetch using node-fetch, handles errors, and returns the response.
    async function fetchUrl(url: string) {
      try {
        const response = await fetch(url);
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        return response;
      } catch (error) {
        console.error(`Error fetching URL: ${url}`, error);
        throw error;
      }
    }
  • Helper function to detect the content type based on response headers or URL extension for automatic format selection.
    function detectContentType(response, url: string): string {
      const contentType = response.headers.get("content-type") || "";
    
      // Check based on content-type header
      if (contentType.includes("json")) return "json";
      if (contentType.includes("html")) return "html";
      if (contentType.includes("markdown") || contentType.includes("md")) return "markdown";
      if (contentType.includes("xml")) return "xml";
      if (contentType.includes("csv")) return "csv";
    
      // If no clear content-type, check the URL extension
      if (url.endsWith(".json")) return "json";
      if (url.endsWith(".html") || url.endsWith(".htm")) return "html";
      if (url.endsWith(".md") || url.endsWith(".markdown")) return "markdown";
      if (url.endsWith(".xml")) return "xml";
      if (url.endsWith(".csv")) return "csv";
    
      // Default to text
      return "text";
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden. It mentions automatic content type detection, which hints at behavior, but lacks critical details like error handling, timeouts, authentication needs, rate limits, or response structure. For a tool that interacts with external URLs, this is a significant gap in behavioral disclosure.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that states the core functionality without waste. It is front-loaded and appropriately sized for the tool's purpose, making it easy to parse and understand quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of fetching from URLs, lack of annotations, and no output schema, the description is incomplete. It does not cover potential issues like network errors, content parsing, or return values, which are crucial for an agent to use the tool effectively in varied contexts.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with clear descriptions for both parameters (url and format). The description adds minimal value beyond the schema, as it only implies content type detection relates to the format parameter. No additional semantics or usage examples are provided, so it meets the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose3/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description states the tool's purpose as fetching content from a URL with automatic content type detection, which is clear but vague about what 'content' entails. It does not distinguish from sibling tools like fetch-html, fetch-json, etc., which likely fetch specific formats, making it less specific. The verb 'fetch' is generic, and the description lacks detail on the resource or output type beyond detection.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus its siblings (fetch-html, fetch-json, etc.), which are explicitly named alternatives. It mentions automatic content type detection but does not specify scenarios where this is preferred over format-specific tools, leaving the agent without clear usage context or exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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/nathanonn/mcp-url-fetcher'

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