{"record":{"id":"0b2128aa4b360976","repo":"microsoft/autogen","slug":"invalid-json-response-from-server-check-if-the-mc-0b2128","errorCode":null,"errorMessage":"Invalid JSON response from server. Check if the MCP route is properly configured.","messagePattern":"Invalid JSON response from server\\. Check if the MCP route is properly configured\\.","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":155,"sourceCode":"\n      if (!response.ok) {\n        const errorText = await response.text();\n        console.error(\"HTTP error response:\", errorText);\n        throw new Error(\n          `HTTP ${response.status}: ${\n            errorText || \"Failed to create WebSocket connection\"\n          }`\n        );\n      }\n\n      let connectionData;\n      try {\n        const responseText = await response.text();\n        console.log(\"Raw response:\", responseText);\n        connectionData = JSON.parse(responseText);\n      } catch (jsonError) {\n        console.error(\"JSON parse error:\", jsonError);\n        throw new Error(\n          `Invalid JSON response from server. Check if the MCP route is properly configured.`\n        );\n      }\n\n      if (!connectionData.status) {\n        throw new Error(\n          connectionData.message || \"Failed to create WebSocket connection\"\n        );\n      }\n\n      const { session_id, websocket_url } = connectionData;\n      const wsUrl = `${getWebSocketUrl()}${websocket_url}`;\n\n      // Create WebSocket connection\n      const ws = new WebSocket(wsUrl);\n      wsRef.current = ws;\n\n      ws.onopen = () => {","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-studio/frontend/src/components/views/teambuilder/builder/component-editor/fields/workbench/useMcpWebSocket.ts#L137-L173","documentation":"Thrown by useMcpWebSocket when the MCP session-initiation endpoint returns HTTP OK but a body that is not valid JSON (response.text() succeeds, JSON.parse throws). The message explicitly suggests the MCP route is misconfigured — typically the POST hit a route that returned HTML or plain text instead of the expected JSON session payload.","triggerScenarios":"The POST URL resolves to a SPA index.html (catch-all route), a proxy error page, or a plain-text 200 response. JSON.parse of that body throws and this error replaces the parse error.","commonSituations":"Frontend base URL wrong so the POST lands on the frontend dev server instead of the API; reverse proxy (nginx) serving index.html for unknown paths; backend route registered under a different prefix than the frontend calls.","solutions":["Open devtools Network tab and inspect the raw response of the failing POST — if it's HTML, the URL is wrong.","Fix the API base URL so the POST reaches the backend's MCP route (should return JSON with session_id and websocket_url).","If behind a proxy, add a rule to pass /api/* (including the MCP route) to the backend instead of the SPA fallback.","Confirm the backend actually mounts the MCP websocket route."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Verify the endpoint returns JSON before relying on it\nconst probe = await fetch(mcpUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{}' });\nconst ct = probe.headers.get('content-type') ?? '';\nif (!ct.includes('application/json')) {\n  throw new Error('MCP endpoint is misconfigured (non-JSON response)');\n}","typeGuard":"const looksLikeJson = (text: string): boolean =>\n  text.trimStart().startsWith('{') || text.trimStart().startsWith('[');","tryCatchPattern":"try {\n  await connectMcp(serverParams);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Invalid JSON response')) {\n    // The POST hit an HTML page: fix the base URL / proxy config, don't retry blindly\n    showConfigError('API base URL misroutes MCP requests; check proxy config.');\n  }\n}","preventionTips":["Configure reverse proxies to send /api/* to the backend, not the SPA fallback.","Check content-type of responses in dev before wiring parsers.","Keep frontend and backend route prefixes in sync via shared config."],"tags":["json","mcp","websocket","frontend","configuration"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}