rate_limit_check
Check whether an operation exceeds the allowed request count within a configurable time window to prevent excessive API calls and ensure fair usage.
Instructions
Check if an operation should be rate-limited
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| resource | Yes | Resource identifier (e.g., 'api.github.com') | |
| max_requests | No | Maximum requests allowed | |
| window_seconds | No | Time window in seconds | |
| increment | No | Increment the counter if allowed |
Implementation Reference
- src/index-v2.ts:640-684 (handler)The handler logic for the rate_limit_check tool. It extracts args (resource, max_requests, window_seconds, increment), checks/initializes rate limit state from the rateLimits Map, determines if the operation is allowed based on count vs max_requests, optionally increments the counter, and returns a JSON response with allowed status, current count, remaining, reset time, etc.
case "rate_limit_check": { const { resource, max_requests = 60, window_seconds = 60, increment = true } = args as any; const now = Date.now(); const windowMs = window_seconds * 1000; let limit = rateLimits.get(resource); // Initialize or reset if window expired if (!limit || limit.resetAt <= now) { limit = { count: 0, resetAt: now + windowMs }; rateLimits.set(resource, limit); } const allowed = limit.count < max_requests; const remaining = Math.max(0, max_requests - limit.count); const resetIn = Math.ceil((limit.resetAt - now) / 1000); if (allowed && increment) { limit.count++; } return { content: [{ type: "text", text: JSON.stringify({ allowed, resource, current_count: limit.count, max_requests, remaining, reset_in_seconds: resetIn, reset_at: new Date(limit.resetAt).toISOString() }) }] }; } - src/index-v2.ts:239-267 (schema)The schema/definition for the rate_limit_check tool, defining its name, description, and inputSchema with properties: resource (string, required), max_requests (number, default 60), window_seconds (number, default 60), and increment (boolean, default true).
{ name: "rate_limit_check", description: "Check if an operation should be rate-limited", inputSchema: { type: "object", properties: { resource: { type: "string", description: "Resource identifier (e.g., 'api.github.com')" }, max_requests: { type: "number", description: "Maximum requests allowed", default: 60 }, window_seconds: { type: "number", description: "Time window in seconds", default: 60 }, increment: { type: "boolean", description: "Increment the counter if allowed", default: true } }, required: ["resource"] } } - src/index-v2.ts:278-281 (registration)Tool registration via server.setRequestHandler(ListToolsRequestSchema) which returns the tools array containing the rate_limit_check definition.
// Register list handler server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools }; }); - src/index-v2.ts:270-271 (helper)The rateLimits Map used by the rate_limit_check handler to store per-resource rate limit state (count and resetAt).
// Rate limiting storage const rateLimits = new Map<string, { count: number; resetAt: number }>();