Skip to main content
Glama

send_request_to_gateway

Send one or multiple requests to the APISIX gateway for processing, including path, method, data, and headers, with optional parallel execution.

Instructions

Send a request or multiple requests to the APISIX gateway

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
requestsYesarray of requests to send in parallel

Implementation Reference

  • Implementation of the 'send_request_to_gateway' tool handler. Sends one or multiple HTTP requests (with optional repeats) to the APISIX gateway, handles responses and errors, flattens results, and returns JSON with summary.
    server.tool("send_request_to_gateway", "Send a request or multiple requests to the APISIX gateway", SendRequestSchema.shape, async (args) => {
      const makeRequest = async (config: RequestConfig) => {
        try {
          const response = await axios.request({
            url: `${APISIX_SERVER_HOST}:${APISIX_SERVER_PORT}${config.path}`,
            method: config.method,
            data: config.data,
            headers: config.headers,
            timeout: 10000,
          });
    
          return {
            status: response.status,
            data: response.data,
            headers: response.headers,
          };
        } catch (error) {
          // handle error
          const axiosError = error as AxiosError;
          if (axiosError.response) {
            // The server responded with an error status code
            return {
              status: axiosError.response.status,
              data: axiosError.response.data || { error: 'Request failed' },
              headers: axiosError.response?.headers || {},
            };
          } else if (axiosError.request) {
            // The request was sent but no response was received
            return {
              status: 503, // Use 503 to indicate service is unavailable
              data: { error: 'Gateway is not responding' },
              headers: axiosError.request?.headers || {},
            };
          } else {
            // An error occurred while setting up the request
            return {
              status: 500,
              data: { error: axiosError.message || 'Request error' },
              headers: axiosError.request?.headers || {},
            };
          }
        }
      };
    
      const makeRepeatedRequests = async (config: RequestConfig) => {
        const repeatCount = config.repeatCount || 1;
        if (repeatCount > 1) {
          return Promise.all(Array(repeatCount).fill(null).map(() => makeRequest(config)));
        } else {
          return makeRequest(config);
        }
      };
    
      let results = [];
      results = await Promise.all(args.requests.map(req => makeRepeatedRequests(req)));
    
    
      // Flatten results if needed and count
      const flatResults = results.flat();
      const singleResults = flatResults.filter(r => !Array.isArray(r));
      const multiResults = flatResults.filter(r => Array.isArray(r)).flat();
      const allResults = [...singleResults, ...multiResults];
    
      return {
        content: [{
          type: "text" as const,
          text: JSON.stringify({
            results: allResults,
            summary: {
              total: allResults.length,
              successful: allResults.filter(r => r.status >= 200 && r.status < 300).length,
              failed: allResults.filter(r => r.status >= 400).length
            }
          }, null, 2)
        }]
      };
    });
  • Zod schema defining the input for 'send_request_to_gateway': array of requests with path, method, optional data, headers, repeatCount.
    export const SendRequestSchema = z.object({
      requests: z.array(z.object({
        path: z.string().describe("request path"),
        method: z.string().describe("request method"),
        data: z.any().optional().describe("request data"),
        headers: z.record(z.string(), z.string()).optional().describe("request headers"),
        repeatCount: z.number().optional().describe("number of requests to send in parallel").default(1),
      })).describe("array of requests to send in parallel"),
    });
  • src/index.ts:22-22 (registration)
    Call to setupCommonTools which registers the 'send_request_to_gateway' tool among others on the MCP server.
    setupCommonTools(server);

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/api7/apisix-mcp'

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