feat: add support for case-insensitive API key headers and improve error handling

This commit is contained in:
enesgules 2025-08-19 17:53:06 +03:00
parent e1fff9ac39
commit 1bf5f4fdeb
2 changed files with 30 additions and 7 deletions

View File

@ -295,9 +295,14 @@ async function main() {
// Check headers in order of preference
const 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");

View File

@ -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;
}