Skip to main content
Glama

fetch_json

Retrieve JSON data from a specified URL using optional custom headers to handle web content fetching efficiently with Fetch MCP Server.

Instructions

Fetch a JSON file from a URL

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
headersNoOptional headers to include in the request
urlYesURL of the JSON to fetch

Implementation Reference

  • The main handler function for the 'fetch_json' tool. Fetches the JSON from the provided URL, parses it, stringifies the result, and returns it as tool content. Handles errors by returning an error message.
    static async json(requestPayload: RequestPayload) { try { const response = await this._fetch(requestPayload); const json = await response.json(); return { content: [{ type: "text", text: JSON.stringify(json) }], isError: false, }; } catch (error) { return { content: [{ type: "text", text: (error as Error).message }], isError: true, }; }
  • src/index.ts:83-100 (registration)
    Registers the 'fetch_json' tool in the ListTools response, including its name, description, and input schema.
    { name: "fetch_json", description: "Fetch a JSON file from a URL", inputSchema: { type: "object", properties: { url: { type: "string", description: "URL of the JSON to fetch", }, headers: { type: "object", description: "Optional headers to include in the request", }, }, required: ["url"], }, },
  • src/index.ts:114-117 (registration)
    Routes calls to the 'fetch_json' tool by invoking Fetcher.json with validated arguments in the CallToolRequest handler.
    if (request.params.name === "fetch_json") { const fetchResult = await Fetcher.json(validatedArgs); return fetchResult; }
  • Zod schema for validating the input payload (url and optional headers) used for 'fetch_json' and other fetch tools.
    export const RequestPayloadSchema = z.object({ url: z.string().url(), headers: z.record(z.string()).optional(), });
  • Shared helper method for performing the HTTP fetch with custom headers and User-Agent, used by fetch_json 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`); } }

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/mcp-npx-fetch'

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