UNPKG

@nutrient-sdk/dws-mcp-server

Version:

MCP server for Nutrient DWS Processor API

42 lines (41 loc) 1.89 kB
import FormData from 'form-data'; import fs from 'fs'; import path from 'path'; import { handleApiError, handleFileResponse } from './utils.js'; import { createErrorResponse } from '../responses.js'; import { resolveReadFilePath, resolveWriteFilePath } from '../fs/sandbox.js'; /** * Performs an AI redaction call to the Nutrient DWS AI Redact API. */ export async function performAiRedactCall(filePath, criteria, outputPath, apiClient, stage, apply) { // Resolve paths first to fail early try { const resolvedInputPath = await resolveReadFilePath(filePath); const resolvedOutputPath = await resolveWriteFilePath(outputPath); if (stage && apply) { return createErrorResponse('Error: stage and apply cannot both be true. Choose one mode.'); } // Guard against output overwriting input if (resolvedInputPath === resolvedOutputPath) { return createErrorResponse('Error: Output path must be different from input path to prevent data corruption.'); } const fileBuffer = await fs.promises.readFile(resolvedInputPath); const fileName = path.basename(resolvedInputPath); const redactionState = stage ? 'stage' : apply ? 'apply' : undefined; const dataPayload = { documents: [{ documentId: 'file1' }], criteria, }; if (redactionState) { dataPayload.redaction_state = redactionState; } const formData = new FormData(); formData.append('file1', fileBuffer, { filename: fileName }); formData.append('data', JSON.stringify(dataPayload)); const response = await apiClient.post('ai/redact', formData); return handleFileResponse(response, resolvedOutputPath, 'AI redaction completed successfully. Output saved to'); } catch (e) { return handleApiError(e); } }