rate_limit_check
Check if an operation should be rate-limited by evaluating request counts against defined thresholds for specific resources.
Instructions
Check if an operation should be rate-limited
Input Schema
TableJSON 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 main handler function for the 'rate_limit_check' tool. It checks if a request for a given resource is within rate limits, optionally increments the counter, and returns status including remaining requests and reset time.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 tool specification including name, description, and input schema for 'rate_limit_check', which defines the expected input parameters and validation.{ 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:270-271 (helper)Global storage Map for rate limit counters per resource, essential for tracking usage across tool calls.// Rate limiting storage const rateLimits = new Map<string, { count: number; resetAt: number }>();