What You’ll Learn
By the end of this guide, you’ll know how to:- ✅ Enable caching for specific tools
- ✅ Configure TTL (time-to-live) per tool
- ✅ Use sliding windows to keep hot data cached
- ✅ Switch between memory and Redis storage
- ✅ Handle cache misses and invalidation
Prerequisites
- A FrontMCP project with at least one app and tool
- Understanding of tool execution flow
- (Optional) Redis server for production caching
Step 1: Install the Cache Plugin
Step 2: Add Plugin to Your App
Step 3: Enable Caching on Tools
Caching is opt-in per tool. Add thecache field to your tool metadata:
- Class-based Tool
- Function-based Tool
- Custom TTL
- With Sliding Window
Step 4: Test Cache Behavior
1
Start your server
2
Call the tool twice
Use the MCP Inspector or a client to call your cached tool twice with the same input:The first call executes the tool normally (cache miss).
3
Observe cache hit
The second call returns instantly from cache! Check your logs for:
[DEBUG] Cache hit for get-user-profile4
Test cache expiration
Wait for the TTL to expire, then call again. The cache will miss and the tool will execute.
How Caching Works
1
Cache Key Generation
When a tool is called, the plugin creates a deterministic hash from:
- Tool name (e.g.,
get-user-profile) - Validated input (e.g.,
{ userId: "user-123" })
2
Before Execution (Will Hook)
The plugin checks the cache store for the key: - Cache Hit: Return cached result immediately, skip execution -
Cache Miss: Allow tool to execute normally
3
After Execution (Did Hook)
If the tool executed, the plugin stores the result in the cache with the configured TTL
4
Sliding Window (Optional)
If
slideWindow: true, each cache read refreshes the TTL, keeping popular data cached longerThe cache operates at the hook level, so it works transparently without modifying your tool code.
Configuration Options
Tool-Level Cache Options
Enable caching for this tool
true- Use plugin’s default TTL{ ttl, slideWindow }- Custom configuration
Time-to-live in seconds. Overrides plugin’s
defaultTTL.Examples:60- 1 minute300- 5 minutes3600- 1 hour86400- 1 day
When
true, reading from cache refreshes the TTL
Use cases:- Trending/popular data
- Frequently accessed reports
- User dashboards
Common Patterns
Pattern 1: Fast-Changing Data (Short TTL)
Pattern 1: Fast-Changing Data (Short TTL)
For data that changes frequently:
Pattern 2: Expensive Reports (Long TTL)
Pattern 2: Expensive Reports (Long TTL)
For computationally expensive operations:
Pattern 3: Hot Data with Sliding Window
Pattern 3: Hot Data with Sliding Window
For frequently accessed data:
Pattern 4: Multi-Tenant Isolation
Pattern 4: Multi-Tenant Isolation
Include tenant ID in input for automatic isolation:Each tenant’s data is cached separately!
Memory vs Redis
When to Use Memory Cache
Development
Perfect for local development and testing
Single Instance
When running one server instance
Non-Critical Data
Data loss on restart is acceptable
Simple Setup
No external dependencies needed
When to Use Redis
Production
Recommended for production deployments
Multi-Instance
Cache shared across multiple server instances
Persistence
Cache survives server restarts
Better Eviction
Redis handles memory limits gracefully
Redis provides persistence, sharing, and better memory management for production use.
Troubleshooting
Cache not working
Cache not working
Checklist:
- Tool has
cache: trueorcache: { ... }in metadata - Plugin is registered in app’s
pluginsarray - Redis is running (if using Redis backend)
- No errors in server logs
Stale data being returned
Stale data being returned
Problem: Cache TTL is too long for your data freshness requirements.Solution: Reduce the TTL:
Cache not shared across instances
Cache not shared across instances
Non-deterministic tools being cached
Non-deterministic tools being cached
Problem: Tool output varies even with same input (e.g., returns current timestamp).Solution: Don’t cache non-deterministic tools:
Best Practices
1. Only Cache Deterministic Tools
1. Only Cache Deterministic Tools
Cache tools where the same input produces the same output:✅ Good candidates:
- Database queries by ID
- API calls with stable responses
- Report generation
- Static data lookup
- Tools that return current time/date
- Tools with random output
- Tools with side effects (mutations)
2. Choose Appropriate TTLs
2. Choose Appropriate TTLs
Match TTL to data change frequency:
| Data Type | Suggested TTL |
|---|---|
| Real-time prices | 5-10 seconds |
| User profiles | 5-15 minutes |
| Reports | 30 minutes - 1 hour |
| Static content | Hours to days |
3. Include Scoping Fields
3. Include Scoping Fields
Always include tenant/user IDs in inputs:
4. Use Redis for Production
4. Use Redis for Production
Redis provides:
- Persistence across restarts
- Sharing across instances
- Better memory management
- Monitoring and debugging tools
5. Monitor Cache Performance
5. Monitor Cache Performance
Enable debug logging to see cache hits/misses:Look for:
- High miss rates (TTL too short? Tool not deterministic?)
- Memory growth (TTL too long?)

