2025-04-17 01:03:45 +03:00
|
|
|
import { SearchResponse } from "./types.js";
|
2025-07-18 19:28:55 +03:00
|
|
|
import { createCipheriv, randomBytes } from "crypto";
|
2025-04-03 11:50:02 +03:00
|
|
|
|
2025-04-17 01:03:45 +03:00
|
|
|
const CONTEXT7_API_BASE_URL = "https://context7.com/api";
|
|
|
|
|
const DEFAULT_TYPE = "txt";
|
2025-04-03 11:50:02 +03:00
|
|
|
|
2025-07-18 18:18:55 +03:00
|
|
|
// Encryption configuration
|
2025-07-18 19:24:48 +03:00
|
|
|
const ENCRYPTION_KEY =
|
|
|
|
|
process.env.CLIENT_IP_ENCRYPTION_KEY ||
|
|
|
|
|
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
|
|
|
|
|
const ALGORITHM = "aes-256-cbc";
|
2025-07-18 18:18:55 +03:00
|
|
|
|
2025-07-18 19:01:03 +03:00
|
|
|
// Validate encryption key
|
|
|
|
|
function validateEncryptionKey(key: string): boolean {
|
|
|
|
|
// Must be exactly 64 hex characters (32 bytes)
|
|
|
|
|
return /^[0-9a-fA-F]{64}$/.test(key);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 18:18:55 +03:00
|
|
|
function encryptClientIp(clientIp: string): string {
|
2025-07-18 19:01:03 +03:00
|
|
|
if (!validateEncryptionKey(ENCRYPTION_KEY)) {
|
|
|
|
|
console.error("Invalid encryption key format. Must be 64 hex characters.");
|
|
|
|
|
return clientIp; // Fallback to unencrypted
|
|
|
|
|
}
|
2025-07-18 19:24:48 +03:00
|
|
|
|
2025-07-18 18:18:55 +03:00
|
|
|
try {
|
|
|
|
|
const iv = randomBytes(16);
|
2025-07-18 19:24:48 +03:00
|
|
|
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;
|
2025-07-18 18:18:55 +03:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error encrypting client IP:", error);
|
|
|
|
|
return clientIp; // Fallback to unencrypted
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 11:50:02 +03:00
|
|
|
/**
|
2025-04-17 01:03:45 +03:00
|
|
|
* Searches for libraries matching the given query
|
|
|
|
|
* @param query The search query
|
2025-07-18 11:10:45 +03:00
|
|
|
* @param clientIp Optional client IP address to include in headers
|
2025-04-17 01:03:45 +03:00
|
|
|
* @returns Search results or null if the request fails
|
2025-04-03 11:50:02 +03:00
|
|
|
*/
|
2025-07-18 11:10:45 +03:00
|
|
|
export async function searchLibraries(query: string, clientIp?: string): Promise<SearchResponse> {
|
2025-04-03 11:50:02 +03:00
|
|
|
try {
|
2025-04-17 01:03:45 +03:00
|
|
|
const url = new URL(`${CONTEXT7_API_BASE_URL}/v1/search`);
|
|
|
|
|
url.searchParams.set("query", query);
|
2025-07-18 19:24:48 +03:00
|
|
|
|
2025-07-18 11:10:45 +03:00
|
|
|
const headers: Record<string, string> = {};
|
|
|
|
|
if (clientIp) {
|
2025-07-18 19:21:46 +03:00
|
|
|
headers["mcp-client-ip"] = encryptClientIp(clientIp);
|
2025-07-18 11:10:45 +03:00
|
|
|
}
|
2025-07-18 19:24:48 +03:00
|
|
|
|
2025-07-18 11:10:45 +03:00
|
|
|
const response = await fetch(url, { headers });
|
2025-04-03 11:50:02 +03:00
|
|
|
if (!response.ok) {
|
2025-06-07 00:20:52 +03:00
|
|
|
const errorCode = response.status;
|
|
|
|
|
if (errorCode === 429) {
|
|
|
|
|
console.error(`Rate limited due to too many requests. Please try again later.`);
|
|
|
|
|
return {
|
|
|
|
|
results: [],
|
|
|
|
|
error: `Rate limited due to too many requests. Please try again later.`,
|
|
|
|
|
} as SearchResponse;
|
|
|
|
|
}
|
|
|
|
|
console.error(`Failed to search libraries. Please try again later. Error code: ${errorCode}`);
|
|
|
|
|
return {
|
|
|
|
|
results: [],
|
|
|
|
|
error: `Failed to search libraries. Please try again later. Error code: ${errorCode}`,
|
|
|
|
|
} as SearchResponse;
|
2025-04-03 11:50:02 +03:00
|
|
|
}
|
|
|
|
|
return await response.json();
|
|
|
|
|
} catch (error) {
|
2025-04-17 01:03:45 +03:00
|
|
|
console.error("Error searching libraries:", error);
|
2025-06-07 00:20:52 +03:00
|
|
|
return { results: [], error: `Error searching libraries: ${error}` } as SearchResponse;
|
2025-04-03 11:50:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-04-04 18:20:07 +03:00
|
|
|
* Fetches documentation context for a specific library
|
2025-04-17 01:03:45 +03:00
|
|
|
* @param libraryId The library ID to fetch documentation for
|
|
|
|
|
* @param options Options for the request
|
2025-07-18 11:10:45 +03:00
|
|
|
* @param clientIp Optional client IP address to include in headers
|
2025-04-03 11:50:02 +03:00
|
|
|
* @returns The documentation text or null if the request fails
|
|
|
|
|
*/
|
2025-04-04 18:20:07 +03:00
|
|
|
export async function fetchLibraryDocumentation(
|
2025-04-17 01:03:45 +03:00
|
|
|
libraryId: string,
|
|
|
|
|
options: {
|
|
|
|
|
tokens?: number;
|
|
|
|
|
topic?: string;
|
2025-07-18 11:10:45 +03:00
|
|
|
} = {},
|
|
|
|
|
clientIp?: string
|
2025-04-03 11:50:02 +03:00
|
|
|
): Promise<string | null> {
|
|
|
|
|
try {
|
2025-04-17 01:03:45 +03:00
|
|
|
if (libraryId.startsWith("/")) {
|
|
|
|
|
libraryId = libraryId.slice(1);
|
2025-04-08 20:29:04 +03:00
|
|
|
}
|
2025-04-17 01:03:45 +03:00
|
|
|
const url = new URL(`${CONTEXT7_API_BASE_URL}/v1/${libraryId}`);
|
|
|
|
|
if (options.tokens) url.searchParams.set("tokens", options.tokens.toString());
|
|
|
|
|
if (options.topic) url.searchParams.set("topic", options.topic);
|
|
|
|
|
url.searchParams.set("type", DEFAULT_TYPE);
|
2025-07-18 19:24:48 +03:00
|
|
|
|
2025-07-18 11:10:45 +03:00
|
|
|
const headers: Record<string, string> = {
|
|
|
|
|
"X-Context7-Source": "mcp-server",
|
|
|
|
|
};
|
|
|
|
|
if (clientIp) {
|
2025-07-18 19:21:46 +03:00
|
|
|
headers["mcp-client-ip"] = encryptClientIp(clientIp);
|
2025-07-18 11:10:45 +03:00
|
|
|
}
|
2025-07-18 19:24:48 +03:00
|
|
|
|
2025-07-18 11:10:45 +03:00
|
|
|
const response = await fetch(url, { headers });
|
2025-04-03 11:50:02 +03:00
|
|
|
if (!response.ok) {
|
2025-06-07 00:20:52 +03:00
|
|
|
const errorCode = response.status;
|
|
|
|
|
if (errorCode === 429) {
|
|
|
|
|
const errorMessage = `Rate limited due to too many requests. Please try again later.`;
|
|
|
|
|
console.error(errorMessage);
|
|
|
|
|
return errorMessage;
|
|
|
|
|
}
|
|
|
|
|
const errorMessage = `Failed to fetch documentation. Please try again later. Error code: ${errorCode}`;
|
|
|
|
|
console.error(errorMessage);
|
|
|
|
|
return errorMessage;
|
2025-04-03 11:50:02 +03:00
|
|
|
}
|
|
|
|
|
const text = await response.text();
|
|
|
|
|
if (!text || text === "No content available" || text === "No context data available") {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
} catch (error) {
|
2025-06-07 00:20:52 +03:00
|
|
|
const errorMessage = `Error fetching library documentation. Please try again later. ${error}`;
|
|
|
|
|
console.error(errorMessage);
|
|
|
|
|
return errorMessage;
|
2025-04-03 11:50:02 +03:00
|
|
|
}
|
|
|
|
|
}
|