How to securely provide env variables to MCP servers?
Status Fixed / completed
Maintainer reply ✓ Yes — ashwin-ant
Workaround ✓ Mentioned in description ↑
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:
- 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.
- Set an environment variable
PGPASSWORDin 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
5 Comments
A way we've seen some users do this is by having the
commandstring in the.mcp.jsonbe 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."env": {
"PGPASSWORD": "${PSQLPWD}",
...
i think something like this would be better than having to create scripts for each mcp server
According to these Claude Code docs, it appears that this is now possible.
This is amazing, thanks Claude Code team ❤️
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.