Guard is powered by the
@frontmcp/guard library and integrates directly into tool and agent flows. All guard checks run automatically before execution, with cleanup handled in finalize stages.Why Guard?
Quick Start
Add rate limiting and a timeout to any tool with decorator options:How Guard Integrates with Flows
Guard checks are implemented as flow stages that run automatically in the tool and agent execution pipelines:- acquireQuota — Checks global and per-entity rate limits. Throws
RateLimitErrorif exceeded. - acquireSemaphore — Acquires a concurrency slot. Throws
ConcurrencyLimitErrorif no slot available. - execute — Wrapped with
withTimeoutif a timeout is configured. ThrowsExecutionTimeoutErrorif exceeded. - releaseSemaphore — Releases the concurrency slot back to the pool.
- releaseQuota — Cleans up rate limit state.
Rate Limiting
FrontMCP uses a sliding window algorithm for rate limiting. It provides smooth, accurate throttling with O(1) storage per key.Per-Tool Rate Limiting
Global Rate Limiting
Set a server-wide rate limit in your app configuration:Partition Strategies
Partition keys determine how rate limits are bucketed:
Custom partition key example:
Concurrency Control
Concurrency control uses a distributed semaphore to limit how many instances of a tool or agent can execute simultaneously.Per-Tool Concurrency
Mutex Pattern
SetmaxConcurrent: 1 to ensure only one execution at a time:
Queue Behavior
WhenqueueTimeoutMs is set, requests that cannot acquire a slot immediately will wait in a queue:
queueTimeoutMs: 0(default) — Immediately reject if no slot available. ThrowsConcurrencyLimitError.queueTimeoutMs: 5000— Wait up to 5 seconds for a slot. ThrowsQueueTimeoutErrorif the wait expires.
Execution Timeout
Timeout wraps theexecute stage with a deadline. If execution exceeds the configured duration, it throws ExecutionTimeoutError.
Per-Tool Timeout
Default Timeout
Set a default timeout for all tools and agents at the app level:IP Filtering
IP filtering allows or blocks requests based on client IP address, supporting IPv4, IPv6, and CIDR ranges.Filter Precedence
- Deny list is checked first. If matched, the request is blocked with
IpBlockedError(403). - Allow list is checked next. If matched, the request proceeds.
- Default action applies if neither list matches:
'allow'(default) — Request proceeds.'deny'— Request is blocked withIpNotAllowedError(403).
Supported IP Formats
Proxy Configuration
When your server is behind a reverse proxy (Nginx, CloudFront, etc.), enabletrustProxy to read the client IP from the X-Forwarded-For header:
App-Level Configuration
Thethrottle field in @FrontMcp configures all guard features at the app level:
Configuration Precedence
Storage Backends
Guard supports multiple storage backends for distributed deployments.Memory (Development)
The default backend. Suitable for single-instance development. No configuration needed.Redis (Production)
For distributed rate limiting across multiple server instances:Vercel KV / Upstash
For serverless environments:Error Handling
Guard throws specific error classes when limits are exceeded:
These errors are automatically serialized to appropriate MCP error responses by the transport layer.
Agent Guard
Agents support the same guard options as tools:acquireQuota → acquireSemaphore → execute (with timeout) → releaseSemaphore → releaseQuota.