const fs = require('fs'); const file = 'vibn-frontend/app/api/mcp/route.ts'; let code = fs.readFileSync(file, 'utf8'); // 1. Fix toolFsWrite const oldFsWriteReturn = ` return NextResponse.json({ result: { path, bytesWritten: Buffer.byteLength(content, "utf8") }, });`; const newFsWriteReturn = ` const { createHash } = require('crypto'); const bytes = Buffer.byteLength(content, "utf8"); const sha256 = createHash("sha256").update(content, "utf8").digest("hex"); return NextResponse.json({ result: { ok: true, path, bytes, sha256 }, });`; code = code.replace(oldFsWriteReturn, newFsWriteReturn); // 2. Fix toolFsEdit const oldFsEditCmd = `const cmd = \`python3 -c "$(printf %s \${shq(pyB64)} | base64 -d)" <<< "$(printf %s \${shq(b64)} | base64 -d)"\`;`; const newFsEditCmd = `const cmd = \`python3 -c "$(printf %s \${shq(pyB64)} | base64 -d)" <<< "$(printf %s \${shq(b64)} | base64 -d)" && echo "---" && sha256sum \${shq(path)} | cut -d' ' -f1 && wc -c < \${shq(path)}\`;`; code = code.replace(oldFsEditCmd, newFsEditCmd); const oldFsEditReturn = ` return NextResponse.json({ result: { path, replacements: parseInt(r.stdout.trim() || "0", 10) }, });`; const newFsEditReturn = ` const stdoutParts = r.stdout.split('---'); const replacementsStr = stdoutParts[0].trim(); const hashAndSize = stdoutParts[1] ? stdoutParts[1].trim().split('\\n') : []; return NextResponse.json({ result: { ok: true, path, replacements: parseInt(replacementsStr || "0", 10), sha256: hashAndSize[0] ? hashAndSize[0].trim() : undefined, bytes: hashAndSize[1] ? parseInt(hashAndSize[1].trim(), 10) : undefined }, });`; code = code.replace(oldFsEditReturn, newFsEditReturn); fs.writeFileSync(file, code); console.log("Patched toolFsWrite and toolFsEdit");