{"record":{"id":"180c1d098cbb692b","repo":"gildas-lormeau/SingleFile","slug":"writeresponse-error-failed-to-write-file-via-m","errorCode":null,"errorMessage":"writeResponse.error || \"Failed to write file via MCP\"","messagePattern":"writeResponse\\.error \\|\\| \"Failed to write file via MCP\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/mcp/mcp.js","lineNumber":108,"sourceCode":"                    path = await prompt(path);\n                    if (path) {\n                        return await upload(path, content, options);\n                    } else {\n                        return { url: path, skipped: true };\n                    }\n                } else {\n                    options.filenameConflictAction = CONFLICT_ACTION_UNIQUIFY;\n                    return await upload(path, content, options);\n                }\n            }\n        }\n\n        const writeResponse = await writeFile(serverUrl, authToken, path, content, signal, getRequestId);\n\n        if (writeResponse.success) {\n            return { url: path };\n        } else {\n            throw new Error(writeResponse.error || \"Failed to write file via MCP\");\n        }\n    } catch (error) {\n        if (error.name != ABORT_ERROR_NAME) {\n            throw error;\n        }\n    }\n}\n\nasync function checkFileExists(serverUrl, authToken, path, signal, getRequestId) {\n    const requestBody = {\n        jsonrpc: MCP_JSONRPC_VERSION,\n        id: getRequestId(),\n        method: \"tools/call\",\n        params: {\n            name: \"get_file_info\",\n            arguments: {\n                path: path\n            }","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/gildas-lormeau/SingleFile/blob/517fb7c5cf2096d89933b747e862d8ecf616a9f9/src/lib/mcp/mcp.js#L90-L126","documentation":"The MCP library's upload calls writeFile on the configured MCP server; if the response indicates success === false, it throws writeResponse.error if present, otherwise the generic \"Failed to write file via MCP\". This means the server accepted the request but could not write the file — the reason is server-defined and only available via writeResponse.error.","triggerScenarios":"writeFile resolves with {success: false} and no error field — e.g. the MCP tool returned an error result, the path is not writable on the server, the server-side tool rejected the content, or the server returned an empty/unexpected success payload.","commonSituations":"MCP server filesystem tool lacks permission for the target directory; path outside the server's allowed roots; server tool returning isError results without a message; misconfigured serverUrl pointing at a server whose write tool has a different contract.","solutions":["Inspect the full writeResponse (log it before throwing) to capture any server-provided error detail.","Verify the target path is inside the MCP server's configured allowed directories.","Confirm the server's write tool is reachable and its result shape matches {success: boolean, error?: string}.","Test the same write with an MCP inspector/client against serverUrl to isolate client vs server."],"exampleFix":"// before\nawait mcp.upload(url, token, path, content); // Error: Failed to write file via MCP\n// after\nconst res = await writeFile(serverUrl, token, path, content, signal, getRequestId);\nif (!res.success) throw new Error(`MCP write failed for ${path}: ${res.error ?? JSON.stringify(res)}`);","handlingStrategy":"type-guard","validationCode":"function isWritableMcpPath(serverConfig, path) {\n  return serverConfig.allowedRoots.some(root => path.startsWith(root));\n}\nif (!isWritableMcpPath(cfg, path)) throw new Error(\"path outside MCP server allowed roots\");","typeGuard":"function isWriteSuccess(r) {\n  return r !== null && typeof r === \"object\" && r.success === true;\n}\n// usage: const res = await writeFile(...); if (!isWriteSuccess(res)) handle(res.error);","tryCatchPattern":"try { await mcp.upload(url, token, path, content); }\ncatch (e) {\n  // e.message is writeResponse.error or the generic fallback\n  logMcpWriteFailure(path, e.message);\n  throw e;\n}","preventionTips":["Keep target paths inside the MCP server's allowed directories","Confirm the server's write tool result includes an `error` field for diagnostics","Validate serverUrl and tool contract with an MCP inspector during setup"],"tags":["mcp","file-write","server-error"],"backgroundTag":"mcp-write-failed","analyzedSha":"517fb7c5cf2096d89933b747e862d8ecf616a9f9","analyzedAt":"2026-09-01T10:05:25.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}