MCP server environment variables are configured in your host's JSON config file using the env block inside each server definition. Claude Desktop uses claude_desktop_config.json, Cursor uses .cursor/mcp.json, and each platform has slightly different syntax. Environment variables are the correct way to pass API keys and secrets to MCP servers — never hardcode them in your server code.
Configuring Environment Variables for MCP Servers
Most MCP servers need API keys, database URLs, or other configuration values to work. The MCP specification lets you set environment variables in your host's config file, and they are automatically injected into the server process when it starts. This tutorial covers the configuration syntax for every major MCP host and shows you how to read these values in both TypeScript and Python servers.
Prerequisites
- An MCP host installed (Claude Desktop, Cursor, or similar)
- An MCP server you want to configure
- The API key or secret you need to pass to the server
Step-by-step guide
Locate your MCP host configuration file
Locate your MCP host configuration file
Each MCP host stores its server configuration in a specific JSON file. Claude Desktop on macOS uses ~/Library/Application Support/Claude/claude_desktop_config.json. On Windows, it is %APPDATA%\Claude\claude_desktop_config.json. Cursor uses .cursor/mcp.json in your project directory for project-scoped servers, or ~/.cursor/mcp.json for global servers. Open this file in any text editor.
1# Claude Desktop config location2# macOS:3~/Library/Application Support/Claude/claude_desktop_config.json45# Windows:6%APPDATA%\Claude\claude_desktop_config.json78# Cursor config location9# Project-scoped:10.cursor/mcp.json1112# Global:13~/.cursor/mcp.jsonExpected result: You have found and opened your host's configuration file.
Add environment variables to your server config
Add environment variables to your server config
Inside your server definition, add an env object with key-value pairs for each environment variable. The keys become environment variable names and the values are their contents. These variables are set in the server process environment when the host starts the server. This is the correct and secure way to pass API keys — never put them directly in your server's source code.
1{2 "mcpServers": {3 "my-server": {4 "command": "node",5 "args": ["./dist/index.js"],6 "env": {7 "API_KEY": "sk-your-api-key-here",8 "DATABASE_URL": "postgresql://user:pass@localhost:5432/mydb",9 "LOG_LEVEL": "debug"10 }11 }12 }13}Expected result: Your config file now includes an env block with the required API keys and configuration values.
Access environment variables in your server code
Access environment variables in your server code
In your MCP server, read environment variables using the standard approach for your language. In TypeScript/Node.js, use process.env.VARIABLE_NAME. In Python, use os.environ["VARIABLE_NAME"] or os.environ.get("VARIABLE_NAME", "default"). Always validate that required variables are present at startup and fail with a clear error message if they are missing.
1// TypeScript — reading env vars in your MCP server2const apiKey = process.env.API_KEY;3if (!apiKey) {4 console.error("ERROR: API_KEY environment variable is not set");5 console.error("Add it to your MCP host config under the env block");6 process.exit(1);7}89// Python — reading env vars10import os11import sys1213api_key = os.environ.get("API_KEY")14if not api_key:15 print("ERROR: API_KEY environment variable is not set", file=sys.stderr)16 sys.exit(1)Expected result: Your server reads the environment variables at startup and fails clearly if they are missing.
Configure environment variables for npx-based servers
Configure environment variables for npx-based servers
Many community MCP servers are distributed as npm packages and run via npx. You configure environment variables the same way — add an env block alongside the command and args fields. The environment variables are passed to the npx child process which forwards them to the server. This is how you configure servers you did not write, like @modelcontextprotocol/server-github or @modelcontextprotocol/server-slack.
1{2 "mcpServers": {3 "github": {4 "command": "npx",5 "args": ["-y", "@modelcontextprotocol/server-github"],6 "env": {7 "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_your_token_here"8 }9 },10 "slack": {11 "command": "npx",12 "args": ["-y", "@modelcontextprotocol/server-slack"],13 "env": {14 "SLACK_BOT_TOKEN": "xoxb-your-token",15 "SLACK_TEAM_ID": "T01234567"16 }17 }18 }19}Expected result: Third-party MCP servers receive their required API keys and tokens through environment variables.
Restart your MCP host to apply changes
Restart your MCP host to apply changes
After editing the configuration file, you must restart your MCP host for the changes to take effect. Claude Desktop requires a full application restart — quit and reopen it. Cursor typically picks up changes when you reload the window (Cmd+Shift+P then Reload Window), though a full restart is more reliable. Verify the connection by checking that the server's tools appear in your host. If you manage multiple MCP servers with complex configurations and need help streamlining the setup, the RapidDev team can help architect a clean configuration.
Expected result: Your MCP host restarts, connects to the server, and the server has access to all configured environment variables.
Complete working example
1{2 "mcpServers": {3 "my-custom-server": {4 "command": "node",5 "args": ["./mcp-servers/my-server/dist/index.js"],6 "env": {7 "API_KEY": "sk-your-api-key-here",8 "DATABASE_URL": "postgresql://user:pass@localhost:5432/mydb",9 "LOG_LEVEL": "info",10 "MAX_RESULTS": "50"11 }12 },13 "github": {14 "command": "npx",15 "args": ["-y", "@modelcontextprotocol/server-github"],16 "env": {17 "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_your_token_here"18 }19 },20 "filesystem": {21 "command": "npx",22 "args": [23 "-y",24 "@modelcontextprotocol/server-filesystem",25 "/Users/yourname/projects",26 "/Users/yourname/documents"27 ]28 }29 }30}Common mistakes when configuring environment variables for MCP servers
Why it's a problem: Hardcoding API keys in server source code instead of using env
How to avoid: Always use the env block in your host config to pass secrets. This keeps them out of your codebase and version control.
Why it's a problem: Forgetting to restart the MCP host after changing config
How to avoid: Environment variables are only read when the server starts. Restart Claude Desktop or reload Cursor after any config changes.
Why it's a problem: Using the wrong environment variable name
How to avoid: Check the MCP server's documentation for the exact variable names it expects. Names are case-sensitive — GITHUB_TOKEN is different from github_token.
Why it's a problem: Putting env vars in a .env file instead of the host config
How to avoid: MCP hosts do not read .env files. Environment variables must be in the env block of your host's JSON configuration.
Best practices
- Always use the env block in your MCP host config for API keys and secrets
- Validate required environment variables at server startup with clear error messages
- Never commit configuration files containing real API keys to version control
- Use descriptive environment variable names that match the server's documentation
- Keep a template config file with placeholder values for team members to copy
- Set LOG_LEVEL as an environment variable to control verbosity without code changes
Still stuck?
Copy one of these prompts to get a personalized, step-by-step explanation.
I need to configure environment variables for an MCP server in [Claude Desktop / Cursor]. The server needs [API_KEY, DATABASE_URL, etc.]. Show me the exact JSON configuration and how to access these variables in the server code.
Show me how to configure environment variables for an MCP server that needs a [service name] API key. Include the claude_desktop_config.json entry and the TypeScript code to validate the variable at startup.
Frequently asked questions
Can I reference system environment variables in the MCP config?
No, the env block in MCP host configs uses literal string values. You cannot reference $HOME or %PATH%. If you need system variables, consider wrapping your server in a shell script that reads them.
Are environment variables encrypted in the config file?
No, they are stored in plain text in the JSON config file. Protect the file with appropriate file system permissions and never commit it to version control with real secrets.
Can I use .env files with MCP servers?
Not directly through the host config. However, your server code can use a library like dotenv to load .env files. The env block in the host config takes precedence over .env file values for the same variable name.
Do env variables persist across server restarts?
Yes, the env block is read from the config file every time the host starts the server. As long as the config file has the values, they will be available.
How do I pass environment variables to a Python MCP server?
The same way — use the env block in your host config. Python servers access them with os.environ.get('VARIABLE_NAME'). The host sets them in the process environment before starting the server, regardless of the language.
What happens if I set an env variable that the server does not use?
Nothing — unused environment variables are simply ignored. They exist in the process environment but have no effect unless the server code reads them.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation