Skip to main content
Glama

fetch_url

Fetch web content from URLs with configurable response formats including text, JSON, HTML, and Markdown, while handling errors and timeouts.

Instructions

Fetch content from a URL with proper error handling and response processing

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlYesThe URL to fetch
responseTypeNoExpected response typetext
timeoutNoRequest timeout in milliseconds

Implementation Reference

  • The core handler logic for the 'fetch_url' tool. It implements URL fetching with AbortController for timeout, browser-like headers, retry logic for failures and rate limits, response processing via processResponse helper, and returns MCP-compatible content structure with metadata.
    async (params) => {
      for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
        try {
          const controller = new AbortController();
          const timeout = setTimeout(() => controller.abort(), params.timeout);
    
          const response = await fetch(params.url.toString(), {
            signal: controller.signal,
            headers: BROWSER_HEADERS,
            redirect: 'follow'
          });
    
          clearTimeout(timeout);
    
          // Handle different status codes
          if (!response.ok) {
            if (response.status === 429) {
              if (attempt === MAX_RETRIES - 1) {
                return {
                  content: [{
                    type: "text",
                    text: "Rate limit exceeded. Please try again later."
                  }],
                  isError: true
                };
              }
              await new Promise(resolve => setTimeout(resolve, getRetryDelay(attempt)));
              continue;
            }
    
            return {
              content: [{
                type: "text",
                text: `HTTP ${response.status}: ${response.statusText}`
              }],
              isError: true
            };
          }
    
          // Process the response
          const processedContent = await processResponse(response, params.responseType, params.url);
    
          // Always return as text type with appropriate metadata
          return {
            content: [{
              type: "text",
              text: processedContent,
              mimeType: params.responseType === 'json' ? 'application/json' :
                params.responseType === 'markdown' ? 'text/markdown' :
                  params.responseType === 'html' ? 'text/html' : 'text/plain'
            }],
            metadata: {
              url: params.url.toString(),
              contentType: response.headers.get('content-type'),
              contentLength: response.headers.get('content-length'),
              isGoogleSearch: params.url.origin + params.url.pathname === GOOGLE_SEARCH_URL,
              responseType: params.responseType
            }
          };
    
        } catch (error) {
          if (error instanceof Error && error.name === 'AbortError') {
            return {
              content: [{
                type: "text",
                text: `Request timed out after ${params.timeout}ms`
              }],
              isError: true
            };
          }
    
          if (attempt === MAX_RETRIES - 1) {
            return {
              content: [{
                type: "text",
                text: `Failed to fetch URL: ${error instanceof Error ? error.message : 'Unknown error'}`
              }],
              isError: true
            };
          }
    
          await new Promise(resolve => setTimeout(resolve, getRetryDelay(attempt)));
        }
      }
    
      return {
        content: [{
          type: "text",
          text: "Failed to fetch URL after all retry attempts"
        }],
        isError: true
      };
    }
  • Zod validation schema for 'fetch_url' tool parameters, used in server.tool registration.
    const UrlFetcherSchema = z.object({
      url: z.string()
        .url()
        .transform(url => new URL(url))
        .describe("The URL to fetch"),
      responseType: z.enum(['text', 'json', 'html', 'markdown'])
        .default('text')
        .describe("Expected response type"),
      timeout: z.number()
        .min(1000)
        .max(60000)
        .default(DEFAULT_TIMEOUT)
        .describe("Request timeout in milliseconds")
    });
  • Function that registers the 'fetch_url' tool on the MCP server instance using server.tool(), specifying name, description, input schema, and handler.
    export function registerUrlFetcherTool(server: McpServer) {
      server.tool(
        "fetch_url",
        "Fetch content from a URL with proper error handling and response processing",
        UrlFetcherSchema.shape,
        async (params) => {
          for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
            try {
              const controller = new AbortController();
              const timeout = setTimeout(() => controller.abort(), params.timeout);
    
              const response = await fetch(params.url.toString(), {
                signal: controller.signal,
                headers: BROWSER_HEADERS,
                redirect: 'follow'
              });
    
              clearTimeout(timeout);
    
              // Handle different status codes
              if (!response.ok) {
                if (response.status === 429) {
                  if (attempt === MAX_RETRIES - 1) {
                    return {
                      content: [{
                        type: "text",
                        text: "Rate limit exceeded. Please try again later."
                      }],
                      isError: true
                    };
                  }
                  await new Promise(resolve => setTimeout(resolve, getRetryDelay(attempt)));
                  continue;
                }
    
                return {
                  content: [{
                    type: "text",
                    text: `HTTP ${response.status}: ${response.statusText}`
                  }],
                  isError: true
                };
              }
    
              // Process the response
              const processedContent = await processResponse(response, params.responseType, params.url);
    
              // Always return as text type with appropriate metadata
              return {
                content: [{
                  type: "text",
                  text: processedContent,
                  mimeType: params.responseType === 'json' ? 'application/json' :
                    params.responseType === 'markdown' ? 'text/markdown' :
                      params.responseType === 'html' ? 'text/html' : 'text/plain'
                }],
                metadata: {
                  url: params.url.toString(),
                  contentType: response.headers.get('content-type'),
                  contentLength: response.headers.get('content-length'),
                  isGoogleSearch: params.url.origin + params.url.pathname === GOOGLE_SEARCH_URL,
                  responseType: params.responseType
                }
              };
    
            } catch (error) {
              if (error instanceof Error && error.name === 'AbortError') {
                return {
                  content: [{
                    type: "text",
                    text: `Request timed out after ${params.timeout}ms`
                  }],
                  isError: true
                };
              }
    
              if (attempt === MAX_RETRIES - 1) {
                return {
                  content: [{
                    type: "text",
                    text: `Failed to fetch URL: ${error instanceof Error ? error.message : 'Unknown error'}`
                  }],
                  isError: true
                };
              }
    
              await new Promise(resolve => setTimeout(resolve, getRetryDelay(attempt)));
            }
          }
    
          return {
            content: [{
              type: "text",
              text: "Failed to fetch URL after all retry attempts"
            }],
            isError: true
          };
        }
      );
    }
  • Declarative tool schema in MCP server capabilities for client discovery.
    fetch_url: {
      description: "Fetch content from a URL with proper error handling and response processing",
      parameters: {
        url: "The URL to fetch",
        responseType: "Expected response type (text, json, html, markdown)",
        timeout: "Request timeout in milliseconds (optional)"
      }
  • Helper function called by handler to process fetched response: size check, Google special case, format conversion based on responseType.
    async function processResponse(response: Response, responseType: 'text' | 'json' | 'html' | 'markdown', url: URL): Promise<string> {
      const contentType = response.headers.get('content-type') || '';
    
      // Check response size
      const contentLength = parseInt(response.headers.get('content-length') || '0');
      if (contentLength > MAX_RESPONSE_SIZE) {
        throw new Error('Response too large');
      }
    
      let text = await response.text();
    
      // Special handling for Google search results
      if (url.origin + url.pathname === GOOGLE_SEARCH_URL) {
        const mappedType = responseType === 'json' || responseType === 'markdown' ? responseType : 'json';
        const results = await extractGoogleResults(text, mappedType);
        return typeof results === 'string' ? results : JSON.stringify(results, null, 2);
      }
    
      switch (responseType) {
        case 'json':
          if (!contentType.includes('application/json')) {
            throw new Error('Response is not JSON');
          }
          // Pretty print JSON
          return JSON.stringify(JSON.parse(text), null, 2);
    
        case 'html':
          if (!contentType.includes('text/html')) {
            throw new Error('Response is not HTML');
          }
          return text;
    
        case 'markdown':
          if (contentType.includes('text/html')) {
            return htmlToMarkdown(text);
          } else if (contentType.includes('text/markdown')) {
            return text;
          }
          // If not HTML or Markdown, convert plain text to markdown
          return `\`\`\`\n${text}\n\`\`\``;
    
        case 'text':
        default:
          return text;
      }
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions 'proper error handling and response processing', which hints at robustness, but lacks specifics on authentication needs, rate limits, retry behavior, or what constitutes 'proper' processing. This is insufficient for a tool that interacts with external URLs.

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

Conciseness4/5

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

The description is a single, efficient sentence that front-loads the core purpose. However, it could be more structured by separating purpose from behavioral claims, and the phrase 'proper error handling and response processing' is somewhat vague and could be tightened.

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 (potential for errors, varied content types) and the absence of annotations and output schema, the description is incomplete. It doesn't cover return values, error formats, or detailed behavioral traits needed for reliable use by an AI agent.

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%, so the schema already documents all parameters thoroughly. The description adds no additional meaning about parameters beyond what's in the schema, such as explaining the implications of different response types or timeout values. Baseline 3 is appropriate when the schema does the heavy lifting.

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

Purpose4/5

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

The description clearly states the action ('fetch content from a URL') and resource ('URL'), making the purpose immediately understandable. However, it doesn't explicitly differentiate from the sibling tool 'google_search', which likely serves a different purpose (searching vs. direct fetching).

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 alternatives like 'google_search'. It mentions 'proper error handling and response processing' but doesn't specify scenarios, prerequisites, or exclusions for usage.

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

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/TheSethRose/Fetch-Browser'

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