upstash/context7 · info
Port is in use, trying port ...
Error message
Port ${port} is in use, trying port ${port + 1}... What it means
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.
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
Example fix
// before PORT=3000 npx mcp-server // after PORT=3100 npx mcp-server # or kill the process on 3000 first
Defensive patterns
Strategy: retry
Validate before calling
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); }); Type guard
function isAddrInUse(err: unknown): err is NodeJS.ErrnoException { return err instanceof Error && (err as NodeJS.ErrnoException).code === 'EADDRINUSE'; } Try / catch
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); } }); Prevention
- 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
When it happens
Trigger: 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.
Common situations: Running two MCP servers concurrently; previous instance not killed; another dev service on the same port; container port mapping conflicts.
Related errors
- A fetch implementation is required
- Authentication discovery was redirected. Pass the final…
- authentication_error
- await describeErrorResponse(response, fallback)
- Context7UrlError
AI-assisted analysis of upstash/context7@4416fb855b (2026-09-16).
Data as JSON: /api/errors/9dbf5dcc9424ffc1.
Report an issue: GitHub.
Appendix: source
Thrown at packages/mcp/src/index.ts:651
.map((result) => result.reason);
if (failures.length > 0) {
throw new AggregateError(failures, "MCP HTTP server failed to close cleanly");
}
},
},
{
flush: forceFlushTelemetry,
onerror: (error) => console.error("Failed to close MCP HTTP server:", error),
}
);
const startServer = (port: number, maxAttempts = 10) => {
const httpServer = app.listen(port);
activeHttpServer = httpServer;
httpServer.once("error", (err: NodeJS.ErrnoException) => {
if (err.code === "EADDRINUSE" && port < initialPort + maxAttempts) {
console.warn(`Port ${port} is in use, trying port ${port + 1}...`);
startServer(port + 1, maxAttempts);
} else {
console.error(`Failed to start server: ${err.message}`);
process.exit(1);
}
});
httpServer.once("listening", () => {
console.error(
`Context7 Documentation MCP Server v${SERVER_VERSION} running on HTTP at http://localhost:${port}/mcp`
);
});
};
startServer(initialPort);
} else {
stdioApiKey = cliOptions.apiKey || process.env.CONTEXT7_API_KEY;
stdioSessionId = randomUUID();View on GitHub (pinned to 4416fb855b)