@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.
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.
config.globalConcurrency configuration.
checkIpFilter()
Check if a client IP is allowed.
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.
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.
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.
ticketTtlSeconds: 300 (5 minutes).
acquire()
Acquire a semaphore slot.
- Returns
SemaphoreTicketif a slot is acquired. - Returns
nullifqueueTimeoutMs <= 0and no slot is available. - Throws
QueueTimeoutErrorif queued and timeout expires.
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.
check()
Check if a client IP is allowed.
- Deny list (takes precedence)
- Allow list
- 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:
- Creates storage backend from
config.storage(or in-memory if not set) - Connects the storage backend
- Creates namespaced storage with
config.keyPrefix - 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.
Error Classes
All guard errors extendGuardError, 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'.