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 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.
Source
Thrown at python/packages/autogen-studio/frontend/src/components/views/teambuilder/builder/component-editor/fields/workbench/useMcpWebSocket.ts:155
if (!response.ok) {
const errorText = await response.text();
console.error("HTTP error response:", errorText);
throw new Error(
`HTTP ${response.status}: ${
errorText || "Failed to create WebSocket connection"
}`
);
}
let connectionData;
try {
const responseText = await response.text();
console.log("Raw response:", responseText);
connectionData = JSON.parse(responseText);
} catch (jsonError) {
console.error("JSON parse error:", jsonError);
throw new Error(
`Invalid JSON response from server. Check if the MCP route is properly configured.`
);
}
if (!connectionData.status) {
throw new Error(
connectionData.message || "Failed to create WebSocket connection"
);
}
const { session_id, websocket_url } = connectionData;
const wsUrl = `${getWebSocketUrl()}${websocket_url}`;
// Create WebSocket connection
const ws = new WebSocket(wsUrl);
wsRef.current = ws;
ws.onopen = () => {View on GitHub (pinned to 027ecf0a37)
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.
Defensive patterns
Strategy: try-catch
Validate before calling
// Verify the endpoint returns JSON before relying on it
const probe = await fetch(mcpUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{}' });
const ct = probe.headers.get('content-type') ?? '';
if (!ct.includes('application/json')) {
throw new Error('MCP endpoint is misconfigured (non-JSON response)');
} Type guard
const looksLikeJson = (text: string): boolean =>
text.trimStart().startsWith('{') || text.trimStart().startsWith('['); Try / catch
try {
await connectMcp(serverParams);
} catch (e) {
if (e instanceof Error && e.message.includes('Invalid JSON response')) {
// The POST hit an HTML page: fix the base URL / proxy config, don't retry blindly
showConfigError('API base URL misroutes MCP requests; check proxy config.');
}
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Invalid server URL configuration
- Invalid JSON response from server. Check if the MCP route is
- HTTP ${response.status}: ${errorText || "Failed to create We
- Failed to list MCP tools
- HTTP ${response.status}: ${errorText || "Failed to create We
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/0b2128aa4b360976.
Report an issue: GitHub.