Skip to main content
Full API reference for the @frontmcp/guard package. This library is SDK-agnostic and works with any StorageAdapter backend.
When using @frontmcp/sdk, guard features are integrated automatically via the throttle config and tool/agent decorators. You only need to import from @frontmcp/guard directly if building custom integrations.

Configuration Types

GuardConfig

Top-level configuration for the guard system. Passed to @FrontMcp({ throttle: ... }) or createGuardManager().

RateLimitConfig

Configuration for sliding window rate limiting.

ConcurrencyConfig

Configuration for distributed semaphore concurrency control.

TimeoutConfig

Configuration for execution timeout.

IpFilterConfig

Configuration for IP-based access control.

PartitionKey

Determines how limits are bucketed across requests.
PartitionKeyContext:

Classes

GuardManager

Orchestrates all guard features. Created via createGuardManager() or automatically by the SDK when throttle is configured.

checkRateLimit()

Check per-entity rate limit.
Returns { allowed: true, remaining: Infinity, resetMs: 0 } if no config applies.

checkGlobalRateLimit()

Check global (server-wide) rate limit.
Uses config.global configuration. Returns allowed result if no global config.

acquireSemaphore()

Acquire a concurrency slot for an entity.
Returns SemaphoreTicket on success, null if no config applies. May throw QueueTimeoutError if queue timeout expires.

acquireGlobalSemaphore()

Acquire a global concurrency slot.
Uses config.globalConcurrency configuration.

checkIpFilter()

Check if a client IP is allowed.
Returns undefined if no IP filter configured or clientIp is falsy. Otherwise returns IpFilterResult.

isIpAllowListed()

Check if a client IP is explicitly on the allow list.
Returns true only if an IP filter is configured, clientIp is provided, and the IP matches the allow list.

destroy()

Disconnect storage backend and clean up resources.

SlidingWindowRateLimiter

Implements sliding window rate limiting with O(1) storage per key.

check()

Check and consume a rate limit token.
Algorithm: Uses two adjacent fixed-window counters with weighted interpolation. The estimated count is:
If estimatedCount < maxRequests, the request is allowed and the current window counter is atomically incremented. RateLimitResult:

reset()

Reset rate limit counters for a key.

DistributedSemaphore

Implements a distributed counting semaphore with optional queuing.
Default ticketTtlSeconds: 300 (5 minutes).

acquire()

Acquire a semaphore slot.
  • Returns SemaphoreTicket if a slot is acquired.
  • Returns null if queueTimeoutMs <= 0 and no slot is available.
  • Throws QueueTimeoutError if queued and timeout expires.
Uses pub/sub when available for efficient slot release detection, falls back to polling with exponential backoff (100ms to 1000ms). SemaphoreTicket:

getActiveCount()

Get current number of active tickets for a key.

forceReset()

Force-clear all tickets and reset the counter for a key.

IpFilter

IP-based access control with CIDR support for IPv4 and IPv6.
Parses all CIDR rules at construction time using BigInt bitmask representation.

check()

Check if a client IP is allowed.
Evaluation order:
  1. Deny list (takes precedence)
  2. Allow list
  3. Default action
IpFilterResult:

isAllowListed()

Check if an IP is explicitly on the allow list.

Functions

createGuardManager()

Factory function to create a fully initialized GuardManager.
CreateGuardManagerArgs: Behavior:
  1. Creates storage backend from config.storage (or in-memory if not set)
  2. Connects the storage backend
  3. Creates namespaced storage with config.keyPrefix
  4. Returns initialized GuardManager

withTimeout()

Wraps an async function with an execution deadline.
Throws ExecutionTimeoutError if the deadline is exceeded. Uses AbortController + Promise.race internally.

resolvePartitionKey()

Resolve a partition key strategy to a concrete string value.

buildStorageKey()

Build a namespaced storage key from components.
Examples:

Error Classes

All guard errors extend GuardError, which has code (string) and statusCode (number) properties.

ExecutionTimeoutError

Thrown when execution exceeds the configured timeout.

ConcurrencyLimitError

Thrown when no concurrency slot is available and queueTimeoutMs is 0.

QueueTimeoutError

Thrown when a queued request exceeds its wait time.

IpBlockedError

Thrown when a client IP matches the deny list.

IpNotAllowedError

Thrown when a client IP is not on the allow list and defaultAction is 'deny'.

Zod Schemas

Validation schemas for all configuration types. Useful for validating user-provided configuration.