fetch_json
Retrieve JSON data from a URL using the Fetch MCP Server. Specify the URL and optional headers to fetch and process JSON content for integration or analysis.
Instructions
Fetch a JSON file from a URL
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| headers | No | Optional headers to include in the request | |
| url | Yes | URL of the JSON to fetch |
Implementation Reference
- src/Fetcher.ts:45-58 (handler)The static method `json()` that implements the core logic of the `fetch_json` tool: fetches the response using `_fetch`, parses JSON, stringifies it, and returns as text content or error.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/types.ts:3-6 (schema)Zod schema `RequestPayloadSchema` used to validate input arguments (URL and optional headers) for `fetch_json` and other fetch tools.export const RequestPayloadSchema = z.object({ url: z.string().url(), headers: z.record(z.string()).optional(), });
- src/index.ts:83-100 (registration)Registration of the `fetch_json` tool in the `ListToolsRequestSchema` handler, defining 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/Fetcher.ts:6-29 (helper)Private helper `_fetch` method used by `json()` (and other tools) to perform the HTTP request with custom User-Agent and error handling.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`); } }
- src/index.ts:114-116 (handler)Dispatch logic in the main `CallToolRequestSchema` handler that routes `fetch_json` calls to `Fetcher.json()`.if (request.params.name === "fetch_json") { const fetchResult = await Fetcher.json(validatedArgs); return fetchResult;