Skip to content

Context7 Platform -- Up-to-date docs for AI agents

feat: add CONTEXT7_API_KEY environment variable support

Cedric Hurstaa8f40790661
diff --git a/README.md b/README.md
index 26cd9da4..b4c113db 100644
--- a/README.md
+++ b/README.md
@@ -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.
- `--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 +1150,45 @@ 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 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>
<summary><b>Local Configuration Example</b></summary>
diff --git a/src/index.ts b/src/index.ts
index 35c9ac13..edaabb57 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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");