{"record":{"id":"bff962b8007a2d1e","repo":"microsoft/autogen","slug":"invalid-server-url-configuration-bff962","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/mcp/api.ts","lineNumber":416,"sourceCode":"      timers.forEach((timerId) => clearTimeout(timerId));\n      timers.clear();\n    }\n  }\n\n  // Helper for WebSocket URL construction (similar to chat implementation)\n  private getWebSocketBaseUrl(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      throw new Error(\"Invalid server URL configuration\");\n    }\n  }\n\n  async connect(): Promise<void> {\n    this.updateState({ connecting: true, error: null });\n\n    try {\n      // First, get the WebSocket connection URL using proper API construction\n      const mcpApiInstance = mcpAPI;\n      const connectionData = await mcpApiInstance.createWebSocketConnection(\n        this.serverParams\n      );\n\n      if (!connectionData.status) {\n        throw new Error(\n          connectionData.message || \"Failed to create WebSocket connection\"\n        );\n      }","sourceCodeStart":398,"sourceCodeEnd":434,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-studio/frontend/src/components/views/mcp/api.ts#L398-L434","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect what getServerUrl() actually returns at runtime (console.log before connect())","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)","Normalize the URL before passing it in: trim whitespace, strip trailing slashes, ensure it is a non-empty string","If window.location is unavailable (tests/SSR), inject the server URL explicitly instead of deriving it"],"exampleFix":"// before\nprivate getWebSocketBaseUrl(url: string): string {\n  try {\n    let baseUrl = url.replace(/(^\\w+:|^)\\/\\//, \"\");\n    ...\n  } catch (error) {\n    throw new Error(\"Invalid server URL configuration\");\n  }\n}\n// after\nprivate getWebSocketBaseUrl(url: string): string {\n  if (typeof url !== \"string\" || url.trim() === \"\") {\n    throw new Error(`Invalid server URL configuration: ${JSON.stringify(url)}`);\n  }\n  let baseUrl = url.replace(/(^\\w+:|^)\\/\\//, \"\");\n  baseUrl = baseUrl.replace(\"/api\", \"\").replace(/\\/$/, \"\");\n  return baseUrl;\n}","handlingStrategy":"validation","validationCode":"const serverUrl = getServerUrl();\nif (typeof serverUrl !== \"string\" || serverUrl.trim() === \"\") {\n  throw new Error(\"Server URL is not configured — set the backend URL before connecting.\");\n}","typeGuard":"function isNonEmptyString(v: unknown): v is string {\n  return typeof v === \"string\" && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  await manager.connect();\n} catch (e) {\n  if (/Invalid server URL configuration/.test(String(e))) {\n    showConfigError(\"Check the backend server URL setting and reload.\");\n  }\n  throw e;\n}","preventionTips":["Validate getServerUrl() output once at startup, not inside each call","Include the offending value in the error message for fast diagnosis","Cover getWebSocketBaseUrl with unit tests for '/api', 'http://localhost:8081/api', and full URLs"],"tags":["mcp","websocket","url-configuration","validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}