{"record":{"id":"ecfe17800eb271d7","repo":"microsoft/autogen","slug":"invalid-json-response-from-server-check-if-the-mc","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/mcp/api.ts","lineNumber":289,"sourceCode":"      method: \"POST\",\n      headers: this.getHeaders(),\n      body: JSON.stringify({ server_params: serverParams }),\n    });\n\n    if (!response.ok) {\n      const errorText = await response.text();\n      throw new Error(\n        `HTTP ${response.status}: ${\n          errorText || \"Failed to create WebSocket connection\"\n        }`\n      );\n    }\n\n    try {\n      const responseText = await response.text();\n      return JSON.parse(responseText);\n    } catch (jsonError) {\n      throw new Error(\n        `Invalid JSON response from server. Check if the MCP route is properly configured.`\n      );\n    }\n  }\n}\n\n// WebSocket-based MCP functionality\nexport interface McpWebSocketState {\n  connected: boolean;\n  connecting: boolean;\n  capabilities: ServerCapabilities | null;\n  sessionId: string | null;\n  error: string | null;\n  lastActivity: Date | null;\n  activityMessages: McpActivityMessage[];\n  pendingElicitations: ElicitationRequest[];\n}\n","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-studio/frontend/src/components/views/mcp/api.ts#L271-L307","documentation":"Thrown when POST /mcp/ws/connect succeeds (2xx) but the response body is not valid JSON. This means the endpoint answered with something other than the expected {status, session_id, websocket_url} JSON — typically an HTML error page from a misrouted proxy, an empty body, or a plain-text message from an intermediary.","triggerScenarios":"response.ok is true but JSON.parse(responseText) throws: dev-server fallback index.html returned for the POST path, proxy returning 200 with an HTML body, empty 204-style body, wrong route answering (frontend dev server catch-all), or double body consumption corrupting the text.","commonSituations":"Vite/webpack dev proxy missing the /mcp/ws/connect rule so the SPA fallback answers 200 with index.html, API gateway rewriting the path, backend returning an empty body on some edge path.","solutions":["Log responseText before parsing — if it starts with '<' the request never reached the backend","Fix the dev-server proxy config so /mcp/* (or the whole /api base) forwards to the backend","Verify getServerUrl() is not pointing at the frontend origin when the backend lives elsewhere","curl -X POST the endpoint directly against the backend and confirm it returns JSON"],"exampleFix":"// before\nconst responseText = await response.text();\nreturn JSON.parse(responseText);\n// after\nconst responseText = await response.text();\ntry {\n  return JSON.parse(responseText);\n} catch (jsonError) {\n  throw new Error(\n    `Invalid JSON response from server (HTTP ${response.status}): ${responseText.slice(0, 200)}`\n  );\n}","handlingStrategy":"type-guard","validationCode":"null","typeGuard":"function looksLikeJson(s: string): boolean {\n  const t = s.trim();\n  return t.startsWith(\"{\") || t.startsWith(\"[\");\n}","tryCatchPattern":"try {\n  return await mcpAPI.createWebSocketConnection(serverParams);\n} catch (e) {\n  if (/Invalid JSON/.test(String(e))) {\n    // almost certainly the SPA dev-server answered — fix proxy, don't retry\n    throw new Error(\"MCP route misrouted: backend returned non-JSON (likely proxy/index.html)\");\n  }\n  throw e;\n}","preventionTips":["Add the response body snippet to the error so misroutes are obvious","Keep dev-proxy /mcp rules in the project checklist when adding MCP features","Assert content-type: application/json on this response in e2e tests"],"tags":["mcp","websocket","json","proxy","autogen-studio"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}