Overview
Proper environment configuration is critical for maintaining consistency across development, staging, and production environments while keeping sensitive data secure. This guide covers configuration strategies, environment management, and the 12-factor app methodology.Environment-based configuration enables seamless deployment across multiple environments while maintaining security and flexibility.
Configuration Strategies
12-Factor App Principles
The MCP Server follows the 12-Factor App methodology for configuration:Environment Variables
- Store config in environment
- Never commit secrets to git
- Strict separation of config and code
- Different values per environment
Backing Services
- Attached resources via URLs
- Swap services without code changes
- Database URLs, cache URLs, etc.
- Service discovery via environment
Build, Release, Run
- Separate build and run stages
- Config injected at runtime
- Immutable releases
- Rollback capability
Port Binding
- Export services via port binding
- Completely self-contained
- No runtime injection of webserver
- Configurable port via environment
Environment Types
Development Environment
Purpose: Local development and testing Configuration:- Fast feedback loops
- Verbose logging
- In-memory services
- Mock external dependencies
- Hot reload enabled
Staging Environment
Purpose: Pre-production testing and validation Configuration:- Production-like configuration
- Real backing services
- Monitoring enabled
- Sanitized production data
- Load testing environment
Production Environment
Purpose: Live user-facing deployment Configuration:- Maximum security
- High availability
- Performance optimized
- Full observability
- Strict rate limiting
Environment Variable Management
Required Variables
string
required
Environment name:
development, staging, or productionstring
required
Authentication provider:
inmemory or keycloakstring
required
Session storage:
memory or redisstring
required
LLM provider:
anthropic, openai, google, or ollamaOptional Variables
boolean
default:"false"
Enable debug mode (verbose logging, tracebacks)
string
default:"info"
Logging level:
debug, info, warning, error, criticalinteger
default:"8000"
HTTP server port
integer
default:"4"
Number of worker processes (production)
Provider-Specific Variables
Keycloak:Configuration Files
.env File Structure
.env.example Template
Create a template for new developers:.gitignore
CRITICAL: Never commit secrets to git:Infisical Integration
Centralized Secret Management
Use Infisical to manage secrets across environments:Environment-Specific Secrets
Development:Docker Configuration
docker-compose.yml
Environment-based configuration with Docker Compose:Multi-Stage Dockerfile
Build once, configure at runtime:Kubernetes Configuration
ConfigMaps
Store non-sensitive configuration:Secrets
Store sensitive data in Kubernetes Secrets:External Secrets Operator
Sync from Infisical automatically:Deployment with Environment Config
Configuration Validation
Pydantic Settings
Use Pydantic for type-safe configuration:Startup Validation
Validate configuration on application startup:Environment-Specific Features
Feature Flags
Enable features based on environment:Best Practices
Never Commit Secrets
Never Commit Secrets
Always use .gitignore:Use secret scanning:
Environment Parity
Environment Parity
Keep dev, staging, and production as similar as possible:Same backing services:
- Development: Docker Compose
- Staging: Kubernetes (minikube/kind)
- Production: Kubernetes (GKE/EKS/AKS)
- All environments use same .env format
- Same ConfigMap structure
- Same secret keys
Validation First
Validation First
Validate configuration before starting:
Use Infisical/Vault
Use Infisical/Vault
Centralize secret management:Benefits:
- Automatic secret rotation
- Audit logging
- Access control
- Version history
- Emergency revocation
Troubleshooting
Environment variables not loaded
Environment variables not loaded
Problem: Settings show default values instead of .env valuesSolutions:
Configuration validation fails
Configuration validation fails
Problem: Application exits on startupSolutions:
Kubernetes ConfigMap not updating
Kubernetes ConfigMap not updating
Problem: Pods use old configurationSolutions:
Next Steps
Infisical Setup
Centralized secret management
Secret Rotation
Automated secret rotation
Kubernetes Deployment
Deploy to Kubernetes
Production Checklist
Pre-deployment security
Environment Configuration Ready: Secure, validated configuration across all environments!