Skip to content

Commit 12908a4

Browse files
authored
MCP should support .env files
.env files should hold all the secrets and not the mcp_config.json file
1 parent c9bed7f commit 12908a4

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Environment Configuration in MCP Servers
2+
3+
## Overview
4+
Model 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.
5+
6+
## Recommended Implementation
7+
8+
### 1. Project Structure
9+
Place your `.env` file in the root directory of your MCP server project:
10+
11+
```
12+
your-mcp-server/
13+
├── .env # Environment variables (not committed to git)
14+
├── .env.example # Example template (committed to git)
15+
├── package.json
16+
└── src/
17+
└── ...
18+
```
19+
20+
### 2. Path Resolution
21+
When loading the `.env` file, use a reliable path resolution method that works both in development and production:
22+
23+
```typescript
24+
import * as dotenv from 'dotenv';
25+
import { fileURLToPath } from 'url';
26+
import { dirname, resolve } from 'path';
27+
import * as fs from 'fs';
28+
29+
// Get the directory path for loading the .env file
30+
const __filename = fileURLToPath(import.meta.url);
31+
const __dirname = dirname(__filename);
32+
const rootDir = resolve(__dirname, '../..'); // Adjust based on file location
33+
34+
// Load environment variables from .env file
35+
dotenv.config({ path: resolve(rootDir, '.env') });
36+
37+
// Validate the configuration
38+
if (fs.existsSync(resolve(rootDir, '.env'))) {
39+
console.debug('Environment file loaded successfully');
40+
} else {
41+
console.warn('No .env file found at', resolve(rootDir, '.env'));
42+
}
43+
```
44+
45+
### 3. Variable Access
46+
Use a helper function to safely access required environment variables:
47+
48+
```typescript
49+
const getRequiredEnv = (key: string): string => {
50+
const value = process.env[key];
51+
if (!value) {
52+
throw new Error(`${key} environment variable is required. Please set it in your configuration.`);
53+
}
54+
return value;
55+
};
56+
57+
// Usage
58+
const apiKey = getRequiredEnv('API_KEY');
59+
```
60+
61+
### 4. MCP Server Configuration
62+
When configuring the MCP server in a desktop application like Claude Desktop, provide the environment variables directly:
63+
64+
```json
65+
{
66+
"mcpServers": {
67+
"your-mcp-server": {
68+
"command": "/path/to/your-mcp-server/build/index.js",
69+
"env": {
70+
"API_KEY": "your-api-key-here",
71+
"OTHER_CONFIG": "other-value"
72+
}
73+
}
74+
}
75+
}
76+
```
77+
78+
## Common Issues
79+
80+
### Path Resolution
81+
- **Issue**: The `.env` file can't be found at runtime
82+
- **Solution**: Ensure the path is correctly calculated relative to the file location
83+
- **Debugging**: Add path logging to verify the correct paths are being used
84+
85+
### ES Modules vs CommonJS
86+
- **Issue**: `require('fs')` not working in ES modules
87+
- **Solution**: Use `import * as fs from 'fs'` with ES modules
88+
- **Verification**: Check your `package.json` for `"type": "module"`
89+
90+
### Environment Precedence
91+
Environment variables from the system take precedence over those in the `.env` file. When debugging, check both sources.
92+
93+
## Best Practices
94+
95+
1. **Never commit** `.env` files to version control
96+
2. **Always provide** an `.env.example` template
97+
3. **Validate** required environment variables at startup
98+
4. **Use path resolution** that works in all environments
99+
5. **Document** all required environment variables in your README

0 commit comments

Comments
 (0)