suppa-mcp-2 1.37.0 → 1.38.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.
@@ -173,12 +173,76 @@ export async function resolveCustomFields(entityName, recordId = null, contextFi
173
173
  }
174
174
  try {
175
175
  const result = await makeRequest("POST", `/api/core/custom-fields/${entityName}/resolve`, body);
176
- return jsonResponse(result);
176
+ return jsonResponse(await _withFieldTypes(entityName, result));
177
177
  }
178
178
  catch (e) {
179
179
  return errorResponse(e);
180
180
  }
181
181
  }
182
+ function _isObj(v) {
183
+ return v !== null && typeof v === "object" && !Array.isArray(v);
184
+ }
185
+ /**
186
+ * The platform's /resolve answer with each field's TYPE joined on.
187
+ *
188
+ * /resolve says which custom fields a record may carry and nothing about what
189
+ * may be written into them — no type, no subType. A caller that follows this
190
+ * tool (every refusal in this server points at it) therefore sends a string
191
+ * into a numeric field and gets back "A value has an invalid format for type
192
+ * numeric", which does not even name the field. The entity schema carries the
193
+ * type in its own `customFields` array, keyed by the same field id.
194
+ *
195
+ * Best-effort, like every other join here: an unreadable schema, or an answer
196
+ * that is not the documented shape, returns the platform's response untouched.
197
+ */
198
+ async function _withFieldTypes(entityName, result) {
199
+ if (!_isObj(result) || !Array.isArray(result.fields)) {
200
+ return result;
201
+ }
202
+ let defs;
203
+ try {
204
+ const schema = await makeRequest("GET", `/api/core/schema/${entityName}`);
205
+ const raw = _isObj(schema) ? schema[CUSTOM_FIELDS_KEY] : null;
206
+ if (!Array.isArray(raw)) {
207
+ return result;
208
+ }
209
+ defs = raw.filter(_isObj);
210
+ }
211
+ catch {
212
+ return result;
213
+ }
214
+ const byId = new Map();
215
+ const byName = new Map();
216
+ for (const d of defs) {
217
+ const id = Number(d.id);
218
+ if (Number.isFinite(id)) {
219
+ byId.set(id, d);
220
+ }
221
+ if (typeof d.name === "string") {
222
+ byName.set(d.name, d);
223
+ }
224
+ }
225
+ const fields = result.fields.map((f) => {
226
+ if (!_isObj(f)) {
227
+ return f;
228
+ }
229
+ // By id first: a field renamed between the two reads still matches.
230
+ const def = byId.get(Number(f.fieldId)) ?? byName.get(String(f.fieldName ?? ""));
231
+ if (!def) {
232
+ return f;
233
+ }
234
+ const out = { ...f };
235
+ if (typeof def.type === "string") {
236
+ out.type = def.type;
237
+ }
238
+ // An enum or relation needs its group/target as much as its type.
239
+ if (typeof def.subType === "string") {
240
+ out.subType = def.subType;
241
+ }
242
+ return out;
243
+ });
244
+ return { ...result, fields };
245
+ }
182
246
  /**
183
247
  * Create a custom field on `entityName` and put it in a set.
184
248
  *
@@ -366,9 +430,31 @@ async function _markRequiredInSets(entityName, fieldName, result) {
366
430
  do_this: manual,
367
431
  };
368
432
  }
369
- let links;
433
+ // PAGED, not capped: a fixed page left every link past it optional while the
434
+ // answer still said applied: true — the wrong result, quietly, which is the
435
+ // failure this whole helper exists to avoid.
436
+ const ids = [];
370
437
  try {
371
- links = await searchEntity("CustomFieldSetFields", [makeFilter("field.id", fieldId)], { id: true, set: { id: true } }, 50);
438
+ const PAGE = 200;
439
+ const seen = new Set();
440
+ for (let offset = 0;; offset += PAGE) {
441
+ const page = await searchEntity("CustomFieldSetFields", [makeFilter("field.id", fieldId)], { id: true, set: { id: true } }, PAGE, offset);
442
+ let fresh = 0;
443
+ for (const row of page) {
444
+ const id = row?.id;
445
+ if (typeof id !== "number" || seen.has(id)) {
446
+ continue;
447
+ }
448
+ seen.add(id);
449
+ ids.push(id);
450
+ fresh++;
451
+ }
452
+ // A short page is the last one; no new ids is the backstop for a server
453
+ // that ignores offset, which would otherwise page forever.
454
+ if (page.length < PAGE || fresh === 0) {
455
+ break;
456
+ }
457
+ }
372
458
  }
373
459
  catch (e) {
374
460
  return {
@@ -378,9 +464,6 @@ async function _markRequiredInSets(entityName, fieldName, result) {
378
464
  do_this: manual,
379
465
  };
380
466
  }
381
- const ids = links
382
- .map((l) => l?.id)
383
- .filter((id) => typeof id === "number");
384
467
  if (ids.length === 0) {
385
468
  return {
386
469
  applied: false,
@@ -1780,11 +1780,50 @@ export async function buildFieldDef(entityName, fieldName, fieldType, title, opt
1780
1780
  * 'encrypted', enum/multi-enum have it set by the platform, and no other type
1781
1781
  * takes one.
1782
1782
  */
1783
+ /**
1784
+ * The entity's custom field of this name, or null.
1785
+ *
1786
+ * Best-effort: an unreadable schema answers null, because a failed lookup is
1787
+ * not evidence of a clash and must not block a legitimate migration.
1788
+ */
1789
+ async function _customFieldNamed(entityName, fieldName) {
1790
+ try {
1791
+ const schema = await makeRequest("GET", `/api/core/schema/${entityName}`);
1792
+ return _extractCustomFields(schema).find((f) => f.name === fieldName) ?? null;
1793
+ }
1794
+ catch {
1795
+ return null;
1796
+ }
1797
+ }
1783
1798
  export async function addField(entityName, fieldName, fieldType, title, opts = {}) {
1784
1799
  const built = await buildFieldDef(entityName, fieldName, fieldType, title, opts);
1785
1800
  if (built.error) {
1786
1801
  return built.error;
1787
1802
  }
1803
+ // A custom field of the same name is NOT a collision the platform stops:
1804
+ // custom values live in <Entity>CustomFields, so this call would add a
1805
+ // SECOND, ordinary column of that name on the owner and the entity would
1806
+ // carry two different fields under one name, bound to different values.
1807
+ // _enumNameFromField already refuses to guess between such a pair; refusing
1808
+ // to CREATE the pair is the same rule, one step earlier — and the migration
1809
+ // this call runs cannot be taken back by removing the field afterwards.
1810
+ //
1811
+ // AFTER buildFieldDef, never before: every refusal this server can make from
1812
+ // the arguments alone still costs zero requests.
1813
+ const clash = await _customFieldNamed(entityName, fieldName);
1814
+ if (clash) {
1815
+ return jsonResponse({
1816
+ error: `Refused: '${entityName}' already has a CUSTOM field called '${fieldName}'` +
1817
+ `${clash.type ? ` (type ${String(clash.type)})` : ""}. This call would create a ` +
1818
+ `SECOND field of that name — an ordinary column on '${entityName}', beside the ` +
1819
+ `custom one in '${entityName}${CUSTOM_FIELDS_EXTENSION_SUFFIX}' — and the entity ` +
1820
+ "would then carry two different fields under one name. Nothing was sent.",
1821
+ write_the_custom_one: `suppa_update_record("${entityName}", <record id>, fields_json='{"${CUSTOM_FIELDS_KEY}": ` +
1822
+ `{"${fieldName}": <value>}}')`,
1823
+ see_where_it_is_live: `suppa_resolve_custom_fields("${entityName}", record_id=<record id>)`,
1824
+ really_want_a_separate_column: "Pick a different field_name.",
1825
+ });
1826
+ }
1788
1827
  // Correct endpoint is /builder/{Entity}/add-fields and the body is the bare
1789
1828
  // array of field defs (the old /builder/add-fields with an {entityName,fields}
1790
1829
  // wrapper 404s).
@@ -2788,12 +2827,48 @@ async function _validateRecordWrites(entityName, records) {
2788
2827
  Object.keys(r).filter((k) => !fieldNames.has(k) && k !== CUSTOM_FIELDS_KEY))),
2789
2828
  ];
2790
2829
  if (unknown.length > 0) {
2830
+ // A name that IS a custom field of this entity is not unknown — it is in
2831
+ // the wrong PLACE. This gate used to call it non-existent and point at
2832
+ // suppa_add_field, which would have created a SECOND, ordinary column of
2833
+ // the same name: a migration on the tenant, to fix a shape mistake.
2834
+ const customNames = new Set(customDefs.map((f) => f.name).filter((n) => Boolean(n)));
2835
+ const misplaced = unknown.filter((n) => customNames.has(n));
2836
+ const absent = unknown.filter((n) => !customNames.has(n));
2837
+ const parts = [];
2838
+ const detail = {};
2839
+ if (misplaced.length > 0) {
2840
+ parts.push(`field(s) [${misplaced.join(", ")}] are CUSTOM fields of '${entityName}', and a ` +
2841
+ "custom field is not addressable as a top-level key — the platform drops it in " +
2842
+ `silence. Nest them under "${CUSTOM_FIELDS_KEY}": corrected_fields_json is this ` +
2843
+ "same write, fixed. Do NOT add them with suppa_add_field — that creates a " +
2844
+ "SECOND, ordinary column of the same name on the tenant. custom_fields_on_entity " +
2845
+ "lists them ALL; which are live on a given record depends on its context — " +
2846
+ "suppa_resolve_custom_fields answers that.");
2847
+ detail.custom_fields_at_top_level = misplaced;
2848
+ const offender = records.find((r) => misplaced.some((n) => n in r)) ?? records[0];
2849
+ const fixed = _nestCustomKeys(offender, misplaced);
2850
+ detail.corrected_fields_json = jsonResponse(fixed.fields);
2851
+ if (many) {
2852
+ detail.corrected_from_record_index = records.indexOf(offender);
2853
+ }
2854
+ if (fixed.conflicts.length > 0) {
2855
+ detail.conflict_note =
2856
+ `[${fixed.conflicts.join(", ")}] were written BOTH at the top level and under ` +
2857
+ `"${CUSTOM_FIELDS_KEY}". corrected_fields_json keeps the NESTED value; if the ` +
2858
+ "top-level one was meant, replace it there.";
2859
+ }
2860
+ detail.custom_fields_on_entity = [...customNames].sort();
2861
+ }
2862
+ if (absent.length > 0) {
2863
+ parts.push(`field(s) [${absent.join(", ")}] do not exist on '${entityName}'. The platform ` +
2864
+ "silently drops unknown keys, so this would create a record missing that data " +
2865
+ "(a 'broken' entity). Use only real fields (see valid_fields), or add the " +
2866
+ "field first with suppa_add_field.");
2867
+ detail.unknown_fields = absent;
2868
+ }
2791
2869
  const body = {
2792
- error: `Refused: field(s) [${unknown.join(", ")}] do not exist on '${entityName}'. ` +
2793
- "The platform silently drops unknown keys, so this would create a record missing " +
2794
- "that data (a 'broken' entity). Use only real fields (see valid_fields), or add " +
2795
- "the field first with suppa_add_field.",
2796
- unknown_fields: unknown,
2870
+ error: `Refused: ${parts.join(" Also: ")}`,
2871
+ ...detail,
2797
2872
  valid_fields: [...fieldNames].sort(),
2798
2873
  };
2799
2874
  if (many) {
@@ -3127,6 +3202,38 @@ export async function updateRecord(entityName, recordId, fieldsJson, mergeJson =
3127
3202
  * The schema endpoint may return the field list directly, or a dict carrying it
3128
3203
  * under ``fields``/``properties``/``columns``, or a name→def mapping.
3129
3204
  */
3205
+ /**
3206
+ * The same record with `names` moved from the top level into the custom-values
3207
+ * envelope — the write the caller meant, ready to paste back as fields_json.
3208
+ *
3209
+ * A name present in BOTH places is not guessed at: the value already under
3210
+ * `customFields` is kept and the name comes back as a conflict, because which of
3211
+ * two different values the caller meant is not knowable from here.
3212
+ */
3213
+ function _nestCustomKeys(record, names) {
3214
+ const existing = isPlainObject(record[CUSTOM_FIELDS_KEY])
3215
+ ? record[CUSTOM_FIELDS_KEY]
3216
+ : {};
3217
+ const nested = { ...existing };
3218
+ const conflicts = [];
3219
+ const out = {};
3220
+ for (const [k, v] of Object.entries(record)) {
3221
+ if (k === CUSTOM_FIELDS_KEY) {
3222
+ continue;
3223
+ }
3224
+ if (!names.includes(k)) {
3225
+ out[k] = v;
3226
+ continue;
3227
+ }
3228
+ if (Object.prototype.hasOwnProperty.call(existing, k)) {
3229
+ conflicts.push(k);
3230
+ continue;
3231
+ }
3232
+ nested[k] = v;
3233
+ }
3234
+ out[CUSTOM_FIELDS_KEY] = nested;
3235
+ return { fields: out, conflicts };
3236
+ }
3130
3237
  /**
3131
3238
  * The entity's CUSTOM field definitions — the schema's own `customFields` array.
3132
3239
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "suppa-mcp-2",
3
- "version": "1.37.0",
3
+ "version": "1.38.0",
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": {