Skip to main content
Glama

fetch_url

Fetch data from web URLs using HTTP methods like GET or POST, with configurable headers, body content, and timeout settings for automation workflows.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlYes
methodNoGET
headersNo
bodyNo
timeoutNo
followRedirectsNo

Implementation Reference

  • The core handler function for the 'fetch_url' tool that executes the HTTP request using fetchWithTimeout, processes the response text and headers, and returns formatted content and metadata.
    async ({ url, method, headers, body, timeout, followRedirects }) => { return wrapToolExecution(async () => { const response = await fetchWithTimeout(url, { method, headers, body, timeout, followRedirects }); const responseText = await response.text(); const responseHeaders = extractHeaders(response); return { content: [{ type: "text" as const, text: responseText }], metadata: { status: response.status, statusText: response.statusText, headers: responseHeaders, url: response.url } }; }, { errorCode: ERROR_CODES.HTTP_REQUEST, context: "Failed to fetch URL" }); }
  • Zod schema for input validation of the 'fetch_url' tool parameters: url (required), method, headers, body, timeout, followRedirects.
    { url: z.string().url("Valid URL is required"), method: z.enum(["GET", "POST", "PUT", "DELETE", "PATCH"]).optional().default(DEFAULTS.HTTP_METHOD), headers: z.record(z.string()).optional().default({}), body: z.string().optional(), timeout: z.number().optional().default(DEFAULT_TIMEOUTS.HTTP_REQUEST), followRedirects: z.boolean().optional().default(DEFAULTS.HTTP_FOLLOW_REDIRECTS)
  • The registerFetchUrl function that directly registers the 'fetch_url' tool on the MCP server using server.tool(), including schema and handler.
    function registerFetchUrl(server: McpServer): void { server.tool("fetch_url", { url: z.string().url("Valid URL is required"), method: z.enum(["GET", "POST", "PUT", "DELETE", "PATCH"]).optional().default(DEFAULTS.HTTP_METHOD), headers: z.record(z.string()).optional().default({}), body: z.string().optional(), timeout: z.number().optional().default(DEFAULT_TIMEOUTS.HTTP_REQUEST), followRedirects: z.boolean().optional().default(DEFAULTS.HTTP_FOLLOW_REDIRECTS) }, async ({ url, method, headers, body, timeout, followRedirects }) => { return wrapToolExecution(async () => { const response = await fetchWithTimeout(url, { method, headers, body, timeout, followRedirects }); const responseText = await response.text(); const responseHeaders = extractHeaders(response); return { content: [{ type: "text" as const, text: responseText }], metadata: { status: response.status, statusText: response.statusText, headers: responseHeaders, url: response.url } }; }, { errorCode: ERROR_CODES.HTTP_REQUEST, context: "Failed to fetch URL" }); } ); }
  • src/index.ts:66-66 (registration)
    Invocation of registerWebTools(server) in the main server initialization, which calls registerFetchUrl to register the tool.
    registerWebTools(server);
  • Key helper function fetchWithTimeout used by the handler to perform HTTP fetches with configurable timeout, method, headers, body, and redirect handling using AbortController.
    async function fetchWithTimeout( url: string, options: { method: HttpMethod; headers?: Record<string, string>; body?: string; timeout: number; followRedirects: boolean; } ): Promise<Response> { const { method, headers, body, timeout, followRedirects } = options; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeout); const requestInit: RequestInit = { method, headers, signal: controller.signal, redirect: followRedirects ? "follow" : "manual" }; if (body && ["POST", "PUT", "PATCH"].includes(method)) { requestInit.body = body; } try { const response = await fetch(url, requestInit); return response; } catch (error) { if (error instanceof Error && error.name === 'AbortError') { throw new Error(`Request timeout after ${timeout}ms`); } throw error; } finally { clearTimeout(timeoutId); }

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/ishuru/open-mcp'

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