Skip to main content
Security is crucial when working with MCP servers and LLM agents. This guide covers best practices for protecting your applications, data, and infrastructure.
Important: MCP servers can have powerful capabilities including file system access, network requests, and code execution. Always follow security best practices to protect your systems.

API Key Management

Environment Variables

Never hardcode API keys in your source code. Use environment variables:

.env File Security

Create a secure .env file:
.env
Never commit .env files: Add .env to your .gitignore file to prevent accidentally committing API keys to version control.

Secrets Management

For production environments, use proper secrets management:

MCP Server Security

Filesystem Server Security

When using filesystem servers, restrict access to safe directories:
secure_filesystem_config.json

Network Access Restrictions

Limit network access for web-based MCP servers:
secure_network_config.json

Database Security

Secure database connections with proper credentials and restrictions:
secure_database_config.json

Agent Security Configuration

Restrict Tool Access

Limit which tools the agent can use:
secure_agent.py

Input Validation

Validate user inputs before processing:
input_validation.py

Rate Limiting

Implement rate limiting to prevent abuse:
rate_limiting.py

Logging and Monitoring

Security Logging

Implement comprehensive security logging:
security_logging.py

Monitoring Dashboard

Create monitoring for security events:
monitoring.py

Production Deployment Security

Container Security

Use secure container configurations:
Dockerfile

Network Security

Configure network policies and firewalls:
kubernetes_network_policy.yaml

Security Checklist

  • API keys stored in environment variables or secrets manager
  • No hardcoded credentials in source code
  • .env files added to .gitignore
  • Regular API key rotation implemented
  • Least privilege access for API keys
  • Filesystem access restricted to safe directories
  • Network access limited to necessary domains
  • Database connections use read-only accounts where possible
  • Input validation on all server parameters
  • Resource limits configured (timeouts, file sizes, etc.)
  • Tool access restricted using allowed/disallowed lists
  • Maximum execution steps limited
  • Timeouts configured for agent operations
  • Input validation implemented
  • Rate limiting in place
  • Security events logged
  • Monitoring dashboard configured
  • Alerting set up for security violations
  • Log retention policies in place
  • Regular security audits scheduled

Common Security Vulnerabilities

Path Traversal Prevention

Command Injection Prevention

Next Steps

Client Configuration

Learn secure configuration practices for MCP clients

Deployment Guide

Best practices for secure production deployment

Agent Configuration

Debug security-related issues and errors
Security is an ongoing process. Regularly review and update your security practices, monitor for new vulnerabilities, and keep all dependencies up to date.