Skip to content

Commit 8bb5ce4

Browse files
authored
Merge pull request #757 from upstash/feat/env-var-api-key
2 parents 8a6d344 + 5846c8c commit 8bb5ce4

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

‎README.md‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,13 +518,14 @@ it also can be solved tith the full path to Node.js and the installed package:
518518
```toml
519519
[mcp_servers.context7]
520520
command = "/Users/yourname/.nvm/versions/node/v22.14.0/bin/node" # Node.js full path
521-
args = ["/Users/yourname/.nvm/versions/node/v22.14.0/lib/node_modules/@upstash/context7-mcp/dist/index.js",
521+
args = ["/Users/yourname/.nvm/versions/node/v22.14.0/lib/node_modules/@upstash/context7-mcp/dist/index.js",
522522
"--transport",
523523
"stdio",
524524
"--api-key",
525525
"YOUR_API_KEY"
526526
]
527527
```
528+
528529
This ensures Codex CLI works reliably on MacOS.
529530

530531
</details>
@@ -1136,7 +1137,7 @@ bun run dist/index.js
11361137

11371138
- `--transport <stdio|http>` – Transport to use (`stdio` by default). Note that HTTP transport automatically provides both HTTP and SSE endpoints.
11381139
- `--port <number>` – Port to listen on when using `http` transport (default `3000`).
1139-
- `--api-key <key>` – API key for authentication. You can get your API key by creating an account at [context7.com/dashboard](https://context7.com/dashboard).
1140+
- `--api-key <key>` – API key for authentication (or set `CONTEXT7_API_KEY` env var). You can get your API key by creating an account at [context7.com/dashboard](https://context7.com/dashboard).
11401141

11411142
Example with HTTP transport and port 8080:
11421143

@@ -1150,6 +1151,39 @@ Another example with stdio transport:
11501151
bun run dist/index.js --transport stdio --api-key YOUR_API_KEY
11511152
```
11521153

1154+
### Environment Variables
1155+
1156+
You can use the `CONTEXT7_API_KEY` environment variable instead of passing the `--api-key` flag. This is useful for:
1157+
1158+
- Storing API keys securely in `.env` files
1159+
- Integration with MCP server setups that use dotenv
1160+
- Tools that prefer environment variable configuration
1161+
1162+
**Note:** The `--api-key` CLI flag takes precedence over the environment variable when both are provided.
1163+
1164+
**Example with .env file:**
1165+
1166+
```bash
1167+
# .env
1168+
CONTEXT7_API_KEY=your_api_key_here
1169+
```
1170+
1171+
**Example MCP configuration using environment variable:**
1172+
1173+
```json
1174+
{
1175+
"mcpServers": {
1176+
"context7": {
1177+
"command": "npx",
1178+
"args": ["-y", "@upstash/context7-mcp"],
1179+
"env": {
1180+
"CONTEXT7_API_KEY": "YOUR_API_KEY"
1181+
}
1182+
}
1183+
}
1184+
}
1185+
```
1186+
11531187
<details>
11541188
<summary><b>Local Configuration Example</b></summary>
11551189

‎src/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const DEFAULT_PORT = 3000;
2323
const program = new Command()
2424
.option("--transport <stdio|http>", "transport type", "stdio")
2525
.option("--port <number>", "port for HTTP transport", DEFAULT_PORT.toString())
26-
.option("--api-key <key>", "API key for authentication")
26+
.option("--api-key <key>", "API key for authentication (or set CONTEXT7_API_KEY env var)")
2727
.allowUnknownOption() // let MCP Inspector / other wrappers pass through extra flags
2828
.parse(process.argv);
2929

@@ -415,7 +415,8 @@ async function main() {
415415
startServer(initialPort);
416416
} else {
417417
// Stdio transport - this is already stateless by nature
418-
const server = createServerInstance(undefined, cliOptions.apiKey);
418+
const apiKey = cliOptions.apiKey || process.env.CONTEXT7_API_KEY;
419+
const server = createServerInstance(undefined, apiKey);
419420
const transport = new StdioServerTransport();
420421
await server.connect(transport);
421422
console.error("Context7 Documentation MCP Server running on stdio");

0 commit comments

Comments
 (0)