Skip to content

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

test: add integration tests for CONTEXT7_API_KEY env var

Cedric Hurst1324ee14b2e5
diff --git a/bun.lock b/bun.lock
index 6d92d4e1..f6179da9 100644
--- a/bun.lock
+++ b/bun.lock
@@ -13,6 +13,7 @@
"@types/node": "^22.13.14",
"@typescript-eslint/eslint-plugin": "^8.28.0",
"@typescript-eslint/parser": "^8.28.0",
+ "bun-types": "^1.2.23",
"eslint": "^9.34.0",
"eslint-config-prettier": "^10.1.1",
"eslint-plugin-prettier": "^5.2.5",
@@ -65,6 +66,8 @@
"@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-Zs/Ollc1SJ8nKUAgc7ivOEdIBM8JAKgrqqUYi2J997JuKO7/tpQC+WCetQ1sypiKCQWHdvdg9wBNpUPEWZae7w=="],
+ "@types/react": ["@types/[email protected]", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-1LOH8xovvsKsCBq1wnT4ntDUdCJKmnEakhsuoUSy6ExlHCkGP2hqnatagYTgFk6oeL0VU31u7SNjunPN+GchtA=="],
+
"@typescript-eslint/eslint-plugin": ["@typescript-eslint/[email protected]", "", { "dependencies": { "@eslint-community/regexpp": "^4.10.0", "@typescript-eslint/scope-manager": "8.41.0", "@typescript-eslint/type-utils": "8.41.0", "@typescript-eslint/utils": "8.41.0", "@typescript-eslint/visitor-keys": "8.41.0", "graphemer": "^1.4.0", "ignore": "^7.0.0", "natural-compare": "^1.4.0", "ts-api-utils": "^2.1.0" }, "peerDependencies": { "@typescript-eslint/parser": "^8.41.0", "eslint": "^8.57.0 || ^9.0.0", "typescript": ">=4.8.4 <6.0.0" } }, "sha512-8fz6oa6wEKZrhXWro/S3n2eRJqlRcIa6SlDh59FXJ5Wp5XRZ8B9ixpJDcjadHq47hMx0u+HW6SNa6LjJQ6NLtw=="],
"@typescript-eslint/parser": ["@typescript-eslint/[email protected]", "", { "dependencies": { "@typescript-eslint/scope-manager": "8.41.0", "@typescript-eslint/types": "8.41.0", "@typescript-eslint/typescript-estree": "8.41.0", "@typescript-eslint/visitor-keys": "8.41.0", "debug": "^4.3.4" }, "peerDependencies": { "eslint": "^8.57.0 || ^9.0.0", "typescript": ">=4.8.4 <6.0.0" } }, "sha512-gTtSdWX9xiMPA/7MV9STjJOOYtWwIJIYxkQxnSV1U3xcE+mnJSH3f6zI0RYP+ew66WSlZ5ed+h0VCxsvdC1jJg=="],
@@ -105,6 +108,8 @@
"braces": ["[email protected]", "", { "dependencies": { "fill-range": "^7.1.1" } }, "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA=="],
+ "bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-R9f0hKAZXgFU3mlrA0YpE/fiDvwV0FT9rORApt2aQVWSuJDzZOyB5QLc0N/4HF57CS8IXJ6+L5E4W1bW6NS2Aw=="],
+
"bytes": ["[email protected]", "", {}, "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg=="],
"call-bind-apply-helpers": ["[email protected]", "", { "dependencies": { "es-errors": "^1.3.0", "function-bind": "^1.1.2" } }, "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ=="],
@@ -135,6 +140,8 @@
"cross-spawn": ["[email protected]", "", { "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", "which": "^2.0.1" } }, "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA=="],
+ "csstype": ["[email protected]", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="],
+
"debug": ["[email protected]", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-6WTZ/IxCY/T6BALoZHaE4ctp9xm+Z5kY/pzYaCHRFeyVhojxlrm+46y68HA6hr0TcwEssoxNiDEUJQjfPZ/RYA=="],
"deep-is": ["[email protected]", "", {}, "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ=="],
diff --git a/package.json b/package.json
index cbfed7d8..03d3a4f5 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "MCP server for Context7",
"scripts": {
- "test": "echo \"Error: no test specified\" && exit 1",
+ "test": "bun test",
"build": "tsc && chmod 755 dist/index.js",
"format": "prettier --write .",
"lint": "eslint \"**/*.{js,ts,tsx}\" --fix",
@@ -47,6 +47,7 @@
"@types/node": "^22.13.14",
"@typescript-eslint/eslint-plugin": "^8.28.0",
"@typescript-eslint/parser": "^8.28.0",
+ "bun-types": "^1.2.23",
"eslint": "^9.34.0",
"eslint-config-prettier": "^10.1.1",
"eslint-plugin-prettier": "^5.2.5",
diff --git a/src/__tests__/api-key-env-var.test.ts b/src/__tests__/api-key-env-var.test.ts
new file mode 100644
index 00000000..4c8e272a
--- /dev/null
+++ b/src/__tests__/api-key-env-var.test.ts
@@ -0,0 +1,118 @@
+import { describe, test, expect, beforeAll } from "bun:test";
+import { spawn } from "child_process";
+import { promisify } from "util";
+
+const sleep = promisify(setTimeout);
+
+describe("CONTEXT7_API_KEY environment variable", () => {
+ const serverPath = "./dist/index.js";
+ let serverBuilt = false;
+
+ beforeAll(async () => {
+ // Check if dist/index.js exists, if not skip tests
+ try {
+ await Bun.file(serverPath).text();
+ serverBuilt = true;
+ } catch {
+ console.warn("Dist folder not built, skipping integration tests");
+ }
+ });
+
+ test("uses CONTEXT7_API_KEY env var when --api-key flag not provided", async () => {
+ if (!serverBuilt) {
+ console.log("Skipping test - server not built");
+ return;
+ }
+
+ const testApiKey = "test-env-api-key";
+ const child = spawn("node", [serverPath, "--transport", "stdio"], {
+ env: { ...process.env, CONTEXT7_API_KEY: testApiKey },
+ });
+
+ let stderr = "";
+ child.stderr?.on("data", (data) => {
+ stderr += data.toString();
+ });
+
+ // Give it a moment to start
+ await sleep(1000);
+
+ // Kill the process
+ child.kill();
+
+ // Wait for process to exit
+ await new Promise((resolve) => child.on("exit", resolve));
+
+ // Should start successfully (stderr contains startup message, not errors)
+ expect(stderr).toContain("Context7 Documentation MCP Server running on stdio");
+ });
+
+ test("CLI flag takes precedence over env var", async () => {
+ if (!serverBuilt) {
+ console.log("Skipping test - server not built");
+ return;
+ }
+
+ const envApiKey = "test-env-api-key";
+ const cliApiKey = "test-cli-api-key";
+ const child = spawn("node", [serverPath, "--transport", "stdio", "--api-key", cliApiKey], {
+ env: { ...process.env, CONTEXT7_API_KEY: envApiKey },
+ });
+
+ let stderr = "";
+ child.stderr?.on("data", (data) => {
+ stderr += data.toString();
+ });
+
+ await sleep(1000);
+ child.kill();
+ await new Promise((resolve) => child.on("exit", resolve));
+
+ // Should start successfully
+ expect(stderr).toContain("Context7 Documentation MCP Server running on stdio");
+ });
+
+ test("works without API key (neither flag nor env var)", async () => {
+ if (!serverBuilt) {
+ console.log("Skipping test - server not built");
+ return;
+ }
+
+ const child = spawn("node", [serverPath, "--transport", "stdio"], {
+ env: { ...process.env, CONTEXT7_API_KEY: undefined },
+ });
+
+ let stderr = "";
+ child.stderr?.on("data", (data) => {
+ stderr += data.toString();
+ });
+
+ await sleep(1000);
+ child.kill();
+ await new Promise((resolve) => child.on("exit", resolve));
+
+ // Should still start (with lower rate limits)
+ expect(stderr).toContain("Context7 Documentation MCP Server running on stdio");
+ });
+
+ test("--api-key flag blocked with HTTP transport", async () => {
+ if (!serverBuilt) {
+ console.log("Skipping test - server not built");
+ return;
+ }
+
+ const child = spawn("node", [serverPath, "--transport", "http", "--api-key", "test-key"]);
+
+ let stderr = "";
+ child.stderr?.on("data", (data) => {
+ stderr += data.toString();
+ });
+
+ await new Promise((resolve) => child.on("exit", resolve));
+
+ // Should error and exit
+ expect(stderr).toContain(
+ "The --api-key flag is not allowed when using --transport http"
+ );
+ });
+});
diff --git a/tsconfig.json b/tsconfig.json
index 81dad85b..d4aa479b 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -8,8 +8,9 @@
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
- "forceConsistentCasingInFileNames": true
+ "forceConsistentCasingInFileNames": true,
+ "types": ["bun-types"]
},
"include": ["src/**/*"],
- "exclude": ["node_modules"]
+ "exclude": ["node_modules", "src/**/*.test.ts"]
}