Overview
The Secure MCP Gateway acts as a security middleware that sits between MCP clients (like Claude Desktop or Cursor) and MCP servers. By design, it functions as both:- An MCP Server - to clients requesting tools and resources
- An MCP Client - to actual MCP servers being proxied
High-Level Architecture
Core Components
Gateway Server
Location:src/secure_mcp_gateway/gateway.py:796
The main entry point that runs as a FastMCP server:
enkrypt_list_all_servers- List available MCP serversenkrypt_get_server_info- Get server details and toolsenkrypt_discover_all_tools- Discover tools from serversenkrypt_secure_call_tools- Execute tools with guardrailsenkrypt_get_cache_status- View cache statisticsenkrypt_clear_cache- Clear cached dataenkrypt_get_timeout_metrics- Monitor timeout performance
Gateway Client
Location:src/secure_mcp_gateway/client.py:294
Forwards requests to actual MCP servers via stdio transport:
Plugin System
Architecture: Provider-based plugin system for extensibilityAuthentication Plugins
Location:src/secure_mcp_gateway/plugins/auth/
- Base:
AuthProviderabstract class - Providers:
local_apikey- Validates API keys from local configenkrypt- Remote authentication via Enkrypt API
Guardrail Plugins
Location:src/secure_mcp_gateway/plugins/guardrails/
- Base:
GuardrailProviderabstract class - Providers:
enkrypt- Production Enkrypt guardrail APIopenai- OpenAI moderation APIcustom_keyword- Simple keyword blocking
- Input validation (PII, toxicity, injection attacks)
- Output validation (relevancy, adherence, hallucination)
- PII redaction and de-anonymization
Telemetry Plugins
Location:src/secure_mcp_gateway/plugins/telemetry/
- Base:
TelemetryProviderabstract class - Providers:
opentelemetry- Full OpenTelemetry with OTLP exportstdout- Simple stdout logging
Service Layer
The gateway uses a service-oriented architecture to separate concerns:Cache Services
Location:src/secure_mcp_gateway/services/cache/
- cache_service.py - Core cache operations
- cache_management_service.py - Clear cache functionality
- cache_status_service.py - Cache statistics
Discovery Service
Location:src/secure_mcp_gateway/services/discovery/discovery_service.py
Execution Services
Location:src/secure_mcp_gateway/services/execution/
- secure_tool_execution_service.py - Executes tools with guardrails
- tool_execution_service.py - Basic execution without guardrails
OAuth Service
Location:src/secure_mcp_gateway/services/oauth/
Complete OAuth 2.0/2.1 implementation:
- oauth_service.py - Core OAuth flow (client credentials, authorization code)
- token_manager.py - Token caching with automatic refresh
- integration.py - Injects OAuth tokens into server connections
- validation.py - Scope and security validation
- Client credentials grant
- Mutual TLS (mTLS) support (RFC 8705)
- Automatic token caching and refresh
- Proactive refresh (5 min before expiry)
- Exponential backoff retry logic
Server Services
Location:src/secure_mcp_gateway/services/server/
- server_listing_service.py - List all configured servers
- server_info_service.py - Get detailed server information
Timeout Management
Location:src/secure_mcp_gateway/services/timeout/timeout_manager.py
Tracks operation timeouts and escalations:
default_timeout: 30sguardrail_timeout: 15sauth_timeout: 10stool_execution_timeout: 60sdiscovery_timeout: 20scache_timeout: 5sconnectivity_timeout: 2s
REST API Server
Location:src/secure_mcp_gateway/api_server.py:1049
FastAPI-based REST API for programmatic management:
- Port: 8001
- Auth: Bearer token (admin API key)
- Endpoints:
/api/v1/configs/*- Configuration management/api/v1/projects/*- Project management/api/v1/users/*- User management/api/v1/api-keys/*- API key management/api/v1/system/*- System operations/health- Health check (no auth required)
Data Flow
1. Client Connection
Client connects via stdio or HTTP with API key in request context.2. Authentication
Gateway extracts credentials and validates via auth plugin:3. Configuration Loading
Retrieves gateway config (cached if available):4. Tool Discovery or Execution
Discovery:5. Guardrail Checks
Input Guardrails:- PII detection and redaction
- Injection attack prevention
- Topic/NSFW/toxicity detection
- Policy violation checking
- All input checks
- Relevancy validation
- Adherence checking
- Hallucination detection
- PII de-anonymization
6. Response Return
Processed response returns to client with telemetry logged.Transport Modes
stdio Mode
Default for local MCP clients like Claude Desktop:Streamable HTTP Mode
For remote deployments:Security Architecture
API Key Authentication
All requests require a valid API key mapped to a project and user:Sensitive Data Masking
Environment Variables:src/secure_mcp_gateway/utils.py:458
Sensitive keys (token, key, secret, password, auth) are automatically masked:
src/secure_mcp_gateway/utils.py:546
Authorization headers are masked in logs.
Cache Key Hashing
All cache keys are MD5 hashed to prevent exposure:Observability
Structured Logging
All operations include context:Distributed Tracing
OpenTelemetry spans track operations:Metrics
Prometheus-compatible metrics exported:- Request counts
- Error rates
- Latencies
- Cache hit/miss rates
- Guardrail block counts
Scalability
Horizontal Scaling
Use external cache (Redis/KeyDB) for multi-instance deployments:Cache Expiration
- Tool cache: 4 hours (configurable)
- Gateway config cache: 24 hours (configurable)
Next Steps
How It Works
Learn the detailed request flow and step-by-step process
Configuration
Understand the configuration system and all available settings
Authentication
Deep dive into authentication mechanisms and API keys
Deployment
Deploy the gateway to production environments