Identify what each key protects
Do not treat every secret as the same credential.
Never forward a credential to a service for which it was not issued.
Prefer stronger credential options
Choose the strongest option the upstream system supports:- Workload identity or managed service identity
- Short-lived, scoped OAuth token
- Short-lived generated credential
- Rotatable static API key
Generate strong keys
If you issue API keys:- Use a cryptographically secure random generator.
- Generate enough entropy to resist guessing.
- Add a non-secret prefix that identifies the environment or key type.
- Show the full secret only once.
- Store only a secure hash when the server needs to verify, but not reuse, the key.
- Give each key a unique identifier for rotation and audit.
Store keys safely
Production
Prefer:- A cloud secret manager
- A dedicated vault
- A hardware-backed key service
- Workload identity that avoids stored keys
Local development
Environment variables or a client credential store can be acceptable when access is restricted. Do not commit.env files. Keep them out of logs, crash reports, shell history, and shared screenshots.
Avoid
- Source code
- Git history
- Container images
- Public client configuration
- Database rows in plaintext
- Tool descriptions or prompt templates
- Command-line arguments visible to other processes
- URLs and query strings
Scope keys narrowly
Use a separate key for each:- Environment
- Application or MCP server
- External provider
- Team or workload when practical
- Permission level
- Allowed operations
- Resources or accounts
- Source networks
- Domains or applications
- Usage volume
- Expiration date
Keep keys away from the model
The model should not need to see an API key. The MCP server or host should attach credentials after the model creates a structured tool request.Rotate API keys safely
A safe rotation avoids downtime:- Create a new key.
- Store it in the secret manager.
- Allow the application to accept or use both keys temporarily.
- Deploy the new configuration.
- Verify successful traffic with the new key.
- Revoke the old key.
- Remove the old value from all stores.
- Record the rotation event.
Monitor key usage
Track:- Key identifier, never the full key
- Timestamp
- Calling service
- Target API or operation
- Success or failure
- Source network when appropriate
- Rate and volume anomalies
- Use from an unexpected environment
- Sudden traffic spikes
- Repeated authorization failures
- Access to unusual endpoints
- Use of a retired key
- Changes to secret-manager permissions
Redact secrets from logs
Redact:Authorizationheaders- API-key headers
- Cookies
- Query parameters that may contain credentials
- Environment dumps
- Request and response payload fields marked as secrets
Respond to a leaked key
If a key may be exposed:- Revoke or disable it immediately.
- Issue a replacement with minimum scope.
- Search source control, CI logs, chat, tickets, and telemetry for exposure.
- Review usage records for unauthorized activity.
- Rotate related credentials if the affected system was compromised.
- Remove cached copies and update deployments.
- Document the incident and preventive changes.
Incoming API keys
If your MCP server accepts API keys from callers:- Transmit them only over HTTPS.
- Require a header, not a query parameter.
- Store a keyed hash rather than plaintext when possible.
- Compare values using constant-time logic.
- Associate each key with an owner and policy.
- Support expiration and revocation.
- Apply per-key rate limits.
- Avoid exposing whether a specific key identifier exists.
Upstream API keys in 0mcp
0mcp can forward an API key that the MCP client provides for your upstream API. The key is placed according to the authentication configuration for the selected source:- An OpenAPI 3.x or Swagger 2.0 security scheme
- A directly configured REST API
- A GraphQL API
API key checklist
- Every key has one documented purpose
- Production keys use a secret manager or workload identity
- Keys are separate by environment and service
- Permissions follow least privilege
- Keys never enter model context
- Logs redact credentials
- Rotation is automated and tested
- Revocation is immediate and documented
- Usage is monitored by key identifier
- Leak response has been rehearsed