fix(cli): match listener recovery guidance to system failure

Classify listener errno and lifecycle phase at the transport owner. Preserve native cause and unknown codes while distinguishing host lookup, unavailable address, occupied port and permission recovery.

Refs #5455.
This commit is contained in:
morluto committed 2026-10-20 19:54:56 +08:00
1 parent a4dbb36d8e
commit 0c440bfc07
5 files changed
+302 -15

No files matched your search

+2 -1
View File
@@ -23,6 +23,7 @@ import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
import { isExactJsonNumber } from "../../domain/exactJsonNumber.js";
import { safeParseJson } from "../../domain/safeJson.js";
import type { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js";
import type { StreamableHttpListenError } from "../http/streamableHttpListenError.js";
import { ok, type Result } from "../../domain/result.js";
import { PRODUCT_IDENTITY } from "../../identity.js";
import {
@@ -212,7 +213,7 @@ export interface ServeEnvironmentOptions {
readonly environmentName?: string;
readonly logger?: Logger;
/** Reports a listener failure after binding; the failure is logged either way. */
readonly onerror?: (error: Error) => void;
readonly onerror?: (error: StreamableHttpListenError) => void;
}
/** A listening agent environment and the card it serves. */
+4 -2
View File
@@ -60,7 +60,7 @@ export interface ServeStreamableHttpOptions {
readonly routes?: readonly StreamableHttpRoute[];
readonly logger?: Logger;
/** Reports listener failures after binding; request failures are logged instead. */
readonly onerror?: (error: Error) => void;
readonly onerror?: (error: StreamableHttpListenError) => void;
/** Close the server instance as the last stage of `close`; false when the factory's owner closes it. */
readonly closeInstance?: boolean;
}
@@ -557,7 +557,9 @@ export const serveStreamableHttp = async (
});
});
// Bind failures already rejected the listening wait above; only later listener errors are reported.
httpServer.on("error", (error) => options.onerror?.(error));
httpServer.on("error", (error) =>
options.onerror?.(new StreamableHttpListenError(error, "serving")),
);
let instance: RequestTrackingMcpServer;
try {
instance = await options.createMcpServer({ era: "legacy" });
+76 -3
View File
@@ -1,10 +1,83 @@
/** The listener could not bind before any client connected; the message names the system cause. */
import { err, ok, type Result } from "../../domain/result.js";
/** Parsed system failure facts for listener recovery guidance. */
export type StreamableHttpListenFailure =
| {
readonly kind: "host_lookup";
readonly systemCode:
| "ENOTFOUND"
| "EAI_AGAIN"
| "EAI_FAIL"
| "EAI_NONAME"
| "ENODATA"
| "EAI_NODATA";
}
| { readonly kind: "host_unavailable"; readonly systemCode: "EADDRNOTAVAIL" }
| { readonly kind: "port_in_use"; readonly systemCode: "EADDRINUSE" }
| {
readonly kind: "permission_denied";
readonly systemCode: "EACCES" | "EPERM";
};
export interface UnclassifiedStreamableHttpListenFailure {
readonly kind: "unclassified";
readonly systemCode?: string;
}
export type StreamableHttpListenClassification = Result<
StreamableHttpListenFailure,
UnclassifiedStreamableHttpListenFailure
>;
const systemCode = (cause: unknown): string | undefined => {
if (typeof cause !== "object" || cause === null || !("code" in cause))
return undefined;
const code: unknown = cause.code;
return typeof code === "string" ? code : undefined;
};
/** Normalize recognized Node listener system codes once, at the listener boundary. */
const classify = (cause: unknown): StreamableHttpListenClassification => {
const code = systemCode(cause);
switch (code) {
case "ENOTFOUND":
case "EAI_AGAIN":
case "EAI_FAIL":
case "EAI_NONAME":
case "ENODATA":
case "EAI_NODATA":
return ok({ kind: "host_lookup", systemCode: code });
case "EADDRNOTAVAIL":
return ok({ kind: "host_unavailable", systemCode: "EADDRNOTAVAIL" });
case "EADDRINUSE":
return ok({ kind: "port_in_use", systemCode: "EADDRINUSE" });
case "EACCES":
case "EPERM":
return ok({ kind: "permission_denied", systemCode: code });
default:
return err({
kind: "unclassified",
...(code === undefined ? {} : { systemCode: code }),
});
}
};
export type StreamableHttpListenPhase = "startup" | "serving";
/** Listener failure retaining its native cause, phase, and parsed recovery facts. */
export class StreamableHttpListenError extends Error {
override readonly name = "StreamableHttpListenError";
constructor(cause: unknown) {
readonly classification: StreamableHttpListenClassification;
readonly phase: StreamableHttpListenPhase;
constructor(cause: unknown, phase: StreamableHttpListenPhase = "startup") {
super(
`MCP HTTP listener could not bind: ${cause instanceof Error ? cause.message : String(cause)}`,
phase === "startup"
? `MCP HTTP listener startup failed: ${cause instanceof Error ? cause.message : String(cause)}`
: `MCP HTTP listener failed after binding: ${cause instanceof Error ? cause.message : String(cause)}`,
{ cause },
);
this.phase = phase;
this.classification = classify(cause);
}
}