microsoft/autogen · error · Error

Invalid JSON response from server. Check if the MCP route is

Error message

Invalid JSON response from server. Check if the MCP route is properly configured.

What it means

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.

Source

Thrown at python/packages/autogen-studio/frontend/src/components/views/mcp/api.ts:289

      method: "POST",
      headers: this.getHeaders(),
      body: JSON.stringify({ server_params: serverParams }),
    });

    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(
        `HTTP ${response.status}: ${
          errorText || "Failed to create WebSocket connection"
        }`
      );
    }

    try {
      const responseText = await response.text();
      return JSON.parse(responseText);
    } catch (jsonError) {
      throw new Error(
        `Invalid JSON response from server. Check if the MCP route is properly configured.`
      );
    }
  }
}

// WebSocket-based MCP functionality
export interface McpWebSocketState {
  connected: boolean;
  connecting: boolean;
  capabilities: ServerCapabilities | null;
  sessionId: string | null;
  error: string | null;
  lastActivity: Date | null;
  activityMessages: McpActivityMessage[];
  pendingElicitations: ElicitationRequest[];
}

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Log responseText before parsing — if it starts with '<' the request never reached the backend
  2. Fix the dev-server proxy config so /mcp/* (or the whole /api base) forwards to the backend
  3. Verify getServerUrl() is not pointing at the frontend origin when the backend lives elsewhere
  4. curl -X POST the endpoint directly against the backend and confirm it returns JSON

Example fix

// before
const responseText = await response.text();
return JSON.parse(responseText);
// after
const responseText = await response.text();
try {
  return JSON.parse(responseText);
} catch (jsonError) {
  throw new Error(
    `Invalid JSON response from server (HTTP ${response.status}): ${responseText.slice(0, 200)}`
  );
}
Defensive patterns

Strategy: type-guard

Validate before calling

null

Type guard

function looksLikeJson(s: string): boolean {
  const t = s.trim();
  return t.startsWith("{") || t.startsWith("[");
}

Try / catch

try {
  return await mcpAPI.createWebSocketConnection(serverParams);
} catch (e) {
  if (/Invalid JSON/.test(String(e))) {
    // almost certainly the SPA dev-server answered — fix proxy, don't retry
    throw new Error("MCP route misrouted: backend returned non-JSON (likely proxy/index.html)");
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Related errors


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