Implementation:0sec labs 0 RateLimiter
| Knowledge Sources | |
|---|---|
| Domains | MCP, Rate_Limiting, Traffic_Management |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete tool for per-host token-bucket rate limiting with 429 backoff, provided by the 0security core scope module.
Description
The RateLimiter class enforces per-host request rate limits using a token-bucket algorithm. Hostnames are lowercased and ports stripped before lookup, so api.example.com:443 and API.example.com share a bucket. A conservative 60-second cool-off floor applies when a 429 lands without a useful Retry-After. The parseRateLimitFlag function parses the CLI --rate-limit spec format ("host=rps,host2=rps:burst,default" or plain "5" for default rps).
Usage
Import these when you need to enforce rate limits on outgoing HTTP requests from the MCP server or agent loop. They are Step 5 of the MCP server integration workflow.
Code Reference
Source Location
- Repository: 0security
- File: packages/core/src/scope/rate-limit.ts:L249+ (RateLimiter), rate-limit.ts:L479+ (parseRateLimitFlag)
Signature
export class RateLimiter {
static readonly RETRY_AFTER_FLOOR_MS = 60_000;
constructor(
config: RateLimiterConfig,
opts?: {
nowFn?: () => number;
sleepFn?: (ms: number, signal?: AbortSignal) => Promise<void>;
onThrottle?: () => void;
},
);
// acquire a token for a host (blocks if bucket is dry)
async acquire(host: string, signal?: AbortSignal): Promise<void>;
// park a host after a 429
parkHost(host: string, retryAfterMs?: number): void;
}
export function parseRateLimitFlag(
spec: string,
fallbackRps: number = 5,
): RateLimiterConfig;
interface RateLimiterConfig {
default: HostRateConfig;
perHost?: Record<string, HostRateConfig>;
jitter?: JitterConfig;
}
interface HostRateConfig {
rps: number;
burst?: number;
}
Import
import { RateLimiter, parseRateLimitFlag } from "@0/core";
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | RateLimiterConfig | Yes /// Default + per-host rate config | |
| spec | string | Yes (parseRateLimitFlag) /// CLI spec string: "host=rps,host2=rps:burst,default" | |
| fallbackRps | number | No (parseRateLimitFlag) /// Default rps (default 5) |
Outputs
| Name | Type | Description |
|---|---|---|
| RateLimiter instance | RateLimiter /// Per-host token-bucket rate limiter | |
| parseRateLimitFlag return | RateLimiterConfig /// Parsed config for RateLimiter constructor |
Usage Examples
Via CLI
# Default 5 rps per host
0 mcp-server --rate-limit "5"
# Per-host configuration
0 mcp-server --rate-limit "api.example.com=10,burst=20,default=5"
# With burst
0 mcp-server --rate-limit "target.com=3:10,default"
Programmatic
import { RateLimiter, parseRateLimitFlag } from "@0/core";
const config = parseRateLimitFlag("api.example.com=10,default=5");
const limiter = new RateLimiter(config, {
onThrottle: () => console.log("Throttled!"),
});
// Before each HTTP request:
await limiter.acquire("api.example.com");
// ... make the request