@baruchiro/paperless-mcp
Version:
Model Context Protocol (MCP) server for interacting with Paperless-NGX document management system. Enables AI assistants to manage documents, tags, correspondents, and document types through the Paperless-NGX API.
105 lines (104 loc) • 4.66 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertDocsWithNames = convertDocsWithNames;
function convertDocsWithNames(input, api) {
return __awaiter(this, void 0, void 0, function* () {
if ("results" in input) {
const enhancedResults = yield enhanceDocumentsArray(input.results || [], api);
return {
content: [
{
type: "text",
text: (enhancedResults === null || enhancedResults === void 0 ? void 0 : enhancedResults.length)
? JSON.stringify(Object.assign(Object.assign({}, input), { results: enhancedResults }))
: "No documents found",
},
],
};
}
if (!input) {
return {
content: [
{
type: "text",
text: "No document found",
},
],
};
}
const [enhanced] = yield enhanceDocumentsArray([input], api);
return {
content: [
{
type: "text",
text: JSON.stringify(enhanced),
},
],
};
});
}
function enhanceDocumentsArray(documents, api) {
return __awaiter(this, void 0, void 0, function* () {
if (!(documents === null || documents === void 0 ? void 0 : documents.length)) {
return [];
}
const [correspondents, documentTypes, tags, customFields] = yield Promise.all([
api.getCorrespondents(),
api.getDocumentTypes(),
api.getTags(),
api.getCustomFields(),
]);
const correspondentMap = new Map((correspondents.results || []).map((c) => [c.id, c.name]));
const documentTypeMap = new Map((documentTypes.results || []).map((dt) => [dt.id, dt.name]));
const tagMap = new Map((tags.results || []).map((tag) => [tag.id, tag.name]));
const customFieldMap = new Map((customFields.results || []).map((cf) => [cf.id, cf.name]));
return documents
.map((doc) => {
const { content } = doc, docWithoutContent = __rest(doc, ["content"]);
return docWithoutContent;
})
.map((doc) => (Object.assign(Object.assign({}, doc), { correspondent: doc.correspondent
? {
id: doc.correspondent,
name: correspondentMap.get(doc.correspondent) ||
String(doc.correspondent),
}
: null, document_type: doc.document_type
? {
id: doc.document_type,
name: documentTypeMap.get(doc.document_type) || String(doc.document_type),
}
: null, tags: Array.isArray(doc.tags)
? doc.tags.map((tagId) => ({
id: tagId,
name: tagMap.get(tagId) || String(tagId),
}))
: doc.tags, custom_fields: Array.isArray(doc.custom_fields)
? doc.custom_fields.map((field) => ({
field: field.field,
name: customFieldMap.get(field.field) || String(field.field),
value: field.value,
}))
: doc.custom_fields })));
});
}