{"record":{"id":"8d1598b5d571166c","repo":"linshenkx/prompt-optimizer","slug":"http-port-must-be-between-1-and-65535","errorCode":null,"errorMessage":"HTTP port must be between 1 and 65535","messagePattern":"HTTP port must be between 1 and 65535","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mcp-server/src/config/environment.ts","lineNumber":99,"sourceCode":"export interface MCPServerConfig {\n  httpPort: number;\n  logLevel: 'debug' | 'info' | 'warn' | 'error';\n  defaultLanguage: string;\n  preferredModelProvider?: string;\n}\n\nexport function loadConfig(): MCPServerConfig {\n  return {\n    httpPort: parseInt(process.env.MCP_HTTP_PORT || '3000'),\n    logLevel: (process.env.MCP_LOG_LEVEL as 'debug' | 'info' | 'warn' | 'error') || 'debug',\n    defaultLanguage: process.env.MCP_DEFAULT_LANGUAGE || 'en-US',\n    preferredModelProvider: process.env.MCP_DEFAULT_MODEL_PROVIDER\n  };\n}\n\nexport function validateConfig(config: MCPServerConfig): void {\n  if (config.httpPort < 1 || config.httpPort > 65535) {\n    throw new Error('HTTP port must be between 1 and 65535');\n  }\n\n  const validLogLevels = ['debug', 'info', 'warn', 'error'];\n  if (!validLogLevels.includes(config.logLevel)) {\n    throw new Error(`Log level must be one of: ${validLogLevels.join(', ')}`);\n  }\n}\n","sourceCodeStart":81,"sourceCodeEnd":107,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/mcp-server/src/config/environment.ts#L81-L107","documentation":"validateConfig throws this when MCPServerConfig.httpPort is outside the valid TCP port range (< 1 or > 65535). It guards the HTTP transport port, typically sourced from an environment variable parsed into a number.","triggerScenarios":"Calling validateConfig with httpPort 0, a negative number, or a value above 65535 (e.g. 70000). Typically caused by MCP_HTTP_PORT env var being unset (defaulting incorrectly) or set to a non-routable port number.","commonSituations":"Env var parsed as NaN or 0 when missing; copying a container port like 808080 by typo; using a port from a URL string that includes extra characters; OS assigning port 0 meaning 'random' which this config rejects.","solutions":["Set a valid port (1-65535), e.g. 3000 or 8080, in the environment/config","If the port comes from a string env var, coerce and range-check: const p = Number(process.env.MCP_HTTP_PORT ?? 3000)","Pick an unprivileged port (>1024) to also avoid EACCES at bind time"],"exampleFix":"// before\nconst config = { httpPort: Number(process.env.MCP_HTTP_PORT) }; // NaN or 0 when unset\n\n// after\nconst raw = Number(process.env.MCP_HTTP_PORT ?? 3000);\nconst config = { httpPort: Number.isInteger(raw) && raw >= 1 && raw <= 65535 ? raw : 3000 };","handlingStrategy":"validation","validationCode":"const httpPort = Number(process.env.MCP_HTTP_PORT ?? 3000);\nif (!Number.isInteger(httpPort) || httpPort < 1 || httpPort > 65535) {\n  throw new Error(`Invalid port: ${process.env.MCP_HTTP_PORT}`);\n}","typeGuard":"const isValidPort = (p: unknown): p is number =>\n  typeof p === 'number' && Number.isInteger(p) && p >= 1 && p <= 65535;","tryCatchPattern":"try { validateConfig(config); } catch (e) { if ((e as Error).message.includes('HTTP port')) { config.httpPort = 3000; validateConfig(config); } else throw e; }","preventionTips":["Always default and range-check env-sourced ports before building config","Use unprivileged ports (1024-65535) to avoid both this error and bind-time EACCES"],"tags":["configuration","port","validation","mcp-server"],"backgroundTag":"invalid-port-configuration","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}