{"record":{"id":"0093cd0f232e670b","repo":"angular/angular-cli","slug":"port-input-port-is-unavailable-try-calling-thi","errorCode":null,"errorMessage":"Port ${input.port} is unavailable. Try calling this tool again without the 'port' parameter to auto-assign a free port.","messagePattern":"Port (.+?) is unavailable\\. Try calling this tool again without the 'port' parameter to auto-assign a free port\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/angular/cli/src/commands/mcp/tools/devserver/devserver-start.ts","lineNumber":67,"sourceCode":"    workspacePathInput: input.workspace,\n    projectNameInput: input.project,\n    mcpWorkspace: context.workspace,\n  });\n\n  const key = getDevserverKey(workspacePath, projectName);\n\n  let devserver = context.devservers.get(key);\n  if (devserver) {\n    return createStructuredContentOutput({\n      message: `Development server for project '${projectName}' is already running.`,\n      address: localhostAddress(devserver.port),\n    });\n  }\n\n  let port: number;\n  if (input.port) {\n    if (!(await context.host.isPortAvailable(input.port))) {\n      throw new Error(\n        `Port ${input.port} is unavailable. Try calling this tool again without the 'port' parameter to auto-assign a free port.`,\n      );\n    }\n    port = input.port;\n  } else {\n    port = await context.host.getAvailablePort();\n  }\n\n  devserver = new LocalDevserver({\n    host: context.host,\n    project: projectName,\n    port,\n    workspacePath,\n  });\n  devserver.start();\n\n  context.devservers.set(key, devserver);\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular/cli/src/commands/mcp/tools/devserver/devserver-start.ts#L49-L85","documentation":"The devserver_start tool checks the requested port via context.host.isPortAvailable before binding. If the port is already in use (or otherwise unavailable), it throws and suggests omitting the port parameter so the tool auto-assigns a free one.","triggerScenarios":"Calling devserver_start with input.port set to a port already occupied by another process, or a port below 1024 without privileges, or raced by another service binding between check and bind.","commonSituations":"A previous dev server still running on 4200; another app (Docker, other dev server) holding the port; re-running the tool after a previous session that didn't shut down; reusing a hardcoded port across projects.","solutions":["Call the tool again without the 'port' parameter so a free port is auto-assigned.","Stop the process occupying the port (lsof -i :<port> / netstat to identify it, then kill it).","Choose a different, higher, unprivileged port (e.g. 49152+).","Shut down previously started dev servers via the devserver stop tool before restarting."],"exampleFix":"// before\ndevserver_start({ port: 4200 });\n// after\ndevserver_start({}); // port auto-assigned\n","handlingStrategy":"retry","validationCode":"import { createServer } from 'node:net';\nfunction isPortFree(port: number): Promise<boolean> {\n  return new Promise((res) => {\n    const srv = createServer();\n    srv.once('error', () => res(false));\n    srv.once('listening', () => srv.close(() => res(true)));\n    srv.listen(port);\n  });\n}\nif (!(await isPortFree(4200))) console.warn('port 4200 busy — omit port param');","typeGuard":null,"tryCatchPattern":"try {\n  await devserver_start({ port: 4200 });\n} catch (e) {\n  if ((e as Error).message.includes('is unavailable')) {\n    await devserver_start({}); // auto-assign free port\n  } else throw e;\n}","preventionTips":["Omit the 'port' parameter unless a fixed port is truly required","Check for lingering dev server processes before restarting on the same port","Use distinct fixed ports per project to avoid collisions","Prefer unprivileged ports (>1024)"],"tags":["port-in-use","devserver","angular-cli","network"],"backgroundTag":"port-already-in-use","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}