Skip to main content
This guide walks through adding production-grade traffic controls to your FrontMCP server using the Guard system.
Prerequisites: You should have a working FrontMCP server with at least one tool. See Your First Tool if you need to get started.

What You’ll Build

By the end of this guide, your server will have:
  • Per-user rate limiting on tools
  • Concurrency control to prevent resource exhaustion
  • Execution timeouts to catch hanging requests
  • IP filtering for production security
  • Redis-backed distributed rate limiting

Step 1: Add Rate Limiting to a Tool

1

Configure rate limiting on a tool

Add a rateLimit option to your tool decorator:
This limits each user to 30 search requests per minute.
2

Register the tool and enable guard

Enable the guard system in your app configuration:
Setting throttle.enabled: true is required. Without it, rate limit decorators on tools are ignored.
3

Test the rate limit

Start your server and send rapid requests. After 30 requests within a minute, the server returns a 429 error:

Step 2: Add Concurrency Control

Prevent expensive tools from running too many instances simultaneously.
1

Add concurrency to a tool

This allows at most 2 report generations at once. Additional requests wait up to 15 seconds for a slot.
2

Understand queue behavior

When all slots are occupied:
  • With queueTimeoutMs: 0 (default), the request is immediately rejected with ConcurrencyLimitError (429).
  • With queueTimeoutMs: 15_000, the request waits up to 15 seconds. If a slot opens, it proceeds. If not, it fails with QueueTimeoutError (429).
For mutex-like behavior (only one execution at a time), set maxConcurrent: 1:

Step 3: Add Execution Timeout

Protect against hanging requests by setting a maximum execution time.
1

Add timeout to a tool

If execution takes longer than 30 seconds, it throws ExecutionTimeoutError (408).
2

Set a default timeout for all tools

Instead of adding timeout to every tool, set a default at the app level:
Tools with their own timeout override the default. Tools without timeout use the app default.

Step 4: Global Rate Limiting

Add a server-wide rate limit that applies to all requests, regardless of which tool is called.
1

Configure global limits

Global limits are checked before per-tool limits. Both must pass for a request to proceed.
2

Combine with per-tool limits

Global and per-tool limits work independently. A tool can have its own stricter limit:
Even if the global limit allows 500 requests/min per IP, this tool is limited to 5 requests/min per user.

Step 5: IP Filtering

Block malicious IPs and restrict access to known networks.
1

Configure IP filtering

2

Understand filter precedence

The deny list is always checked first:
  1. IP on deny list → blocked (403, IpBlockedError)
  2. IP on allow list → allowed
  3. IP on neither list → defaultAction applies ('allow' or 'deny')
With defaultAction: 'deny', only IPs explicitly on the allow list can access your server.
3

Enable proxy trust

If your server is behind a load balancer or reverse proxy, the client IP will be the proxy’s IP unless you enable trustProxy:

Step 6: Production Setup with Redis

In-memory storage works for development but does not persist across restarts or share state between server instances. Use Redis for production.
1

Configure Redis storage

All rate limit counters and semaphore tickets are stored in Redis, shared across all server instances.
2

Verify distributed behavior

With Redis storage:
  • Rate limit counters are shared across instances — a user hitting different instances still sees a single limit.
  • Semaphore tickets use atomic operations — concurrency is enforced globally.
  • Pub/sub notifications make semaphore slot release detection near-instant.
For serverless environments (Vercel, AWS Lambda), use Vercel KV or Upstash:

Testing Guard Behavior

Test that your guards work correctly using the FrontMCP testing utilities.

Testing Rate Limits

Testing Concurrency Limits

Testing Timeout


Complete Example

Here is a full app with all guard features enabled: