MCP Server Security Best Practices: Protecting Your Data
Essential security guidelines for running MCP servers. Learn about credential management, access control, and data protection strategies.
Generate & Validate Multi-Client MCP Config
One-click export with environment variables & path locators for Claude Desktop, Cursor, Windsurf, and OpenAI Codex CLI.
MCP Server Security Best Practices
Because MCP servers act as bridges between intelligent, unpredictable AI agents and your sensitive data sources, security must be your primary concern. A rogue agent with a poorly configured MCP server can accidentally drop production databases, leak confidential API keys, or expose sensitive local files.
Prerequisites
- βΈFamiliarity with JSON configuration files.
- βΈBasic understanding of environment variables and your operating system's file permissions.
Principle of Least Privilege
Never give your AI agent "God Mode." Always grant the minimum permissions necessary for the specific task.
Database Servers
Do not connect the agent using a root or admin account. Create a read-only user specifically for the agent:
-- MySQL Example: Create read-only users for analysis tasks
CREATE USER 'mcp_readonly'@'localhost' IDENTIFIED BY 'strong_password';
GRANT SELECT ON analytics.* TO 'mcp_readonly'@'localhost';API Tokens
- βΈUse fine-grained tokens (GitHub, Slack, Notion) instead of legacy classic tokens.
- βΈSet expiration dates on all tokens used by MCP servers.
- βΈRequest only the necessary OAuth scopes (e.g.,
issues:readinstead ofrepo:write).
File System Security
- βΈLimit the Filesystem MCP server access to specific, isolated directories only.
- βΈNever grant access to root (
/), user home directories (~), or.sshfolders. - βΈUse read-only access flags if the agent only needs to analyze code, not write it.
Credential Management
Never hardcode credentials in configuration files that are committed to version control.
Use Environment Variables
Your MCP client configuration should inject credentials via environment variables:
{
"mcpServers": {
"database": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgresql://mcp_readonly:${DB_PASSWORD}@/db"
}
}
}
}Network Security & Sandboxing
- βΈLocal-Only Bindings: When possible, ensure your MCP servers and target databases bind only to
127.0.0.1(localhost) so they cannot be accessed over the external network. - βΈContainer Isolation: Run MCP servers in Docker containers without
--privilegedmode to heavily restrict OS-level access. - βΈFirewall Rules: Restrict outbound connections from the MCP servers. If the server only needs to talk to a local database, block its internet access.
Data Classification Strategy
Before connecting a data source to an MCP client, classify the data and apply the appropriate security posture:
| Level | Examples | Recommendation |
|---|---|---|
| Public | Documentation, public APIs | Safe to connect with minimal restrictions |
| Internal | Project source code, analytics | Use strict read-only access |
| Confidential | Customer data, financials | Requires careful access control, auditing, and anonymization |
| Restricted | PII, health records, secrets | Avoid MCP access entirely |
Troubleshooting & Incident Response
Have a plan for security incidents involving your AI agents:
- βΈMonitor Logs: Keep strict logs of the JSON-RPC traffic. If you see the agent hallucinating calls like
execute_sql("DROP TABLE users"), immediately investigate. - βΈRevoke Keys: If you suspect an agent leaked data to a public LLM provider, instantly revoke the API tokens used in the
envblock. - βΈAudit: Review the agent's chat history. LLMs leave a perfect audit trail of their "thought process" and the exact tools they invoked.
Build your full agent toolstack in the Visual Generator
Combine MCP Server Security Best Practices: Protecting Your Data with databases, search APIs, and memory graphs in a single configuration file.
Related Guides
How to Set Up Your First MCP Server in 5 Minutes
A quick-start guide to installing and configuring your very first MCP server with Claude Desktop. Get up and running in minutes.
Getting StartedWhat Is the Model Context Protocol (MCP)? A Complete Introduction
Learn the fundamentals of MCP β what it is, why it matters, and how it enables AI agents to connect with external tools and data sources through a standardized protocol.
Getting StartedHow to Configure MCP Servers in OpenAI Codex CLI (2026 Guide)
Step-by-step config.toml guide and CLI commands for connecting MCP servers to OpenAI Codex CLI.