{"record":{"id":"9dbf5dcc9424ffc1","repo":"upstash/context7","slug":"port-port-is-in-use-trying-port-port-1","errorCode":null,"errorMessage":"Port ${port} is in use, trying port ${port + 1}...","messagePattern":"Port (.+?) is in use, trying port (.+?)\\.\\.\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"info","filePath":"packages/mcp/src/index.ts","lineNumber":651,"sourceCode":"            .map((result) => result.reason);\n          if (failures.length > 0) {\n            throw new AggregateError(failures, \"MCP HTTP server failed to close cleanly\");\n          }\n        },\n      },\n      {\n        flush: forceFlushTelemetry,\n        onerror: (error) => console.error(\"Failed to close MCP HTTP server:\", error),\n      }\n    );\n\n    const startServer = (port: number, maxAttempts = 10) => {\n      const httpServer = app.listen(port);\n      activeHttpServer = httpServer;\n\n      httpServer.once(\"error\", (err: NodeJS.ErrnoException) => {\n        if (err.code === \"EADDRINUSE\" && port < initialPort + maxAttempts) {\n          console.warn(`Port ${port} is in use, trying port ${port + 1}...`);\n          startServer(port + 1, maxAttempts);\n        } else {\n          console.error(`Failed to start server: ${err.message}`);\n          process.exit(1);\n        }\n      });\n\n      httpServer.once(\"listening\", () => {\n        console.error(\n          `Context7 Documentation MCP Server v${SERVER_VERSION} running on HTTP at http://localhost:${port}/mcp`\n        );\n      });\n    };\n\n    startServer(initialPort);\n  } else {\n    stdioApiKey = cliOptions.apiKey || process.env.CONTEXT7_API_KEY;\n    stdioSessionId = randomUUID();","sourceCodeStart":633,"sourceCodeEnd":669,"githubUrl":"https://github.com/upstash/context7/blob/4416fb855b8f752be735e34f943b5d0762701aad/packages/mcp/src/index.ts#L633-L669","documentation":"Console warning from the MCP HTTP server bootstrap: `app.listen(port)` emitted EADDRINUSE, so startServer retries on port+1 up to maxAttempts (10) past the initial port. It is informational — the server self-heals — unless all attempts are exhausted, in which case a fatal error and exit(1) follow.","triggerScenarios":"Another process (often a stale previous instance of this MCP server) already holds a socket on the requested port; httpServer 'error' event fires with code EADDRINUSE while port < initialPort + maxAttempts.","commonSituations":"Running two MCP servers concurrently; previous instance not killed; another dev service on the same port; container port mapping conflicts.","solutions":["Let it retry — the server automatically binds the next port","Find and stop the process holding the port (lsof -i :PORT / netstat)","Set a different base port via the server's port option/env","Kill stale instances of the MCP server before restarting"],"exampleFix":"// before\nPORT=3000 npx mcp-server\n// after\nPORT=3100 npx mcp-server  # or kill the process on 3000 first","handlingStrategy":"retry","validationCode":"const free = (p: number) => new Promise(res => { const s = net.createServer(); s.once('error', () => res(false)); s.once('listening', () => s.close(() => res(true))); s.listen(p); });","typeGuard":"function isAddrInUse(err: unknown): err is NodeJS.ErrnoException { return err instanceof Error && (err as NodeJS.ErrnoException).code === 'EADDRINUSE'; }","tryCatchPattern":"httpServer.once('error', (err: NodeJS.ErrnoException) => { if (isAddrInUse(err) && attemptsLeft) startServer(port + 1); else { console.error(`Failed to start server: ${err.message}`); process.exit(1); } });","preventionTips":["Pick a non-default base port for concurrent instances","Kill stale server processes before restart (lsof -i :PORT)","Use port 0 (ephemeral) when the exact port doesn't matter"],"tags":["http","port","server-startup"],"backgroundTag":"address-already-in-use","analyzedSha":"4416fb855b8f752be735e34f943b5d0762701aad","analyzedAt":"2026-09-16T20:28:07.148Z","contentChangedAt":"2026-09-16T20:28:07.148Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}