feat: add CONTEXT7_API_KEY environment variable support

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Cedric Hurst 2025-10-02 18:38:17 -05:00
parent 8a6d3445e2
commit aa8f407906
2 changed files with 43 additions and 3 deletions

View File

@ -1136,7 +1136,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. - `--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`). - `--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: Example with HTTP transport and port 8080:
@ -1150,6 +1150,45 @@ Another example with stdio transport:
bun run dist/index.js --transport stdio --api-key YOUR_API_KEY 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 environment variable:**
```bash
export CONTEXT7_API_KEY=your_api_key_here
npx -y @upstash/context7-mcp
```
**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> <details>
<summary><b>Local Configuration Example</b></summary> <summary><b>Local Configuration Example</b></summary>

View File

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