Context7 Platform -- Up-to-date docs for AI agents
fix: use stateless pattern for HTTP transport to prevent connection leaks (#1471)
Enes Gules8a2ad1aba4cc
diff --git a/.changeset/silver-chairs-bet.md b/.changeset/silver-chairs-bet.mdnew file mode 100644index 00000000..7512de55--- /dev/null+++ b/.changeset/silver-chairs-bet.md@@ -0,0 +1,5 @@+---+"@upstash/context7-mcp": patch+---++Use stateless pattern for HTTP transport to prevent connection leaks. Creates a new McpServer instance per HTTP request instead of sharing a single global instance.diff --git a/packages/mcp/src/index.ts b/packages/mcp/src/index.tsindex b4defaef..0c831384 100644--- a/packages/mcp/src/index.ts+++ b/packages/mcp/src/index.ts@@ -119,33 +119,23 @@ function getClientIp(req: express.Request): string | undefined {return undefined;}-const server = new McpServer(- {- name: "Context7",- version: SERVER_VERSION,- },- {- instructions:- "Use this server to retrieve up-to-date documentation and code examples for any library.",- }-);--// Capture client info from MCP initialize handshake-server.server.oninitialized = () => {- const clientVersion = server.server.getClientVersion();- if (clientVersion) {- stdioClientInfo = {- ide: clientVersion.name,- version: clientVersion.version,- };- }-};+function createServer(): McpServer {+ const server = new McpServer(+ {+ name: "Context7",+ version: SERVER_VERSION,+ },+ {+ instructions:+ "Use this server to retrieve up-to-date documentation and code examples for any library.",+ }+ );-server.registerTool(- "resolve-library-id",- {- title: "Resolve Context7 Library ID",- description: `Resolves a package/product name to a Context7-compatible library ID and returns matching libraries.+ server.registerTool(+ "resolve-library-id",+ {+ title: "Resolve Context7 Library ID",+ description: `Resolves a package/product name to a Context7-compatible library ID and returns matching libraries.You MUST call this function before 'query-docs' to obtain a valid Context7-compatible library ID UNLESS the user explicitly provides a library ID in the format '/org/project' or '/org/project/version' in their query.@@ -167,39 +157,39 @@ Response Format:For ambiguous queries, request clarification before proceeding with a best-guess match.IMPORTANT: Do not call this tool more than 3 times per question. If you cannot find what you need after 3 calls, use the best result you have.`,- inputSchema: {- query: z- .string()- .describe(- "The user's original question or task. This is used to rank library results by relevance to what the user is trying to accomplish. IMPORTANT: Do not include any sensitive or confidential information such as API keys, passwords, credentials, or personal data in your query."- ),- libraryName: z- .string()- .describe("Library name to search for and retrieve a Context7-compatible library ID."),- },- annotations: {- readOnlyHint: true,+ inputSchema: {+ query: z+ .string()+ .describe(+ "The user's original question or task. This is used to rank library results by relevance to what the user is trying to accomplish. IMPORTANT: Do not include any sensitive or confidential information such as API keys, passwords, credentials, or personal data in your query."+ ),+ libraryName: z+ .string()+ .describe("Library name to search for and retrieve a Context7-compatible library ID."),+ },+ annotations: {+ readOnlyHint: true,+ },},- },- async ({ query, libraryName }) => {- const searchResponse = await searchLibraries(query, libraryName, getClientContext());-- if (!searchResponse.results || searchResponse.results.length === 0) {- return {- content: [- {- type: "text",- text: searchResponse.error- ? searchResponse.error- : "No libraries found matching the provided name.",- },- ],- };- }+ async ({ query, libraryName }) => {+ const searchResponse = await searchLibraries(query, libraryName, getClientContext());++ if (!searchResponse.results || searchResponse.results.length === 0) {+ return {+ content: [+ {+ type: "text",+ text: searchResponse.error+ ? searchResponse.error+ : "No libraries found matching the provided name.",+ },+ ],+ };+ }- const resultsText = formatSearchResults(searchResponse);+ const resultsText = formatSearchResults(searchResponse);- const responseText = `Available Libraries:+ const responseText = `Available Libraries:Each result includes:- Library ID: Context7-compatible identifier (format: /org/project)@@ -216,55 +206,58 @@ For best results, select libraries based on name match, source reputation, snipp${resultsText}`;- return {- content: [- {- type: "text",- text: responseText,- },- ],- };- }-);+ return {+ content: [+ {+ type: "text",+ text: responseText,+ },+ ],+ };+ }+ );-server.registerTool(- "query-docs",- {- title: "Query Documentation",- description: `Retrieves and queries up-to-date documentation and code examples from Context7 for any programming library or framework.+ server.registerTool(+ "query-docs",+ {+ title: "Query Documentation",+ description: `Retrieves and queries up-to-date documentation and code examples from Context7 for any programming library or framework.You must call 'resolve-library-id' first to obtain the exact Context7-compatible library ID required to use this tool, UNLESS the user explicitly provides a library ID in the format '/org/project' or '/org/project/version' in their query.IMPORTANT: Do not call this tool more than 3 times per question. If you cannot find what you need after 3 calls, use the best information you have.`,- inputSchema: {- libraryId: z- .string()- .describe(- "Exact Context7-compatible library ID (e.g., '/mongodb/docs', '/vercel/next.js', '/supabase/supabase', '/vercel/next.js/v14.3.0-canary.87') retrieved from 'resolve-library-id' or directly from user query in the format '/org/project' or '/org/project/version'."- ),- query: z- .string()- .describe(- "The question or task you need help with. Be specific and include relevant details. Good: 'How to set up authentication with JWT in Express.js' or 'React useEffect cleanup function examples'. Bad: 'auth' or 'hooks'. IMPORTANT: Do not include any sensitive or confidential information such as API keys, passwords, credentials, or personal data in your query."- ),- },- annotations: {- readOnlyHint: true,+ inputSchema: {+ libraryId: z+ .string()+ .describe(+ "Exact Context7-compatible library ID (e.g., '/mongodb/docs', '/vercel/next.js', '/supabase/supabase', '/vercel/next.js/v14.3.0-canary.87') retrieved from 'resolve-library-id' or directly from user query in the format '/org/project' or '/org/project/version'."+ ),+ query: z+ .string()+ .describe(+ "The question or task you need help with. Be specific and include relevant details. Good: 'How to set up authentication with JWT in Express.js' or 'React useEffect cleanup function examples'. Bad: 'auth' or 'hooks'. IMPORTANT: Do not include any sensitive or confidential information such as API keys, passwords, credentials, or personal data in your query."+ ),+ },+ annotations: {+ readOnlyHint: true,+ },},- },- async ({ query, libraryId }) => {- const response = await fetchLibraryContext({ query, libraryId }, getClientContext());-- return {- content: [- {- type: "text",- text: response.data,- },- ],- };- }-);+ async ({ query, libraryId }) => {+ const response = await fetchLibraryContext({ query, libraryId }, getClientContext());++ return {+ content: [+ {+ type: "text",+ text: response.data,+ },+ ],+ };+ }+ );++ return server;+}async function main() {const transportType = TRANSPORT_TYPE;@@ -367,13 +360,16 @@ async function main() {transport: "http",};+ const server = createServer();+const transport = new StreamableHTTPServerTransport({sessionIdGenerator: undefined,enableJsonResponse: true,});- res.on("close", () => {- transport.close();+ res.on("close", async () => {+ await transport.close();+ await server.close();});await requestContext.run(context, async () => {@@ -477,6 +473,18 @@ async function main() {startServer(initialPort);} else {stdioApiKey = cliOptions.apiKey || process.env.CONTEXT7_API_KEY;+ const server = createServer();++ server.server.oninitialized = () => {+ const clientVersion = server.server.getClientVersion();+ if (clientVersion) {+ stdioClientInfo = {+ ide: clientVersion.name,+ version: clientVersion.version,+ };+ }+ };+const transport = new StdioServerTransport();await server.connect(transport);diff --git a/packages/sdk/src/client.test.ts b/packages/sdk/src/client.test.tsindex e1fab938..24f9c50f 100644--- a/packages/sdk/src/client.test.ts+++ b/packages/sdk/src/client.test.ts@@ -70,7 +70,7 @@ describe("Context7 Client", () => {const result = await client.searchLibrary(`I want to use ${query}`, query);expect(result.length).toBeGreaterThan(0);}- });+ }, 15000);});describe("getContext - JSON format (default)", () => {