Context7 Platform -- Up-to-date docs for AI agents
CTX7-272: ip address header
buggyhuntere1b6d7caaeb6
diff --git a/src/index.ts b/src/index.tsindex bc1b3f9b..898e30f8 100644--- a/src/index.ts+++ b/src/index.ts@@ -10,6 +10,7 @@ import { createServer } from "http";import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";import { Command } from "commander";+import { IncomingMessage } from "http";const DEFAULT_MINIMUM_TOKENS = 10000;@@ -46,8 +47,12 @@ const CLI_PORT = (() => {// Store SSE transports by session IDconst sseTransports: Record<string, SSEServerTransport> = {};+function getClientIp(req: IncomingMessage): string | undefined {+ return req.socket?.remoteAddress || undefined;+}+// Function to create a new server instance with all tools registered-function createServerInstance() {+function createServerInstance(clientIp?: string) {const server = new McpServer({name: "Context7",@@ -87,7 +92,7 @@ For ambiguous queries, request clarification before proceeding with a best-guess.describe("Library name to search for and retrieve a Context7-compatible library ID."),},async ({ libraryName }) => {- const searchResponse: SearchResponse = await searchLibraries(libraryName);+ const searchResponse: SearchResponse = await searchLibraries(libraryName, clientIp);if (!searchResponse.results || searchResponse.results.length === 0) {return {@@ -154,7 +159,7 @@ ${resultsText}`,const fetchDocsResponse = await fetchLibraryDocumentation(context7CompatibleLibraryID, {tokens,topic,- });+ }, clientIp);if (!fetchDocsResponse) {return {@@ -205,8 +210,11 @@ async function main() {}try {+ // Extract client IP address using socket remote address (most reliable)+ const clientIp = getClientIp(req);+// Create new server instance for each request- const requestServer = createServerInstance();+ const requestServer = createServerInstance(clientIp);if (url === "/mcp") {const transport = new StreamableHTTPServerTransport({diff --git a/src/lib/api.ts b/src/lib/api.tsindex 026e1b0e..2a27ff80 100644--- a/src/lib/api.ts+++ b/src/lib/api.ts@@ -6,13 +6,20 @@ const DEFAULT_TYPE = "txt";/*** Searches for libraries matching the given query* @param query The search query+ * @param clientIp Optional client IP address to include in headers* @returns Search results or null if the request fails*/-export async function searchLibraries(query: string): Promise<SearchResponse> {+export async function searchLibraries(query: string, clientIp?: string): Promise<SearchResponse> {try {const url = new URL(`${CONTEXT7_API_BASE_URL}/v1/search`);url.searchParams.set("query", query);- const response = await fetch(url);++ const headers: Record<string, string> = {};+ if (clientIp) {+ headers["X-Client-IP"] = clientIp;+ }++ const response = await fetch(url, { headers });if (!response.ok) {const errorCode = response.status;if (errorCode === 429) {@@ -39,6 +46,7 @@ export async function searchLibraries(query: string): Promise<SearchResponse> {* Fetches documentation context for a specific library* @param libraryId The library ID to fetch documentation for* @param options Options for the request+ * @param clientIp Optional client IP address to include in headers* @returns The documentation text or null if the request fails*/export async function fetchLibraryDocumentation(@@ -46,7 +54,8 @@ export async function fetchLibraryDocumentation(options: {tokens?: number;topic?: string;- } = {}+ } = {},+ clientIp?: string): Promise<string | null> {try {if (libraryId.startsWith("/")) {@@ -56,11 +65,15 @@ export async function fetchLibraryDocumentation(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);- const response = await fetch(url, {- headers: {- "X-Context7-Source": "mcp-server",- },- });++ const headers: Record<string, string> = {+ "X-Context7-Source": "mcp-server",+ };+ if (clientIp) {+ headers["X-Client-IP"] = clientIp;+ }++ const response = await fetch(url, { headers });if (!response.ok) {const errorCode = response.status;if (errorCode === 429) {