{"record":{"id":"03d603152be3984c","repo":"can1357/oh-my-pi","slug":"failed-to-parse-mcp-response","errorCode":null,"errorMessage":"Failed to parse MCP response","messagePattern":"Failed to parse MCP response","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/mcp/json-rpc.ts","lineNumber":118,"sourceCode":"\t\tsignal: options?.signal ?? AbortSignal.timeout(MCP_DEFAULT_TIMEOUT_MS),\n\t});\n\n\tif (!response.ok) {\n\t\tconst errorMsg = `MCP request failed: ${response.status} ${response.statusText}`;\n\t\tlogger.error(errorMsg, { url: redactUrlForLog(url), method, params });\n\t\tthrow new Error(errorMsg);\n\t}\n\n\tconst text = await response.text();\n\tconst result = parseSSE(text) as JsonRpcResponse<T> | null;\n\n\tif (!result) {\n\t\tlogger.error(\"Failed to parse MCP response\", {\n\t\t\turl: redactUrlForLog(url),\n\t\t\tmethod,\n\t\t\tresponseText: text.slice(0, 500),\n\t\t});\n\t\tthrow new Error(\"Failed to parse MCP response\");\n\t}\n\n\treturn result;\n}\n","sourceCodeStart":100,"sourceCodeEnd":123,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/mcp/json-rpc.ts#L100-L123","documentation":"callMCP() received an HTTP 2xx response but parseSSE() could not extract a JSON-RPC response object from the body, so it throws \"Failed to parse MCP response\". The first 500 chars of the raw response text are logged (with the redacted URL) to aid debugging. This indicates the endpoint returned something other than a well-formed JSON-RPC reply — often an HTML error page or an SSE stream without a data payload.","triggerScenarios":"The server returned 200 but the body is HTML (login page, error page), plain text, truncated JSON, an SSE stream with no parsable data event, or a proxy/WAF rewriting the response.","commonSituations":"Pointing the url at a website root instead of the MCP endpoint (200 HTML page); auth-protected endpoint serving a redirect/login page with 200; misconfigured reverse proxy; server bug emitting malformed JSON-RPC or wrong Content-Type; TLS-terminating proxy injecting content.","solutions":["Inspect the log entry — responseText (first 500 chars) shows what actually came back (HTML? empty? truncated JSON?).","Verify the url is the actual MCP/JSON-RPC endpoint, not the site root or a docs page.","Open the responseText URL in a browser/curl to see if a login or error page is served.","Check proxy/gateway config for response rewriting or content injection.","Confirm the server implements the MCP JSON-RPC protocol with SSE or JSON responses."],"exampleFix":"// before\n{ \"type\": \"http\", \"url\": \"https://mcp.example.com\" }        // serves HTML landing page\n// after\n{ \"type\": \"http\", \"url\": \"https://mcp.example.com/mcp\" }   // actual JSON-RPC endpoint","handlingStrategy":"validation","validationCode":"// Verify the endpoint speaks JSON-RPC/SSE before use\nconst probe = await fetch(endpointUrl, { headers: { Accept: \"text/event-stream, application/json\" } });\nconst ct = probe.headers.get(\"content-type\") ?? \"\";\nif (ct.includes(\"text/html\")) throw new Error(`Endpoint returns HTML, not MCP: ${endpointUrl}`);","typeGuard":"function isHtmlErrorPage(text: string): boolean {\n  return /^\\s*(<!doctype html|<html)/i.test(text) || /<title>.*(login|sign in|error)<\\/title>/i.test(text);\n}","tryCatchPattern":"try {\n  return await callMCP(url, method, params);\n} catch (e) {\n  if (e instanceof Error && e.message === \"Failed to parse MCP response\") {\n    log.error(\"Non-JSON-RPC body from MCP endpoint — check url and proxy\", { url });\n    throw new Error(`Endpoint at ${url} did not return a JSON-RPC response; verify the MCP endpoint path`);\n  }\n  throw e;\n}","preventionTips":["Use the exact MCP endpoint path (often /mcp or /sse), not the site root.","Check the response content-type with curl before configuring a new server.","Watch for auth flows that serve 200 HTML login pages instead of 401.","Inspect the logged responseText snippet in the omp log when debugging.","Beware reverse proxies/WAFs that rewrite or intercept responses."],"tags":["mcp","json-rpc","parsing","network"],"backgroundTag":"malformed-json-rpc-response","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}