2025-04-17 01:03:45 +03:00
|
|
|
import { SearchResponse } from "./types.js";
|
2025-07-18 18:18:55 +03:00
|
|
|
import { createCipheriv, createDecipheriv, 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
|
|
|
|
|
const ENCRYPTION_KEY = process.env.CLIENT_IP_ENCRYPTION_KEY || "default";
|
|
|
|
|
const ALGORITHM = 'aes-256-cbc';
|
|
|
|
|
|
|
|
|
|
function encryptClientIp(clientIp: string): string {
|
|
|
|
|
|
|
|
|
|
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 decryptClientIp(encryptedIp: string): string | null {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const parts = encryptedIp.split(':');
|
|
|
|
|
if (parts.length !== 2) {
|
|
|
|
|
// Not encrypted, return as-is
|
|
|
|
|
return encryptedIp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const iv = Buffer.from(parts[0], 'hex');
|
|
|
|
|
const encrypted = parts[1];
|
|
|
|
|
const decipher = createDecipheriv(ALGORITHM, Buffer.from(ENCRYPTION_KEY, 'hex'), iv);
|
|
|
|
|
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
|
|
|
|
decrypted += decipher.final('utf8');
|
|
|
|
|
return decrypted;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error decrypting client IP:", error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 11:10:45 +03:00
|
|
|
|
|
|
|
|
const headers: Record<string, string> = {};
|
|
|
|
|
if (clientIp) {
|
2025-07-18 18:18:55 +03:00
|
|
|
headers["X-Client-IP"] = encryptClientIp(clientIp);
|
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 11:10:45 +03:00
|
|
|
|
|
|
|
|
const headers: Record<string, string> = {
|
|
|
|
|
"X-Context7-Source": "mcp-server",
|
|
|
|
|
};
|
|
|
|
|
if (clientIp) {
|
2025-07-18 18:18:55 +03:00
|
|
|
headers["X-Client-IP"] = encryptClientIp(clientIp);
|
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
|
|
|
}
|
|
|
|
|
}
|