Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Store & Rotate API Keys for MCP Servers

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to securely store and rotate API keys used by Model Context Protocol (MCP) servers. You’ll learn a practical pattern that keeps keys out of code and configuration files, automates rotation, and minimizes downtime when a key is compromised or expires. By the end, you’ll have a repeatable process that works for most MCP server setups, whether you’re running them locally or in the cloud.

What You'll Need

  • Access to the MCP server’s configuration file (e.g., mcp.json, claude_desktop_config.json, or a custom config).
  • A secret manager: at minimum a local file with restricted permissions, but ideally a tool like AWS Secrets Manager, HashiCorp Vault, or a password manager with a CLI.
  • The ability to generate a new API key from the provider’s dashboard or CLI (e.g., OpenAI, Anthropic, GitHub, or your own backend).
  • Basic familiarity with environment variables and command-line tools.

Step-by-Step

  1. Identify every place the API key is used. Search your MCP server configuration files, startup scripts, and any code that references the key. Make a list so you don’t miss any during rotation.
  2. Move the key out of plain text. If the key is currently hardcoded in a config file or code, replace it with a placeholder like ${MCP_API_KEY} or env:MCP_API_KEY (depending on your MCP client). This forces the server to read the key from the environment.
  3. Store the key in a secret manager. Create a secret entry for the key. For example, with AWS Secrets Manager: aws secretsmanager create-secret --name mcp/api-key --secret-string "YOUR_KEY". For local use, store it in a file with permissions set to 600 (only your user can read/write).
  4. Load the key into the environment. In your MCP server’s startup script (e.g., a systemd service or a shell script), fetch the key from the secret manager and export it as an environment variable. Example: export MCP_API_KEY=$(aws secretsmanager get-secret-value --secret-id mcp/api-key --query SecretString --output text). For local testing, you can source a file that contains export MCP_API_KEY=....
  5. Test that the MCP server works with the environment variable. Restart the MCP server and confirm it can connect to the provider. Make a test request to verify the key is being read correctly.
  6. Set up a rotation schedule. Decide how often to rotate. Common choices: every 30, 60, or 90 days, or immediately if a key is leaked. Mark the date on your calendar or use a cron job to remind you.
  7. Create a new key. When it’s time to rotate, generate a new key from the provider’s dashboard. Do not delete the old key yet.
  8. Update the secret manager. Replace the stored secret value with the new key. If using AWS Secrets Manager, you can update the secret: aws secretsmanager update-secret --secret-id mcp/api-key --secret-string "NEW_KEY".
  9. Restart the MCP server. Restart the server process so it picks up the new environment variable. Verify that the server still works.
  10. Deactivate the old key. After confirming the new key works, go to the provider’s dashboard and revoke/delete the old key. This prevents any leaked copies from being used.
  11. Document the rotation. Note the date of rotation, the key’s expiration (if any), and any issues encountered. This helps future rotations go smoothly.

Common Mistakes

  • Hardcoding keys in config files that are committed to version control. Even if you remove them later, they remain in git history. Use environment variables or secret files from the start.
  • Rotating without testing the new key first. Always verify the new key works before deactivating the old one, or you’ll face downtime.
  • Using the same key for multiple MCP servers. If one server is compromised, all are. Give each server its own key if possible.
  • Forgetting to update all copies of the key. If you have the key in multiple places (e.g., a local file and a CI/CD pipeline), you must update all of them.
  • Ignoring key expiration dates. Some providers set expiration. Set a reminder well before the expiry.

Tips

  • Use a dedicated secret manager like Vault or AWS Secrets Manager. They often have built-in rotation features that can automate the whole process.
  • Set short expiration times on keys if the provider supports it. This forces regular rotation and limits the blast radius of a leak.
  • Use different keys for development and production. This way, a dev key leak doesn’t affect production.
  • Log rotation events (without the key itself) so you have an audit trail.
  • Automate with a script that generates a new key, updates the secret, and restarts the service. Even a simple bash script saves time and reduces errors.

Troubleshooting

  • The MCP server fails to start after moving to environment variables. Check that the environment variable is actually set. Run echo $MCP_API_KEY in the same shell that starts the server. Also verify the variable name matches exactly what the server expects.
  • The server starts but returns authentication errors. The key may be malformed or have extra whitespace. Try echoing the variable and comparing it to the key in the provider dashboard. Also ensure the key hasn’t been revoked.
  • After rotation, the old key still works. Some providers have a grace period. Wait a few minutes and try again. If it persists, you may have missed a copy of the key in another config file.
  • Secret manager access fails in a script. Check IAM permissions or the local file permissions. Ensure the script runs as a user with read access to the secret.
  • The MCP client (e.g., Claude Desktop) caches the old key. Restart the client application completely, not just the server. Sometimes the client holds the connection open.

Frequently Asked Questions

Can I store API keys in a .env file?

Yes, for local development a .env file that is not committed to version control is acceptable. Ensure the file has restrictive permissions (e.g., chmod 600 .env). For production, use a proper secret manager.

How often should I rotate API keys?

There’s no one-size-fits-all. A common practice is every 30–90 days, or immediately if you suspect a leak. Check your provider’s recommendations; some keys have a maximum lifetime.

What if my MCP server doesn’t support environment variables?

Most do, but if not, you can use a configuration file that is read at runtime. Store the key in a separate file with restricted permissions and reference it from the config, or patch the server to support env vars.

Can I use the same API key for multiple MCP servers?

Technically yes, but it’s not recommended. If one server is compromised, all are. Use a separate key per server to isolate risk.

What happens if I rotate a key and the new one doesn’t work?

You should have kept the old key active. Revert the secret to the old key, restart the server, and troubleshoot why the new key failed (e.g., wrong permissions, typo). Only deactivate the old key after the new one is verified.

Is it safe to store API keys in a password manager?

Yes, for personal use. Password managers with CLI access (like Bitwarden or 1Password) can be used to retrieve keys in scripts. Just ensure the script doesn’t log the key.