@retiform/paperless-mcp 2.3.6 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +301 -4
- package/build/api/PaperlessAPI.js +1 -6
- package/build/search/build-outcome.d.ts +28 -0
- package/build/search/build-outcome.js +31 -0
- package/build/search/chunk.d.ts +16 -0
- package/build/search/chunk.js +64 -0
- package/build/search/config.d.ts +120 -0
- package/build/search/config.js +264 -0
- package/build/search/embedder.d.ts +346 -0
- package/build/search/embedder.js +734 -0
- package/build/search/rank.d.ts +11 -0
- package/build/search/rank.js +87 -0
- package/build/search/store.d.ts +406 -0
- package/build/search/store.js +847 -0
- package/build/search/sync.d.ts +204 -0
- package/build/search/sync.js +819 -0
- package/build/search/webhook.d.ts +193 -0
- package/build/search/webhook.js +445 -0
- package/build/server.d.ts +29 -0
- package/build/server.js +77 -0
- package/build/tools/documents.d.ts +13 -3
- package/build/tools/documents.js +23 -6
- package/build/tools/search.d.ts +518 -0
- package/build/tools/search.js +1199 -0
- package/build/tools/utils/middlewares.d.ts +5 -0
- package/build/tools/utils/middlewares.js +282 -9
- package/package.json +9 -3
- package/paperless-mcp.dxt +0 -0
- package/build/api/documentEnhancer.test.d.ts +0 -1
- package/build/api/documentEnhancer.test.js +0 -80
- package/build/manifest.test.d.ts +0 -1
- package/build/manifest.test.js +0 -66
- package/build/resources/documents.test.d.ts +0 -1
- package/build/resources/documents.test.js +0 -124
- package/build/server.test.d.ts +0 -1
- package/build/server.test.js +0 -54
- package/build/tools/boundedContent.test.d.ts +0 -1
- package/build/tools/boundedContent.test.js +0 -565
- package/build/tools/documents.test.d.ts +0 -1
- package/build/tools/documents.test.js +0 -658
- package/build/tools/mail.test.d.ts +0 -1
- package/build/tools/mail.test.js +0 -212
- package/build/tools/notes.test.d.ts +0 -1
- package/build/tools/notes.test.js +0 -177
- package/build/tools/schemaCompat.test.d.ts +0 -1
- package/build/tools/schemaCompat.test.js +0 -59
- package/build/tools/utils/monetary.test.d.ts +0 -1
- package/build/tools/utils/monetary.test.js +0 -25
- package/build/tools/utils/resourceUri.test.d.ts +0 -1
- package/build/tools/utils/resourceUri.test.js +0 -50
- package/build/tools/utils/selectFields.test.d.ts +0 -1
- package/build/tools/utils/selectFields.test.js +0 -164
|
@@ -1,658 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const strict_1 = __importDefault(require("node:assert/strict"));
|
|
16
|
-
const node_fs_1 = require("node:fs");
|
|
17
|
-
const node_os_1 = require("node:os");
|
|
18
|
-
const node_path_1 = require("node:path");
|
|
19
|
-
const node_test_1 = require("node:test");
|
|
20
|
-
const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
|
|
21
|
-
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
22
|
-
const documents_1 = require("./documents");
|
|
23
|
-
const documentQuery_1 = require("./utils/documentQuery");
|
|
24
|
-
const resourceUri_1 = require("./utils/resourceUri");
|
|
25
|
-
function getQueryParams(queryString) {
|
|
26
|
-
return new URLSearchParams(queryString.replace(/^\?/, ""));
|
|
27
|
-
}
|
|
28
|
-
function getDocumentQueryParamsFromOpenApi() {
|
|
29
|
-
const openApiPath = (0, node_path_1.join)(process.cwd(), "Paperless_ngx_REST_API.yaml");
|
|
30
|
-
const text = (0, node_fs_1.readFileSync)(openApiPath, "utf8");
|
|
31
|
-
const start = text.indexOf(" /api/documents/:");
|
|
32
|
-
const end = text.indexOf(" /api/documents/{id}/:");
|
|
33
|
-
strict_1.default.ok(start >= 0, "OpenAPI docs marker '/api/documents/' not found");
|
|
34
|
-
strict_1.default.ok(end > start, "OpenAPI docs marker '/api/documents/{id}/' not found or out of order");
|
|
35
|
-
const section = text.slice(start, end);
|
|
36
|
-
return Array.from(section.matchAll(/^\s*-?\s*name:\s+(.+)$/gm), (match) => match[1]).sort();
|
|
37
|
-
}
|
|
38
|
-
// ALLOWED_UPLOAD_PATHS is read from the environment at module load, so the
|
|
39
|
-
// allowlist can only be exercised by re-importing the module with the env set.
|
|
40
|
-
function validateFilePathWithAllowlist(allowedPaths) {
|
|
41
|
-
const modulePath = require.resolve("./documents");
|
|
42
|
-
const previous = process.env.PAPERLESS_MCP_UPLOAD_PATHS;
|
|
43
|
-
process.env.PAPERLESS_MCP_UPLOAD_PATHS = allowedPaths;
|
|
44
|
-
delete require.cache[modulePath];
|
|
45
|
-
try {
|
|
46
|
-
return require("./documents").validateFilePath;
|
|
47
|
-
}
|
|
48
|
-
finally {
|
|
49
|
-
delete require.cache[modulePath];
|
|
50
|
-
if (previous === undefined) {
|
|
51
|
-
delete process.env.PAPERLESS_MCP_UPLOAD_PATHS;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
process.env.PAPERLESS_MCP_UPLOAD_PATHS = previous;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
(0, node_test_1.test)("buildBulkEditParameters sends Paperless bulk custom fields as id:value map", () => {
|
|
59
|
-
const parameters = (0, documents_1.buildBulkEditParameters)({ remove_custom_fields: [] }, [
|
|
60
|
-
{ field: 9, value: "" },
|
|
61
|
-
{ field: 10, value: "2026-05-14" },
|
|
62
|
-
]);
|
|
63
|
-
strict_1.default.deepEqual(parameters, {
|
|
64
|
-
remove_custom_fields: [],
|
|
65
|
-
add_custom_fields: {
|
|
66
|
-
"9": "",
|
|
67
|
-
"10": "2026-05-14",
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
strict_1.default.ok(!("assign_custom_fields" in parameters));
|
|
71
|
-
strict_1.default.ok(!("assign_custom_fields_values" in parameters));
|
|
72
|
-
});
|
|
73
|
-
(0, node_test_1.test)("buildBulkEditParameters preserves null custom field values", () => {
|
|
74
|
-
const parameters = (0, documents_1.buildBulkEditParameters)({}, [{ field: 9, value: null }]);
|
|
75
|
-
strict_1.default.deepEqual(parameters, {
|
|
76
|
-
add_custom_fields: {
|
|
77
|
-
"9": null,
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
(0, node_test_1.test)("buildBulkEditParameters includes Paperless-required empty custom field keys", () => {
|
|
82
|
-
const parameters = (0, documents_1.buildBulkEditParameters)({}, undefined, true);
|
|
83
|
-
strict_1.default.deepEqual(parameters, {
|
|
84
|
-
add_custom_fields: {},
|
|
85
|
-
remove_custom_fields: [],
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
(0, node_test_1.test)("buildBulkEditParameters preserves an empty custom fields array", () => {
|
|
89
|
-
const parameters = (0, documents_1.buildBulkEditParameters)({}, []);
|
|
90
|
-
strict_1.default.deepEqual(parameters, {
|
|
91
|
-
add_custom_fields: {},
|
|
92
|
-
});
|
|
93
|
-
strict_1.default.ok(!("remove_custom_fields" in parameters));
|
|
94
|
-
});
|
|
95
|
-
(0, node_test_1.test)("buildBulkEditParameters preserves an empty custom fields array with defaults", () => {
|
|
96
|
-
const parameters = (0, documents_1.buildBulkEditParameters)({}, [], true);
|
|
97
|
-
strict_1.default.deepEqual(parameters, {
|
|
98
|
-
add_custom_fields: {},
|
|
99
|
-
remove_custom_fields: [],
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
(0, node_test_1.test)("buildBulkEditParameters combines base parameters with custom fields", () => {
|
|
103
|
-
const parameters = (0, documents_1.buildBulkEditParameters)({ add_tags: [3], remove_tags: [1, 2] }, [{ field: 9, value: "pending" }]);
|
|
104
|
-
strict_1.default.deepEqual(parameters, {
|
|
105
|
-
add_tags: [3],
|
|
106
|
-
remove_tags: [1, 2],
|
|
107
|
-
add_custom_fields: {
|
|
108
|
-
"9": "pending",
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
(0, node_test_1.test)("buildBulkEditParameters preserves supported custom field value types", () => {
|
|
113
|
-
const parameters = (0, documents_1.buildBulkEditParameters)({}, [
|
|
114
|
-
{ field: 1, value: 42 },
|
|
115
|
-
{ field: 2, value: true },
|
|
116
|
-
{ field: 3, value: "" },
|
|
117
|
-
{ field: 4, value: null },
|
|
118
|
-
{ field: 5, value: [123, 456] },
|
|
119
|
-
]);
|
|
120
|
-
strict_1.default.deepEqual(parameters.add_custom_fields, {
|
|
121
|
-
"1": 42,
|
|
122
|
-
"2": true,
|
|
123
|
-
"3": "",
|
|
124
|
-
"4": null,
|
|
125
|
-
"5": [123, 456],
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
(0, node_test_1.test)("paperless filter allowlist stays in sync with the document OpenAPI section", () => {
|
|
129
|
-
const documentedParams = getDocumentQueryParamsFromOpenApi();
|
|
130
|
-
const allowedParams = [...documentQuery_1.DOCUMENT_QUERY_PAPERLESS_FILTER_KEYS].sort();
|
|
131
|
-
strict_1.default.deepEqual(allowedParams, documentedParams);
|
|
132
|
-
});
|
|
133
|
-
(0, node_test_1.test)("serializes full-text query_documents arguments", () => {
|
|
134
|
-
const query = getQueryParams((0, documentQuery_1.buildDocumentQueryString)({
|
|
135
|
-
query: "invoice 2024",
|
|
136
|
-
search: "jan",
|
|
137
|
-
more_like_id: 42,
|
|
138
|
-
}));
|
|
139
|
-
strict_1.default.equal(query.get("query"), "invoice 2024");
|
|
140
|
-
strict_1.default.equal(query.get("search"), "jan");
|
|
141
|
-
strict_1.default.equal(query.get("more_like_id"), "42");
|
|
142
|
-
});
|
|
143
|
-
(0, node_test_1.test)("serializes first-class document filters using Paperless parameter names", () => {
|
|
144
|
-
const query = getQueryParams((0, documentQuery_1.buildDocumentQueryString)({
|
|
145
|
-
page: 2,
|
|
146
|
-
page_size: 50,
|
|
147
|
-
ordering: "-created",
|
|
148
|
-
correspondent: 3,
|
|
149
|
-
document_type: 4,
|
|
150
|
-
tag: 5,
|
|
151
|
-
storage_path: 6,
|
|
152
|
-
created__date__gte: "2024-01-01",
|
|
153
|
-
created__date__lte: "2024-12-31",
|
|
154
|
-
archive_serial_number: 99,
|
|
155
|
-
archive_serial_number__isnull: false,
|
|
156
|
-
custom_fields__icontains: "invoice",
|
|
157
|
-
}));
|
|
158
|
-
strict_1.default.equal(query.get("page"), "2");
|
|
159
|
-
strict_1.default.equal(query.get("page_size"), "50");
|
|
160
|
-
strict_1.default.equal(query.get("ordering"), "-created");
|
|
161
|
-
strict_1.default.equal(query.get("correspondent__id"), "3");
|
|
162
|
-
strict_1.default.equal(query.get("document_type__id"), "4");
|
|
163
|
-
strict_1.default.equal(query.get("tags__id"), "5");
|
|
164
|
-
strict_1.default.equal(query.get("storage_path__id"), "6");
|
|
165
|
-
strict_1.default.equal(query.get("created__date__gte"), "2024-01-01");
|
|
166
|
-
strict_1.default.equal(query.get("created__date__lte"), "2024-12-31");
|
|
167
|
-
strict_1.default.equal(query.get("archive_serial_number"), "99");
|
|
168
|
-
strict_1.default.equal(query.get("archive_serial_number__isnull"), "false");
|
|
169
|
-
strict_1.default.equal(query.get("custom_fields__icontains"), "invoice");
|
|
170
|
-
});
|
|
171
|
-
(0, node_test_1.test)("serializes paperless_filters arrays as comma-separated values", () => {
|
|
172
|
-
const query = getQueryParams((0, documentQuery_1.buildDocumentQueryString)({
|
|
173
|
-
paperless_filters: {
|
|
174
|
-
fields: ["title", "tags"],
|
|
175
|
-
id__in: [1, 2, 3],
|
|
176
|
-
},
|
|
177
|
-
}));
|
|
178
|
-
strict_1.default.equal(query.get("fields"), "title,tags");
|
|
179
|
-
strict_1.default.equal(query.get("id__in"), "1,2,3");
|
|
180
|
-
});
|
|
181
|
-
(0, node_test_1.test)("serializes raw list custom_field_query strings without JSON encoding", () => {
|
|
182
|
-
const rawCustomFieldQuery = '[7, "icontains", "value"]';
|
|
183
|
-
const query = getQueryParams((0, documentQuery_1.buildDocumentQueryString)({
|
|
184
|
-
custom_field_query: rawCustomFieldQuery,
|
|
185
|
-
}));
|
|
186
|
-
strict_1.default.equal(query.get("custom_field_query"), rawCustomFieldQuery);
|
|
187
|
-
});
|
|
188
|
-
(0, node_test_1.test)("serializes leaf custom_field_query values as JSON", () => {
|
|
189
|
-
const query = getQueryParams((0, documentQuery_1.buildDocumentQueryString)({
|
|
190
|
-
custom_field_query: ["Invoice Number", "exact", "12345"],
|
|
191
|
-
}));
|
|
192
|
-
strict_1.default.equal(query.get("custom_field_query"), JSON.stringify(["Invoice Number", "exact", "12345"]));
|
|
193
|
-
});
|
|
194
|
-
(0, node_test_1.test)("serializes numeric custom_field_query field IDs as JSON", () => {
|
|
195
|
-
const query = getQueryParams((0, documentQuery_1.buildDocumentQueryString)({
|
|
196
|
-
custom_field_query: [7, "exact", "12345"],
|
|
197
|
-
}));
|
|
198
|
-
strict_1.default.equal(query.get("custom_field_query"), JSON.stringify([7, "exact", "12345"]));
|
|
199
|
-
});
|
|
200
|
-
(0, node_test_1.test)("serializes grouped custom_field_query values as JSON", () => {
|
|
201
|
-
const groupedQuery = [
|
|
202
|
-
"OR",
|
|
203
|
-
[
|
|
204
|
-
["Invoice Number", "isnull", true],
|
|
205
|
-
["Invoice Number", "exact", ""],
|
|
206
|
-
],
|
|
207
|
-
];
|
|
208
|
-
const query = getQueryParams((0, documentQuery_1.buildDocumentQueryString)({
|
|
209
|
-
custom_field_query: groupedQuery,
|
|
210
|
-
}));
|
|
211
|
-
strict_1.default.equal(query.get("custom_field_query"), JSON.stringify(groupedQuery));
|
|
212
|
-
});
|
|
213
|
-
(0, node_test_1.test)("rejects unsupported paperless_filters keys", () => {
|
|
214
|
-
strict_1.default.throws(() => (0, documentQuery_1.buildDocumentQueryString)({
|
|
215
|
-
paperless_filters: {
|
|
216
|
-
not_a_real_filter: "value",
|
|
217
|
-
},
|
|
218
|
-
}), /Unsupported paperless_filters key/);
|
|
219
|
-
});
|
|
220
|
-
(0, node_test_1.test)("rejects duplicate first-class and paperless_filters definitions", () => {
|
|
221
|
-
strict_1.default.throws(() => (0, documentQuery_1.buildDocumentQueryString)({
|
|
222
|
-
correspondent: 7,
|
|
223
|
-
paperless_filters: {
|
|
224
|
-
correspondent__id: 7,
|
|
225
|
-
},
|
|
226
|
-
}), /Duplicate filter 'correspondent__id'/);
|
|
227
|
-
});
|
|
228
|
-
(0, node_test_1.test)("rejects invalid custom_field_query shapes", () => {
|
|
229
|
-
strict_1.default.equal(documentQuery_1.customFieldQuerySchema.safeParse(["field", "exact"]).success, false);
|
|
230
|
-
strict_1.default.equal(documentQuery_1.customFieldQuerySchema.safeParse(["AND", []]).success, false);
|
|
231
|
-
strict_1.default.equal(documentQuery_1.customFieldQuerySchema.safeParse(["AND", [["field"]]]).success, false);
|
|
232
|
-
strict_1.default.equal(documentQuery_1.customFieldQuerySchema.safeParse(["AND", "iexact", "foo"]).success, false);
|
|
233
|
-
});
|
|
234
|
-
(0, node_test_1.describe)("validateFilePath", () => {
|
|
235
|
-
let testDir;
|
|
236
|
-
let testFile;
|
|
237
|
-
let emptyFile;
|
|
238
|
-
let symlinkPath;
|
|
239
|
-
(0, node_test_1.before)(() => {
|
|
240
|
-
testDir = (0, node_fs_1.mkdtempSync)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "paperless-mcp-test-"));
|
|
241
|
-
testFile = (0, node_path_1.join)(testDir, "test.pdf");
|
|
242
|
-
(0, node_fs_1.writeFileSync)(testFile, "%PDF-1.4\ntest content");
|
|
243
|
-
emptyFile = (0, node_path_1.join)(testDir, "empty.pdf");
|
|
244
|
-
(0, node_fs_1.writeFileSync)(emptyFile, "");
|
|
245
|
-
symlinkPath = (0, node_path_1.join)(testDir, "link.pdf");
|
|
246
|
-
try {
|
|
247
|
-
(0, node_fs_1.symlinkSync)(testFile, symlinkPath);
|
|
248
|
-
}
|
|
249
|
-
catch (_a) {
|
|
250
|
-
// Skip if unsupported.
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
(0, node_test_1.after)(() => {
|
|
254
|
-
(0, node_fs_1.rmSync)(testDir, { recursive: true, force: true });
|
|
255
|
-
});
|
|
256
|
-
(0, node_test_1.test)("accepts a valid absolute file path", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
257
|
-
yield strict_1.default.doesNotReject(() => (0, documents_1.validateFilePath)(testFile));
|
|
258
|
-
}));
|
|
259
|
-
(0, node_test_1.test)("rejects relative paths", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
260
|
-
yield strict_1.default.rejects(() => (0, documents_1.validateFilePath)("relative/path.pdf"), {
|
|
261
|
-
message: "file_path must be an absolute path",
|
|
262
|
-
});
|
|
263
|
-
}));
|
|
264
|
-
(0, node_test_1.test)("rejects non-existent files", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
265
|
-
yield strict_1.default.rejects(() => (0, documents_1.validateFilePath)((0, node_path_1.join)(testDir, "missing.pdf")), { message: "File not found" });
|
|
266
|
-
}));
|
|
267
|
-
(0, node_test_1.test)("rejects directories", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
268
|
-
yield strict_1.default.rejects(() => (0, documents_1.validateFilePath)(testDir), {
|
|
269
|
-
message: "Path must point to a regular file",
|
|
270
|
-
});
|
|
271
|
-
}));
|
|
272
|
-
(0, node_test_1.test)("rejects empty files", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
273
|
-
yield strict_1.default.rejects(() => (0, documents_1.validateFilePath)(emptyFile), {
|
|
274
|
-
message: "File is empty",
|
|
275
|
-
});
|
|
276
|
-
}));
|
|
277
|
-
(0, node_test_1.test)("resolves symlinks to the real file", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
278
|
-
try {
|
|
279
|
-
yield strict_1.default.doesNotReject(() => (0, documents_1.validateFilePath)(symlinkPath));
|
|
280
|
-
}
|
|
281
|
-
catch (_a) {
|
|
282
|
-
// Skip on systems without symlink support.
|
|
283
|
-
}
|
|
284
|
-
}));
|
|
285
|
-
(0, node_test_1.test)("rejects files exceeding the maximum size", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
286
|
-
const largeFile = (0, node_path_1.join)(testDir, "large.pdf");
|
|
287
|
-
(0, node_fs_1.writeFileSync)(largeFile, "%PDF-1.4\n");
|
|
288
|
-
(0, node_fs_1.truncateSync)(largeFile, 101 * 1024 * 1024);
|
|
289
|
-
yield strict_1.default.rejects(() => (0, documents_1.validateFilePath)(largeFile), {
|
|
290
|
-
message: /exceeds maximum allowed size/,
|
|
291
|
-
});
|
|
292
|
-
}));
|
|
293
|
-
(0, node_test_1.test)("accepts files within allowed upload paths", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
294
|
-
const validate = validateFilePathWithAllowlist((0, node_fs_1.realpathSync)(testDir));
|
|
295
|
-
yield strict_1.default.doesNotReject(() => validate(testFile));
|
|
296
|
-
}));
|
|
297
|
-
(0, node_test_1.test)("rejects files outside allowed upload paths", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
298
|
-
const validate = validateFilePathWithAllowlist("/some/other/path");
|
|
299
|
-
yield strict_1.default.rejects(() => validate(testFile), {
|
|
300
|
-
message: /outside allowed upload directories/,
|
|
301
|
-
});
|
|
302
|
-
}));
|
|
303
|
-
});
|
|
304
|
-
class TestTransport {
|
|
305
|
-
start() {
|
|
306
|
-
return __awaiter(this, void 0, void 0, function* () { });
|
|
307
|
-
}
|
|
308
|
-
send(message) {
|
|
309
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
310
|
-
queueMicrotask(() => { var _a, _b; return (_b = (_a = this.peer) === null || _a === void 0 ? void 0 : _a.onmessage) === null || _b === void 0 ? void 0 : _b.call(_a, message); });
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
close() {
|
|
314
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
315
|
-
var _a;
|
|
316
|
-
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
function createTransportPair() {
|
|
321
|
-
const clientTransport = new TestTransport();
|
|
322
|
-
const serverTransport = new TestTransport();
|
|
323
|
-
clientTransport.peer = serverTransport;
|
|
324
|
-
serverTransport.peer = clientTransport;
|
|
325
|
-
return { clientTransport, serverTransport };
|
|
326
|
-
}
|
|
327
|
-
function parseToolText(result) {
|
|
328
|
-
var _a;
|
|
329
|
-
const item = (_a = result.content) === null || _a === void 0 ? void 0 : _a[0];
|
|
330
|
-
if (!item || item.type !== "text") {
|
|
331
|
-
throw new Error("Expected text tool response");
|
|
332
|
-
}
|
|
333
|
-
return JSON.parse(item.text);
|
|
334
|
-
}
|
|
335
|
-
function withDocumentClient(api, run) {
|
|
336
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
337
|
-
const server = new mcp_js_1.McpServer({ name: "paperless-doc-test", version: "1.0.0" });
|
|
338
|
-
(0, documents_1.registerDocumentTools)(server, api);
|
|
339
|
-
const client = new index_js_1.Client({
|
|
340
|
-
name: "paperless-doc-test-client",
|
|
341
|
-
version: "1.0.0",
|
|
342
|
-
});
|
|
343
|
-
const { clientTransport, serverTransport } = createTransportPair();
|
|
344
|
-
yield server.connect(serverTransport);
|
|
345
|
-
yield client.connect(clientTransport);
|
|
346
|
-
try {
|
|
347
|
-
yield run(client);
|
|
348
|
-
}
|
|
349
|
-
finally {
|
|
350
|
-
yield client.close();
|
|
351
|
-
yield server.close();
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
function createDocumentApi(fields) {
|
|
356
|
-
const calls = {
|
|
357
|
-
updateDocument: [],
|
|
358
|
-
bulkEditDocuments: [],
|
|
359
|
-
getCustomField: [],
|
|
360
|
-
};
|
|
361
|
-
const fieldMap = new Map(fields.map((field) => [field.id, field]));
|
|
362
|
-
const api = {
|
|
363
|
-
getCustomField: (id) => __awaiter(this, void 0, void 0, function* () {
|
|
364
|
-
calls.getCustomField.push(id);
|
|
365
|
-
const field = fieldMap.get(id);
|
|
366
|
-
if (!field)
|
|
367
|
-
throw new Error(`custom field ${id} not found`);
|
|
368
|
-
return field;
|
|
369
|
-
}),
|
|
370
|
-
updateDocument: (id, data) => __awaiter(this, void 0, void 0, function* () {
|
|
371
|
-
var _a;
|
|
372
|
-
calls.updateDocument.push([id, data]);
|
|
373
|
-
return { id, custom_fields: (_a = data.custom_fields) !== null && _a !== void 0 ? _a : [] };
|
|
374
|
-
}),
|
|
375
|
-
bulkEditDocuments: (documents, method, parameters) => __awaiter(this, void 0, void 0, function* () {
|
|
376
|
-
calls.bulkEditDocuments.push([documents, method, parameters]);
|
|
377
|
-
return { result: "OK" };
|
|
378
|
-
}),
|
|
379
|
-
getCorrespondents: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
|
|
380
|
-
getDocumentTypes: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
|
|
381
|
-
getTags: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
|
|
382
|
-
getCustomFields: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
|
|
383
|
-
};
|
|
384
|
-
return { api, calls };
|
|
385
|
-
}
|
|
386
|
-
const LEGACY_SELECT_FIELD = {
|
|
387
|
-
id: 2,
|
|
388
|
-
name: "Retention period",
|
|
389
|
-
data_type: "select",
|
|
390
|
-
extra_data: { select_options: ["1 year", "7 years", "2 years"], default_currency: null },
|
|
391
|
-
document_count: 10,
|
|
392
|
-
};
|
|
393
|
-
const OBJECT_SELECT_FIELD = {
|
|
394
|
-
id: 3,
|
|
395
|
-
name: "Priority",
|
|
396
|
-
data_type: "select",
|
|
397
|
-
extra_data: {
|
|
398
|
-
select_options: [
|
|
399
|
-
{ id: "abc123", label: "Low" },
|
|
400
|
-
{ id: "def456", label: "High" },
|
|
401
|
-
],
|
|
402
|
-
},
|
|
403
|
-
document_count: 5,
|
|
404
|
-
};
|
|
405
|
-
(0, node_test_1.describe)("select custom field value resolution in document handlers", () => {
|
|
406
|
-
(0, node_test_1.test)("update_document translates a select label to its zero-based index", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
407
|
-
const { api, calls } = createDocumentApi([LEGACY_SELECT_FIELD]);
|
|
408
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
409
|
-
var _a;
|
|
410
|
-
const result = (yield client.callTool({
|
|
411
|
-
name: "update_document",
|
|
412
|
-
arguments: { id: 42, custom_fields: [{ field: 2, value: "1 year" }] },
|
|
413
|
-
}));
|
|
414
|
-
strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
|
|
415
|
-
}));
|
|
416
|
-
strict_1.default.equal(calls.updateDocument.length, 1);
|
|
417
|
-
const [, data] = calls.updateDocument[0];
|
|
418
|
-
strict_1.default.deepEqual(data.custom_fields, [{ field: 2, value: 0 }]);
|
|
419
|
-
}));
|
|
420
|
-
(0, node_test_1.test)("update_document sends the option id for 2.17+ select fields (stored form)", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
421
|
-
const { api, calls } = createDocumentApi([OBJECT_SELECT_FIELD]);
|
|
422
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
423
|
-
var _a;
|
|
424
|
-
const result = (yield client.callTool({
|
|
425
|
-
name: "update_document",
|
|
426
|
-
arguments: { id: 7, custom_fields: [{ field: 3, value: "High" }] },
|
|
427
|
-
}));
|
|
428
|
-
strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
|
|
429
|
-
}));
|
|
430
|
-
const [, data] = calls.updateDocument[0];
|
|
431
|
-
strict_1.default.deepEqual(data.custom_fields, [{ field: 3, value: "def456" }]);
|
|
432
|
-
}));
|
|
433
|
-
(0, node_test_1.test)("bulk_edit_documents translates a select label in add_custom_fields", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
434
|
-
const { api, calls } = createDocumentApi([LEGACY_SELECT_FIELD]);
|
|
435
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
436
|
-
var _a;
|
|
437
|
-
const result = (yield client.callTool({
|
|
438
|
-
name: "bulk_edit_documents",
|
|
439
|
-
arguments: {
|
|
440
|
-
documents: [1, 2],
|
|
441
|
-
method: "modify_custom_fields",
|
|
442
|
-
add_custom_fields: [{ field: 2, value: "7 years" }],
|
|
443
|
-
},
|
|
444
|
-
}));
|
|
445
|
-
strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
|
|
446
|
-
}));
|
|
447
|
-
strict_1.default.equal(calls.bulkEditDocuments.length, 1);
|
|
448
|
-
const [, , parameters] = calls.bulkEditDocuments[0];
|
|
449
|
-
strict_1.default.deepEqual(parameters.add_custom_fields, { "2": 1 });
|
|
450
|
-
}));
|
|
451
|
-
(0, node_test_1.test)("bulk_edit_documents sends the option id for 2.17+ select fields (stored form)", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
452
|
-
const { api, calls } = createDocumentApi([OBJECT_SELECT_FIELD]);
|
|
453
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
454
|
-
var _a;
|
|
455
|
-
const result = (yield client.callTool({
|
|
456
|
-
name: "bulk_edit_documents",
|
|
457
|
-
arguments: {
|
|
458
|
-
documents: [1],
|
|
459
|
-
method: "modify_custom_fields",
|
|
460
|
-
add_custom_fields: [{ field: 3, value: "High" }],
|
|
461
|
-
},
|
|
462
|
-
}));
|
|
463
|
-
strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
|
|
464
|
-
}));
|
|
465
|
-
const [, , parameters] = calls.bulkEditDocuments[0];
|
|
466
|
-
strict_1.default.deepEqual(parameters.add_custom_fields, { "3": "def456" });
|
|
467
|
-
}));
|
|
468
|
-
(0, node_test_1.test)("update_document rejects an unknown select option with a helpful error", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
469
|
-
const { api, calls } = createDocumentApi([LEGACY_SELECT_FIELD]);
|
|
470
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
471
|
-
var _a, _b;
|
|
472
|
-
const result = (yield client.callTool({
|
|
473
|
-
name: "update_document",
|
|
474
|
-
arguments: { id: 42, custom_fields: [{ field: 2, value: "forever" }] },
|
|
475
|
-
}));
|
|
476
|
-
strict_1.default.ok(result.isError, "expected an error for an unknown select option");
|
|
477
|
-
const message = (_b = (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error) !== null && _b !== void 0 ? _b : "";
|
|
478
|
-
strict_1.default.match(message, /forever/);
|
|
479
|
-
strict_1.default.match(message, /1 year/);
|
|
480
|
-
}));
|
|
481
|
-
strict_1.default.equal(calls.updateDocument.length, 0, "no document update should be sent when the option is invalid");
|
|
482
|
-
}));
|
|
483
|
-
});
|
|
484
|
-
(0, node_test_1.describe)("document resource reference tools", () => {
|
|
485
|
-
// Expected URIs come from the builders rather than literals: their exact
|
|
486
|
-
// format is already pinned by utils/resourceUri.test.ts, and what matters
|
|
487
|
-
// here is that the handler surfaces that URI in both content blocks.
|
|
488
|
-
const cases = [
|
|
489
|
-
{
|
|
490
|
-
tool: "download_document",
|
|
491
|
-
args: { id: 4 },
|
|
492
|
-
uri: (0, resourceUri_1.buildDocumentResourceUri)(4),
|
|
493
|
-
mimeType: "application/octet-stream",
|
|
494
|
-
},
|
|
495
|
-
{
|
|
496
|
-
tool: "download_document",
|
|
497
|
-
args: { id: 4, original: true },
|
|
498
|
-
uri: (0, resourceUri_1.buildDocumentResourceUri)(4, { original: true }),
|
|
499
|
-
mimeType: "application/octet-stream",
|
|
500
|
-
},
|
|
501
|
-
{
|
|
502
|
-
tool: "get_document_thumbnail",
|
|
503
|
-
args: { id: 123 },
|
|
504
|
-
uri: (0, resourceUri_1.buildThumbnailResourceUri)(123),
|
|
505
|
-
mimeType: "image/webp",
|
|
506
|
-
},
|
|
507
|
-
];
|
|
508
|
-
for (const { tool, args, uri, mimeType } of cases) {
|
|
509
|
-
(0, node_test_1.test)(`${tool} ${JSON.stringify(args)} returns the URI as text beside the resource`, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
510
|
-
const { api } = createDocumentApi([]);
|
|
511
|
-
let result;
|
|
512
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
513
|
-
result = (yield client.callTool({
|
|
514
|
-
name: tool,
|
|
515
|
-
arguments: args,
|
|
516
|
-
}));
|
|
517
|
-
}));
|
|
518
|
-
strict_1.default.ok(result && !result.isError, `${tool} failed`);
|
|
519
|
-
const [text, embedded] = result.content;
|
|
520
|
-
// Legacy clients read only content[].text (issue #134).
|
|
521
|
-
strict_1.default.equal(text.type, "text");
|
|
522
|
-
strict_1.default.equal(text.text, uri);
|
|
523
|
-
strict_1.default.equal(embedded.type, "resource");
|
|
524
|
-
const { resource } = embedded;
|
|
525
|
-
strict_1.default.equal(resource.uri, uri, "both blocks must reference the same URI");
|
|
526
|
-
strict_1.default.equal(resource.mimeType, mimeType);
|
|
527
|
-
}));
|
|
528
|
-
}
|
|
529
|
-
});
|
|
530
|
-
(0, node_test_1.describe)("bulk_edit_documents set_permissions", () => {
|
|
531
|
-
(0, node_test_1.test)("sends set_permissions, owner and merge at the top level of parameters", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
532
|
-
const { api, calls } = createDocumentApi([]);
|
|
533
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
534
|
-
var _a;
|
|
535
|
-
const result = (yield client.callTool({
|
|
536
|
-
name: "bulk_edit_documents",
|
|
537
|
-
arguments: {
|
|
538
|
-
documents: [4103],
|
|
539
|
-
method: "set_permissions",
|
|
540
|
-
set_permissions: {
|
|
541
|
-
view: { users: [], groups: [10] },
|
|
542
|
-
change: { users: [], groups: [10] },
|
|
543
|
-
},
|
|
544
|
-
owner: 3,
|
|
545
|
-
merge: true,
|
|
546
|
-
},
|
|
547
|
-
}));
|
|
548
|
-
strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
|
|
549
|
-
}));
|
|
550
|
-
strict_1.default.equal(calls.bulkEditDocuments.length, 1);
|
|
551
|
-
const [documents, method, parameters] = calls.bulkEditDocuments[0];
|
|
552
|
-
strict_1.default.deepEqual(documents, [4103]);
|
|
553
|
-
strict_1.default.equal(method, "set_permissions");
|
|
554
|
-
// Paperless reads parameters["set_permissions"] directly; nesting it under
|
|
555
|
-
// another key makes the server crash with a KeyError (HTTP 500).
|
|
556
|
-
strict_1.default.deepEqual(parameters, {
|
|
557
|
-
set_permissions: {
|
|
558
|
-
view: { users: [], groups: [10] },
|
|
559
|
-
change: { users: [], groups: [10] },
|
|
560
|
-
},
|
|
561
|
-
owner: 3,
|
|
562
|
-
merge: true,
|
|
563
|
-
});
|
|
564
|
-
}));
|
|
565
|
-
(0, node_test_1.test)("forwards partial permissions without filling in omitted actions or lists", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
566
|
-
const { api, calls } = createDocumentApi([]);
|
|
567
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
568
|
-
var _a;
|
|
569
|
-
const result = (yield client.callTool({
|
|
570
|
-
name: "bulk_edit_documents",
|
|
571
|
-
arguments: {
|
|
572
|
-
documents: [1],
|
|
573
|
-
method: "set_permissions",
|
|
574
|
-
set_permissions: { view: { groups: [10] } },
|
|
575
|
-
},
|
|
576
|
-
}));
|
|
577
|
-
strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
|
|
578
|
-
}));
|
|
579
|
-
// Paperless only touches the actions/lists that are present, and clears
|
|
580
|
-
// the owner when it is omitted without merge — so nothing may be added.
|
|
581
|
-
const [, , parameters] = calls.bulkEditDocuments[0];
|
|
582
|
-
strict_1.default.deepEqual(parameters, { set_permissions: { view: { groups: [10] } } });
|
|
583
|
-
}));
|
|
584
|
-
(0, node_test_1.test)("owner-only changes send an empty set_permissions object", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
585
|
-
const { api, calls } = createDocumentApi([]);
|
|
586
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
587
|
-
var _a;
|
|
588
|
-
for (const owner of [3, null]) {
|
|
589
|
-
const result = (yield client.callTool({
|
|
590
|
-
name: "bulk_edit_documents",
|
|
591
|
-
arguments: { documents: [1], method: "set_permissions", owner },
|
|
592
|
-
}));
|
|
593
|
-
strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
|
|
594
|
-
}
|
|
595
|
-
}));
|
|
596
|
-
strict_1.default.deepEqual(calls.bulkEditDocuments.map(([, , parameters]) => parameters), [
|
|
597
|
-
{ set_permissions: {}, owner: 3 },
|
|
598
|
-
{ set_permissions: {}, owner: null },
|
|
599
|
-
]);
|
|
600
|
-
}));
|
|
601
|
-
(0, node_test_1.test)("rejects method set_permissions with neither set_permissions nor owner", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
602
|
-
const { api, calls } = createDocumentApi([]);
|
|
603
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
604
|
-
var _a, _b;
|
|
605
|
-
const result = (yield client.callTool({
|
|
606
|
-
name: "bulk_edit_documents",
|
|
607
|
-
arguments: { documents: [1], method: "set_permissions", merge: true },
|
|
608
|
-
}));
|
|
609
|
-
strict_1.default.ok(result.isError, "expected an error when both are missing");
|
|
610
|
-
strict_1.default.match((_b = (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error) !== null && _b !== void 0 ? _b : "", /set_permissions and\/or owner/);
|
|
611
|
-
}));
|
|
612
|
-
strict_1.default.equal(calls.bulkEditDocuments.length, 0);
|
|
613
|
-
}));
|
|
614
|
-
(0, node_test_1.test)("rejects malformed set_permissions input before calling Paperless", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
615
|
-
const { api, calls } = createDocumentApi([]);
|
|
616
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
617
|
-
for (const set_permissions of [
|
|
618
|
-
{ view: { groups: ["ai-agents"] } },
|
|
619
|
-
{ read: { groups: [10] } },
|
|
620
|
-
{ view: [10] },
|
|
621
|
-
]) {
|
|
622
|
-
// Depending on the installed MCP SDK version, an input-schema (zod)
|
|
623
|
-
// violation either rejects with a protocol error (-32602) or resolves
|
|
624
|
-
// with a CallToolResult carrying isError: true.
|
|
625
|
-
let rejected = false;
|
|
626
|
-
let result;
|
|
627
|
-
try {
|
|
628
|
-
result = (yield client.callTool({
|
|
629
|
-
name: "bulk_edit_documents",
|
|
630
|
-
arguments: { documents: [1], method: "set_permissions", set_permissions },
|
|
631
|
-
}));
|
|
632
|
-
}
|
|
633
|
-
catch (_a) {
|
|
634
|
-
rejected = true;
|
|
635
|
-
}
|
|
636
|
-
strict_1.default.ok(rejected || (result === null || result === void 0 ? void 0 : result.isError), `expected ${JSON.stringify(set_permissions)} to be rejected`);
|
|
637
|
-
}
|
|
638
|
-
}));
|
|
639
|
-
strict_1.default.equal(calls.bulkEditDocuments.length, 0);
|
|
640
|
-
}));
|
|
641
|
-
});
|
|
642
|
-
(0, node_test_1.describe)("nullable foreign keys can still be cleared (#138)", () => {
|
|
643
|
-
(0, node_test_1.test)("update_document forwards an explicit null so a foreign key can be cleared", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
644
|
-
const { api, calls } = createDocumentApi([]);
|
|
645
|
-
yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
646
|
-
var _a;
|
|
647
|
-
const result = (yield client.callTool({
|
|
648
|
-
name: "update_document",
|
|
649
|
-
arguments: { id: 42, correspondent: null, owner: null },
|
|
650
|
-
}));
|
|
651
|
-
strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
|
|
652
|
-
}));
|
|
653
|
-
strict_1.default.equal(calls.updateDocument.length, 1);
|
|
654
|
-
const [, data] = calls.updateDocument[0];
|
|
655
|
-
strict_1.default.equal(data.correspondent, null);
|
|
656
|
-
strict_1.default.equal(data.owner, null);
|
|
657
|
-
}));
|
|
658
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|