{"record":{"id":"73cc4970bdaf4b92","repo":"gildas-lormeau/SingleFile","slug":"mcp-server-error-response-status-response-st","errorCode":null,"errorMessage":"MCP server error: ${response.status} ${response.statusText}","messagePattern":"MCP server error: (.+?) (.+?)","errorType":"http","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/mcp/mcp.js","lineNumber":144,"sourceCode":"            }\n        }\n    };\n\n    const headers = {\n        [CONTENT_TYPE_HEADER]: JSON_CONTENT_TYPE,\n        \"Accept\": \"application/json, text/event-stream\"\n    };\n    if (authToken) {\n        headers.Authorization = `Bearer ${authToken}`;\n    }\n    const response = await fetch(serverUrl, {\n        method: \"POST\",\n        headers,\n        body: JSON.stringify(requestBody),\n        signal\n    });\n    if (!response.ok) {\n        throw new Error(`MCP server error: ${response.status} ${response.statusText}`);\n    }\n    const data = await response.json();\n    if (data.error) {\n        return { exists: false };\n    }\n    if (data.result && data.result.isError) {\n        return { exists: false };\n    }\n    if (data.result) {\n        return { exists: true };\n    }\n    return { exists: false };\n}\n\nasync function writeFile(serverUrl, authToken, path, content, signal, getRequestId) {\n    const requestBody = {\n        jsonrpc: MCP_JSONRPC_VERSION,\n        id: getRequestId(),","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/gildas-lormeau/SingleFile/blob/517fb7c5cf2096d89933b747e862d8ecf616a9f9/src/lib/mcp/mcp.js#L126-L162","documentation":"checkFileExists performs a POST to the MCP server and throws \"MCP server error: <status> <statusText>\" when the HTTP response is not ok (response.ok is false, i.e. status outside 200-299). This is a transport-level failure — the server rejected the request before any JSON-RPC/tool result could be evaluated.","triggerScenarios":"Auth token missing/expired → 401; token lacking access to the target → 403; wrong serverUrl or tool name → 404; server crash or bad gateway → 5xx; request body rejected → 400.","commonSituations":"Stale authToken after server restart or key rotation; serverUrl typo or missing /mcp path prefix; MCP server not running or redeployed with a changed tool name; reverse proxy returning 502 while the server is down.","solutions":["Log response.status/statusText and, if possible, response.text() for the server's error detail.","Re-authenticate: refresh authToken and confirm it is sent in the expected header.","Verify serverUrl and the tool/endpoint name against the server's current configuration.","If status is 5xx, retry with backoff — it is likely transient server unavailability.","Confirm the MCP server is running and reachable (curl/health check) before calling."],"exampleFix":"// before\nawait checkFileExists(serverUrl, token, path); // Error: MCP server error: 401 Unauthorized\n// after\ntry {\n  await checkFileExists(serverUrl, token, path);\n} catch (e) {\n  if (/MCP server error: (401|403)/.test(e.message)) {\n    token = await refreshAuthToken();\n    return checkFileExists(serverUrl, token, path);\n  }\n  throw e;\n}","handlingStrategy":"retry","validationCode":"async function serverReachable(serverUrl) {\n  try { const r = await fetch(serverUrl, { method: \"OPTIONS\" }); return r.status < 500; }\n  catch { return false; }\n}\nif (!token) throw new Error(\"authToken required before checkFileExists\");","typeGuard":null,"tryCatchPattern":"try { await checkFileExists(serverUrl, token, path); }\ncatch (e) {\n  const m = /MCP server error: (\\d+)/.exec(e.message);\n  const status = m && Number(m[1]);\n  if (status >= 500) return retryWithBackoff(() => checkFileExists(serverUrl, token, path));\n  if (status === 401 || status === 403) return reauthThenRetry();\n  throw e;\n}","preventionTips":["Refresh authToken before it expires and re-auth on 401","Health-check the MCP server URL at startup","Keep serverUrl/tool names in one config source to avoid drift","Retry 5xx with exponential backoff and jitter"],"tags":["mcp","http-error","auth","server-error"],"backgroundTag":"mcp-server-http-error","analyzedSha":"517fb7c5cf2096d89933b747e862d8ecf616a9f9","analyzedAt":"2026-09-01T10:05:25.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}