get_consumer_requests
Retrieve and analyze API requests for a specific consumer on Kong Konnect MCP Server. Filter by time range, success or failure status, and view detailed statistics, including latency, success rate, and service distribution.
Instructions
Retrieve and analyze API requests made by a specific consumer.
INPUT:
consumerId: String - ID of the consumer to analyze. The format of this field must be "controlPlaneID:consumerId".
timeRange: String - Time range for data retrieval (15M, 1H, 6H, 12H, 24H, 7D)
successOnly: Boolean - Filter to only show successful (2xx) requests (default: false)
failureOnly: Boolean - Filter to only show failed (non-2xx) requests (default: false)
maxResults: Number - Maximum number of results to return (1-1000)
OUTPUT:
metadata: Object - Contains consumerId, totalRequests, timeRange, and filters
statistics: Object - Usage statistics including:
averageLatencyMs: Number - Average response time in milliseconds
successRate: Number - Percentage of successful requests
statusCodeDistribution: Array - Breakdown of requests by status code
serviceDistribution: Array - Breakdown of requests by service
requests: Array - List of requests with details for each request
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| consumerId | Yes | Consumer ID to filter by (obtainable from analyze-failed-requests or query-api-requests tools) | |
| failureOnly | No | Show only failed (non-2xx) requests | |
| maxResults | No | Number of items to return per page | |
| successOnly | No | Show only successful (2xx) requests | |
| timeRange | No | Time range for data retrieval (15M = 15 minutes, 1H = 1 hour, etc.) | 1H |
Implementation Reference
- src/operations/analytics.ts:196-318 (handler)The core handler function that executes the tool logic: queries API requests for a specific consumer using KongApi, applies filters for success/failure, computes statistics like avg latency and success rate, and formats the response.export async function getConsumerRequests( api: KongApi, consumerId: string, timeRange: string, successOnly = false, failureOnly = false, maxResults = 100 ) { try { // Build filters array const filters: ApiRequestFilter[] = [ { field: "consumer", operator: "in", value: [consumerId] } ]; // Add status code filter if needed if (successOnly) { filters.push({ field: "status_code_grouped", operator: "in", value: ["2XX"] }); } else if (failureOnly) { filters.push({ field: "status_code_grouped", operator: "in", value: ["4XX", "5XX"] }); } const result = await api.queryApiRequests(timeRange, filters, maxResults); // Calculate some statistics if we have results let avgLatency = 0; let successRate = 0; let statusCodeCounts: Record<string, number> = {}; let serviceBreakdown: Record<string, { count: number, statusCodes: Record<string, number> }> = {}; if (result.results.length > 0) { // Calculate average latency avgLatency = result.results.reduce((sum, req) => sum + (req.latencies_response_ms || 0), 0) / result.results.length; // Calculate success rate const successCount = result.results.filter(req => { const status = req.status_code ?? req.response_http_status ?? 0; // Default to 0 if both are undefined return status >= 200 && status < 300; }).length; successRate = (successCount / result.results.length) * 100; // Count status codes result.results.forEach(req => { const status = req.status_code ?? req.response_http_status ?? 0; // Default to 0 if both are undefined statusCodeCounts[status] = (statusCodeCounts[status] || 0) + 1; }); // Service breakdown result.results.forEach(req => { const service = req.gateway_service ?? "unknown"; // Using ?? instead of || for null/undefined handling if (!serviceBreakdown[service]) { serviceBreakdown[service] = { count: 0, statusCodes: {} }; } serviceBreakdown[service].count++; const status = req.status_code ?? req.response_http_status ?? 0; // Default to 0 if both are undefined serviceBreakdown[service].statusCodes[status] = (serviceBreakdown[service].statusCodes[status] || 0) + 1; }); } // Format the response in a readable way return { metadata: { consumerId: consumerId, totalRequests: result.results.length, timeRange: { start: result.meta.time_range.start, end: result.meta.time_range.end, }, filters: { successOnly, failureOnly } }, statistics: { averageLatencyMs: parseFloat(avgLatency.toFixed(2)), successRate: parseFloat(successRate.toFixed(2)), statusCodeDistribution: Object.entries(statusCodeCounts).map(([code, count]) => ({ statusCode: parseInt(code), count: count, percentage: parseFloat(((count / result.results.length) * 100).toFixed(2)) })).sort((a, b) => b.count - a.count), serviceDistribution: Object.entries(serviceBreakdown).map(([service, data]) => ({ serviceId: service, count: data.count, percentage: parseFloat(((data.count / result.results.length) * 100).toFixed(2)), statusCodeBreakdown: Object.entries(data.statusCodes).map(([code, count]) => ({ statusCode: parseInt(code), count: count })).sort((a, b) => b.count - a.count) })).sort((a, b) => b.count - a.count) }, requests: result.results.map(req => ({ timestamp: req.request_start, httpMethod: req.http_method, uri: req.request_uri, statusCode: req.status_code || req.response_http_status, serviceId: req.gateway_service, routeId: req.route, latency: { totalMs: req.latencies_response_ms, gatewayMs: req.latencies_kong_gateway_ms, upstreamMs: req.latencies_upstream_ms }, clientIp: req.client_ip, traceId: req.trace_id })) }; } catch (error) { throw error; } }
- src/parameters.ts:49-60 (schema)Zod schema defining the input parameters for the get_consumer_requests tool, including consumerId, timeRange, successOnly, failureOnly, and maxResults.export const getConsumerRequestsParameters = () => z.object({ consumerId: z.string() .describe("Consumer ID to filter by (obtainable from analyze-failed-requests or query-api-requests tools)"), timeRange: timeRangeSchema, successOnly: z.boolean() .default(false) .describe("Show only successful (2xx) requests"), failureOnly: z.boolean() .default(false) .describe("Show only failed (non-2xx) requests"), maxResults: pageSizeSchema, });
- src/tools.ts:25-29 (registration)Tool registration object defining the method name, description (from prompts), parameters schema (from parameters), and category.method: "get_consumer_requests", name: "Get Consumer Requests", description: prompts.getConsumerRequestsPrompt(), parameters: parameters.getConsumerRequestsParameters(), category: "analytics"
- src/index.ts:62-71 (registration)Dispatch registration in the main MCP server switch statement that maps the tool method to the analytics handler function call.case "get_consumer_requests": result = await analytics.getConsumerRequests( this.api, args.consumerId, args.timeRange, args.successOnly, args.failureOnly, args.maxResults ); break;
- src/prompts.ts:32-50 (helper)Helper function providing the detailed prompt/description for the tool's input/output format, used in registration.export const getConsumerRequestsPrompt = () => ` Retrieve and analyze API requests made by a specific consumer. INPUT: - consumerId: String - ID of the consumer to analyze. The format of this field must be "controlPlaneID:consumerId". - timeRange: String - Time range for data retrieval (15M, 1H, 6H, 12H, 24H, 7D) - successOnly: Boolean - Filter to only show successful (2xx) requests (default: false) - failureOnly: Boolean - Filter to only show failed (non-2xx) requests (default: false) - maxResults: Number - Maximum number of results to return (1-1000) OUTPUT: - metadata: Object - Contains consumerId, totalRequests, timeRange, and filters - statistics: Object - Usage statistics including: - averageLatencyMs: Number - Average response time in milliseconds - successRate: Number - Percentage of successful requests - statusCodeDistribution: Array - Breakdown of requests by status code - serviceDistribution: Array - Breakdown of requests by service - requests: Array - List of requests with details for each request `;