Skip to content

Conversation

divideby0
Copy link
Contributor

@divideby0 divideby0 commented Oct 3, 2025

Summary

Adds support for CONTEXT7_API_KEY environment variable as a fallback when the --api-key CLI flag is not provided.

Motivation

Currently, the API key can only be passed via the --api-key CLI argument, which makes it difficult to:

  • Store API keys securely in .env files (best practice for secret management)
  • Use with tools that prefer environment variable configuration
  • Integrate with MCP server setups that use dotenv for credential management

Changes

Core Feature (Commit 1):

  • Added env var fallback in src/index.ts: cliOptions.apiKey || process.env.CONTEXT7_API_KEY
  • CLI flag still takes precedence when both are present
  • Updated --api-key option description to document env var support
  • Updated README.md with comprehensive environment variable usage examples and MCP configuration

Optional Testing (Commits 2-3):

  • Integration tests using Bun's test runner (can be reverted if not desired)
  • GitHub Actions workflow enhancement (can be reverted if not desired)

Behavior

  1. If --api-key flag is provided → uses CLI value (existing behavior)
  2. If --api-key flag is NOT provided → falls back to CONTEXT7_API_KEY env var
  3. If neither is provided → runs without API key (existing behavior, lower rate limits)

Usage Example: Secure Key Management with dotenv-cli

For enhanced security, you can use dotenv-cli to load API keys from a local .env file that's excluded from version control:

.env.mcp.local (add to .gitignore):

CONTEXT7_API_KEY=your_api_key_here

MCP Configuration (e.g., .mcp.json for Claude Code):

{
  "mcpServers": {
    "context7": {
      "type": "stdio",
      "command": "npx",
      "args": [
        "-y",
        "dotenv-cli",
        "-e",
        ".env.mcp.local",
        "--",
        "npx",
        "-y",
        "@upstash/context7-mcp"
      ]
    }
  }
}

Testing

Manually tested with:

  • --api-key flag only (existing behavior works)
  • CONTEXT7_API_KEY env var only (new fallback works)
  • Both provided (CLI flag takes precedence)
  • Neither provided (works without auth, lower rate limits)

Optional: Integration Tests (commits 2-3)

Note: The last two commits add integration tests and CI workflow updates. These are optional and can be reverted before merge if the project maintainers prefer not to include them at this time.

Commit 2 (1324ee1): test: add integration tests for CONTEXT7_API_KEY env var

  • Adds Bun test infrastructure (bun test in package.json)
  • Creates src/__tests__/api-key-env-var.test.ts with integration tests
  • Tests all four scenarios listed above programmatically
  • Updates tsconfig.json to exclude test files from build

Commit 3 (e2c6cbb): ci: add test step to GitHub Actions workflow

  • Adds bun test step to .github/workflows/check.yaml
  • Runs tests after build on all PRs and master pushes

To use only the core feature (commit 1), you can:

# Cherry-pick only the feature commit
git cherry-pick aa8f407

All tests pass locally (8/8) and validate the expected behavior.

Breaking Changes

None - this is purely additive. All existing configurations continue to work exactly as before.

🤖 Generated with Claude Code

divideby0 and others added 3 commits October 2, 2025 18:38
- Add bun test infrastructure
- Test env var fallback behavior
- Test CLI flag precedence over env var
- Test operation without API key
- Test HTTP transport blocks --api-key flag

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

Co-Authored-By: Claude <[email protected]>
Run bun test after build to verify integration tests

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

Co-Authored-By: Claude <[email protected]>
@enesgules
Copy link
Collaborator

closes #656

Copy link

@txzzitxxx txzzitxxx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

M

@enesgules
Copy link
Collaborator

I think tests are a bit overkill for such a simple change so I will remove them and merge after. Thanks for the contribution @divideby0 !

@enesgules
Copy link
Collaborator

i don't have read access so moving with #757

@enesgules enesgules closed this Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants