linshenkx/prompt-optimizer · error · Error

HTTP port must be between 1 and 65535

Error message

HTTP port must be between 1 and 65535

What it means

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.

Source

Thrown at packages/mcp-server/src/config/environment.ts:99

export interface MCPServerConfig {
  httpPort: number;
  logLevel: 'debug' | 'info' | 'warn' | 'error';
  defaultLanguage: string;
  preferredModelProvider?: string;
}

export function loadConfig(): MCPServerConfig {
  return {
    httpPort: parseInt(process.env.MCP_HTTP_PORT || '3000'),
    logLevel: (process.env.MCP_LOG_LEVEL as 'debug' | 'info' | 'warn' | 'error') || 'debug',
    defaultLanguage: process.env.MCP_DEFAULT_LANGUAGE || 'en-US',
    preferredModelProvider: process.env.MCP_DEFAULT_MODEL_PROVIDER
  };
}

export function validateConfig(config: MCPServerConfig): void {
  if (config.httpPort < 1 || config.httpPort > 65535) {
    throw new Error('HTTP port must be between 1 and 65535');
  }

  const validLogLevels = ['debug', 'info', 'warn', 'error'];
  if (!validLogLevels.includes(config.logLevel)) {
    throw new Error(`Log level must be one of: ${validLogLevels.join(', ')}`);
  }
}

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Set a valid port (1-65535), e.g. 3000 or 8080, in the environment/config
  2. If the port comes from a string env var, coerce and range-check: const p = Number(process.env.MCP_HTTP_PORT ?? 3000)
  3. Pick an unprivileged port (>1024) to also avoid EACCES at bind time

Example fix

// before
const config = { httpPort: Number(process.env.MCP_HTTP_PORT) }; // NaN or 0 when unset

// after
const raw = Number(process.env.MCP_HTTP_PORT ?? 3000);
const config = { httpPort: Number.isInteger(raw) && raw >= 1 && raw <= 65535 ? raw : 3000 };
Defensive patterns

Strategy: validation

Validate before calling

const httpPort = Number(process.env.MCP_HTTP_PORT ?? 3000);
if (!Number.isInteger(httpPort) || httpPort < 1 || httpPort > 65535) {
  throw new Error(`Invalid port: ${process.env.MCP_HTTP_PORT}`);
}

Type guard

const isValidPort = (p: unknown): p is number =>
  typeof p === 'number' && Number.isInteger(p) && p >= 1 && p <= 65535;

Try / catch

try { validateConfig(config); } catch (e) { if ((e as Error).message.includes('HTTP port')) { config.httpPort = 3000; validateConfig(config); } else throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/8d1598b5d571166c. Report an issue: GitHub.