Context7 Platform -- Up-to-date docs for AI agents
fix(context7): validate CLI port strictly (#3403)
CRYPTONIKAV6a54ec3e3612
diff --git a/.changeset/mcp-strict-port-validation.md b/.changeset/mcp-strict-port-validation.mdnew file mode 100644index 00000000..a9f9e83f--- /dev/null+++ b/.changeset/mcp-strict-port-validation.md@@ -0,0 +1,5 @@+---+"@upstash/context7-mcp": patch+---++`--port` rejects values that are not an integer from 0 to 65535, and the port-in-use retry stops at 65535 instead of crashing.diff --git a/packages/mcp/src/index.ts b/packages/mcp/src/index.tsindex 198c4dc2..989e2c4d 100644--- a/packages/mcp/src/index.ts+++ b/packages/mcp/src/index.ts@@ -18,7 +18,7 @@ import {envelopeClientInfo,} from "./lib/utils.js";import express from "express";-import { Command } from "commander";+import { Command, InvalidArgumentError } from "commander";import { AsyncLocalStorage } from "async_hooks";import { randomUUID } from "node:crypto";import {@@ -74,14 +74,26 @@ function getPluginFromRequest(req: express.Request): typeof CLAUDE_CODE_PLUGIN |const program = new Command().version(SERVER_VERSION, "-v, --version", "output the current version").option("--transport <stdio|http>", "transport type", "stdio")- .option("--port <number>", "port for HTTP transport", DEFAULT_PORT.toString())+ .option(+ "--port <number>",+ "port for HTTP transport",+ (value: string) => {+ // 0 is valid and means "let the OS pick", which #3000 made reportable.+ // Everything else is a plain integer inside the TCP port range.+ if (!/^\d+$/.test(value) || Number(value) > 65535) {+ throw new InvalidArgumentError("Must be an integer 0-65535.");+ }+ return Number(value);+ },+ DEFAULT_PORT+ ).option("--api-key <key>", "API key for authentication (or set CONTEXT7_API_KEY env var)").allowUnknownOption() // let MCP Inspector / other wrappers pass through extra flags.parse(process.argv);const cliOptions = program.opts<{transport: string;- port: string;+ port: number;apiKey?: string;}>();@@ -113,12 +125,6 @@ if (TRANSPORT_TYPE === "stdio" && passedPortFlag) {process.exit(1);}-// HTTP port configuration-const CLI_PORT = (() => {- const parsed = parseInt(cliOptions.port, 10);- return isNaN(parsed) ? undefined : parsed;-})();-const requestContext = new AsyncLocalStorage<ClientContext>();// Global state for stdio mode only@@ -465,7 +471,8 @@ async function main() {});if (TRANSPORT_TYPE === "http") {- const initialPort = CLI_PORT ?? DEFAULT_PORT;+ // Commander parsed and range-checked this already; 0 means ephemeral.+ const initialPort = cliOptions.port;const app = express();// Only private/local infrastructure may supply forwarding headers. Express@@ -798,7 +805,9 @@ async function main() {activeHttpServer = httpServer;httpServer.once("error", (err: NodeJS.ErrnoException) => {- if (err.code === "EADDRINUSE" && port < initialPort + maxAttempts) {+ // Stop at the top of the TCP range: the next listen would be 65536,+ // which throws inside this handler instead of retrying (#3387).+ if (err.code === "EADDRINUSE" && port < 65535 && port < initialPort + maxAttempts) {console.warn(`Port ${port} is in use, trying port ${port + 1}...`);startServer(port + 1, maxAttempts);} else {