rate_limit_check
Determine if an operation exceeds rate limits by specifying a resource, maximum requests, and time window. Optionally increment the counter to track usage and ensure compliance.
Instructions
Check if an operation should be rate-limited
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| increment | No | Increment the counter if allowed | |
| max_requests | No | Maximum requests allowed | |
| resource | Yes | Resource identifier (e.g., 'api.github.com') | |
| window_seconds | No | Time window in seconds |
Implementation Reference
- src/index-v2.ts:640-684 (handler)Handler function for the 'rate_limit_check' tool. It manages rate limiting counters for resources, checking if requests are allowed within the specified window and optionally incrementing the count.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:242-266 (schema)Input schema for the 'rate_limit_check' tool, defining parameters like resource, max_requests, window_seconds, and increment.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:239-267 (registration)Tool definition object in the tools array, used by the listTools handler to register the 'rate_limit_check' tool with the MCP server.{ 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:271-272 (helper)Global Map storing rate limit state (count and reset time) for each resource, used by the rate_limit_check handler.const rateLimits = new Map<string, { count: number; resetAt: number }>();