can1357/oh-my-pi · warning · Error

Server "${name}" was disconnected during reconnection

Error message

Server "${name}" was disconnected during reconnection

What it means

During a background reconnection, after the connection is re-established the manager re-checks whether the server still has a config entry and whether the manager epoch changed while connecting (e.g. /mcp reload called disconnectAll). If either happened, it detaches and disconnects the fresh connection and throws this error naming the server. It is the reconnection-path twin of error 1747.

Source

Thrown at packages/coding-agent/src/mcp/manager.ts:1211

		const resolvedConfig = await this.#resolveAuthConfig(config);
		const connection = await connectToServer(name, resolvedConfig, {
			onNotification: (method, params) => {
				this.#handleServerNotification(name, method, params);
			},
			onRequest: (method, params) => {
				return this.#handleServerRequest(method, params);
			},
		});

		connection.config = config;
		if (source) connection._source = source;

		// Bail out if the server was disconnected or the manager was reset
		// while we were connecting (e.g. /mcp reload called disconnectAll).
		if (!this.#serverConfigs.has(name) || this.#epoch !== reconnectEpoch) {
			this.#detachConnection(name, connection);
			void disconnectServer(connection).catch(() => {});
			throw new Error(`Server "${name}" was disconnected during reconnection`);
		}

		this.#connections.set(name, connection);

		// Wire auth refresh for HTTP-like transports, and reconnect for any transport.
		// Same gate as connectServers: any resolvable managed credential.
		if (isAuthRefreshableMCPTransport(connection.transport) && lookupMcpOAuthCredential(this.#authStorage, config)) {
			connection.transport.onAuthError = async () => {
				const refreshed = await this.#resolveAuthConfig(config, { forceRefresh: true });
				if (refreshed.type === "http" || refreshed.type === "sse") {
					return refreshed.headers ?? null;
				}
				return null;
			};
		}
		connection.transport.onClose = () => {
			logger.debug("MCP transport lost, triggering reconnect", { path: `mcp:${name}` });
			this.#emitConnectionStatus({ type: "connecting", serverNames: [name] });

View on GitHub (pinned to 9690622007)

Solutions

  1. Retry the reconnection after the reload/disconnect operation finishes.
  2. If the server is genuinely removed, don't reconnect — the error is expected; filter removed servers from reconnect lists.
  3. Avoid triggering config reload or disconnectAll while reconnects are in flight; await them.
  4. Re-read the current config to confirm the server still exists before attempting reconnect.
  5. Catch and swallow this error in fire-and-forget reconnect paths where the server was intentionally removed.

Example fix

// before
void manager.reconnect("github"); // later /mcp reload cancels it -> throws
// after
try {
  await manager.reconnect("github");
} catch (e) {
  if (e instanceof Error && /disconnected during reconnection/.test(e.message)) return; // removed by reload
  throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

import { readMCPConfigFile } from "@oh-my-pi/pi-coding-agent/mcp/config";
const cfg = await readMCPConfigFile(configPath);
if (!cfg.mcpServers?.[name]) return; // server removed; skip reconnect entirely

Type guard

null

Try / catch

try {
  await manager.reconnect(name);
} catch (e) {
  if (e instanceof Error && /was disconnected during reconnection/.test(e.message)) {
    return; // intentional removal or reload; not an operator-visible failure
  }
  throw e;
}

Prevention

When it happens

Trigger: A pending reconnection for server "name" completes while: the server's config entry was removed (#serverConfigs no longer has the name), disconnectAll/reset bumped #epoch (config reload, shutdown), or another lifecycle change invalidated the reconnect epoch.

Common situations: User runs /mcp reload while a dropped server is mid-reconnect; server is removed from config but its auto-reconnect was still in flight; manager reset/shutdown overlapping a reconnect after a network blip or server crash; OAuth refresh triggering reconnect just as config changes.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/60691a93d5f79ef3. Report an issue: GitHub.