@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.
162 lines (161 loc) • 7.96 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.registerCorrespondentTools = registerCorrespondentTools;
const zod_1 = require("zod");
const types_1 = require("../api/types");
const utils_1 = require("../api/utils");
const middlewares_1 = require("./utils/middlewares");
const queryString_1 = require("./utils/queryString");
function registerCorrespondentTools(server, api) {
server.tool("list_correspondents", "List all correspondents with optional filtering and pagination. Correspondents represent entities that send or receive documents.", {
page: zod_1.z.number().optional(),
page_size: zod_1.z.number().optional(),
name__icontains: zod_1.z.string().optional(),
name__iendswith: zod_1.z.string().optional(),
name__iexact: zod_1.z.string().optional(),
name__istartswith: zod_1.z.string().optional(),
ordering: zod_1.z.string().optional(),
}, (0, middlewares_1.withErrorHandling)((args, extra) => __awaiter(this, void 0, void 0, function* () {
if (!api)
throw new Error("Please configure API connection first");
const queryString = (0, queryString_1.buildQueryString)(args);
const response = yield api.getCorrespondents(queryString);
const enhancedResults = (0, utils_1.enhanceMatchingAlgorithmArray)(response.results || []);
return {
content: [
{
type: "text",
text: JSON.stringify(Object.assign(Object.assign({}, response), { results: enhancedResults })),
},
],
};
})));
server.tool("get_correspondent", "Get a specific correspondent by ID with full details including matching rules.", { id: zod_1.z.number() }, (0, middlewares_1.withErrorHandling)((args, extra) => __awaiter(this, void 0, void 0, function* () {
if (!api)
throw new Error("Please configure API connection first");
const response = yield api.getCorrespondent(args.id);
const enhancedCorrespondent = (0, utils_1.enhanceMatchingAlgorithm)(response);
return {
content: [
{ type: "text", text: JSON.stringify(enhancedCorrespondent) },
],
};
})));
server.tool("create_correspondent", "Create a new correspondent with optional matching pattern and algorithm for automatic document assignment.", {
name: zod_1.z.string(),
match: zod_1.z.string().optional(),
matching_algorithm: zod_1.z
.number()
.int()
.min(0)
.max(6)
.optional()
.describe(types_1.MATCHING_ALGORITHM_DESCRIPTION),
}, (0, middlewares_1.withErrorHandling)((args, extra) => __awaiter(this, void 0, void 0, function* () {
if (!api)
throw new Error("Please configure API connection first");
const response = yield api.createCorrespondent(args);
const enhancedCorrespondent = (0, utils_1.enhanceMatchingAlgorithm)(response);
return {
content: [
{ type: "text", text: JSON.stringify(enhancedCorrespondent) },
],
};
})));
server.tool("update_correspondent", "Update an existing correspondent's name, matching pattern, or matching algorithm.", {
id: zod_1.z.number(),
name: zod_1.z.string(),
match: zod_1.z.string().optional(),
matching_algorithm: zod_1.z
.number()
.int()
.min(0)
.max(6)
.optional()
.describe(types_1.MATCHING_ALGORITHM_DESCRIPTION),
}, (0, middlewares_1.withErrorHandling)((args, extra) => __awaiter(this, void 0, void 0, function* () {
if (!api)
throw new Error("Please configure API connection first");
const { id } = args, data = __rest(args, ["id"]);
const response = yield api.updateCorrespondent(id, data);
const enhancedCorrespondent = (0, utils_1.enhanceMatchingAlgorithm)(response);
return {
content: [
{ type: "text", text: JSON.stringify(enhancedCorrespondent) },
],
};
})));
server.tool("delete_correspondent", "⚠️ DESTRUCTIVE: Permanently delete a correspondent from the entire system. This will affect ALL documents that use this correspondent.", {
id: zod_1.z.number(),
confirm: zod_1.z
.boolean()
.describe("Must be true to confirm this destructive operation"),
}, (0, middlewares_1.withErrorHandling)((args, extra) => __awaiter(this, void 0, void 0, function* () {
if (!api)
throw new Error("Please configure API connection first");
if (!args.confirm) {
throw new Error("Confirmation required for destructive operation. Set confirm: true to proceed.");
}
yield api.deleteCorrespondent(args.id);
return {
content: [
{ type: "text", text: JSON.stringify({ status: "deleted" }) },
],
};
})));
server.tool("bulk_edit_correspondents", "Bulk edit correspondents. ⚠️ WARNING: 'delete' operation permanently removes correspondents from the entire system.", {
correspondent_ids: zod_1.z.array(zod_1.z.number()),
operation: zod_1.z.enum(["set_permissions", "delete"]),
confirm: zod_1.z
.boolean()
.optional()
.describe("Must be true when operation is 'delete' to confirm destructive operation"),
owner: zod_1.z.number().optional(),
permissions: zod_1.z
.object({
view: zod_1.z.object({
users: zod_1.z.array(zod_1.z.number()).optional(),
groups: zod_1.z.array(zod_1.z.number()).optional(),
}),
change: zod_1.z.object({
users: zod_1.z.array(zod_1.z.number()).optional(),
groups: zod_1.z.array(zod_1.z.number()).optional(),
}),
})
.optional(),
merge: zod_1.z.boolean().optional(),
}, (0, middlewares_1.withErrorHandling)((args, extra) => __awaiter(this, void 0, void 0, function* () {
if (!api)
throw new Error("Please configure API connection first");
if (args.operation === "delete" && !args.confirm) {
throw new Error("Confirmation required for destructive operation. Set confirm: true to proceed.");
}
return api.bulkEditObjects(args.correspondent_ids, "correspondents", args.operation, args.operation === "set_permissions"
? {
owner: args.owner,
permissions: args.permissions,
merge: args.merge,
}
: {});
})));
}