{"record":{"id":"1ec3b177cce89e74","repo":"microsoft/autogen","slug":"invalid-server-url-configuration-1ec3b1","errorCode":null,"errorMessage":"Invalid server URL configuration","messagePattern":"Invalid server URL configuration","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-studio/frontend/src/components/views/teambuilder/builder/component-editor/fields/workbench/useMcpWebSocket.ts","lineNumber":90,"sourceCode":"  >(new Map());\n  const reconnectAttempts = useRef(0);\n  const maxReconnectAttempts = 5;\n  const baseReconnectDelay = 1000; // 1 second\n\n  const getWebSocketBaseUrl = useCallback((url: string): string => {\n    try {\n      let baseUrl = url.replace(/(^\\w+:|^)\\/\\//, \"\");\n      if (baseUrl.startsWith(\"localhost\")) {\n        baseUrl = baseUrl.replace(\"/api\", \"\");\n      } else if (baseUrl === \"/api\") {\n        baseUrl = window.location.host;\n      } else {\n        baseUrl = baseUrl.replace(\"/api\", \"\").replace(/\\/$/, \"\");\n      }\n      return baseUrl;\n    } catch (error) {\n      console.error(\"Error processing server URL:\", error);\n      throw new Error(\"Invalid server URL configuration\");\n    }\n  }, []);\n\n  const getWebSocketUrl = useCallback(() => {\n    const serverUrl = getServerUrl();\n    const baseUrl = getWebSocketBaseUrl(serverUrl);\n    const protocol = window.location.protocol === \"https:\" ? \"wss:\" : \"ws:\";\n    return `${protocol}//${baseUrl}`;\n  }, [getWebSocketBaseUrl]);\n\n  const cleanup = useCallback(() => {\n    if (wsRef.current) {\n      wsRef.current.close();\n      wsRef.current = null;\n    }\n    if (reconnectTimeoutRef.current) {\n      clearTimeout(reconnectTimeoutRef.current);\n      reconnectTimeoutRef.current = null;","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-studio/frontend/src/components/views/teambuilder/builder/component-editor/fields/workbench/useMcpWebSocket.ts#L72-L108","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check what getServerUrl() returns at runtime (console.log before the transform); ensure it is a non-empty string like '/api' or an absolute URL.","Ensure the app is configured with the correct backend base URL (e.g. VITE_BACKEND_URL or equivalent) for your deployment.","If running under SSR/tests, guard the hook so it only builds the URL in a browser (typeof window !== 'undefined').","Update the server deployment so the MCP routes are reachable at the same host the frontend computes."],"exampleFix":"// before\nlet baseUrl = url.replace(/(^\\w+:|^)\\/\\//, \"\");\n\n// after\nif (typeof url !== \"string\" || url.length === 0) {\n  throw new Error(\"Invalid server URL configuration\");\n}\nlet baseUrl = url.replace(/(^\\w+:|^)\\/\\//, \"\");","handlingStrategy":"validation","validationCode":"const getServerUrlSafe = (): string => {\n  const url = getServerUrl();\n  if (typeof url !== 'string' || url.trim().length === 0) {\n    throw new Error('Invalid server URL configuration');\n  }\n  return url;\n};","typeGuard":"const isValidServerUrl = (u: unknown): u is string =>\n  typeof u === 'string' && (u === '/api' || /^https?:\\/\\/|^localhost/.test(u));","tryCatchPattern":"try {\n  const wsUrl = getWebSocketUrl();\n} catch (e) {\n  if (e instanceof Error && e.message === 'Invalid server URL configuration') {\n    showConfigError('Backend URL is not configured for this deployment.');\n  }\n}","preventionTips":["Assert window is defined before using window.location in shared hooks.","Centralize base-URL resolution in one module and unit-test it.","Fail the app build/deploy when the API base URL env var is missing."],"tags":["websocket","typescript","frontend","mcp","configuration"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}