microsoft/autogen · error · Error

Invalid server URL configuration

Error message

Invalid server URL configuration

What it means

Thrown inside useMcpWebSocket's getWebSocketBaseUrl when transforming the server URL into a WebSocket host fails. The transform itself (regex replace on a string) can only throw if url is not a string or window.location is unavailable, so in practice this error means the input URL was undefined/null/malformed rather than the regex failing. It aborts MCP WebSocket setup before any connection is attempted.

Source

Thrown at python/packages/autogen-studio/frontend/src/components/views/teambuilder/builder/component-editor/fields/workbench/useMcpWebSocket.ts:90

  >(new Map());
  const reconnectAttempts = useRef(0);
  const maxReconnectAttempts = 5;
  const baseReconnectDelay = 1000; // 1 second

  const getWebSocketBaseUrl = useCallback((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) {
      console.error("Error processing server URL:", error);
      throw new Error("Invalid server URL configuration");
    }
  }, []);

  const getWebSocketUrl = useCallback(() => {
    const serverUrl = getServerUrl();
    const baseUrl = getWebSocketBaseUrl(serverUrl);
    const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
    return `${protocol}//${baseUrl}`;
  }, [getWebSocketBaseUrl]);

  const cleanup = useCallback(() => {
    if (wsRef.current) {
      wsRef.current.close();
      wsRef.current = null;
    }
    if (reconnectTimeoutRef.current) {
      clearTimeout(reconnectTimeoutRef.current);
      reconnectTimeoutRef.current = null;

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Check what getServerUrl() returns at runtime (console.log before the transform); ensure it is a non-empty string like '/api' or an absolute URL.
  2. Ensure the app is configured with the correct backend base URL (e.g. VITE_BACKEND_URL or equivalent) for your deployment.
  3. If running under SSR/tests, guard the hook so it only builds the URL in a browser (typeof window !== 'undefined').
  4. Update the server deployment so the MCP routes are reachable at the same host the frontend computes.

Example fix

// before
let baseUrl = url.replace(/(^\w+:|^)\/\//, "");

// after
if (typeof url !== "string" || url.length === 0) {
  throw new Error("Invalid server URL configuration");
}
let baseUrl = url.replace(/(^\w+:|^)\/\//, "");
Defensive patterns

Strategy: validation

Validate before calling

const getServerUrlSafe = (): string => {
  const url = getServerUrl();
  if (typeof url !== 'string' || url.trim().length === 0) {
    throw new Error('Invalid server URL configuration');
  }
  return url;
};

Type guard

const isValidServerUrl = (u: unknown): u is string =>
  typeof u === 'string' && (u === '/api' || /^https?:\/\/|^localhost/.test(u));

Try / catch

try {
  const wsUrl = getWebSocketUrl();
} catch (e) {
  if (e instanceof Error && e.message === 'Invalid server URL configuration') {
    showConfigError('Backend URL is not configured for this deployment.');
  }
}

Prevention

When it happens

Trigger: getServerUrl() returned undefined or an empty value (env misconfiguration of the app's base URL), or the hook ran in a non-browser context where window.location is undefined, making the property access inside the try throw.

Common situations: Frontend built/served without the correct API base URL env var; a proxy rewriting paths so the '/api' assumptions in the branch logic fail; SSR or test rendering of the component outside a browser.

Related errors


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