Skip to content

feat: AI redaction, Instant JSON, credit tracking & enhanced document processing#12

Closed
jdrhyne wants to merge 4 commits intomainfrom
feat/ai-redaction-and-instant-json
Closed

feat: AI redaction, Instant JSON, credit tracking & enhanced document processing#12
jdrhyne wants to merge 4 commits intomainfrom
feat/ai-redaction-and-instant-json

Conversation

@jdrhyne
Copy link
Contributor

@jdrhyne jdrhyne commented Feb 4, 2026

Summary

This PR adds several major features to the Nutrient DWS MCP Server:

AI Redaction (ai_redactor tool)

  • AI-powered PII detection and permanent removal via /ai/redact endpoint
  • Configurable criteria (names, SSNs, emails, PHI, etc.)
  • 5-minute timeout for large documents
  • Output≠input safety guard

Form Filling & Annotations (applyInstantJson action)

  • New action in document_processor tool
  • Fill PDF form fields and import annotations via Instant JSON format

HTML-to-PDF Layout

  • Page orientation control (portrait/landscape)
  • Page size selection (A4, Letter, Legal, etc.)
  • Custom margin configuration

Watermark Positioning

  • Fine-grained watermark placement (top/center/bottom × left/center/right)

Credit Tracking (check_credits tool) — NEW

  • Automatic credit usage logging from API response headers (x-pspdfkit-credit-usage, x-pspdfkit-remaining-credits)
  • SQLite-based local storage (better-sqlite3 + env-paths)
  • Three actions:
    • balance — remaining credits, daily rate, days until exhaustion
    • usage — breakdown by operation type (day/week/month/all)
    • forecast — projected exhaustion date with confidence level
  • Operation classification from /build instructions and endpoint routing

Testing

  • 67 tests passing across 4 test files
  • 41 existing unit tests (unchanged)
  • 10 AI redaction tests
  • 3 directory tree tests
  • 13 new credit tracking tests (storage, aggregator, forecaster)
  • Build compiles clean
  • All changes backward-compatible

Commits

  1. feat: add AI redaction tool, applyInstantJson, HTML layout & watermark positioning
  2. docs: update README with AI redaction, form filling, and HTML layout sections
  3. fix: address review findings — validation, safety guards, test gaps
  4. feat: add credit tracking with check_credits MCP tool

New Files

  • src/credits/storage.ts — SQLite database layer
  • src/credits/aggregator.ts — Usage summaries + forecasting
  • src/credits/index.ts — Module exports

Modified Files

  • src/dws/api.ts — Header interception for credit tracking + operation classification
  • src/index.ts — New check_credits and ai_redactor tools registered
  • src/schemas.ts — Schemas for credit tracking and AI redaction
  • package.json — Added better-sqlite3, env-paths dependencies
  • README.md — Documentation for all new features

…k positioning

- Add new 'ai_redactor' MCP tool wrapping /ai/redact endpoint
  - AI-powered PII detection and permanent redaction
  - 5-minute timeout for 60-120s AI analysis
  - Supports custom criteria (default: all PII)

- Enable 'applyInstantJson' build action (was commented out)
  - PDF form filling, form field creation, annotation import
  - Same file reference pattern as applyXfdf

- Add HTML-to-PDF layout options to FilePartSchema
  - Page orientation (landscape/portrait)
  - Page size presets (A0-A8, Letter, Legal) and custom dimensions
  - Margin control (top/right/bottom/left in mm)

- Uncomment watermark positioning and font options
  - Position offsets (top/right/bottom/left)
  - Font customization (fontFamily, fontSize, fontStyle)

- Add optional timeout parameter to callNutrientApi()
- 8 new unit tests for AI redaction (51 total, all passing)
- Update document_processor tool description
…sections

- Add AI Redaction, Form Filling, and HTML-to-PDF sections to features table
- Add detailed usage sections with example prompts
- Note 60-120s latency for AI redaction
- Link to Instant JSON documentation
- Update example prompts to include form filling
Architecture review fixes:
- Add .min(1).max(1000) validation on criteria param
- Add guard: output path !== input path (corruption prevention)
- Add applyInstantJson test in build pipeline (was P0 with zero tests)
- Add FormData structure assertion for AI redaction
- Add output===input rejection test

Code review fixes:
- Use ES6 shorthand for criteria in JSON.stringify
- Simplify timeout spread in api.ts
- Remove unused path import in ai-redact.test.ts

Test count: 54 passing (was 51)
@jdrhyne jdrhyne requested a review from nickwinder February 4, 2026 18:00
- Add SQLite-based credit usage storage (better-sqlite3 + env-paths)
- Intercept x-pspdfkit-credit-usage and x-pspdfkit-remaining-credits
  response headers after each API call
- Classify operations from endpoint and /build instructions
- Add aggregator with period-based usage summaries (day/week/month/all)
- Add forecaster projecting credit exhaustion with confidence levels
- Register check_credits tool with balance/usage/forecast actions
- 13 new tests for storage, aggregator, and forecaster
@jdrhyne jdrhyne changed the title feat: AI redaction tool, applyInstantJson, HTML layout & watermark positioning feat: AI redaction, Instant JSON, credit tracking & enhanced document processing Feb 4, 2026
Copy link
Collaborator

@nickwinder nickwinder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I don't agree with adding a local DB for credit tracking. I would opt for querying the API instead to get the most up-to-date and 100% correct credit information. https://www.nutrient.io/api/reference/public/#tag/Account/operation/get-account-info

  2. We should split the addition of each feature into it's own PR to follow best development practices and make reviews easier.

  3. The addition of the apply JSON action testing should follow the same pattern as the other build tests, i.e. create a build example and add it to build.tests.ts file.

  4. AI redaction should support stage and apply options in the payload. Right now it takes the default which may not be desirable. https://www.nutrient.io/api/reference/public/#tag/AI/operation/ai-redact.body.data

@jdrhyne
Copy link
Contributor Author

jdrhyne commented Feb 5, 2026

Nick - Thanks for the thorough review! I've split the original PR into four focused PRs addressing your feedback:

PR Feature Addresses
#13 HTML Layout + Watermark Positioning Point #2 (smaller PRs)
#14 applyInstantJson action Points #2, #3 (tests follow build pattern)
#15 AI Redaction with stage/apply Points #2, #4 (stage/apply options)
#16 Credit Tracking via API Points #1, #2 (API-based, no local SQLite)

Each PR is independent and can be reviewed/merged in any order. All tests passing.

This PR can be closed once the replacements are reviewed.

@jdrhyne jdrhyne requested a review from nickwinder February 6, 2026 16:33
@jdrhyne
Copy link
Contributor Author

jdrhyne commented Feb 7, 2026

Closing — this has been split into focused PRs per review feedback:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants