mirror of
https://github.com/upstash/context7.git
synced 2025-06-26 23:50:04 +00:00
feat: add port fallback mechanism when initial port is in use
This commit is contained in:
parent
f8472ff1ec
commit
d513d1a59f
32
src/index.ts
32
src/index.ts
@ -177,7 +177,10 @@ async function main() {
|
||||
const transportType = process.env.MCP_TRANSPORT || "stdio";
|
||||
|
||||
if (transportType === "http" || transportType === "sse") {
|
||||
const port = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
|
||||
// Get initial port from environment or use default
|
||||
const initialPort = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
|
||||
// Keep track of which port we end up using
|
||||
let actualPort = initialPort;
|
||||
const httpServer = createServer(async (req, res) => {
|
||||
const url = parse(req.url || "").pathname;
|
||||
|
||||
@ -250,11 +253,28 @@ async function main() {
|
||||
}
|
||||
});
|
||||
|
||||
httpServer.listen(port, () => {
|
||||
console.error(
|
||||
`Context7 Documentation MCP Server running on ${transportType.toUpperCase()} at http://localhost:${port}/mcp and legacy SSE at /sse`
|
||||
);
|
||||
});
|
||||
// Function to attempt server listen with port fallback
|
||||
const startServer = (port: number, maxAttempts = 10) => {
|
||||
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.listen(port, () => {
|
||||
actualPort = port;
|
||||
console.error(
|
||||
`Context7 Documentation MCP Server running on ${transportType.toUpperCase()} at http://localhost:${actualPort}/mcp and legacy SSE at /sse`
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
// Start the server with initial port
|
||||
startServer(initialPort);
|
||||
} else {
|
||||
// Stdio transport - this is already stateless by nature
|
||||
const server = createServerInstance();
|
||||
|
Loading…
x
Reference in New Issue
Block a user