Context7 Platform -- Up-to-date docs for AI agents
feat: add support for case-insensitive API key headers and improve error handling
enesgules1bf5f4fdeb49
diff --git a/src/index.ts b/src/index.tsindex dd2933d2..cca38f6d 100644--- a/src/index.ts+++ b/src/index.ts@@ -295,9 +295,14 @@ async function main() {// Check headers in order of preferenceconst apiKey =extractBearerToken(req.headers.authorization) ||- extractHeaderValue(req.headers["X-Context7-API-Key"]) || // Standard with x- prefix- extractHeaderValue(req.headers["Context7-API-Key"]) || // Without x- prefix- extractHeaderValue(req.headers["X-API-Key"]); // Generic API key header+ extractHeaderValue(req.headers["Context7-API-Key"]) ||+ extractHeaderValue(req.headers["X-API-Key"]) ||+ extractHeaderValue(req.headers["context7-api-key"]) ||+ extractHeaderValue(req.headers["x-api-key"]) ||+ extractHeaderValue(req.headers["Context7_API_Key"]) ||+ extractHeaderValue(req.headers["X_API_Key"]) ||+ extractHeaderValue(req.headers["context7_api_key"]) ||+ extractHeaderValue(req.headers["x_api_key"]);try {// Extract client IP address using socket remote address (most reliable)@@ -384,7 +389,7 @@ async function main() {startServer(initialPort);} else {// Stdio transport - this is already stateless by nature- const server = createServerInstance(cliOptions.apiKey);+ const server = createServerInstance(undefined, cliOptions.apiKey);const transport = new StdioServerTransport();await server.connect(transport);console.error("Context7 Documentation MCP Server running on stdio");diff --git a/src/lib/api.ts b/src/lib/api.tsindex 57a1f7cc..8e277648 100644--- a/src/lib/api.ts+++ b/src/lib/api.ts@@ -26,10 +26,17 @@ export async function searchLibraries(if (!response.ok) {const errorCode = response.status;if (errorCode === 429) {- console.error(`Rate limited due to too many requests. Please try again later.`);+ 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.`,+ error: "Rate limited due to too many requests. Please try again later.",+ } as SearchResponse;+ }+ if (errorCode === 401) {+ console.error("Unauthorized. Please check your API key.");+ return {+ results: [],+ error: "Unauthorized. Please check your API key.",} as SearchResponse;}console.error(`Failed to search libraries. Please try again later. Error code: ${errorCode}`);@@ -77,7 +84,18 @@ export async function fetchLibraryDocumentation(if (!response.ok) {const errorCode = response.status;if (errorCode === 429) {- const errorMessage = `Rate limited due to too many requests. Please try again later.`;+ const errorMessage = "Rate limited due to too many requests. Please try again later.";+ console.error(errorMessage);+ return errorMessage;+ }+ if (errorCode === 404) {+ const errorMessage =+ "The library you are trying to access does not exist. Please try with a different library ID.";+ console.error(errorMessage);+ return errorMessage;+ }+ if (errorCode === 401) {+ const errorMessage = "Unauthorized. Please check your API key.";console.error(errorMessage);return errorMessage;}