Configuration
This guide covers advanced configuration options, deployment strategies, and best practices for production MCP servers.Server Configuration
Basic Configuration
The server accepts configuration during initialization:Environment Variables
Use environment variables for configuration:Configuration File
Load configuration from JSON or YAML:Express Middleware Configuration
CORS Configuration
Configure Cross-Origin Resource Sharing:Rate Limiting
Implement rate limiting for API protection:Authentication Middleware
Add authentication to your server:Logging Configuration
Set up comprehensive logging:Security Configuration
DNS Rebinding Protection
The server supports DNS rebinding protection through theallowedOrigins configuration option. This is important for security when exposing MCP servers over HTTP.
Development vs Production Behavior:
-
Development mode (NODE_ENV !== “production”):
- If
allowedOriginsis not set: All origins are allowed (DNS rebinding protection disabled) - This enables direct browser connections from any origin for easier development
- Perfect for local development with tools like the MCP Inspector
- If
-
Production mode (NODE_ENV === “production”):
- If
allowedOriginsis not set: DNS rebinding protection is disabled (not recommended) - If set to empty array: DNS rebinding protection is disabled
- If set with origins: DNS rebinding protection is enabled with those specific origins
- Always explicitly set allowed origins in production
- If
- Always set
allowedOriginsin production - Never leave it undefined in production environments - Use environment variables - Store allowed origins in environment variables, not in code
- Be specific - Only include origins that actually need access
- Include protocol - Always specify the full origin including protocol (http/https)
- Test in production mode - Test your server with
NODE_ENV=productionto ensure proper origin validation
/mcp and /sse for compatibility. Both endpoints respect the same allowedOrigins configuration.
Session Management
The server manages client sessions to maintain state across multiple requests. Sessions are stored in memory and can be configured with idle timeouts and reconnection behavior. Session Configuration Options:sessionIdleTimeoutMs: Controls how long a session remains active when idle (default: 5 minutes)autoCreateSessionOnInvalidId: Controls behavior when a client sends a request with an invalid or expired session ID
InitializeRequest when they receive HTTP 404 in response to a request containing an MCP-Session-Id. However, some clients (like ChatGPT) don’t properly implement this behavior and fail to reconnect.
-
autoCreateSessionOnInvalidId: true(default):- Enables seamless reconnection after server restarts
- Compatible with non-compliant clients like ChatGPT
- Clients can continue using old session IDs (server auto-creates new session)
- Recommended for most use cases, especially when using ChatGPT or other OpenAI clients
-
autoCreateSessionOnInvalidId: false:- Follows MCP protocol specification strictly
- Clients must explicitly send
initializerequest after server restart - More predictable behavior for protocol-compliant clients
- Use only if you’re certain all clients properly handle 404 errors by reinitializing
- Use default behavior (true) in most cases - Provides compatibility with ChatGPT and other common clients
- Set to false only for compliant clients - Only disable if you’re certain all clients properly reinitialize on 404
- Monitor session creation - Log warnings when auto-creating sessions to detect connection issues
- Set appropriate timeouts - Configure
sessionIdleTimeoutMsbased on your application’s needs - Reference the MCP spec - Understand that clients should reinitialize on 404 per the MCP protocol specification
Input Validation
Validate and sanitize all inputs:Secure Headers
Add security headers:Performance Configuration
Caching
Implement caching for expensive operations:Database Connection Pooling
Configure database connections efficiently:Deployment Configuration
Docker Configuration
Create a Dockerfile for containerization:PM2 Configuration
Use PM2 for process management:Kubernetes Configuration
Deploy to Kubernetes:Monitoring and Metrics
Health Check Endpoint
Implement comprehensive health checks:Metrics Collection
Collect and expose metrics:Client Configuration
MCP Client Setup
Configure MCP clients to connect to your server:Best Practices
- Use Environment Variables: Never hardcode secrets
- Implement Health Checks: Monitor service health
- Add Logging: Log all important operations
- Rate Limiting: Protect against abuse
- Input Validation: Always validate user input
- Error Handling: Graceful error recovery
- Caching: Cache expensive operations
- Security Headers: Use Helmet.js
- Process Management: Use PM2 or similar
- Monitoring: Implement metrics and alerts
Next Steps
- Getting Started - Initial setup
- API Reference - Complete API docs
- Examples - Real-world implementations
- UI Widgets - Widget configuration