Skip to main content

Overview

Secure MCP Gateway implements a sophisticated caching system to improve performance and reduce latency. The cache system supports both local in-memory caching and external distributed caching using Redis or KeyDB.

Cache Types

The gateway caches three main types of data:

Tool Cache

Caches discovered tools from MCP servers (4 hours default)

Gateway Config

Caches user gateway configurations (24 hours default)

OAuth Tokens

Caches OAuth access tokens until expiry

Cache Architecture

Configuration

Local Cache (Default)

enkrypt_mcp_config.json

External Cache (Redis/KeyDB)

enkrypt_mcp_config.json

CacheService Implementation

Initialization

src/secure_mcp_gateway/services/cache/cache_service.py

Cache Operations

Tool Caching

src/secure_mcp_gateway/client.py

Tool Retrieval

src/secure_mcp_gateway/client.py

Gateway Config Caching

src/secure_mcp_gateway/client.py

Cache Management

Cache Status Service

src/secure_mcp_gateway/services/cache/cache_status_service.py

Cache Clear Service

src/secure_mcp_gateway/services/cache/cache_management_service.py

Cache CLI Commands

Cache MCP Tools

The gateway exposes cache management via MCP tools:

Performance Optimization

Cache Hit Ratio

Monitor cache effectiveness:

Preloading Cache

For frequently used servers, preload the cache:

External Cache Benefits

Multi-Instance

Shared cache across multiple gateway instances

Persistence

Survives gateway restarts with Redis persistence

Scalability

Better performance under high load

Distributed

Cache accessible from multiple data centers

Security Considerations

Key Hashing: All cache keys are MD5 hashed to prevent exposure of sensitive identifiers in cache storage.
Token Security: OAuth tokens in cache are in-memory only by default. For external cache, consider encryption.
Cache Expiration: Set appropriate expiration times based on your server update frequency and security requirements.

Docker Compose with KeyDB

docker-compose.yml

Monitoring Cache Performance

View cache metrics in Grafana:
  • Cache hit/miss ratio over time
  • Cache size and memory usage
  • Average cache lookup latency
  • Cache eviction rate