How to securely provide env variables to MCP servers?

Status Fixed / completed
Maintainer reply ✓ Yes — ashwin-ant
Activity 5 comments · opened Jun 13, 2025 · closed Jul 20, 2025
💡 Likely answer: A maintainer (ashwin-ant, collaborator) responded on this thread — see the highlighted reply below.

I am preparing a shared .mcp.json file that I want to commit to our repository, but facing an issue in how to authenticate in the database.

There are two solutions that I see:

  1. Provide it plaintext
    "postgres": {
      "command": "docker",
      "args": [
        "run", 
        "-i", 
        "--rm", 
	"--network=host",
        "mcp/postgres", 
        "postgresql://user_name:<our_actual_plaintext_password>@postgres_ip:5432/database_name"]
    }

This one is a big no for obvious reasons. Env variables and code should not go together.

  1. Set an environment variable PGPASSWORD in my env
    "postgres": {
      "command": "docker",
      "args": [
        "run", 
        "-i", 
        "--rm", 
	"--network=host",
        "-e",
        "PGPASSWORD",
        "mcp/postgres", 
        "postgresql://user_name@postgres_ip:5432/database_name"]
    }

It's acceptable, but inconvenient, because the env variable for the database that we have is named differently, say DBPWD

Ideally, I would like to see an option to expand env variables similar to

    "postgres": {
      "command": "docker",
      "args": [
        "run", 
        "-i", 
        "--rm", 
	"--network=host",
        "mcp/postgres", 
        "postgresql://user_name:$DBPWD@postgres_ip:5432/database_name"]
    }

or, maybe:

...
      "env": {
        "PGPASSWORD": "${PSQLPWD}",
...

I believe currently there is no option to do so (please correct me if I'm wrong), are there any plans on adding this functionality and recommendations for a workaround

View original on GitHub ↗

5 Comments

ashwin-ant collaborator · 1 year ago

A way we've seen some users do this is by having the command string in the .mcp.json be a script rather than a direct invocation of the MCP server. Then if you have a secure place to store secrets, you can have the script pull the secret in and start the server.

superhappychris · 1 year ago
...

"env": {
"PGPASSWORD": "${PSQLPWD}",
...

i think something like this would be better than having to create scripts for each mcp server

techpeace · 1 year ago
or, maybe: `` ... "env": { "PGPASSWORD": "${PSQLPWD}", ... ``

According to these Claude Code docs, it appears that this is now possible.

Guitaricet · 1 year ago

This is amazing, thanks Claude Code team ❤️

github-actions[bot] · 1 year ago

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.