suppa-mcp-2 1.2.3 → 1.2.4

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.
@@ -5,6 +5,7 @@
5
5
  * global `fetch` in place of `urllib`, with an AbortController for timeouts.
6
6
  */
7
7
  import { config } from "./config.js";
8
+ import { assertMergeLists } from "./utils.js";
8
9
  import * as tenants from "./tenants.js";
9
10
  // HTTP statuses that signal a transient upstream problem worth one retry.
10
11
  const TRANSIENT_CODES = new Set([429, 502, 503, 504]);
@@ -259,10 +260,40 @@ export async function insertEntity(entity, fieldsList, returning = null) {
259
260
  }
260
261
  return result ? [result] : [];
261
262
  }
262
- export async function updateEntity(entity, instanceId, fields) {
263
+ /**
264
+ * Update one instance.
265
+ *
266
+ * A field sent in `fields` REPLACES the stored value — for a list-valued field
267
+ * that means every item not in the payload is dropped. `merge` and `unmerge`
268
+ * (arrays of field names) opt individual fields out of that: merged names have
269
+ * their items ADDED to the existing list, unmerged names have theirs REMOVED.
270
+ * Both lists are pre-flighted (assertMergeLists) so a name the platform would
271
+ * silently ignore — and thus a replace the caller did not intend — never leaves
272
+ * this process. Empty lists are omitted from the body entirely.
273
+ *
274
+ * The platform reads the current values before writing, which is why merge /
275
+ * unmerge is bounded by the number of records the update MATCHES (500), not by
276
+ * how many items are sent (data.txt:396). This call matches one instance by id.
277
+ */
278
+ export async function updateEntity(entity, instanceId, fields, merge = null, unmerge = null) {
279
+ assertMergeLists(fields, merge ?? [], unmerge ?? []);
280
+ // Body keys per docs/superpowers/specs/captures/api-entity-builder-data.txt:
281
+ // fields :367, merge :370 ("string[] ... Array fields to add to instead of
282
+ // replacing"), unmerge :371. Both are arrays of field NAMES, never values.
263
283
  const body = { fields };
284
+ if (merge && merge.length > 0) {
285
+ body.merge = [...merge];
286
+ }
287
+ if (unmerge && unmerge.length > 0) {
288
+ body.unmerge = [...unmerge];
289
+ }
264
290
  return makeRequest("POST", dataUrl(entity, "update") + `/${instanceId}`, body);
265
291
  }
292
+ /**
293
+ * SOFT-delete the matching instances: /remove sets deletedAt and leaves the row
294
+ * in place, so the delete is reversible (restore is UI-only for now). The rows
295
+ * do drop out of every normal read, which is why callers still gate on confirm.
296
+ */
266
297
  export async function removeEntity(entity, filters) {
267
298
  const body = { conditions: { operator: "and", filters } };
268
299
  return makeRequest("POST", dataUrl(entity, "remove"), body);
package/dist/server.js CHANGED
@@ -51,6 +51,26 @@ const RecordValuesJson = z
51
51
  .string()
52
52
  .describe('JSON object of field values. Relations/enums as {"id": N}, e.g. ' +
53
53
  '{"title": "X", "customer": {"id": 7}, "status": {"id": 12}}.');
54
+ const MergeNamesJson = z
55
+ .string()
56
+ .nullable()
57
+ .describe('JSON array of field NAMES from fields_json to MERGE instead of replace, e.g. ' +
58
+ '["watchers"] — the items you send are ADDED to the stored list and the ' +
59
+ "existing ones are kept (without this the list is REPLACED wholesale). Every " +
60
+ "name must also appear in fields_json with an array value and must not appear " +
61
+ "in unmerge_json. There is no cap on how many items one list may carry: the " +
62
+ "platform reads the current values before writing, so the bound is on RECORDS " +
63
+ 'MATCHED — an update matching more than 500 records fails with "Cannot perform ' +
64
+ "'merge'/'unmerge': update query returned maximum allowed records; limit is " +
65
+ '500." This tool updates ONE record by id, so it cannot reach that bound.');
66
+ const UnmergeNamesJson = z
67
+ .string()
68
+ .nullable()
69
+ .describe('JSON array of field NAMES from fields_json to UNMERGE, e.g. ["watchers"] — the ' +
70
+ "items you send are REMOVED from the stored list and the rest are kept. Same " +
71
+ "rules as merge_json (name must be in fields_json with an array value, not in " +
72
+ "both lists; no item-count cap — the platform's 500 limit bounds the RECORDS an " +
73
+ "update matches, and this tool matches one record by id).");
54
74
  const TenantArg = z
55
75
  .string()
56
76
  .describe("Tenant alias ('testing'), host ('testing.modern.me'), or full URL.");
@@ -158,6 +178,8 @@ DATA BASICS
158
178
  - Dates are ISO-8601 strings. A naive datetime (no offset) is treated as UTC — include an explicit offset (e.g. +03:00) or pre-convert local times to UTC to avoid a silent shift.
159
179
  - Task deadlines also accept 'today', 'tomorrow', '+3d', '+2h', '+30m', '+1w'.
160
180
  - WRITE relations as {"id": N} and enum fields as {"id": <enumValueId>} — a bare number/string would return 201 but silently store null; suppa_create_record/suppa_update_record therefore REFUSE malformed shapes with guidance.
181
+ - REPLACE vs MERGE — an update REPLACES a list-valued field. {"watchers": [{"id": 15}]} does NOT add watcher 15; it makes 15 the ONLY watcher and the others are gone. To ADD items pass their field names in suppa_update_record's merge_json (e.g. ["watchers"]); to REMOVE only the items you list, pass them in unmerge_json. Every merged/unmerged name must also be in fields_json with an array value and no name may be in both lists; there is no cap on list length — the platform's 500 bounds the RECORDS one merge/unmerge update matches ("Cannot perform 'merge'/'unmerge': update query returned maximum allowed records; limit is 500"), and suppa_update_record matches a single record by id. suppa_update_task / suppa_update_doc / suppa_update_page / suppa_update_block have NO merge option — every list-valued field they write is replaced wholesale.
182
+ - DELETE IS A SOFT DELETE — every delete tool here sets deletedAt and leaves the row in the database; the delete is reversible (restore in the Suppa UI; no restore tool yet). Don't tell the user their data was destroyed. It still needs confirm=true, because the record disappears from all normal reads.
161
183
  - Record writes are validated against the live schema BEFORE writing: an unknown field name or a non-existent entity is REFUSED (the platform silently drops unknown keys → a record missing data). Use only real fields (suppa_describe_entity), or add the field first with suppa_add_field. Don't invent field names.
162
184
  - READ enum fields back as objects: project {"myEnum": {"value": true}} and read .value, never as a bare string.
163
185
  - HTML content (descriptions, comments, blocks): plain text is auto-wrapped in <p>…</p>; to pass raw HTML start the string with <.
@@ -197,6 +219,23 @@ export const PROMPT_NAMES = [];
197
219
  function text(result) {
198
220
  return { content: [{ type: "text", text: result }] };
199
221
  }
222
+ /**
223
+ * Best-effort read of a merge_json/unmerge_json argument for the confirmation
224
+ * preview. Malformed input yields [] here — updateRecord is the authority and
225
+ * rejects it with a proper message once the call is confirmed.
226
+ */
227
+ function nameList(raw) {
228
+ if (!raw) {
229
+ return [];
230
+ }
231
+ try {
232
+ const parsed = typeof raw === "string" ? JSON.parse(raw) : raw;
233
+ return Array.isArray(parsed) ? parsed.filter((n) => typeof n === "string") : [];
234
+ }
235
+ catch {
236
+ return [];
237
+ }
238
+ }
200
239
  export function buildServer() {
201
240
  // Reset so repeated buildServer() calls (e.g. in tests) do not double-count.
202
241
  TOOL_NAMES.length = 0;
@@ -275,6 +314,9 @@ export function buildServer() {
275
314
  }, async (a) => text(await tasks.createTask(a.title, a.description, a.assigned_to_id, a.project_id, a.workflow_id, a.stage_id, a.type_id, a.priority_id, a.deadline, a.parent_id)));
276
315
  reg("suppa_update_task", {
277
316
  description: "Update fields on an existing task. Only provided fields are changed.\n\n" +
317
+ " Any list-valued field written here is REPLACED wholesale — this tool has no\n" +
318
+ " merge option. To ADD to / REMOVE from a task's list field, use\n" +
319
+ ' suppa_update_record("Tasks", ...) with merge_json / unmerge_json.\n\n' +
278
320
  " APPROVAL GATE: without confirm=true this returns a confirmation request and\n" +
279
321
  " changes nothing. Get the user's approval, then re-call with confirm=true.",
280
322
  inputSchema: {
@@ -315,6 +357,9 @@ export function buildServer() {
315
357
  });
316
358
  reg("suppa_delete_task", {
317
359
  description: "Soft-delete a task.\n\n" +
360
+ " SOFT DELETE: sets deletedAt — the row remains in the database and the delete\n" +
361
+ " is reversible (restore in the Suppa UI; no restore tool here yet). The task\n" +
362
+ " does drop out of every normal read.\n\n" +
318
363
  " APPROVAL GATE: without confirm=true this returns a confirmation request and\n" +
319
364
  " deletes nothing. Get the user's approval, then re-call with confirm=true.",
320
365
  inputSchema: { task_id: z.number().int(), confirm: Confirm },
@@ -448,6 +493,8 @@ export function buildServer() {
448
493
  }, async (a) => text(await docs.createDoc(a.title, a.is_public)));
449
494
  reg("suppa_update_doc", {
450
495
  description: "Update a document's title or visibility.\n\n" +
496
+ " Any list-valued field written here is REPLACED wholesale — this tool has no\n" +
497
+ ' merge option; use suppa_update_record("Docs", ...) with merge_json to add.\n\n' +
451
498
  " APPROVAL GATE: without confirm=true this returns a confirmation request and\n" +
452
499
  " changes nothing. Get the user's approval, then re-call with confirm=true.",
453
500
  inputSchema: {
@@ -469,7 +516,10 @@ export function buildServer() {
469
516
  return text(await docs.updateDoc(a.doc_id, a.title, a.is_public));
470
517
  });
471
518
  reg("suppa_delete_doc", {
472
- description: "Delete a document (and its pages).\n\n" +
519
+ description: "Soft-delete a document (and its pages).\n\n" +
520
+ " SOFT DELETE: sets deletedAt — the row remains in the database and the delete\n" +
521
+ " is reversible (restore in the Suppa UI; no restore tool here yet). The doc\n" +
522
+ " does drop out of every normal read.\n\n" +
473
523
  " APPROVAL GATE: without confirm=true this returns a confirmation request and\n" +
474
524
  " deletes nothing. Get the user's approval, then re-call with confirm=true.",
475
525
  inputSchema: { doc_id: z.number().int(), confirm: Confirm },
@@ -515,6 +565,8 @@ export function buildServer() {
515
565
  }, async (a) => text(await docs.createPage(a.doc_id, a.title, a.parent_id, a.description, a.icon)));
516
566
  reg("suppa_update_page", {
517
567
  description: "Update a page's metadata.\n\n" +
568
+ " Any list-valued field written here is REPLACED wholesale — this tool has no\n" +
569
+ ' merge option; use suppa_update_record("Pages", ...) with merge_json to add.\n\n' +
518
570
  " APPROVAL GATE: without confirm=true this returns a confirmation request and\n" +
519
571
  " changes nothing. Get the user's approval, then re-call with confirm=true.",
520
572
  inputSchema: {
@@ -544,8 +596,11 @@ export function buildServer() {
544
596
  return text(await docs.updatePage(a.page_id, a.title, a.description, a.icon, a.is_public));
545
597
  });
546
598
  reg("suppa_delete_page", {
547
- description: "Delete a page. The platform refuses to delete a document's LAST page\n" +
599
+ description: "Soft-delete a page. The platform refuses to delete a document's LAST page\n" +
548
600
  " (HTTP 400) — delete the whole doc instead (suppa_delete_doc).\n\n" +
601
+ " SOFT DELETE: sets deletedAt — the row remains in the database and the delete\n" +
602
+ " is reversible (restore in the Suppa UI; no restore tool here yet). The page\n" +
603
+ " does drop out of every normal read.\n\n" +
549
604
  " APPROVAL GATE: without confirm=true this returns a confirmation request and\n" +
550
605
  " deletes nothing. Get the user's approval, then re-call with confirm=true.",
551
606
  inputSchema: { page_id: z.number().int(), confirm: Confirm },
@@ -598,6 +653,9 @@ export function buildServer() {
598
653
  }, async (a) => text(await docs.insertBlock(a.page_id, a.blocks_json, a.after_id, a.before_id)));
599
654
  reg("suppa_update_block", {
600
655
  description: "Update a block's content. Provide title (text/HTML) or full properties_json.\n\n" +
656
+ " properties_json REPLACES the block's properties wholesale (as does any\n" +
657
+ " list-valued field) — this tool has no merge option; read the current\n" +
658
+ " properties with suppa_get_blocks first and re-send them in full.\n\n" +
601
659
  " APPROVAL GATE: without confirm=true this returns a confirmation request and\n" +
602
660
  " changes nothing. Get the user's approval, then re-call with confirm=true.",
603
661
  inputSchema: {
@@ -627,7 +685,10 @@ export function buildServer() {
627
685
  return text(await docs.updateBlock(a.block_id, a.title, a.properties_json));
628
686
  });
629
687
  reg("suppa_delete_block", {
630
- description: "Delete a content block.\n\n" +
688
+ description: "Soft-delete a content block.\n\n" +
689
+ " SOFT DELETE: sets deletedAt — the row remains in the database and the delete\n" +
690
+ " is reversible (restore in the Suppa UI; no restore tool here yet). The block\n" +
691
+ " does drop out of every normal read.\n\n" +
631
692
  " APPROVAL GATE: without confirm=true this returns a confirmation request and\n" +
632
693
  " deletes nothing. Get the user's approval, then re-call with confirm=true.",
633
694
  inputSchema: { block_id: z.number().int(), confirm: Confirm },
@@ -732,7 +793,8 @@ export function buildServer() {
732
793
  }, async (a) => text(await entity.getRecord(a.entity_name, a.record_id, a.fields_json)));
733
794
  reg("suppa_create_entity", {
734
795
  description: "Create a new entity (database table). By default also creates the display-title\n" +
735
- " field so records have a label and titleFieldName resolves to a real field.\n\n" +
796
+ " field so records have a label and titleFieldName resolves to a real field, and\n" +
797
+ " sets that field as the entity's representative (what the UI shows for a record).\n\n" +
736
798
  " NOT for repeating child rows that belong to one parent record (invoice\n" +
737
799
  ' line-items, "X by category", per-period breakdowns) — those must be a tabular\n' +
738
800
  " part: use suppa_create_tabular_part instead, or they end up as a stray separate\n" +
@@ -749,7 +811,7 @@ export function buildServer() {
749
811
  auto_title_field: z
750
812
  .boolean()
751
813
  .default(true)
752
- .describe("Also create the title field so records have a label (default true)."),
814
+ .describe("Also create the title field (and make it the representative) so records have a label (default true)."),
753
815
  title_field_type: z
754
816
  .string()
755
817
  .default("multi-language")
@@ -780,7 +842,7 @@ export function buildServer() {
780
842
  auto_title_field: z
781
843
  .boolean()
782
844
  .default(true)
783
- .describe("Also create the title field (default true)."),
845
+ .describe("Also create the title field and make it the row's representative (default true)."),
784
846
  title_field_type: z
785
847
  .string()
786
848
  .default("multi-language")
@@ -803,6 +865,10 @@ export function buildServer() {
803
865
  " as an alias for many-to-one. Provide relation_entity for relations, enum_name\n" +
804
866
  " for enums. Constraints: max_length/min_length (text), decimal_places /\n" +
805
867
  " min_value / max_value / only_positive (numeric).\n" +
868
+ " required=true makes the column NOT NULL and REQUIRES default_value (a SQL\n" +
869
+ " expression or literal: '0', 'false', \"'draft'\", 'now()'); the call is refused\n" +
870
+ " before any request if it is missing — no default is ever invented for you.\n" +
871
+ " many-to-many / multi-enum take no default_value.\n" +
806
872
  " 'one-to-many' is REFUSED: an owned child collection is a TABULAR PART\n" +
807
873
  " (suppa_create_tabular_part, which auto-creates the child's 'owner' and the\n" +
808
874
  " parent link); never hand-build parent.one-to-many + child.owner. Use\n" +
@@ -817,6 +883,11 @@ export function buildServer() {
817
883
  .describe("A real Suppa 2.0 type (see suppa_list_field_types). NO date/richtext/email type exists — use 'timestamp' (+sub_type='date') or 'multi-language'. 'one-to-many' is refused — use suppa_create_tabular_part for owned children, 'many-to-many' for shared lookups."),
818
884
  title: z.string().nullable().default(null),
819
885
  required: z.boolean().default(false),
886
+ default_value: z
887
+ .string()
888
+ .nullable()
889
+ .default(null)
890
+ .describe("Value for rows that leave the field empty — a SQL expression or literal ('0', 'false', \"'draft'\", 'now()'). REQUIRED when required=true (except many-to-many/multi-enum); the platform rejects NOT NULL without it."),
820
891
  unique: z.boolean().default(false),
821
892
  relation_entity: z
822
893
  .string()
@@ -869,6 +940,7 @@ export function buildServer() {
869
940
  annotations: _ADD,
870
941
  }, async (a) => text(await entity.addField(a.entity_name, a.field_name, a.field_type, a.title, {
871
942
  required: a.required,
943
+ defaultValue: a.default_value,
872
944
  unique: a.unique,
873
945
  relationEntity: a.relation_entity,
874
946
  enumName: a.enum_name,
@@ -921,12 +993,18 @@ export function buildServer() {
921
993
  description: "Update a record in any entity. fields_json: JSON object of fields to change.\n\n" +
922
994
  ' Write relations as {"id": N} and enum fields as {"id": <enumValueId>} —\n' +
923
995
  " a bare number/string returns HTTP 201 but silently stores null.\n\n" +
996
+ " LIST FIELDS ARE REPLACED, NOT APPENDED: fields_json={\"watchers\":\n" +
997
+ ' [{"id": 15}]} makes 15 the ONLY watcher — every other watcher is dropped.\n' +
998
+ ' To ADD the listed items instead, pass merge_json=["watchers"]; to REMOVE\n' +
999
+ ' only the listed items, pass unmerge_json=["watchers"].\n\n' +
924
1000
  " APPROVAL GATE: without confirm=true this returns a confirmation request and\n" +
925
1001
  " changes nothing. Get the user's approval, then re-call with confirm=true.",
926
1002
  inputSchema: {
927
1003
  entity_name: EntityName,
928
1004
  record_id: z.number().int(),
929
1005
  fields_json: RecordValuesJson,
1006
+ merge_json: MergeNamesJson.default(null),
1007
+ unmerge_json: UnmergeNamesJson.default(null),
930
1008
  confirm: Confirm,
931
1009
  },
932
1010
  annotations: _MUT,
@@ -939,12 +1017,35 @@ export function buildServer() {
939
1017
  catch {
940
1018
  proposed = a.fields_json;
941
1019
  }
942
- return text(confirmRequired("update record", { entity: a.entity_name, record_id: a.record_id }, proposed));
1020
+ const merge = nameList(a.merge_json);
1021
+ const unmerge = nameList(a.unmerge_json);
1022
+ const preview = { fields: proposed };
1023
+ if (merge.length > 0) {
1024
+ preview.merge_add_to_existing_list = merge;
1025
+ }
1026
+ if (unmerge.length > 0) {
1027
+ preview.unmerge_remove_from_existing_list = unmerge;
1028
+ }
1029
+ // Name the lists this call would wipe, so the human approving sees the
1030
+ // replace BEFORE it happens rather than in the result. Schema-aware: a
1031
+ // json column holding an array is one value being overwritten, not a
1032
+ // list being wiped, and must not be announced as a replacement.
1033
+ const replaced = proposed && typeof proposed === "object" && !Array.isArray(proposed)
1034
+ ? await entity.previewReplacedLists(a.entity_name, proposed, merge, unmerge)
1035
+ : [];
1036
+ if (replaced.length > 0) {
1037
+ preview.replaces_entire_list = replaced;
1038
+ }
1039
+ return text(confirmRequired("update record", { entity: a.entity_name, record_id: a.record_id }, preview));
943
1040
  }
944
- return text(await entity.updateRecord(a.entity_name, a.record_id, a.fields_json));
1041
+ return text(await entity.updateRecord(a.entity_name, a.record_id, a.fields_json, a.merge_json, a.unmerge_json));
945
1042
  });
946
1043
  reg("suppa_delete_record", {
947
- description: "Delete a record (instance) from any entity.\n\n" +
1044
+ description: "Soft-delete a record (instance) from any entity.\n\n" +
1045
+ " SOFT DELETE: the platform sets deletedAt — the row remains in the database\n" +
1046
+ " and the delete is reversible (restore in the Suppa UI; this server has no\n" +
1047
+ " restore tool yet). Nothing is purged. The record does disappear from every\n" +
1048
+ " normal read, so treat it as gone for further work.\n\n" +
948
1049
  " Removes a single instance, not the entity/schema. Entity definitions and\n" +
949
1050
  " fields are add-only here (create_entity / add_field) — removing or modifying\n" +
950
1051
  " fields is UI-only by design.\n\n" +
@@ -1121,6 +1222,11 @@ export function buildServer() {
1121
1222
  });
1122
1223
  reg("suppa_delete_automation", {
1123
1224
  description: "Soft-delete an automation workflow.\n\n" +
1225
+ " SOFT DELETE: this goes through the same /remove as every other delete here —\n" +
1226
+ " the platform sets deletedAt, the row remains in the database and the delete\n" +
1227
+ " is reversible (restore in the Suppa UI; this server has no restore tool yet).\n" +
1228
+ " Nothing is purged. The workflow does disappear from every normal read and\n" +
1229
+ " stops running, so treat it as gone for further work.\n\n" +
1124
1230
  " APPROVAL GATE: without confirm=true this returns a confirmation request and\n" +
1125
1231
  " deletes nothing. Get the user's approval, then re-call with confirm=true.",
1126
1232
  inputSchema: { automation_id: AutomationId, confirm: Confirm },
@@ -269,7 +269,10 @@ export async function updateDoc(docId, title = null, isPublic = null) {
269
269
  const result = await updateEntity(ENTITY_DOCS, docId, fields);
270
270
  return jsonResponse(result);
271
271
  }
272
- /** Delete a document. */
272
+ /**
273
+ * Soft-delete a document: /remove sets deletedAt and keeps the row, so this is
274
+ * reversible (restore is UI-only until 1.3.0) — nothing is purged.
275
+ */
273
276
  export async function deleteDoc(docId) {
274
277
  const result = await removeEntity(ENTITY_DOCS, [makeFilter("id", docId)]);
275
278
  return jsonResponse(result);
@@ -323,7 +326,10 @@ export async function updatePage(pageId, title = null, description = null, icon
323
326
  const result = await updateEntity(ENTITY_PAGES, pageId, fields);
324
327
  return jsonResponse(result);
325
328
  }
326
- /** Delete a page. */
329
+ /**
330
+ * Soft-delete a page: /remove sets deletedAt and keeps the row, so this is
331
+ * reversible (restore is UI-only until 1.3.0) — nothing is purged.
332
+ */
327
333
  export async function deletePage(pageId) {
328
334
  const result = await removeEntity(ENTITY_PAGES, [makeFilter("id", pageId)]);
329
335
  return jsonResponse(result);
@@ -466,7 +472,10 @@ export async function updateBlock(blockId, title = null, propertiesJson = null)
466
472
  const result = await updateEntity(ENTITY_PAGE_BLOCKS, blockId, fields);
467
473
  return jsonResponse(result);
468
474
  }
469
- /** Delete a single block. */
475
+ /**
476
+ * Soft-delete a single block: /remove sets deletedAt and keeps the row, so this
477
+ * is reversible (restore is UI-only until 1.3.0) — nothing is purged.
478
+ */
470
479
  export async function deleteBlock(blockId) {
471
480
  const result = await removeEntity(ENTITY_PAGE_BLOCKS, [makeFilter("id", blockId)]);
472
481
  return jsonResponse(result);