UNPKG

@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.

143 lines (142 loc) 7.15 kB
"use strict"; 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.registerCustomFieldTools = registerCustomFieldTools; const zod_1 = require("zod"); const middlewares_1 = require("./utils/middlewares"); const queryString_1 = require("./utils/queryString"); function registerCustomFieldTools(server, api) { server.tool("list_custom_fields", "List all custom fields. IMPORTANT: When a user query may refer to a custom field, you should fetch all custom fields up front (with a large enough page_size), cache them for the session, and search locally for matches by name before making further API calls. This reduces redundant requests and handles ambiguity efficiently.", { 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_1) => __awaiter(this, [...args_1], void 0, function* (args = {}) { if (!api) throw new Error("Please configure API connection first"); const queryString = (0, queryString_1.buildQueryString)(args); const response = yield api.request(`/custom_fields/${queryString ? `?${queryString}` : ""}`); return { content: [ { type: "text", text: JSON.stringify(response), }, ], }; }))); server.tool("get_custom_field", "Get a specific custom field by ID with full details including data type and extra configuration.", { 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.getCustomField(args.id); return { content: [{ type: "text", text: JSON.stringify(response) }], }; }))); server.tool("create_custom_field", "Create a new custom field with a specified data type (string, url, date, boolean, integer, float, monetary, documentlink, or select).", { name: zod_1.z.string(), data_type: zod_1.z.enum([ "string", "url", "date", "boolean", "integer", "float", "monetary", "documentlink", "select", ]), extra_data: zod_1.z.record(zod_1.z.unknown()).nullable().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 response = yield api.createCustomField(args); return { content: [{ type: "text", text: JSON.stringify(response) }], }; }))); server.tool("update_custom_field", "Update an existing custom field's name, data type, or extra configuration data.", { id: zod_1.z.number(), name: zod_1.z.string().optional(), data_type: zod_1.z .enum([ "string", "url", "date", "boolean", "integer", "float", "monetary", "documentlink", "select", ]) .optional(), extra_data: zod_1.z.record(zod_1.z.unknown()).nullable().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 { id } = args, data = __rest(args, ["id"]); const response = yield api.updateCustomField(id, data); return { content: [{ type: "text", text: JSON.stringify(response) }], }; }))); server.tool("delete_custom_field", "⚠️ DESTRUCTIVE: Permanently delete a custom field from the entire system. This will remove the field from ALL documents that use it.", { 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.deleteCustomField(args.id); return { content: [ { type: "text", text: JSON.stringify({ status: "deleted" }) }, ], }; }))); server.tool("bulk_edit_custom_fields", "Bulk edit custom fields. ⚠️ WARNING: 'delete' operation permanently removes custom fields from the entire system.", { custom_fields: zod_1.z.array(zod_1.z.number()), operation: zod_1.z.enum(["delete"]), confirm: zod_1.z.boolean().optional().describe("Must be true when operation is 'delete' to confirm 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.operation === "delete" && !args.confirm) { throw new Error("Confirmation required for destructive operation. Set confirm: true to proceed."); } const response = yield api.bulkEditObjects(args.custom_fields, "custom_field", args.operation); return { content: [ { type: "text", text: JSON.stringify(response), }, ], }; }))); }