Skip to main content
Glama
rayss868

Web-curl MCP Server

fetch_api

Make REST API requests using HTTP methods like GET, POST, PUT, and DELETE with custom headers, body content, and timeout settings.

Instructions

Make a REST API request with various methods, headers, and body.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlYesThe URL for the API endpoint.
methodYesHTTP method for the request.
headersNoRequest headers (e.g., for authorization).
bodyNoRequest body (JSON object, string, etc.).
timeoutNoRequest timeout in milliseconds (default: 60000).
limitYesMaximum number of characters to return in the response body (required).
redirectNoRedirect mode for the request (default: follow).

Implementation Reference

  • The core handler function that performs the HTTP fetch request, handles timeouts, body serialization, response parsing (JSON/text/binary), truncation to limit characters, and returns structured response.
    export const fetchApi = async (args: FetchApiArgs): Promise<FetchApiResponse> => {
      const { url, method, headers, body, timeout = 60000, limit, redirect = 'follow' } = args;
    
      const controller = new AbortController();
      const timeoutId = setTimeout(() => controller.abort(), timeout);
    
      const startTime = performance.now(); // Start time measurement
    
      try {
        const options: RequestInit = {
          method,
          headers,
          signal: controller.signal,
          redirect, // Add redirect option
          // The `timeout` option is specific to `node-fetch` and not part of the standard `RequestInit`.
          // The timeout logic is now handled manually using AbortController.
        };
    
        if (body !== undefined) {
          if (typeof body === 'object' && headers && headers['Content-Type'] === 'application/json') {
            options.body = JSON.stringify(body);
          } else {
            options.body = body; // Works for string, Buffer, FormData, URLSearchParams
          }
        }
    
        const response: Response = await fetch(url, options);
        clearTimeout(timeoutId);
    
        const endTime = performance.now(); // End time measurement
        const responseTimeMs = endTime - startTime; // Calculate response time
    
        let responseBody: any;
        const contentType = response.headers.get('content-type');
    
        if (contentType && contentType.includes('application/json')) {
          responseBody = await response.json();
        } else if (contentType && (contentType.includes('text/') || contentType.includes('application/xml') || contentType.includes('application/xhtml+xml'))) {
          responseBody = await response.text();
        } else {
          // For binary data or unknown content types, try to get as buffer then base64 encode
          try {
            const buffer = await response.arrayBuffer();
            responseBody = Buffer.from(buffer).toString('base64');
          } catch (e) {
            responseBody = 'Could not parse body (binary or unknown content type)';
          }
        }
        
        const responseHeaders: Record<string, string> = {};
        response.headers.forEach((value, name) => {
          responseHeaders[name] = value;
        });
    
        // Enforce output limit: convert body to string for length measurement/truncation.
        let fullBodyString: string;
        try {
          if (typeof responseBody === 'string') {
            fullBodyString = responseBody;
          } else {
            fullBodyString = JSON.stringify(responseBody);
          }
        } catch (e) {
          fullBodyString = String(responseBody);
        }
    
        const bodyLength = fullBodyString.length;
        let truncated = false;
        let finalBody: any = responseBody;
    
        if (typeof limit === 'number' && bodyLength > limit) {
          // When truncation is necessary, return a string truncated to 'limit' characters.
          finalBody = fullBodyString.substring(0, limit);
          truncated = true;
        } else {
          // If not truncated and original was JSON-parsed, keep original parsed object,
          // otherwise keep the string.
          finalBody = responseBody;
        }
    
        return {
          status: response.status,
          statusText: response.statusText,
          headers: responseHeaders,
          body: finalBody,
          ok: response.ok,
          url: response.url,
          bodyLength,
          truncated,
          responseTimeMs, // Include response time
        };
      } catch (error: any) {
        clearTimeout(timeoutId);
        if (error.name === 'AbortError') {
          throw new Error(`Request timed out after ${timeout / 1000} seconds`);
        }
        throw error;
      }
    };
  • src/index.ts:170-211 (registration)
    Registration of the 'fetch_api' tool in the MCP server's listTools handler, including name, description, autoApprove, and detailed inputSchema.
      name: 'fetch_api',
      description: 'Make a REST API request with various methods, headers, and body.',
      autoApprove: true,
      inputSchema: {
        type: 'object',
        properties: {
          url: {
            type: 'string',
            description: 'The URL for the API endpoint.',
          },
          method: {
            type: 'string',
            enum: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'],
            description: 'HTTP method for the request.',
          },
          headers: {
            type: 'object',
            description: 'Request headers (e.g., for authorization).',
          },
          body: {
            type: ['object', 'string', 'null'], // Allow object, string, or null for body
            description: 'Request body (JSON object, string, etc.).',
          },
          timeout: {
            type: 'number',
            description: 'Request timeout in milliseconds (default: 60000).',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of characters to return in the response body (required).'
          },
          redirect: {
            type: 'string',
            enum: ['follow', 'error', 'manual'],
            description: 'Redirect mode for the request (default: follow).'
          },
        },
        required: ['url', 'method', 'limit'],
        additionalProperties: false,
        description: 'HTTP API request (GET/POST/etc), custom header/body, timeout, debug mode for verbose output/logging.'
      },
    },
  • TypeScript interface defining the input parameters for the fetch_api tool.
    export interface FetchApiArgs {
      url: string;
      method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
      headers?: Record<string, string>;
      body?: any; // Can be string, Buffer, stream, or URLSearchParams
      timeout?: number; // Timeout in milliseconds
      limit: number; // Maximum number of characters to return in the response body (required)
      redirect?: 'follow' | 'error' | 'manual'; // Redirect mode
    }
  • TypeScript interface defining the output response structure from the fetch_api tool.
    export interface FetchApiResponse {
      status: number;
      statusText: string;
      headers: Record<string, string>;
      body: any;
      ok: boolean;
      url: string;
      bodyLength?: number; // length of the un-truncated body (characters)
      truncated?: boolean; // whether the body was truncated to satisfy limit
      responseTimeMs: number; // Add response time in milliseconds
  • Validation function to check if provided arguments conform to FetchApiArgs interface before calling the handler.
    export const isValidFetchApiArgs = (args: any): args is FetchApiArgs => {
      if (typeof args !== 'object' || args === null) return false;
      if (typeof args.url !== 'string') return false;
      if (!['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'].includes(args.method)) return false;
      if (args.headers !== undefined && (typeof args.headers !== 'object' || args.headers === null || Array.isArray(args.headers))) return false;
      // body can be of various types, so a simple check might not be sufficient,
      // but for now, we'll assume it's provided correctly if it exists.
      if (args.timeout !== undefined && typeof args.timeout !== 'number') return false;
      if (args.limit === undefined || typeof args.limit !== 'number') return false; // limit is required and must be a number
      if (args.redirect !== undefined && !['follow', 'error', 'manual'].includes(args.redirect)) return false;
      return true;
    };
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 making requests with 'various methods, headers, and body' but lacks critical details: it doesn't specify authentication requirements, rate limits, error handling, response format, or side effects. For a general-purpose API tool with 7 parameters, this leaves significant gaps in understanding its behavior.

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

Conciseness5/5

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

The description is a single, efficient sentence that front-loads the core functionality. Every word earns its place without redundancy or fluff, making it easy to parse quickly.

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 tool's complexity (7 parameters, no annotations, no output schema), the description is insufficient. It doesn't explain what the tool returns, how errors are handled, or practical constraints like authentication. For a versatile API tool, more context is needed to use it effectively beyond the parameter definitions.

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 minimal value beyond the schema—it mentions 'various methods, headers, and body' which are covered in the schema's enum and descriptions. No additional parameter semantics are provided, meeting the baseline for high schema coverage.

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 tool's purpose as making REST API requests with various methods, headers, and body. It specifies the verb ('Make a REST API request') and resource ('API endpoint'), but doesn't explicitly differentiate from sibling tools like fetch_webpage or download_file, which might have overlapping functionality for HTTP requests.

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. It doesn't mention sibling tools like fetch_webpage or download_file, nor does it specify use cases, prerequisites, or exclusions. The agent must infer usage from the tool name and parameters alone.

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/rayss868/MCP-Web-Curl'

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