microsoft/autogen · error · Error

Invalid server URL configuration

Error message

Invalid server URL configuration

What it means

A catch-all thrown by McpWebSocketManager.getWebSocketBaseUrl when transforming the configured server URL into a WebSocket host fails. The wrapped code only does string replaces, so in practice the try block almost never throws; this error fires when the input url is not a string (null/undefined from a broken getServerUrl) or the regex replace throws on an unexpected type. It usually indicates a broken server URL configuration rather than a runtime regex failure.

Source

Thrown at python/packages/autogen-studio/frontend/src/components/views/mcp/api.ts:416

      timers.forEach((timerId) => clearTimeout(timerId));
      timers.clear();
    }
  }

  // Helper for WebSocket URL construction (similar to chat implementation)
  private getWebSocketBaseUrl(url: string): string {
    try {
      let baseUrl = url.replace(/(^\w+:|^)\/\//, "");
      if (baseUrl.startsWith("localhost")) {
        baseUrl = baseUrl.replace("/api", "");
      } else if (baseUrl === "/api") {
        baseUrl = window.location.host;
      } else {
        baseUrl = baseUrl.replace("/api", "").replace(/\/$/, "");
      }
      return baseUrl;
    } catch (error) {
      throw new Error("Invalid server URL configuration");
    }
  }

  async connect(): Promise<void> {
    this.updateState({ connecting: true, error: null });

    try {
      // First, get the WebSocket connection URL using proper API construction
      const mcpApiInstance = mcpAPI;
      const connectionData = await mcpApiInstance.createWebSocketConnection(
        this.serverParams
      );

      if (!connectionData.status) {
        throw new Error(
          connectionData.message || "Failed to create WebSocket connection"
        );
      }

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Inspect what getServerUrl() actually returns at runtime (console.log before connect())
  2. Ensure the server URL config is set (e.g. REACT_APP_/VITE_ backend URL env var, or window.location.host when served by the backend)
  3. Normalize the URL before passing it in: trim whitespace, strip trailing slashes, ensure it is a non-empty string
  4. If window.location is unavailable (tests/SSR), inject the server URL explicitly instead of deriving it

Example fix

// before
private getWebSocketBaseUrl(url: string): string {
  try {
    let baseUrl = url.replace(/(^\w+:|^)\/\//, "");
    ...
  } catch (error) {
    throw new Error("Invalid server URL configuration");
  }
}
// after
private getWebSocketBaseUrl(url: string): string {
  if (typeof url !== "string" || url.trim() === "") {
    throw new Error(`Invalid server URL configuration: ${JSON.stringify(url)}`);
  }
  let baseUrl = url.replace(/(^\w+:|^)\/\//, "");
  baseUrl = baseUrl.replace("/api", "").replace(/\/$/, "");
  return baseUrl;
}
Defensive patterns

Strategy: validation

Validate before calling

const serverUrl = getServerUrl();
if (typeof serverUrl !== "string" || serverUrl.trim() === "") {
  throw new Error("Server URL is not configured — set the backend URL before connecting.");
}

Type guard

function isNonEmptyString(v: unknown): v is string {
  return typeof v === "string" && v.trim().length > 0;
}

Try / catch

try {
  await manager.connect();
} catch (e) {
  if (/Invalid server URL configuration/.test(String(e))) {
    showConfigError("Check the backend server URL setting and reload.");
  }
  throw e;
}

Prevention

When it happens

Trigger: getServerUrl() returning null/undefined/non-string (missing config, window.location unavailable), or a url value whose shape the branch logic mishandles so a downstream string method throws inside the try.

Common situations: Server URL env/config not set during local dev, getServerUrl() returning an object after a utils refactor, SSR or test environment where window.location is absent, trailing-slash/protocol variants that slip past the replace chain.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/bff962b8007a2d1e. Report an issue: GitHub.