can1357/oh-my-pi · error · Error

Could not connect to OAuth server. Please check the URLs and

Error message

Could not connect to OAuth server. Please check the URLs and your network connection.

What it means

The MCP OAuth flow failed and the underlying error contained 'ECONNREFUSED' or 'fetch failed', mapped to this message. The client could not establish a TCP/HTTP connection to the OAuth authorization or token endpoint.

Source

Thrown at packages/coding-agent/src/modes/controllers/mcp-command-controller.ts:1038

		} catch (error) {
			// Esc, an external abort, or a newer MCP flow are neutral
			// cancellations. The timeout path also aborts the controller but does
			// not set this flag, so it remains a surfaced error.
			if (cancellationRequested) {
				throw new MCPOAuthCancelledError();
			}

			const errorMsg = error instanceof Error ? error.message : String(error);

			// Provide helpful error messages based on failure type
			if (errorMsg.includes("timeout") || errorMsg.includes("timed out")) {
				throw new Error("OAuth flow timed out. Please try again.");
			} else if (errorMsg.includes("403") || errorMsg.includes("unauthorized")) {
				throw new Error("OAuth authorization failed. Please check your client credentials.");
			} else if (errorMsg.includes("invalid_grant")) {
				throw new Error("OAuth authorization code is invalid or expired. Please try again.");
			} else if (errorMsg.includes("ECONNREFUSED") || errorMsg.includes("fetch failed")) {
				throw new Error("Could not connect to OAuth server. Please check the URLs and your network connection.");
			} else {
				throw new Error(`OAuth authentication failed: ${errorMsg}`);
			}
		} finally {
			this.ctx.editor.onEscape = originalOnEscape;
			externalSignal?.removeEventListener("abort", onExternalAbort);
			manualInputClaim?.clear("Manual MCP OAuth input cleared");
			flowClaim.release();
		}
	}

	/**
	 * Fold a completed OAuth flow back into a server config. Owns the
	 * persistence policy in one place: the auth block records the credential
	 * pointer plus refresh material, the oauth block echoes the client id for
	 * pre-auth reuse, and only a user-supplied client secret is ever written —
	 * DCR-issued secrets stay embedded in the stored credential so they cannot
	 * leak into (possibly shared/committed) config files.

View on GitHub (pinned to 9690622007)

Solutions

  1. Verify the auth/token endpoints are reachable: curl the token URL and check the port/host
  2. Start the local OAuth server if the URLs point to localhost
  3. Set proxy environment variables (HTTPS_PROXY/HTTP_PROXY) if behind a corporate proxy
  4. Check DNS, firewall, and VPN connectivity, then retry the flow

Example fix

// before
tokenUrl = "https://localhost:9999/token" // server not listening on 9999
// after
tokenUrl = "https://localhost:8080/token" // matching the running local server
Defensive patterns

Strategy: validation

Validate before calling

// Reachability pre-check before starting the flow
const reachable = await fetch(tokenUrl, { method: 'HEAD', signal: AbortSignal.timeout(5000) })
  .then(() => true).catch(() => false);
if (!reachable) {
  console.error(`Cannot reach OAuth server at ${tokenUrl} — check host, port, proxy, VPN`);
  return;
}

Try / catch

try {
  await runMcpOAuthFlow();
} catch (err) {
  if (err instanceof Error && err.message.includes('Could not connect to OAuth server')) {
    // show connectivity diagnostics: URL, proxy env, DNS
  }
}

Prevention

When it happens

Trigger: OAuth server not running or wrong port in the configured URLs; DNS failure; firewall/proxy blocking outbound HTTPS; offline machine.

Common situations: Local dev OAuth server (e.g. localhost:8080) not started; typo'd host/port in config; corporate proxy requiring env vars (HTTPS_PROXY) that aren't set; VPN disconnected.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


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