Skip to main content
Glama
zcaceres

Fetch MCP Server

by zcaceres

fetch_json

Retrieve JSON data from a URL by specifying the source address, optional headers, and content length parameters for web content integration.

Instructions

Fetch a JSON file from a URL

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlYesURL of the JSON to fetch
headersNoOptional headers to include in the request
max_lengthNoMaximum number of characters to return (default: 5000)
start_indexNoStart content from this character index (default: 0)

Implementation Reference

  • Core handler logic for 'fetch_json': fetches URL, parses JSON, stringifies it, applies length limits, returns formatted content or error.
    static async json(requestPayload: RequestPayload) {
      try {
        const response = await this._fetch(requestPayload);
        const json = await response.json();
        let jsonString = JSON.stringify(json);
        
        // Apply length limits
        jsonString = this.applyLengthLimits(
          jsonString,
          requestPayload.max_length ?? 5000,
          requestPayload.start_index ?? 0
        );
        
        return {
          content: [{ type: "text", text: jsonString }],
          isError: false,
        };
      } catch (error) {
        return {
          content: [{ type: "text", text: (error as Error).message }],
          isError: true,
        };
      }
  • Zod schema for validating input parameters to 'fetch_json' and other fetch tools.
    export const RequestPayloadSchema = z.object({
      url: z.string().url(),
      headers: z.record(z.string()).optional(),
      max_length: z.number().int().min(0).optional().default(downloadLimit),
      start_index: z.number().int().min(0).optional().default(0),
    });
  • src/index.ts:109-134 (registration)
    Tool registration in ListTools handler, defining name, description, and input schema for 'fetch_json'.
    {
      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",
          },
          max_length: {
            type: "number",
            description: `Maximum number of characters to return (default: ${downloadLimit})`,
          },
          start_index: {
            type: "number",
            description: "Start content from this character index (default: 0)",
          },
        },
        required: ["url"],
      },
    },
  • Dispatch logic in CallToolRequest handler that routes 'fetch_json' calls to Fetcher.json.
    if (request.params.name === "fetch_json") {
      const fetchResult = await Fetcher.json(validatedArgs);
      return fetchResult;
  • Shared helper method for performing the HTTP fetch with security checks and error handling, used by 'fetch_json'.
    private static async _fetch({
      url,
      headers,
    }: RequestPayload): Promise<Response> {
      try {
        if (is_ip_private(url)) {
          throw new Error(
            `Fetcher blocked an attempt to fetch a private IP ${url}. This is to prevent a security vulnerability where a local MCP could fetch privileged local IPs and exfiltrate data.`,
          );
        }
        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`);
        }
      }
    }
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/zcaceres/fetch-mcp'

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