---
title: How should MCP servers store API keys and secrets?
url: https://opencontroller.lyzr.com/blog/store-secrets-for-mcp-servers-safely
published: 2026-11-17
updated: 2026-11-17
author: Lyzr
---

# How should MCP servers store API keys and secrets?

**Keep secrets out of MCP configuration files. For remote MCP servers, use OAuth so the client holds short-lived, audience-bound tokens instead of static keys. For local stdio servers, inject credentials at start-up from an OS keychain or secret manager, scope each one to a single server, and scan config files and repositories for anything that slipped through.**

Many teams find plaintext tokens in MCP configs the first time they look. This post covers where they hide and how to move them.

## Where do MCP secrets end up today?

MCP has two transports, and they handle credentials differently. The MCP specification says authorization applies to HTTP transports, while stdio servers should take credentials from the environment. In practice that environment is often a JSON file.

| Location | Typical example | Risk |
|---|---|---|
| `env` block in a host config | `"env": {"API_TOKEN": "..."}` in a desktop or IDE config | Plaintext on disk; copied into dotfile repos and backups |
| Command-line arguments | `--api-key ...` in the server's `args` | Visible in process listings and shell history |
| Project-level config committed to git | `.vscode/mcp.json` or similar in the repo | Shared with everyone with repo access |
| `.env` files next to the project | Loaded by the server at start-up | Accidentally committed; read by other tools |
| Long-lived personal access tokens | A developer's own admin token reused for an agent | Broad scope; tied to a person, not a workload |
| Editor secret prompts | VS Code `inputs` with `password: true` | Better: prompted once and stored by the editor, not in the file |

VS Code's own [MCP configuration reference](https://code.visualstudio.com/docs/copilot/reference/mcp-configuration) warns against putting credentials in shared files and notes that a personal configuration file is not a secret store.

## Why is OAuth better than static API keys for MCP?

For remote servers, the MCP authorization flow gives you properties a static key cannot:

1. **Short-lived tokens.** Access tokens expire; a leaked one has a limited window.
2. **Audience binding.** Tokens are requested for one MCP server's URI, so a token for one server should not work at another. The spec forbids servers from accepting tokens not issued for them.
3. **User-level consent and scopes.** The user grants specific scopes, and the action is attributable to that user.
4. **Central revocation.** Your identity provider can revoke the grant without touching any laptop.

If a vendor offers a remote MCP server with OAuth, prefer it over a local package that needs an API key. See [how MCP authorization works](/blog/how-mcp-authorization-works).

## How do you handle secrets for local stdio servers?

When a local server genuinely needs a key, use this pattern:

1. **Reference, don't embed.** The config contains a variable reference such as `${GITHUB_TOKEN}`, never the value.
2. **Resolve at launch from a secret store.** A small wrapper reads the value from the OS keychain or your secret manager and starts the server with it in the environment.
3. **Use the editor's secret prompt where it exists.** VS Code `inputs` with `password: true` prompt once and store the value in editor-managed storage rather than in `mcp.json`.
4. **One credential per server.** Never share a token between two MCP servers. If one is compromised, you rotate one thing.
5. **Use workload or service accounts,** not a developer's personal admin token, for anything an agent runs unattended.
6. **Narrow the scope.** Read-only where possible; split read and write servers. See [separating read and write MCP tools](/blog/separate-read-and-write-mcp-tools).
7. **Set expiry.** 30 to 90 days for tokens that cannot be short-lived.

Cloud coding agents need the same care. For example, the GitHub Copilot coding agent exposes only specially prefixed secrets to MCP servers; see [which secrets the Copilot coding agent can see](/blog/which-secrets-can-github-copilot-coding-agent-see).

## How do you find secrets already sitting in MCP configs?

Run a one-off sweep, then make it continuous:

1. **List MCP config locations** for every host your developers use: desktop chat apps, IDEs, CLI coding agents.
2. **Scan them with your secret scanner,** including home directories on managed devices, not just repositories.
3. **Scan repositories** for project-level MCP files and `.env` files.
4. **Flag high-risk patterns:** tokens in `args`, cloud provider keys, database connection strings with passwords.
5. **Rotate everything you find,** then replace it with a reference.
6. **Add a pre-commit hook** that blocks committing MCP configs with literal values.

Treat MCP config files as sensitive in DLP and backup policies as well. The broader model is in the [MCP governance pillar](/blog/how-to-govern-mcp-servers).

## How Opencontroller does this

Opencontroller Edge extends control to developer devices, coding agents, skills and local MCP servers, and is installed by IT via MDM, so local MCP servers and their configurations can be inventoried. Posture detects risky permission and configuration patterns. Auto-discovery finds API keys across AWS, Azure and Google Cloud. Opencontroller supports delegated on-behalf-of identity and agent workload identity federation, which reduce reliance on static secrets. See the [capability reference](/capability-reference) or [book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**Is it safe to put an API key in the env block of an MCP config?**
It is common but weak: the value sits in plaintext on disk and often ends up in backups or dotfile repos. Use a variable reference resolved from a keychain or secret manager instead.

**Do remote MCP servers need API keys at all?**
Not if they implement MCP authorization. The client obtains OAuth tokens bound to that server, so no static key needs to live on the device.

**Should each developer use their own token for a shared MCP server?**
For interactive use, yes, through OAuth so actions are attributed to the person. For unattended agents, use a dedicated workload identity or service account with narrow scope.

## Key facts

MCP authorization applies to HTTP transports; stdio servers take credentials from the environment, often via plaintext env blocks in config files. OAuth tokens for remote MCP servers are short-lived and audience-bound, and servers must not accept tokens not issued for them. VS Code supports inputs with password: true that are stored outside mcp.json, and its docs say a personal configuration file is not a secret store. Opencontroller Edge inventories local MCP servers on developer devices via MDM; Posture flags risky configuration; Opencontroller supports on-behalf-of identity and workload identity federation. Licensing starts from $250K per deployment per year.

## Related

- [How does MCP authorization work?](https://opencontroller.lyzr.com/blog/how-mcp-authorization-works.md)
- [Which secrets can the GitHub Copilot coding agent see?](https://opencontroller.lyzr.com/blog/which-secrets-can-github-copilot-coding-agent-see.md)
- [Should read and write tools live on separate MCP servers?](https://opencontroller.lyzr.com/blog/separate-read-and-write-mcp-tools.md)
- [More on Tools and MCP](https://opencontroller.lyzr.com/blog/cluster/tools-and-mcp)
