Join our Newsletter — 33% off our NHI Course
Home› Guides› How to give MCP servers and local agents…
Guide Agent Identity & Access

How to give MCP servers and local agents secrets without plain-text config files: practical patterns that keep tokens out of JSON

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, under the editorial direction of Lalit Choda Updated 20 November 2026 14 min read
On this page

Most MCP servers still get their credentials from a token pasted into a JSON config file, and that file is one commit, one backup or one shared screen away from a leak. The fix is rarely exotic. MCP clients already support references instead of literal values, prompts that store values in the operating system's secure storage, helper commands that fetch credentials at connection time, and OAuth for remote servers. This guide sets out where plain-text secrets end up in agent and MCP configurations, the patterns that replace them, in order of preference, and how to roll the change out across a team without breaking anyone's tools.

It is written for engineers and platform teams who run MCP servers on laptops, in developer containers and in shared project repositories. It focuses on storage and delivery of the credential. For keeping secrets out of the model's context once the agent is running, read Runtime Secrets Injection for AI Agents Without Prompt Exposure. For finding servers that already use static tokens, use the MCP Static Token Audit.

Key takeaways

  • A config file is not a secret store. One client's own documentation states that a personal configuration file is not a secret store, and the same reasoning applies to every client.
  • The MCP specification says STDIO servers should retrieve credentials from the environment, so the job is to get a secret into the server's environment without writing it into the config.
  • Replace literal values with references: environment variable expansion, prompted inputs held in secure storage, or a helper command that fetches a short-lived token on each connection.
  • For remote servers, prefer OAuth. The specification requires secure token storage, audience-bound tokens and no tokens in query strings.
  • Project-level config files are meant to be shared through version control, so they must only ever contain references, never values.
  • Rotate every token that has ever sat in a plain-text config. Moving it to a vault does not un-leak it.

Why plain-text MCP config is a leak path

Local MCP servers are usually launched by the client as a child process, with a command, arguments and an env block defined in JSON. The quickest way to get a server working is to paste a personal access token into that env block. Remote servers have a similar shortcut: a static Authorization header in the config. Both leave a long-lived credential in a file that was never designed to protect it.

That file then travels. Typical paths include:

  • Version control. Project-scoped MCP files sit in the repository root and are intended to be committed so the whole team shares the same tools. A literal token in one of them is a committed secret.
  • Dotfile sync and backups. User-level config files in the home directory are swept up by dotfile repositories, cloud backups and machine migration tools.
  • Copy and paste between clients. Engineers move a working server definition from one client to another, through chat, tickets and wiki pages. Each copy is another place the token lives.
  • The agent itself. A coding agent that can read files in the workspace can read the config, and an injected instruction can ask it to.
  • Support bundles and screen shares. Diagnostic output and screenshots of config panels capture whatever is in them.

Adversaries searching files for stored credentials is a documented technique, MITRE ATT&CK: Credentials In Files (T1552.001). MCP configs are attractive because the tokens in them are often broad: a source control token with full repository scope, a cloud key, or a SaaS admin token, granted to make a demo work and never reduced.

The broader picture of agents and servers that appear on developer machines without review is covered in the Unsanctioned AI Agents and MCP Servers Guide. This page assumes the server is sanctioned and asks only how it should receive its credential.

What the MCP specification expects

The MCP authorization specification is explicit about the split between transports. Authorization is optional for MCP implementations. Where it is supported, implementations using an HTTP-based transport should conform to the specification's OAuth-based flow, while implementations using STDIO should not follow it and should instead retrieve credentials from the environment.

That gives two distinct problems:

  • Local STDIO servers read credentials from their process environment. The client builds that environment from its config. The question is how to populate it without the config holding the value.
  • Remote HTTP servers should use OAuth. The specification requires clients to send the access token in the Authorization header, never in the URI query string; requires clients and servers to implement secure token storage; says authorization servers should issue short-lived access tokens; and requires refresh token rotation for public clients. It also requires servers to accept only tokens issued for them and forbids passing a client's token through to upstream APIs.

Where a remote server cannot do OAuth and insists on a static API key, treat it as a STDIO-style problem: the key must come from somewhere other than the file.

The patterns, in order of preference

Each pattern below removes the literal value from the config. They are listed from the strongest to the weakest. Use the highest one your client and server support.

PatternWhere the secret livesWhat the config holdsBest for
1. OAuth to the remote serverClient's secure token storage; short-lived access tokensServer URL onlyRemote servers that support MCP authorization
2. Helper command that fetches a token per connectionYour secrets manager or identity providerPath to the helperInternal remote servers, short-lived tokens, single sign-on
3. Prompted input saved to secure storageOperating system or editor secure storageAn input referencePersonal API keys on a single machine
4. Environment variable referenceThe environment of the launching shell, populated from a vault or keychain${VAR} style referenceShared project configs, CI and containers
5. Wrapper launch commandSecrets manager, read at process startA command that fetches and then execs the serverServers or clients without native reference support

1. OAuth for remote servers

If the remote server supports MCP authorization, the config needs nothing but the URL. The client discovers the authorization server, runs the browser flow with PKCE and the resource parameter the specification requires, and stores the resulting tokens. Access tokens should be short-lived, so a token copied out of storage has a limited life. Revocation is handled at the identity provider, not by editing files. The trade-off between keys and OAuth for MCP is covered in depth in API Keys vs OAuth for MCP Servers.

2. A helper command that fetches credentials

Some clients can run a command at connection time that prints headers or a token. The helper authenticates to your secrets manager or identity provider using the engineer's existing sign-in, obtains a short-lived credential and hands it to the client. The config holds only the helper's path. This is the best option for internal servers fronted by your own single sign-on, because nothing long-lived ever reaches the machine.

3. Prompted inputs saved to secure storage

Some clients let a config declare an input, prompt the user for it the first time the server starts, and keep the value in the editor's or operating system's secure storage. The config contains a placeholder such as ${input:my-key}. This removes the token from the file and from version control, though the value is still long-lived, so pair it with a scoped, expiring token.

4. Environment variable references

Most clients can expand a reference like ${API_KEY} in the config from the environment of the process that launched the client. The file is safe to commit. The secret has to be put into the environment by something else, which is where the real choice lies:

  • A shell profile that reads the value from the operating system keychain at login, rather than exporting a literal.
  • A secrets manager command-line tool that injects variables for the duration of a single command.
  • In containers and CI, the platform's own secret injection, mounted at runtime.

What to avoid is moving the token from the JSON into a .env file or a shell profile in plain text. That is the same problem in a different file. See Environment Variables and .env Files for the failure modes.

5. A wrapper launch command

Where a client has no native reference support, set the server's command to a small wrapper that fetches the credential from your secrets manager, exports it into its own environment and then starts the real server. The config points at the wrapper. Keep the wrapper in a reviewed location, make sure it never echoes the value, and fail closed if the fetch fails rather than falling back to a cached copy.

Client settings that support these patterns

The following client sections are listed alphabetically. Each describes only that client's own documented settings, as of November 2026. Other clients have their own equivalents; check their documentation before relying on any of these names.

Claude Code

According to the Claude Code MCP documentation, .mcp.json supports environment variable expansion with ${VAR} and ${VAR:-default}, in the command, args, env, url and headers fields. If a referenced variable is not set and has no default, the config still loads, a missing-variable warning is reported, and the unexpanded text is used as written. The documentation also lists certain credential variable names that are read as empty rather than expanded in a remote server's url and headers.

Servers can be defined at local, project or user scope. Local and user scope are stored in ~/.claude.json; project scope is stored in .mcp.json at the project root, which the documentation says to check into version control. The client prompts for approval before using project-scoped servers in interactive sessions, but the documentation notes that in non-interactive runs it cannot show that prompt and loads them without asking.

For remote servers, the documentation describes OAuth sign-in through /mcp, with tokens stored securely and refreshed automatically, and a client secret stored in the system keychain on macOS or a credentials file rather than in the config. A headersHelper setting runs a command at connection time that writes a JSON object of headers to standard output; it runs fresh on each connection, is not cached and times out after 10 seconds.

Visual Studio Code

According to the VS Code MCP configuration reference, the config file can declare an inputs array of input variable definitions for sensitive information such as API keys. A promptString input opens an input box, and password: true hides typed input. VS Code prompts the first time the server starts, and the documentation says the value is then securely stored for subsequent use. Values are referenced with ${input:variable-id}, for example inside env or in an HTTP server's headers.

The reference also documents an envFile field to load variables from a file. It describes inputs and envFile as editor-only fields that should not be copied into a portable file, and states that input variables are not supported in portable files. Its guidance is direct: do not store literal credentials in shared files, do not commit tokens, passwords or populated authentication headers, and a personal configuration file is not a secret store. The companion VS Code MCP servers page adds that credentials should not be pasted into chat or put in shared configuration.

Shared project configs and teams

Project-level MCP files are where team practice matters most, because they are designed to be committed. A safe team convention looks like this:

  • References only. The committed file contains server definitions with ${VAR} or input references. Reviewers reject any pull request that adds a literal value to env or headers.
  • A documented variable list. The repository's contributor notes list each variable the MCP config expects, what scope the token needs, and where to obtain it, typically a self-service path in your secrets manager.
  • Secret scanning on the file. Make sure your secret scanner and push protection cover the MCP config paths your clients use. See Push Protection Guide for setting that up.
  • Personal overrides stay personal. Where a client supports local or user scope, engineers put anything that must hold a value there, never in the project file.
  • Non-interactive runs reviewed. If your pipelines run agents headlessly, remember that approval prompts may not appear. Decide explicitly which project servers those runs may load.

Scoping and lifetime of the credential itself

Moving a token out of a file reduces exposure. It does not reduce what the token can do. Treat every MCP server as a non-human identity with its own credential, and size that credential to the server's job:

  • One credential per server. Do not reuse a single broad personal token across five servers. When one leaks, you want to revoke one thing.
  • Least scope. A server that reads issues does not need repository write. Use fine-grained or resource-scoped tokens where the provider supports them.
  • Expiry. Prefer tokens that expire in days or weeks over tokens with no expiry. A helper or OAuth flow makes short lifetimes painless.
  • Attribution. Where possible, issue the token to an identity the provider's audit log can tie back to the server and its owner, not to a shared team account.

The Local MCP Server Security Guide covers the wider hardening baseline for servers running on a workstation.

Rolling out the change

  1. Find what exists. Inventory MCP config files on managed devices and in repositories. Look in project roots and the user-level paths your sanctioned clients document. The How to audit MCP servers on developer laptops has a method.
  2. Classify each credential. Record the provider, scope, owner and whether the token has ever been committed, synced or shared.
  3. Rotate first. Any token that has lived in a plain-text config should be treated as exposed. Issue a new, narrower token before or as part of the move.
  4. Pick the pattern per server. OAuth where the remote server supports it; a helper for internal servers; prompted inputs or environment references for personal keys.
  5. Update the shared configs. Replace literal values with references in one pull request per repository, with the variable list documented alongside.
  6. Add guardrails. Turn on secret scanning for config paths, add a review rule, and include MCP config checks in device posture where your tooling allows.
  7. Measure. Track the number of MCP configs holding literal values over time, and aim for zero.

Common failure patterns

  • Moving the token to a .env file next to the config. The literal value still sits in plain text in the project directory, often outside the ignore rules.
  • Committing a populated header. Remote server configs with a static Authorization header slip past reviewers who only check env.
  • Copying editor-only fields into portable files. One client's documentation warns against this; input references then fail and engineers paste values in to make it work.
  • Silent fallback. A missing variable is expanded as literal text or an empty string, the server fails with a confusing error, and the engineer hard-codes the value to fix it.
  • Vaulting without rotating. The token is moved to secure storage but is the same token that was committed last year.
  • One broad token for everything. Secure storage protects the value, but the value still grants far more than the server needs.
  • Forgetting headless runs. Pipelines and scheduled agents load project servers without the interactive prompt the team relied on.

Practitioner checklist

  • No MCP config file, project or personal, contains a literal token, key, password or populated authentication header.
  • Remote servers that support MCP authorization use OAuth, with no static header in the config.
  • Internal remote servers use a helper command that fetches a short-lived credential per connection.
  • Personal API keys use prompted inputs held in secure storage or environment references populated from a keychain or secrets manager.
  • Project-level MCP files are covered by secret scanning and push protection, and reviewers reject literal values.
  • Each MCP server has its own scoped, expiring credential with a named owner.
  • Every token that was ever stored in plain text has been rotated.
  • The team's contributor notes list the variables each project's MCP config expects and where to obtain them.
  • Non-interactive agent runs have an explicit allow list of project servers they may load.

Standards and references

Related NHI Mgmt Group resources: MCP Static Token Audit · Runtime Secrets Injection for AI Agents Without Prompt Exposure · Local MCP Server Security Guide · Unsanctioned AI Agents and MCP Servers Guide

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick both boxes to continue.