

Also from Kynth Studios


Also from Kynth Studios


Also from Kynth Studios
12345678910# Environment Configuration in MCP Servers1112## Overview13Model Context Protocol (MCP) servers often require access to API keys and configuration values that should not be committed to version control. This document outlines the recommended approach for managing environment variables in MCP servers.1415## Recommended Implementation1617### 1. Project Structure18Place your `.env` file in the root directory of your MCP server project:1920```21your-mcp-server/22├── .env # Environment variables (not committed to git)23├── .env.example # Example template (committed to git)24├── package.json25└── src/26 └── ...27```2829### 2. Path Resolution30When loading the `.env` file, use a reliable path resolution method that works both in development and production:3132```typescript33import * as dotenv from 'dotenv';34import { fileURLToPath } from 'url';35import { dirname, resolve } from 'path';36import * as fs from 'fs';3738// Get the directory path for loading the .env file39const __filename = fileURLToPath(import.meta.url);40const __dirname = dirname(__filename);41const rootDir = resolve(__dirname, '../..'); // Adjust based on file location4243// Load environment variables from .env file44dotenv.config({ path: resolve(rootDir, '.env') });4546// Validate the configuration47if (fs.existsSync(resolve(rootDir, '.env'))) {48 console.debug('Environment file loaded successfully');49} else {50 console.warn('No .env file found at', resolve(rootDir, '.env'));51}52```5354### 3. Variable Access55Use a helper function to safely access required environment variables:5657```typescript58const getRequiredEnv = (key: string): string => {59 const value = process.env[key];60 if (!value) {61 throw new Error(`${key} environment variable is required. Please set it in your configuration.`);62 }63 return value;64};6566// Usage67const apiKey = getRequiredEnv('API_KEY');68```6970### 4. MCP Server Configuration71When configuring the MCP server in a desktop application like Claude Desktop, provide the environment variables directly:7273```json74{75 "mcpServers": {76 "your-mcp-server": {77 "command": "/path/to/your-mcp-server/build/index.js",78 "env": {79 "API_KEY": "your-api-key-here",80 "OTHER_CONFIG": "other-value"81 }82 }83 }84}85```8687## Common Issues8889### Path Resolution90- **Issue**: The `.env` file can't be found at runtime91- **Solution**: Ensure the path is correctly calculated relative to the file location92- **Debugging**: Add path logging to verify the correct paths are being used9394### ES Modules vs CommonJS95- **Issue**: `require('fs')` not working in ES modules96- **Solution**: Use `import * as fs from 'fs'` with ES modules97- **Verification**: Check your `package.json` for `"type": "module"`9899### Environment Precedence100Environment variables from the system take precedence over those in the `.env` file. When debugging, check both sources.101102## Best Practices1031041. **Never commit** `.env` files to version control1052. **Always provide** an `.env.example` template1063. **Validate** required environment variables at startup1074. **Use path resolution** that works in all environments1085. **Document** all required environment variables in your README109
One repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| cline/prompts.clinerules/ai-dlc-adaptive-workflow.md · 1.2k | Cline rules | agent-behaviour | 54/100 | today | |
| cline/prompts.clinerules/audio-plugin-developer.md · 1.2k | Cline rules | styleperformancedo-notagent-behaviour | 57/100 | today | |
| cline/prompts.clinerules/ba.md · 1.2k | Cline rules | archgitagent-behaviour | 50/100 | today | |
| cline/prompts.clinerules/baby-steps.md · 1.2k | Cline rules | do-notagent-behaviour | 50/100 | today | |
| cline/prompts.clinerules/c#-guide.md · 1.2k | Cline rules | style | 27/100 | today | |
| cline/prompts.clinerules/claude-code-subagents.md · 1.2k | Cline rules | testarchdo-notagent-behaviour | 77/100 | today | |
| cline/prompts.clinerules/cline-architecture.md · 1.2k | Cline rules | archtypesapi | 54/100 | today | |
| cline/prompts.clinerules/cline-continuous-improvement-protocol.md · 1.2k | Cline rules | testgitperformance | 58/100 | today | |
| cline/prompts.clinerules/cline-for-research.md · 1.2k | Cline rules | agent-behaviour | 34/100 | today | |
| cline/prompts.clinerules/cline-for-slides.md · 1.2k | Cline rules | setupbuildstylearch+1 | 86/100 | today | |
| cline/prompts.clinerules/cline-for-webdev-ui.md · 1.2k | Cline rules | archagent-behaviour | 58/100 | today | |
| cline/prompts.clinerules/code-review.md · 1.2k | Cline rules | lint-formatgitsecurityperformance | 48/100 | today | |
| cline/prompts.clinerules/codebase-onboarding.md · 1.2k | Cline rules | lint-formatstylearchdependencies | 56/100 | today | |
| cline/prompts.clinerules/comprehensive-slide-dev-guide.md · 1.2k | Cline rules | buildarchtypesui | 62/100 | today | |
| cline/prompts.clinerules/create-documentation.md · 1.2k | Cline rules | apidocs | 44/100 | today | |
| cline/prompts.clinerules/gemini-comprehensive-software-engineering-guide.md · 1.2k | Cline rules | buildstyletesting-strategysecurity+4 | 36/100 | today | |
| cline/prompts.clinerules/general-development-rules.md · 1.2k | Cline rules | stylegitdeploymentdo-not | 73/100 | today | |
| cline/prompts.clinerules/google-apps-script-developer.md · 1.2k | Cline rules | setupstylegitsecurity+3 | 66/100 | today | |
| cline/prompts.clinerules/helm-chart-developer.md · 1.2k | Cline rules | setuplint-formatstylearch+6 | 81/100 | today | |
| cline/prompts.clinerules/mcp-development-protocol.md · 1.2k | Cline rules | setupteststyle | 73/100 | today |
A badge carrying the measured quality of the strongest agent config file in this repository, out of 100. It reads from this index every time somebody loads your page, so it changes when the measurement changes and there is nothing to keep up to date. Free, no account, and the value is not something you or we can set by hand.
[](https://rulestack.kynth.studio/configs/cline-prompts-clinerules-mcp-env-configuration)Would rather not hotlink us? Every badge is also served in shields.io’s endpoint schema, so shields renders the image and your readers never talk to our domain:
Published by Toolproof, the masthead over this index and eight others. The method behind the number is at toolproof.kynth.studio/methodology, and the whole thing is readable as JSON with no key at /api.