{ "name": "Validate a CSV before writing to a CRM", "nodes": [ { "id": "called-by-parent", "name": "Called by parent", "type": "n8n-nodes-base.executeWorkflowTrigger", "typeVersion": 1.1, "position": [ 0, 280 ], "parameters": { "inputSource": "passthrough" } }, { "id": "operator-settings", "name": "Operator settings", "type": "n8n-nodes-base.set", "typeVersion": 3.4, "position": [ 220, 280 ], "parameters": { "mode": "raw", "jsonOutput": "{\n \"baseUrl\": \"https://mcp.contentsweep.ai\",\n \"contractId\": \"YOUR_APPROVED_LEAD_CONTRACT_ID\",\n \"authorizeEvaluation\": false\n}" } }, { "id": "bind-original-csv", "name": "Bind original CSV", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 440, 280 ], "parameters": { "jsCode": "const settings = $input.first().json;\nconst inputs = $('Called by parent').all();\nif (inputs.length !== 1) throw new Error('Supply exactly one CSV per call.');\nconst { phase, csv, idempotencyKey, quoteId } = inputs[0].json;\nif (!['prepare', 'evaluate'].includes(phase)) throw new Error('Use prepare or evaluate.');\nif (!/^quality_contract_[A-Za-z0-9_-]+$/.test(settings.contractId)) {\n throw new Error('Configure the approved lead contract in Operator settings.');\n}\nif (typeof csv !== 'string' || !csv || !csv.isWellFormed()) {\n throw new Error('Supply the exact original CSV as UTF-8 text.');\n}\nif (typeof idempotencyKey !== 'string' || !idempotencyKey.trim() || idempotencyKey.length > 200) {\n throw new Error('Supply a stable delivery key of 1\u2013200 characters.');\n}\nif (phase === 'evaluate' && (settings.authorizeEvaluation !== true ||\n typeof quoteId !== 'string' || !/^quality_quote_[A-Za-z0-9_-]+$/.test(quoteId))) {\n throw new Error('Enable approved evaluation and supply the saved quote ID.');\n}\nconst bytes = Buffer.from(csv, 'utf8');\nif (bytes.length > 10 * 1024 * 1024) throw new Error('CSV exceeds 10 MiB.');\nconst sha256 = require('crypto').createHash('sha256').update(bytes).digest('hex');\nreturn [{ json: { phase, quoteId, csv, idempotencyKey, sha256, contractId: settings.contractId } }];" } }, { "id": "prepare-or-evaluate", "name": "Prepare or evaluate", "type": "n8n-nodes-base.if", "typeVersion": 2.2, "position": [ 660, 280 ], "parameters": { "conditions": { "options": { "typeValidation": "strict" }, "conditions": [ { "leftValue": "={{ $json.phase === 'prepare' }}", "rightValue": true, "operator": { "type": "boolean", "operation": "true", "singleValue": true } } ], "combinator": "and" } } }, { "id": "upload-original-csv", "name": "Upload original CSV", "type": "@n8n/n8n-nodes-langchain.mcpClient", "typeVersion": 1.1, "position": [ 880, 0 ], "parameters": { "serverTransport": "httpStreamable", "endpointUrl": "={{ $('Operator settings').first().json.baseUrl + '/v1/quality/mcp/' }}", "authentication": "headerAuth", "tool": { "__rl": true, "mode": "id", "value": "upload_quality_file" }, "inputMode": "json", "jsonInput": "={{ { content: $json.csv, format: 'csv' } }}", "options": { "timeout": 15000, "convertToBinary": false } }, "credentials": { "httpHeaderAuth": { "id": "CONFIGURE_QUALITY_KEY", "name": "Quality organization X-API-Key" } } }, { "id": "verify-uploaded-bytes", "name": "Verify uploaded bytes", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1100, 0 ], "parameters": { "jsCode": "const envelope = $input.first().json;\nconst body = envelope.structuredContent ?? envelope.content?.[0]?.text;\nconst uploaded = body?.result ?? body;\nconst original = $('Bind original CSV').first().json;\nif (!uploaded?.id || uploaded.sha256 !== original.sha256) {\n throw new Error('Uploaded bytes do not match the original CSV.');\n}\nreturn [{ json: { uploadId: uploaded.id } }];" } }, { "id": "quote-one-evaluation", "name": "Quote one evaluation", "type": "@n8n/n8n-nodes-langchain.mcpClient", "typeVersion": 1.1, "position": [ 1320, 0 ], "parameters": { "serverTransport": "httpStreamable", "endpointUrl": "={{ $('Operator settings').first().json.baseUrl + '/v1/quality/mcp/' }}", "authentication": "headerAuth", "tool": { "__rl": true, "mode": "id", "value": "quote_quality_check" }, "inputMode": "json", "jsonInput": "={{ { upload_id: $json.uploadId, contract_id: $('Bind original CSV').first().json.contractId } }}", "options": { "timeout": 15000, "convertToBinary": false } }, "credentials": { "httpHeaderAuth": { "id": "CONFIGURE_QUALITY_KEY", "name": "Quality organization X-API-Key" } } }, { "id": "return-quote-checkpoint", "name": "Return quote checkpoint", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1540, 0 ], "parameters": { "jsCode": "const envelope = $input.first().json;\nconst body = envelope.structuredContent ?? envelope.content?.[0]?.text;\nconst quote = body?.result ?? body;\nconst original = $('Bind original CSV').first().json;\nif (quote?.status === 'review_required') {\n return [{ json: { status: 'review_required', allow_write: false, reason: quote.reason } }];\n}\nif (quote?.status !== 'quoted' || !quote.id || quote.input_fingerprint !== original.sha256 ||\n quote.contract_id !== original.contractId || quote.checks !== 1 || quote.cash_charge_cents !== 0) {\n throw new Error('Quality returned a quote that does not match the request.');\n}\nreturn [{ json: {\n status: quote.funded ? 'quoted' : 'unfunded', allow_write: false,\n quote_id: quote.id, idempotency_key: original.idempotencyKey, input_fingerprint: original.sha256,\n} }];" } }, { "id": "read-saved-quote", "name": "Read saved quote", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 880, 420 ], "parameters": { "method": "GET", "url": "={{ $('Operator settings').first().json.baseUrl + '/v1/quality/quotes/' + $json.quoteId }}", "authentication": "genericCredentialType", "genericAuthType": "httpHeaderAuth", "options": { "timeout": 15000 } }, "credentials": { "httpHeaderAuth": { "id": "CONFIGURE_QUALITY_KEY", "name": "Quality organization X-API-Key" } } }, { "id": "check-quote-and-file-binding", "name": "Check quote and file binding", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1100, 420 ], "parameters": { "jsCode": "const quote = $input.first().json;\nconst original = $('Bind original CSV').first().json;\nif (quote.id !== original.quoteId || quote.status !== 'quoted' || quote.funded !== true ||\n quote.input_fingerprint !== original.sha256 || quote.contract_id !== original.contractId ||\n quote.baseline_id !== null || quote.checks !== 1 || quote.cash_charge_cents !== 0) {\n throw new Error('Saved quote is unfunded or does not match the approved contract and original CSV.');\n}\nreturn [{ json: { quoteId: quote.id, idempotencyKey: original.idempotencyKey } }];" } }, { "id": "start-idempotent-evaluation", "name": "Start idempotent evaluation", "type": "@n8n/n8n-nodes-langchain.mcpClient", "typeVersion": 1.1, "position": [ 1320, 420 ], "parameters": { "serverTransport": "httpStreamable", "endpointUrl": "={{ $('Operator settings').first().json.baseUrl + '/v1/quality/mcp/' }}", "authentication": "headerAuth", "tool": { "__rl": true, "mode": "id", "value": "start_quality_check" }, "inputMode": "json", "jsonInput": "={{ { quote_id: $json.quoteId, idempotency_key: $json.idempotencyKey } }}", "options": { "timeout": 15000, "convertToBinary": false } }, "credentials": { "httpHeaderAuth": { "id": "CONFIGURE_QUALITY_KEY", "name": "Quality organization X-API-Key" } } }, { "id": "wait-five-seconds", "name": "Wait five seconds", "type": "n8n-nodes-base.wait", "typeVersion": 1.1, "position": [ 1540, 420 ], "parameters": { "amount": 5, "unit": "seconds" } }, { "id": "read-evaluation-status", "name": "Read evaluation status", "type": "@n8n/n8n-nodes-langchain.mcpClient", "typeVersion": 1.1, "position": [ 1760, 420 ], "parameters": { "serverTransport": "httpStreamable", "endpointUrl": "={{ $('Operator settings').first().json.baseUrl + '/v1/quality/mcp/' }}", "authentication": "headerAuth", "tool": { "__rl": true, "mode": "id", "value": "get_quality_check" }, "inputMode": "json", "jsonInput": "={{ { job_id: (() => { const source = $('Start idempotent evaluation').first().json; const body = source.structuredContent ?? source.content?.[0]?.text; return (body?.result ?? body)?.id; })() } }}", "options": { "timeout": 15000, "convertToBinary": false } }, "credentials": { "httpHeaderAuth": { "id": "CONFIGURE_QUALITY_KEY", "name": "Quality organization X-API-Key" } } }, { "id": "check-evaluation-state", "name": "Check evaluation state", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1980, 420 ], "parameters": { "jsCode": "const envelope = $input.first().json;\nconst body = envelope.structuredContent ?? envelope.content?.[0]?.text;\nconst job = body?.result ?? body;\nconst original = $('Bind original CSV').first().json;\nif (!job?.id || job.quote_id !== original.quoteId) throw new Error('Invalid evaluation response.');\nif (job.status === 'completed') {\n if (!['continue', 'block', 'continue_with_warnings'].includes(job.verdict) ||\n job.receipt?.input_fingerprint !== original.sha256 ||\n job.receipt?.contract_id !== original.contractId) {\n throw new Error('Evaluation receipt does not match the original CSV and approved contract.');\n }\n return [{ json: job }];\n}\nif (!['queued', 'running'].includes(job.status) || $runIndex >= 24) {\n throw new Error('Evaluation did not complete. Resume with the saved quote ID and delivery key.');\n}\nreturn [{ json: { status: job.status } }];" } }, { "id": "evaluation-completed", "name": "Evaluation completed", "type": "n8n-nodes-base.if", "typeVersion": 2.2, "position": [ 2200, 420 ], "parameters": { "conditions": { "options": { "typeValidation": "strict" }, "conditions": [ { "leftValue": "={{ $json.status === 'completed' }}", "rightValue": true, "operator": { "type": "boolean", "operation": "true", "singleValue": true } } ], "combinator": "and" } } }, { "id": "read-full-file-findings", "name": "Read full-file findings", "type": "@n8n/n8n-nodes-langchain.mcpClient", "typeVersion": 1.1, "position": [ 2420, 420 ], "parameters": { "serverTransport": "httpStreamable", "endpointUrl": "={{ $('Operator settings').first().json.baseUrl + '/v1/quality/mcp/' }}", "authentication": "headerAuth", "tool": { "__rl": true, "mode": "id", "value": "get_quality_report" }, "inputMode": "json", "jsonInput": "={{ { job_id: $json.id } }}", "options": { "timeout": 15000, "convertToBinary": false } }, "credentials": { "httpHeaderAuth": { "id": "CONFIGURE_QUALITY_KEY", "name": "Quality organization X-API-Key" } } }, { "id": "return-checked-csv-or-review", "name": "Return checked CSV or review", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 2640, 420 ], "parameters": { "jsCode": "const envelope = $input.first().json;\nconst body = envelope.structuredContent ?? envelope.content?.[0]?.text;\nconst report = body?.result ?? body;\nconst job = $('Check evaluation state').last().json;\nconst original = $('Bind original CSV').first().json;\nif (report?.full_file_validation !== true || report.verdict !== job.verdict ||\n report.input_fingerprint !== original.sha256 || report.record_count !== job.record_count ||\n !Array.isArray(report.findings)) {\n throw new Error('Quality report is incomplete or does not match the evaluation receipt.');\n}\nconst allowWrite = report.verdict === 'continue';\nconst result = {\n status: 'completed', verdict: report.verdict, allow_write: allowWrite,\n quote_id: original.quoteId, idempotency_key: original.idempotencyKey,\n job_id: job.id, input_fingerprint: original.sha256, record_count: report.record_count,\n findings: report.findings,\n};\nif (allowWrite) result.checked_csv = original.csv;\nreturn [{ json: result }];" } }, { "id": "setup-and-parent-workflow", "name": "Setup and parent workflow", "type": "n8n-nodes-base.stickyNote", "typeVersion": 1, "position": [ 0, -35 ], "parameters": { "width": 630, "height": 245, "content": "## Validate a CSV before writing to a CRM\nSet the approved lead contract and attach the Quality X-API-Key credential to each request. Enable evaluation after authorizing credit use. Send one exact UTF-8 CSV and a stable delivery key.\n\nCall **prepare**, save the funded quote ID and delivery key, then call **evaluate** with the exact same CSV. Resume with the same quote and key. The parent writes only when `allow_write === true` and uses `checked_csv`." } } ], "connections": { "Called by parent": { "main": [ [ { "node": "Operator settings", "type": "main", "index": 0 } ] ] }, "Operator settings": { "main": [ [ { "node": "Bind original CSV", "type": "main", "index": 0 } ] ] }, "Bind original CSV": { "main": [ [ { "node": "Prepare or evaluate", "type": "main", "index": 0 } ] ] }, "Prepare or evaluate": { "main": [ [ { "node": "Upload original CSV", "type": "main", "index": 0 } ], [ { "node": "Read saved quote", "type": "main", "index": 0 } ] ] }, "Upload original CSV": { "main": [ [ { "node": "Verify uploaded bytes", "type": "main", "index": 0 } ] ] }, "Verify uploaded bytes": { "main": [ [ { "node": "Quote one evaluation", "type": "main", "index": 0 } ] ] }, "Quote one evaluation": { "main": [ [ { "node": "Return quote checkpoint", "type": "main", "index": 0 } ] ] }, "Read saved quote": { "main": [ [ { "node": "Check quote and file binding", "type": "main", "index": 0 } ] ] }, "Check quote and file binding": { "main": [ [ { "node": "Start idempotent evaluation", "type": "main", "index": 0 } ] ] }, "Start idempotent evaluation": { "main": [ [ { "node": "Wait five seconds", "type": "main", "index": 0 } ] ] }, "Wait five seconds": { "main": [ [ { "node": "Read evaluation status", "type": "main", "index": 0 } ] ] }, "Read evaluation status": { "main": [ [ { "node": "Check evaluation state", "type": "main", "index": 0 } ] ] }, "Check evaluation state": { "main": [ [ { "node": "Evaluation completed", "type": "main", "index": 0 } ] ] }, "Evaluation completed": { "main": [ [ { "node": "Read full-file findings", "type": "main", "index": 0 } ], [ { "node": "Wait five seconds", "type": "main", "index": 0 } ] ] }, "Read full-file findings": { "main": [ [ { "node": "Return checked CSV or review", "type": "main", "index": 0 } ] ] } }, "settings": { "executionOrder": "v1", "callerPolicy": "workflowsFromSameOwner", "saveDataErrorExecution": "none", "saveDataSuccessExecution": "none", "saveManualExecutions": false, "saveExecutionProgress": false, "executionTimeout": 600 }, "active": false, "pinData": {}, "meta": { "templateCredsSetupCompleted": false } }