suppa-mcp-2 1.5.0 → 1.6.1
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 +422 -419
- package/dist/bin.js +1 -1
- package/dist/httpClient.js +29 -0
- package/dist/server.js +93 -3
- package/dist/tools/entity.js +26 -3
- package/dist/tools/files.js +239 -0
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* stdio entrypoint for the Suppa MCP server (npm `suppa-mcp-2`).
|
|
4
4
|
*
|
|
5
|
-
* Builds the server (all
|
|
5
|
+
* Builds the server (all 87 tools) and connects it to stdio, exactly like the
|
|
6
6
|
* Python package's console script. Faithful 1:1 port.
|
|
7
7
|
*/
|
|
8
8
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
package/dist/httpClient.js
CHANGED
|
@@ -335,6 +335,20 @@ async function sendWithReauth(ctx, buildHeaders, method, url, data, timeout, opt
|
|
|
335
335
|
}
|
|
336
336
|
throw new Error(`HTTP ${code} ${safeStatusText(res)}: ${errorBody}`);
|
|
337
337
|
}
|
|
338
|
+
// A download returns bytes, not JSON — reading it as text would corrupt
|
|
339
|
+
// anything that is not UTF-8 (every PDF, image and zip). Content-Type comes
|
|
340
|
+
// back with it so the caller can name the file sensibly.
|
|
341
|
+
if (opts.binary) {
|
|
342
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
343
|
+
if (config.DEBUG) {
|
|
344
|
+
debug(`${method} ${url} -> ${code} ${buf.length}B (${((Date.now() - started) / 1000).toFixed(2)}s)`);
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
bytes: buf,
|
|
348
|
+
contentType: res.headers.get("content-type") ?? "",
|
|
349
|
+
contentDisposition: res.headers.get("content-disposition") ?? "",
|
|
350
|
+
};
|
|
351
|
+
}
|
|
338
352
|
const raw = await res.text();
|
|
339
353
|
if (config.DEBUG) {
|
|
340
354
|
debug(`${method} ${url} -> ${code} (${((Date.now() - started) / 1000).toFixed(2)}s)`);
|
|
@@ -375,6 +389,21 @@ export async function makeRequestRawBytes(method, path, data, contentType, opts
|
|
|
375
389
|
const url = path.startsWith("/") ? `${ctx.baseUrl}${path}` : path;
|
|
376
390
|
return sendWithReauth(ctx, (t) => headers(ctx, t, { "Content-Type": contentType }), method, url, data, timeout, { unwrap: false });
|
|
377
391
|
}
|
|
392
|
+
/**
|
|
393
|
+
* Fetch a response as BYTES rather than JSON — downloads
|
|
394
|
+
* (api-entity-builder-files.txt:80 "Buffer", :100 "ZIP (stream)").
|
|
395
|
+
*
|
|
396
|
+
* Goes through the same choke point as every other request, so the
|
|
397
|
+
* permanent-delete refusal covers it too.
|
|
398
|
+
*/
|
|
399
|
+
export async function makeRequestBinary(method, path, body = null, opts = {}) {
|
|
400
|
+
const ctx = tenants.current();
|
|
401
|
+
await tenants.ensureValid(ctx);
|
|
402
|
+
const timeout = opts.timeout ?? Math.max(120, config.HTTP_TIMEOUT); // downloads need headroom
|
|
403
|
+
const url = path.startsWith("/") ? `${ctx.baseUrl}${path}` : path;
|
|
404
|
+
const payload = body === null || body === undefined ? null : JSON.stringify(body);
|
|
405
|
+
return (await sendWithReauth(ctx, (t) => headers(ctx, t, payload === null ? null : { "Content-Type": "application/json" }), method, url, payload, timeout, { unwrap: false, binary: true }));
|
|
406
|
+
}
|
|
378
407
|
// ---------------------------------------------------------------------------
|
|
379
408
|
// High-level entity helpers
|
|
380
409
|
// ---------------------------------------------------------------------------
|
package/dist/server.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Suppa MCP Server — unified gateway to the Suppa platform.
|
|
3
3
|
*
|
|
4
|
-
* Faithful 1:1 port of the Python `suppa_mcp/server.py`. Wires all
|
|
4
|
+
* Faithful 1:1 port of the Python `suppa_mcp/server.py`. Wires all 87 tools,
|
|
5
5
|
* mirrors the tool-behavior annotations (_READ/_ADD/_MUT), reproduces the
|
|
6
6
|
* server `instructions` string verbatim, and OWNS all confirm-gating (the tool
|
|
7
7
|
* modules mutate unconditionally, exactly like the Python tool functions).
|
|
@@ -11,6 +11,7 @@ import { z } from "zod";
|
|
|
11
11
|
import * as tasks from "./tools/tasks.js";
|
|
12
12
|
import * as docs from "./tools/docs.js";
|
|
13
13
|
import * as entity from "./tools/entity.js";
|
|
14
|
+
import * as files from "./tools/files.js";
|
|
14
15
|
import * as forms from "./tools/forms.js";
|
|
15
16
|
import * as migrate from "./tools/migrate.js";
|
|
16
17
|
import * as workspace from "./tools/workspace.js";
|
|
@@ -258,12 +259,19 @@ APPROVAL GATES (human-in-the-loop)
|
|
|
258
259
|
ENTITY BUILDER (create + edit fields; removal is UI-only)
|
|
259
260
|
- Real field types only: text, numeric, integer, serial, boolean, timestamp, json, uuid, multi-language, bytea, tsvector, multi-type, icon, enum, multi-enum, file, multi-file, many-to-one, many-to-many, many-to-many-backref. There is NO date/datetime/richtext/email/url/phone type — use timestamp (+sub_type='date') or multi-language.
|
|
260
261
|
- sub_type matrix: timestamp → 'date' | 'time' | 'timestamp'; text → 'encrypted'; enum/multi-enum → the enum's global name, set by the platform (never pass it); every other type takes none. Anything else is refused before the request.
|
|
261
|
-
- FIELD OPTIONS
|
|
262
|
+
- FIELD OPTIONS: searchable, searchPriority (1-4), readOnly, canGroup, canSort, showInTable, editFromTable, canFilter. Omit options_json and the platform fills in its own defaults — visible, sortable, groupable, filterable, editable, not read-only, and NOT searchable. So the one worth passing is searchable (+searchPriority): suppa_search_records' free-text search only covers fields marked searchable. Turn any of them on later with suppa_update_field.
|
|
262
263
|
- OWNED CHILD COLLECTIONS ARE TABULAR PARTS. Repeating rows that belong to one parent record (e.g. a part's BOM items) → suppa_create_tabular_part(parent_entity, name), which auto-creates the child's 'owner' relation and the parent link. NEVER model this by hand as parent.one-to-many + child.owner: suppa_add_field REFUSES field_type='one-to-many'. Use 'many-to-many' only for a shared lookup referenced by many records.
|
|
263
264
|
- Relations need relation_entity; enums need enum_name (the enum's global name, conventionally 'Entity.field'). Add the values FIRST with suppa_add_enum_values, then create the field with the same enum_name — the field is bound to the enum by that name.
|
|
264
265
|
- An enum value on a RECORD is a reference, not a string: write {"id": <Enums row id>} for enum and [{"id": ...}, ...] for multi-enum. A bare code like "red" is refused with 'field must contain id for many-to-one relationship'. Read the ids with suppa_search_records on 'Enums' filtered by name = the enum name.
|
|
265
266
|
- Existing fields CAN be edited with suppa_update_field (name, title, notNull/unique/default, relation target, and the options above), within the platform's own limits: a field's TYPE can never be changed, its SUBTYPE only toggles 'encrypted' on/off (text fields only), and a system field takes just title + the options keys. Removing a field is still UI-only — this server has no remove-field call.
|
|
266
267
|
|
|
268
|
+
ATTACHMENTS (files on a record)
|
|
269
|
+
- A record can carry files only if its entity has a 'file' or 'multi-file' field; check with suppa_describe_entity and add one with suppa_add_field if it has none.
|
|
270
|
+
- WRITE: suppa_attach_file_to_record(entity, record_id, absolute_path) uploads from the local disk and returns file_ids — keep them, they are the only handle that gets the bytes back.
|
|
271
|
+
- READ: suppa_list_record_files(entity, record_id) lists fileName/size/mimeType/paths for one record. It does NOT return ids; get those from the attach response, or with suppa_search_records on the 'Files' entity.
|
|
272
|
+
- GET THE BYTES: suppa_download_file(file_id, absolute_dest_path) writes one file to disk — file_type='file' (original), 'preview' or 'pdf'. suppa_download_files_zip fetches several as one archive — by file_ids, or by entity_name + record_ids for everything attached to those records. Nothing is overwritten unless overwrite=true.
|
|
273
|
+
- To duplicate a record's attachments onto a copy, use suppa_copy_record(copy_files=true) — there is no local path for a file that already lives on the platform.
|
|
274
|
+
|
|
267
275
|
MIGRATION (Suppa 1.0 → 2.0)
|
|
268
276
|
- Always dry-run first: suppa_migrate_entities_from_v1(dry_run=true, output_file="plan.json"), review/edit the plan, then execute with plan_file="plan.json", dry_run=false.
|
|
269
277
|
`;
|
|
@@ -297,7 +305,7 @@ export function buildServer() {
|
|
|
297
305
|
// Reset so repeated buildServer() calls (e.g. in tests) do not double-count.
|
|
298
306
|
TOOL_NAMES.length = 0;
|
|
299
307
|
PROMPT_NAMES.length = 0;
|
|
300
|
-
const server = new McpServer({ name: "suppa-2.0", version: "1.
|
|
308
|
+
const server = new McpServer({ name: "suppa-2.0", version: "1.6.1" }, { instructions: INSTRUCTIONS });
|
|
301
309
|
const reg = (name, cfg, handler) => {
|
|
302
310
|
server.registerTool(name, {
|
|
303
311
|
description: cfg.description,
|
|
@@ -1466,6 +1474,88 @@ export function buildServer() {
|
|
|
1466
1474
|
},
|
|
1467
1475
|
annotations: _ADD,
|
|
1468
1476
|
}, async (a) => text(await entity.copyRecord(a.entity_name, a.record_id, a.fields_json, a.copy_files, a.copy_related_entities)));
|
|
1477
|
+
reg("suppa_list_record_files", {
|
|
1478
|
+
description: "List the files attached to one record of any entity.\n" +
|
|
1479
|
+
"\n" +
|
|
1480
|
+
" Returns fileName, filePath, pdfPath, previewPath, size, mimeType and type\n" +
|
|
1481
|
+
" for each attachment. It does NOT return the file id, and\n" +
|
|
1482
|
+
" suppa_download_file needs one — read ids from the 'Files' entity with\n" +
|
|
1483
|
+
" suppa_search_records, or keep the id suppa_attach_file_to_record returns.\n" +
|
|
1484
|
+
" An empty list can also mean the entity has no file field at all; check it\n" +
|
|
1485
|
+
" with suppa_describe_entity.\n",
|
|
1486
|
+
inputSchema: {
|
|
1487
|
+
entity_name: EntityName,
|
|
1488
|
+
record_id: z.number().int().describe("Id of the record whose attachments to list."),
|
|
1489
|
+
limit: z.number().int().default(50).describe("Page size, 1-500."),
|
|
1490
|
+
offset: z.number().int().default(0).describe("Rows to skip (paging)."),
|
|
1491
|
+
},
|
|
1492
|
+
annotations: _READ,
|
|
1493
|
+
}, async (a) => text(await files.listRecordFiles(a.entity_name, a.record_id, a.limit, a.offset)));
|
|
1494
|
+
reg("suppa_download_file", {
|
|
1495
|
+
description: "Download one attached file to a local path.\n" +
|
|
1496
|
+
"\n" +
|
|
1497
|
+
" file_id is the id of the row in the 'Files' entity — from\n" +
|
|
1498
|
+
" suppa_search_records on 'Files', or from what suppa_attach_file_to_record\n" +
|
|
1499
|
+
" returned. file_type picks the variant: the original ('file'), its image\n" +
|
|
1500
|
+
" preview ('preview'), or the generated PDF ('pdf').\n" +
|
|
1501
|
+
"\n" +
|
|
1502
|
+
" dest_path must be ABSOLUTE. Point it at a directory to keep the platform's\n" +
|
|
1503
|
+
" own file name. An existing file is never overwritten unless overwrite=true\n" +
|
|
1504
|
+
" — the bytes are discarded instead.\n",
|
|
1505
|
+
inputSchema: {
|
|
1506
|
+
file_id: z.number().int().describe("Id of the row in the 'Files' entity."),
|
|
1507
|
+
dest_path: z
|
|
1508
|
+
.string()
|
|
1509
|
+
.describe("Absolute path to write to — a file, or a directory to keep the sent name."),
|
|
1510
|
+
file_type: z
|
|
1511
|
+
.enum(["file", "preview", "pdf"])
|
|
1512
|
+
.default("file")
|
|
1513
|
+
.describe("Which variant to fetch: the original, its preview, or the generated PDF."),
|
|
1514
|
+
overwrite: z
|
|
1515
|
+
.boolean()
|
|
1516
|
+
.default(false)
|
|
1517
|
+
.describe("Allow replacing an existing file at dest_path."),
|
|
1518
|
+
},
|
|
1519
|
+
annotations: _READ,
|
|
1520
|
+
}, async (a) => text(await files.downloadFile(a.file_id, a.dest_path, a.file_type, a.overwrite)));
|
|
1521
|
+
reg("suppa_download_files_zip", {
|
|
1522
|
+
description: "Download several attachments at once as one ZIP archive.\n" +
|
|
1523
|
+
"\n" +
|
|
1524
|
+
" Two ways to choose what goes in, and exactly one of them at a time:\n" +
|
|
1525
|
+
" file_ids — those specific files\n" +
|
|
1526
|
+
" entity_name + record_ids — EVERY file attached to those records\n" +
|
|
1527
|
+
" file_ids are ids from the 'Files' entity, as returned by\n" +
|
|
1528
|
+
" suppa_list_record_files or suppa_attach_file_to_record.\n" +
|
|
1529
|
+
"\n" +
|
|
1530
|
+
" dest_path must be ABSOLUTE; point it at a directory and the archive is\n" +
|
|
1531
|
+
" named download.zip, since this endpoint sends no filename of its own.\n" +
|
|
1532
|
+
" Nothing is overwritten unless overwrite=true.\n",
|
|
1533
|
+
inputSchema: {
|
|
1534
|
+
file_ids: z
|
|
1535
|
+
.array(z.number().int())
|
|
1536
|
+
.nullable()
|
|
1537
|
+
.default(null)
|
|
1538
|
+
.describe("Ids of rows in the 'Files' entity. Use this OR entity_name + record_ids."),
|
|
1539
|
+
entity_name: z
|
|
1540
|
+
.string()
|
|
1541
|
+
.nullable()
|
|
1542
|
+
.default(null)
|
|
1543
|
+
.describe("Entity whose records' attachments to fetch. Pair with record_ids."),
|
|
1544
|
+
record_ids: z
|
|
1545
|
+
.array(z.number().int())
|
|
1546
|
+
.nullable()
|
|
1547
|
+
.default(null)
|
|
1548
|
+
.describe("Record ids whose every attachment goes into the archive."),
|
|
1549
|
+
dest_path: z
|
|
1550
|
+
.string()
|
|
1551
|
+
.describe("Absolute path to write the .zip to — a file, or a directory."),
|
|
1552
|
+
overwrite: z
|
|
1553
|
+
.boolean()
|
|
1554
|
+
.default(false)
|
|
1555
|
+
.describe("Allow replacing an existing archive at dest_path."),
|
|
1556
|
+
},
|
|
1557
|
+
annotations: _READ,
|
|
1558
|
+
}, async (a) => text(await files.downloadFilesZip(a.file_ids, a.dest_path, a.overwrite, a.entity_name, a.record_ids)));
|
|
1469
1559
|
reg("suppa_attach_file_to_record", {
|
|
1470
1560
|
description: "Attach a local file to a record (instance) of any entity.\n\n" +
|
|
1471
1561
|
" Auto-detects the entity's file/attachment field from its schema; pass\n" +
|
package/dist/tools/entity.js
CHANGED
|
@@ -2317,6 +2317,11 @@ export async function attachFileToRecord(entityName, recordId, filePath, fieldNa
|
|
|
2317
2317
|
if (entityId === null) {
|
|
2318
2318
|
return jsonResponse({ error: `Could not resolve numeric id for entity '${entityName}'.` });
|
|
2319
2319
|
}
|
|
2320
|
+
// files.txt:14-19 documents exactly three required meta keys — entityId,
|
|
2321
|
+
// recordId, entityFieldName — plus optional pdfPath/previewPath. `entityName`
|
|
2322
|
+
// and `type` are NOT in that table; they are inherited from the Python server
|
|
2323
|
+
// and the platform drops unknown keys, so they are harmless extras rather
|
|
2324
|
+
// than doc-backed fields. The documented three are what makes the upload land.
|
|
2320
2325
|
const meta = {
|
|
2321
2326
|
entityId,
|
|
2322
2327
|
recordId: Math.trunc(recordId),
|
|
@@ -2330,14 +2335,32 @@ export async function attachFileToRecord(entityName, recordId, filePath, fieldNa
|
|
|
2330
2335
|
try {
|
|
2331
2336
|
const { body, contentType } = buildFileUpload(meta, filePath);
|
|
2332
2337
|
const result = await makeRequestRawBytes("POST", "/api/core/files/upload", body, contentType);
|
|
2333
|
-
|
|
2334
|
-
|
|
2338
|
+
// files.txt:36-46 — the response is {summary, success[], errors[]}, and
|
|
2339
|
+
// success[].id is the Files record id, the ONLY handle that gets the bytes
|
|
2340
|
+
// back later (suppa_download_file). Lifted out of the raw payload so a
|
|
2341
|
+
// caller does not have to know that shape to keep it.
|
|
2342
|
+
const uploaded = isPlainObject(result) ? result.success : null;
|
|
2343
|
+
const fileIds = Array.isArray(uploaded)
|
|
2344
|
+
? uploaded
|
|
2345
|
+
.filter((f) => f !== null && typeof f === "object")
|
|
2346
|
+
.map((f) => f.id)
|
|
2347
|
+
.filter((id) => typeof id === "number")
|
|
2348
|
+
: [];
|
|
2349
|
+
const failures = isPlainObject(result) ? result.errors : null;
|
|
2350
|
+
const out = {
|
|
2351
|
+
status: Array.isArray(failures) && failures.length > 0 ? "partial" : "ok",
|
|
2335
2352
|
entity: entityName,
|
|
2336
2353
|
record_id: Math.trunc(recordId),
|
|
2337
2354
|
field: fieldName,
|
|
2338
2355
|
file: basename(filePath),
|
|
2356
|
+
file_ids: fileIds,
|
|
2339
2357
|
result,
|
|
2340
|
-
}
|
|
2358
|
+
};
|
|
2359
|
+
if (fileIds.length > 0) {
|
|
2360
|
+
out.next = `Read it back with suppa_download_file(file_id=${fileIds[0]}), or list this ` +
|
|
2361
|
+
`record's attachments with suppa_list_record_files.`;
|
|
2362
|
+
}
|
|
2363
|
+
return jsonResponse(out);
|
|
2341
2364
|
}
|
|
2342
2365
|
catch (e) {
|
|
2343
2366
|
return jsonResponse({
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attachments: reading what is attached to a record, and getting the bytes.
|
|
3
|
+
*
|
|
4
|
+
* Endpoints and shapes are
|
|
5
|
+
* docs/superpowers/specs/captures/api-entity-builder-files.txt:
|
|
6
|
+
* :157 POST core/files/:table — every file attached to one record
|
|
7
|
+
* :68 GET core/files/download — one file's bytes (fileId, fileType)
|
|
8
|
+
* :83 POST core/files/download-all — several files as a ZIP stream
|
|
9
|
+
* Uploading already lives in tools/entity.ts (attachFileToRecord, :2760).
|
|
10
|
+
*
|
|
11
|
+
* NOTE on that capture's "shellAuthorization" header rows: the doc renders a
|
|
12
|
+
* fenced block whose language tag ("shell") is glued to the first line by the
|
|
13
|
+
* text extraction — the header is plain `Authorization`, which httpClient
|
|
14
|
+
* already sends. The same artefact produces "Request examplejson{" elsewhere in
|
|
15
|
+
* the file. Do not "fix" the client to send shellAuthorization.
|
|
16
|
+
*/
|
|
17
|
+
import { existsSync, mkdirSync, statSync, writeFileSync } from "node:fs";
|
|
18
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
19
|
+
import { makeRequest, makeRequestBinary } from "../httpClient.js";
|
|
20
|
+
import { jsonResponse } from "../utils.js";
|
|
21
|
+
/** files.txt:76 — "Which variant to download: file, preview or pdf." */
|
|
22
|
+
export const FILE_TYPES = ["file", "preview", "pdf"];
|
|
23
|
+
/** The platform caps a page of results; keep the tool from asking for absurd ones. */
|
|
24
|
+
const MAX_LIMIT = 500;
|
|
25
|
+
/**
|
|
26
|
+
* Every file attached to one record — POST core/files/:table (files.txt:157).
|
|
27
|
+
*
|
|
28
|
+
* Body is {recordId, limit, offset} (files.txt:164-167; limit is REQUIRED).
|
|
29
|
+
* The response rows carry fileName/filePath/pdfPath/previewPath/size/mimeType/
|
|
30
|
+
* type (files.txt:176-183) plus an `id` the doc's table omits — see the note
|
|
31
|
+
* where the ids are collected.
|
|
32
|
+
*/
|
|
33
|
+
export async function listRecordFiles(entityName, recordId, limit = 50, offset = 0) {
|
|
34
|
+
const name = String(entityName ?? "").trim();
|
|
35
|
+
if (!name) {
|
|
36
|
+
return jsonResponse({ error: "entity_name is required." });
|
|
37
|
+
}
|
|
38
|
+
if (!Number.isFinite(recordId)) {
|
|
39
|
+
return jsonResponse({ error: "record_id must be a number." });
|
|
40
|
+
}
|
|
41
|
+
const body = {
|
|
42
|
+
recordId: Math.trunc(recordId),
|
|
43
|
+
limit: Math.max(1, Math.min(Math.trunc(limit), MAX_LIMIT)),
|
|
44
|
+
offset: Math.max(0, Math.trunc(offset)),
|
|
45
|
+
};
|
|
46
|
+
const result = await makeRequest("POST", `/api/core/files/${name}`, body);
|
|
47
|
+
const rows = Array.isArray(result) ? result : [];
|
|
48
|
+
// The doc's response table (files.txt:176-183) lists no id, but the platform
|
|
49
|
+
// does return one — verified on modern-expo.dev.suppa.me, 2026-08-17, where
|
|
50
|
+
// every row came back with `id` alongside fileName/filePath/size/mimeType.
|
|
51
|
+
// That id is what suppa_download_file takes, so surface it rather than
|
|
52
|
+
// sending the caller off to the Files entity as the doc's shape would imply.
|
|
53
|
+
const ids = rows
|
|
54
|
+
.filter((r) => r !== null && typeof r === "object")
|
|
55
|
+
.map((r) => r.id)
|
|
56
|
+
.filter((id) => typeof id === "number");
|
|
57
|
+
return jsonResponse({
|
|
58
|
+
entity: name,
|
|
59
|
+
record_id: body.recordId,
|
|
60
|
+
count: rows.length,
|
|
61
|
+
file_ids: ids,
|
|
62
|
+
files: rows,
|
|
63
|
+
note: rows.length === 0
|
|
64
|
+
? "No files attached to this record — or the entity has no file field at all " +
|
|
65
|
+
"(check with suppa_describe_entity)."
|
|
66
|
+
: ids.length === rows.length
|
|
67
|
+
? "Pass any file_ids entry to suppa_download_file, or all of them to " +
|
|
68
|
+
"suppa_download_files_zip."
|
|
69
|
+
: "Some rows carry no id; for those, look the file up on the 'Files' entity with " +
|
|
70
|
+
"suppa_search_records to get one.",
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/** Where a download may be written, and under what name. */
|
|
74
|
+
function resolveDestination(destPath, fallbackName, overwrite) {
|
|
75
|
+
const dest = String(destPath ?? "").trim();
|
|
76
|
+
if (!dest) {
|
|
77
|
+
return { error: "dest_path is required — give an absolute file or directory path." };
|
|
78
|
+
}
|
|
79
|
+
if (!isAbsolute(dest)) {
|
|
80
|
+
return { error: `dest_path must be absolute, got '${dest}'.` };
|
|
81
|
+
}
|
|
82
|
+
// A directory target keeps the platform's own file name.
|
|
83
|
+
let target = dest;
|
|
84
|
+
if (existsSync(dest) && statSync(dest).isDirectory()) {
|
|
85
|
+
target = join(dest, fallbackName);
|
|
86
|
+
}
|
|
87
|
+
if (existsSync(target) && !overwrite) {
|
|
88
|
+
return {
|
|
89
|
+
error: `Refused: '${target}' already exists and overwrite=false, so nothing was written. ` +
|
|
90
|
+
"Pass overwrite=true or choose another dest_path.",
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
const parent = dirname(target);
|
|
94
|
+
if (!existsSync(parent)) {
|
|
95
|
+
try {
|
|
96
|
+
mkdirSync(parent, { recursive: true });
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
return { error: `Cannot create directory '${parent}': ${e instanceof Error ? e.message : e}` };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { path: target };
|
|
103
|
+
}
|
|
104
|
+
/** The filename the platform suggests, if it sent one. */
|
|
105
|
+
export function filenameFromDisposition(disposition) {
|
|
106
|
+
if (!disposition)
|
|
107
|
+
return null;
|
|
108
|
+
// RFC 5987 form first — that is what the doc's example uses
|
|
109
|
+
// (files.txt:108: filename*=UTF-8''download_2026-02-25.zip).
|
|
110
|
+
const star = /filename\*\s*=\s*[^']*'[^']*'([^;]+)/i.exec(disposition);
|
|
111
|
+
if (star) {
|
|
112
|
+
try {
|
|
113
|
+
return decodeURIComponent(star[1].trim().replace(/^"|"$/g, "")) || null;
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
return star[1].trim().replace(/^"|"$/g, "") || null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const plain = /filename\s*=\s*("([^"]*)"|[^;]+)/i.exec(disposition);
|
|
120
|
+
if (plain) {
|
|
121
|
+
return (plain[2] ?? plain[1]).trim() || null;
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* One file's bytes — GET core/files/download?fileId&fileType (files.txt:68-77).
|
|
127
|
+
*
|
|
128
|
+
* `fileType` picks the variant: the original, its preview, or the generated PDF
|
|
129
|
+
* (files.txt:76). Writes to disk rather than returning bytes, because an MCP
|
|
130
|
+
* result is text and a binary blob would have to be base64'd through the model's
|
|
131
|
+
* context for no benefit.
|
|
132
|
+
*/
|
|
133
|
+
export async function downloadFile(fileId, destPath, fileType = "file", overwrite = false) {
|
|
134
|
+
if (!Number.isFinite(fileId)) {
|
|
135
|
+
return jsonResponse({ error: "file_id must be a number (the Files record id)." });
|
|
136
|
+
}
|
|
137
|
+
if (!FILE_TYPES.includes(fileType)) {
|
|
138
|
+
return jsonResponse({
|
|
139
|
+
error: `file_type must be one of ${FILE_TYPES.join(", ")} (files.txt:76), got '${fileType}'.`,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
const id = Math.trunc(fileId);
|
|
143
|
+
let res;
|
|
144
|
+
try {
|
|
145
|
+
res = await makeRequestBinary("GET", `/api/core/files/download?fileId=${id}&fileType=${fileType}`);
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
return jsonResponse({
|
|
149
|
+
error: `Download of file ${id} failed: ${e instanceof Error ? e.message : String(e)}`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
const suggested = filenameFromDisposition(res.contentDisposition) ?? `file-${id}`;
|
|
153
|
+
const dest = resolveDestination(destPath, suggested, overwrite);
|
|
154
|
+
if ("error" in dest) {
|
|
155
|
+
return jsonResponse({ ...dest, bytes_received: res.bytes.length, not_written: true });
|
|
156
|
+
}
|
|
157
|
+
writeFileSync(dest.path, res.bytes);
|
|
158
|
+
const out = {
|
|
159
|
+
file_id: id,
|
|
160
|
+
file_type: fileType,
|
|
161
|
+
saved_to: dest.path,
|
|
162
|
+
bytes: res.bytes.length,
|
|
163
|
+
content_type: res.contentType,
|
|
164
|
+
suggested_name: suggested,
|
|
165
|
+
};
|
|
166
|
+
if (fileType !== "file") {
|
|
167
|
+
// Verified on modern-expo.dev.suppa.me, 2026-08-17: asking for 'preview' or
|
|
168
|
+
// 'pdf' on a file that has neither returns the ORIGINAL bytes, byte for
|
|
169
|
+
// byte, with no error — so a caller can end up with a .pdf that is not a
|
|
170
|
+
// PDF. The row's pdfPath / previewPath is what says whether the variant
|
|
171
|
+
// exists at all.
|
|
172
|
+
out.caveat =
|
|
173
|
+
`A '${fileType}' variant is not generated for every file. When it does not exist the ` +
|
|
174
|
+
"platform returns the ORIGINAL bytes with no error, so check the file's " +
|
|
175
|
+
`${fileType === "pdf" ? "pdfPath" : "previewPath"} in suppa_list_record_files before ` +
|
|
176
|
+
"trusting the format.";
|
|
177
|
+
}
|
|
178
|
+
return jsonResponse(out);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Several files at once, as a ZIP — POST core/files/download-all (files.txt:83).
|
|
182
|
+
*
|
|
183
|
+
* THE DOC'S BODY IS WRONG. files.txt:91 gives one required field,
|
|
184
|
+
* `instanceIds: number[]`, described as "List of file IDs (Files)". Sending
|
|
185
|
+
* exactly that is rejected — verified on modern-expo.dev.suppa.me, 2026-08-17:
|
|
186
|
+
*
|
|
187
|
+
* {"instanceIds":[2859,2860]}
|
|
188
|
+
* -> HTTP 400 validation.invalid_payload
|
|
189
|
+
* "Provide either fileIds or entityName with instanceIds"
|
|
190
|
+
*
|
|
191
|
+
* The endpoint takes one of two selections, both of which return a real zip
|
|
192
|
+
* (magic 50 4b 03 04), and neither of which sends Content-Type or
|
|
193
|
+
* Content-Disposition:
|
|
194
|
+
* {fileIds: [...]} — those files
|
|
195
|
+
* {entityName: "X", instanceIds: [...]} — every file of those RECORDS
|
|
196
|
+
* So `instanceIds` means RECORD ids, not file ids, and only alongside an
|
|
197
|
+
* entityName. Both modes are exposed here rather than picking one, because the
|
|
198
|
+
* second answers "give me everything attached to these records" in one call.
|
|
199
|
+
*/
|
|
200
|
+
export async function downloadFilesZip(fileIds, destPath, overwrite = false, entityName = null, recordIds = null) {
|
|
201
|
+
const ids = (fileIds ?? []).map((n) => Math.trunc(Number(n))).filter((n) => Number.isFinite(n));
|
|
202
|
+
const records = (recordIds ?? []).map((n) => Math.trunc(Number(n))).filter((n) => Number.isFinite(n));
|
|
203
|
+
const entity = String(entityName ?? "").trim();
|
|
204
|
+
const byFile = ids.length > 0;
|
|
205
|
+
const byRecord = entity !== "" && records.length > 0;
|
|
206
|
+
if (byFile === byRecord) {
|
|
207
|
+
return jsonResponse({
|
|
208
|
+
error: byFile
|
|
209
|
+
? "Pass EITHER file_ids OR entity_name + record_ids, not both — the platform accepts " +
|
|
210
|
+
"one selection at a time."
|
|
211
|
+
: "Nothing selected. Pass file_ids (ids from the 'Files' entity), or entity_name " +
|
|
212
|
+
"together with record_ids to get every file attached to those records.",
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
const body = byFile ? { fileIds: ids } : { entityName: entity, instanceIds: records };
|
|
216
|
+
let res;
|
|
217
|
+
try {
|
|
218
|
+
res = await makeRequestBinary("POST", "/api/core/files/download-all", body);
|
|
219
|
+
}
|
|
220
|
+
catch (e) {
|
|
221
|
+
return jsonResponse({
|
|
222
|
+
error: `Download failed: ${e instanceof Error ? e.message : String(e)}`,
|
|
223
|
+
sent: body,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
const suggested = filenameFromDisposition(res.contentDisposition) ?? "download.zip";
|
|
227
|
+
const dest = resolveDestination(destPath, suggested, overwrite);
|
|
228
|
+
if ("error" in dest) {
|
|
229
|
+
return jsonResponse({ ...dest, bytes_received: res.bytes.length, not_written: true });
|
|
230
|
+
}
|
|
231
|
+
writeFileSync(dest.path, res.bytes);
|
|
232
|
+
return jsonResponse({
|
|
233
|
+
selection: byFile ? { file_ids: ids } : { entity: entity, record_ids: records },
|
|
234
|
+
saved_to: dest.path,
|
|
235
|
+
bytes: res.bytes.length,
|
|
236
|
+
content_type: res.contentType,
|
|
237
|
+
suggested_name: suggested,
|
|
238
|
+
});
|
|
239
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "suppa-mcp-2",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "MCP server for the Suppa platform (modern.suppa.me) — Tasks, Docs/Pages, Entities & Schema, Forms, Automations, and file attachments, with multi-tenant Google auth. Node port of PyPI suppa-mcp.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|