Skip to content

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

refactor: header generation and ip encryption

enesgules2ba1b619609f
diff --git a/src/lib/api.ts b/src/lib/api.ts
index 41d4e7a7..208cafab 100644
--- a/src/lib/api.ts
+++ b/src/lib/api.ts
@@ -1,39 +1,9 @@
import { SearchResponse } from "./types.js";
-import { createCipheriv, randomBytes } from "crypto";
+import { generateHeaders } from "./encryption.js";
const CONTEXT7_API_BASE_URL = "https://context7.com/api";
const DEFAULT_TYPE = "txt";
-// Encryption configuration
-const ENCRYPTION_KEY =
- process.env.CLIENT_IP_ENCRYPTION_KEY ||
- "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
-const ALGORITHM = "aes-256-cbc";
-
-// Validate encryption key
-function validateEncryptionKey(key: string): boolean {
- // Must be exactly 64 hex characters (32 bytes)
- return /^[0-9a-fA-F]{64}$/.test(key);
-}
-
-function encryptClientIp(clientIp: string): string {
- if (!validateEncryptionKey(ENCRYPTION_KEY)) {
- console.error("Invalid encryption key format. Must be 64 hex characters.");
- return clientIp; // Fallback to unencrypted
- }
-
- try {
- const iv = randomBytes(16);
- const cipher = createCipheriv(ALGORITHM, Buffer.from(ENCRYPTION_KEY, "hex"), iv);
- let encrypted = cipher.update(clientIp, "utf8", "hex");
- encrypted += cipher.final("hex");
- return iv.toString("hex") + ":" + encrypted;
- } catch (error) {
- console.error("Error encrypting client IP:", error);
- return clientIp; // Fallback to unencrypted
- }
-}
-
/**
* Searches for libraries matching the given query
* @param query The search query
@@ -45,10 +15,7 @@ export async function searchLibraries(query: string, clientIp?: string): Promise
const url = new URL(`${CONTEXT7_API_BASE_URL}/v1/search`);
url.searchParams.set("query", query);
- const headers: Record<string, string> = {};
- if (clientIp) {
- headers["mcp-client-ip"] = encryptClientIp(clientIp);
- }
+ const headers = generateHeaders(clientIp);
const response = await fetch(url, { headers });
if (!response.ok) {
@@ -97,12 +64,7 @@ export async function fetchLibraryDocumentation(
if (options.topic) url.searchParams.set("topic", options.topic);
url.searchParams.set("type", DEFAULT_TYPE);
- const headers: Record<string, string> = {
- "X-Context7-Source": "mcp-server",
- };
- if (clientIp) {
- headers["mcp-client-ip"] = encryptClientIp(clientIp);
- }
+ const headers = generateHeaders(clientIp, { "X-Context7-Source": "mcp-server" });
const response = await fetch(url, { headers });
if (!response.ok) {
diff --git a/src/lib/encryption.ts b/src/lib/encryption.ts
new file mode 100644
index 00000000..fae084b5
--- /dev/null
+++ b/src/lib/encryption.ts
@@ -0,0 +1,40 @@
+import { createCipheriv, randomBytes } from "crypto";
+
+export const ENCRYPTION_KEY =
+ process.env.CLIENT_IP_ENCRYPTION_KEY ||
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
+export const ALGORITHM = "aes-256-cbc";
+
+export function validateEncryptionKey(key: string): boolean {
+ // Must be exactly 64 hex characters (32 bytes)
+ return /^[0-9a-fA-F]{64}$/.test(key);
+}
+
+export function encryptClientIp(clientIp: string): string {
+ if (!validateEncryptionKey(ENCRYPTION_KEY)) {
+ console.error("Invalid encryption key format. Must be 64 hex characters.");
+ return clientIp; // Fallback to unencrypted
+ }
+
+ try {
+ const iv = randomBytes(16);
+ const cipher = createCipheriv(ALGORITHM, Buffer.from(ENCRYPTION_KEY, "hex"), iv);
+ let encrypted = cipher.update(clientIp, "utf8", "hex");
+ encrypted += cipher.final("hex");
+ return iv.toString("hex") + ":" + encrypted;
+ } catch (error) {
+ console.error("Error encrypting client IP:", error);
+ return clientIp; // Fallback to unencrypted
+ }
+}
+
+export function generateHeaders(
+ clientIp?: string,
+ extraHeaders: Record<string, string> = {}
+): Record<string, string> {
+ const headers: Record<string, string> = { ...extraHeaders };
+ if (clientIp) {
+ headers["mcp-client-ip"] = encryptClientIp(clientIp);
+ }
+ return headers;
+}