Merge pull request #757 from upstash/feat/env-var-api-key

This commit is contained in:
Enes Gules 2025-10-07 14:56:45 +03:00 committed by GitHub
commit 8bb5ce46c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 4 deletions

View File

@ -518,13 +518,14 @@ it also can be solved tith the full path to Node.js and the installed package:
```toml
[mcp_servers.context7]
command = "/Users/yourname/.nvm/versions/node/v22.14.0/bin/node" # Node.js full path
args = ["/Users/yourname/.nvm/versions/node/v22.14.0/lib/node_modules/@upstash/context7-mcp/dist/index.js",
args = ["/Users/yourname/.nvm/versions/node/v22.14.0/lib/node_modules/@upstash/context7-mcp/dist/index.js",
"--transport",
"stdio",
"--api-key",
"YOUR_API_KEY"
]
```
This ensures Codex CLI works reliably on MacOS.
</details>
@ -1136,7 +1137,7 @@ bun run dist/index.js
- `--transport <stdio|http>` Transport to use (`stdio` by default). Note that HTTP transport automatically provides both HTTP and SSE endpoints.
- `--port <number>` Port to listen on when using `http` transport (default `3000`).
- `--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).
- `--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).
Example with HTTP transport and port 8080:
@ -1150,6 +1151,39 @@ Another example with stdio transport:
bun run dist/index.js --transport stdio --api-key YOUR_API_KEY
```
### Environment Variables
You can use the `CONTEXT7_API_KEY` environment variable instead of passing the `--api-key` flag. This is useful for:
- Storing API keys securely in `.env` files
- Integration with MCP server setups that use dotenv
- Tools that prefer environment variable configuration
**Note:** The `--api-key` CLI flag takes precedence over the environment variable when both are provided.
**Example with .env file:**
```bash
# .env
CONTEXT7_API_KEY=your_api_key_here
```
**Example MCP configuration using environment variable:**
```json
{
"mcpServers": {
"context7": {
"command": "npx",
"args": ["-y", "@upstash/context7-mcp"],
"env": {
"CONTEXT7_API_KEY": "YOUR_API_KEY"
}
}
}
}
```
<details>
<summary><b>Local Configuration Example</b></summary>

View File

@ -23,7 +23,7 @@ const DEFAULT_PORT = 3000;
const program = new Command()
.option("--transport <stdio|http>", "transport type", "stdio")
.option("--port <number>", "port for HTTP transport", DEFAULT_PORT.toString())
.option("--api-key <key>", "API key for authentication")
.option("--api-key <key>", "API key for authentication (or set CONTEXT7_API_KEY env var)")
.allowUnknownOption() // let MCP Inspector / other wrappers pass through extra flags
.parse(process.argv);
@ -415,7 +415,8 @@ async function main() {
startServer(initialPort);
} else {
// Stdio transport - this is already stateless by nature
const server = createServerInstance(undefined, cliOptions.apiKey);
const apiKey = cliOptions.apiKey || process.env.CONTEXT7_API_KEY;
const server = createServerInstance(undefined, apiKey);
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Context7 Documentation MCP Server running on stdio");