suppa-mcp-2 1.27.1 → 1.28.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 CHANGED
@@ -52,6 +52,24 @@ All variables are optional — the server works out of the box and authenticates
52
52
  | `SUPPA_MCP_HOME` | `~/.suppa-mcp` | Directory for the tenant registry |
53
53
  | `SUPPA_HTTP_TIMEOUT` | `60` | Per-request timeout, seconds |
54
54
  | `SUPPA_MCP_DEBUG` | (off) | `1` logs each request line to stderr |
55
+ | `SUPPA_MCP_NO_CACHE_REPAIR` | (off) | Set to keep the server from removing its own broken `_npx/<hash>` directory after an incomplete download (see below) |
56
+
57
+ ### When npx leaves a half-downloaded package
58
+
59
+ An interrupted `npx` download leaves the package in the cache with files
60
+ missing, and the symptom points at the wrong thing: Node cannot resolve a
61
+ dependency, the process dies *before* the MCP handshake, and the host reports
62
+ `CONNECT_TIMEOUT` / `CONNECTION_CLOSED` / `Server disconnected` — as though the
63
+ server had crashed, when it never ran.
64
+
65
+ The server now recognises this and repairs it. On a module-resolution failure it
66
+ removes **only** its own `_npx/<hash>` directory and exits, so the next launch
67
+ downloads a clean copy; the message on stderr names the missing module and the
68
+ directory. It never clears the npm cache as a whole, never touches a global
69
+ install or a project's `node_modules`, and never runs on a healthy start — a
70
+ repair is only ever a response to a failure that already happened. A second
71
+ failure within a minute leaves the directory alone, so a host that restarts the
72
+ server cannot loop.
55
73
 
56
74
  ---
57
75
 
package/dist/bin.js CHANGED
@@ -2,12 +2,13 @@
2
2
  /**
3
3
  * stdio entrypoint for the Suppa MCP server (npm `suppa-mcp-2`).
4
4
  *
5
- * Builds the server (all 87 tools) and connects it to stdio, exactly like the
6
- * Python package's console script. Faithful 1:1 port.
5
+ * The server is loaded through a DYNAMIC import, which looks like a detail and
6
+ * is the whole point: a static `import` is resolved before the first statement
7
+ * of this module runs, so an incomplete install killed the process before any
8
+ * handler below existed. See ./startupRepair.ts for the incident.
7
9
  */
8
- import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
9
- import { buildServer } from "./server.js";
10
- import { refreshLatestVersionInBackground } from "./version.js";
10
+ import { fileURLToPath } from "node:url";
11
+ import { applyRepair, planRepair, readStamp, stampPath } from "./startupRepair.js";
11
12
  // Safety net: a long-running MCP stdio server must never be killed by a stray
12
13
  // async rejection or a synchronous throw in some callback (a background token
13
14
  // refresh, an aborted login-loopback request, a write to a closed socket…).
@@ -23,8 +24,12 @@ process.on("uncaughtException", (err) => {
23
24
  process.stderr.write(`[suppa-mcp] uncaught exception (ignored): ${err instanceof Error ? err.stack || err.message : String(err)}\n`);
24
25
  });
25
26
  // Startup is the one place a failure SHOULD stop the process — if the server
26
- // can't build or connect, there's nothing to serve.
27
+ // can't build or connect, there's nothing to serve. What it must not do is stop
28
+ // without saying why.
27
29
  try {
30
+ const { StdioServerTransport } = await import("@modelcontextprotocol/sdk/server/stdio.js");
31
+ const { buildServer } = await import("./server.js");
32
+ const { refreshLatestVersionInBackground } = await import("./version.js");
28
33
  const server = buildServer();
29
34
  await server.connect(new StdioServerTransport());
30
35
  // Only once the server is actually serving, and detached: refresh the cached
@@ -34,6 +39,16 @@ try {
34
39
  refreshLatestVersionInBackground();
35
40
  }
36
41
  catch (e) {
37
- process.stderr.write(`[suppa-mcp] fatal startup error: ${e instanceof Error ? e.stack || e.message : String(e)}\n`);
42
+ const stamp = stampPath();
43
+ const plan = planRepair({
44
+ error: e,
45
+ // A path, never import.meta.url: a file:// URL is percent-encoded and names
46
+ // no directory, and the repair planner refuses one outright.
47
+ selfPath: fileURLToPath(import.meta.url),
48
+ lastRepairAt: readStamp(stamp),
49
+ now: Date.now(),
50
+ disabled: Boolean(process.env.SUPPA_MCP_NO_CACHE_REPAIR),
51
+ });
52
+ process.stderr.write(`[suppa-mcp] ${applyRepair(plan, stamp)}\n`);
38
53
  process.exit(1);
39
54
  }
package/dist/server.js CHANGED
@@ -287,6 +287,8 @@ ENTITY BUILDER (create + edit fields; core-field removal is UI-only) — suppa_a
287
287
  - OWNED CHILD COLLECTIONS ARE TABULAR PARTS: repeating rows that belong to ONE parent record (a part's BOM items) go through suppa_create_tabular_part(parent_entity, name), which creates the child's 'owner' relation and the parent link. Never model it by hand — suppa_add_field REFUSES field_type='one-to-many'; 'many-to-many' is for a shared lookup referenced by many records.
288
288
  - An enum value on a RECORD is a reference, {"id": <Enums row id>} (an array of those for multi-enum), never a bare code like "red" — that is refused. Read the ids with suppa_search_records on 'Enums' filtered by name.
289
289
 
290
+ CUSTOM FIELDS — ANY entity, not just Tasks: one whose options.customFields is on (an admin enables it; suppa_describe_entity shows it) carries extra columns that vary BY CONTEXT, so which are live differs from record to record — suppa_resolve_custom_fields answers for a given record. Their values ride on the OWNER record under the reserved key customFields, e.g. fields_json {"customFields": {"budget": 600}}.
291
+
290
292
  ${HOUSE_RULES_DIGEST}
291
293
 
292
294
  ATTACHMENTS — an entity holds files only through a 'file' or 'multi-file' field (add one with suppa_add_field). suppa_attach_file_to_record uploads from local disk and returns the file_ids that are the only handle to the bytes; suppa_list_record_files lists what a record carries; suppa_download_file and suppa_download_files_zip fetch them. Those descriptions carry the rest.
@@ -0,0 +1,175 @@
1
+ /**
2
+ * A truncated install, made to say so — and to fix itself.
3
+ *
4
+ * A host reported CONNECT_TIMEOUT and CONNECTION_CLOSED on a server that was
5
+ * never reached: npx had left an INCOMPLETE package in its cache, and Node
6
+ * failed to resolve a transitive dependency (ajv, via the MCP SDK).
7
+ *
8
+ * ERR_MODULE_NOT_FOUND: Cannot find package
9
+ * '.../_npx/e5adf329fab52150/node_modules/ajv/dist/ajv.js'
10
+ *
11
+ * bin.ts already had three guards — unhandledRejection, uncaughtException and a
12
+ * try/catch around startup — and NONE of them could fire. Module resolution
13
+ * happens before the first statement of an ES module runs, so the process died
14
+ * before a single handler was installed. The operator got a bare Node stack on
15
+ * a stream most hosts do not surface, and a closed pipe. Hence bin.ts now loads
16
+ * the server through a dynamic import, and that catch lands here.
17
+ *
18
+ * WHY THIS DOES NOT SIMPLY CLEAN THE CACHE. Clearing on every start would cost
19
+ * a full re-download each launch (~11 s against ~0.4 s warm), make an offline
20
+ * start impossible, and `npm cache clean` would take every OTHER package's
21
+ * cache with it. Worse, _npx/<hash> is the directory this process is running
22
+ * FROM. So the rule is: act only on a failure that has already happened, only
23
+ * on our own cache directory, and never on anything else.
24
+ */
25
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
26
+ import { dirname, join } from "node:path";
27
+ import { tmpdir } from "node:os";
28
+ /** Written as a constant so no regex in this file needs a backslash escape. */
29
+ const BACKSLASH = String.fromCharCode(92);
30
+ /** How long after a repair we refuse to repair again. */
31
+ export const REPAIR_COOLDOWN_MS = 60_000;
32
+ /** Where the last repair is stamped, so an auto-restarting host cannot loop. */
33
+ export function stampPath() {
34
+ return join(tmpdir(), "suppa-mcp-last-cache-repair");
35
+ }
36
+ /**
37
+ * The package or file a module-resolution failure was about, or null when the
38
+ * error is something else — in which case nothing in this file applies.
39
+ */
40
+ export function missingModule(err) {
41
+ const e = err;
42
+ if (!e)
43
+ return null;
44
+ if (e.code !== "ERR_MODULE_NOT_FOUND" && e.code !== "MODULE_NOT_FOUND")
45
+ return null;
46
+ const quoted = /Cannot find (?:package|module) ['"]([^'"]+)['"]/.exec(String(e.message ?? ""));
47
+ return quoted ? quoted[1] : "(unnamed module)";
48
+ }
49
+ /**
50
+ * The `_npx/<hash>` directory a path sits in, or null.
51
+ *
52
+ * This is the safety boundary of the whole file: it answers with a directory
53
+ * ONLY for a path running through an `_npx` segment followed by a hex hash. A
54
+ * global install, a repo checkout, a plain node_modules, and a hand-made folder
55
+ * called `_npx/mystuff` all answer null — and null means nothing is deleted.
56
+ */
57
+ export function npxCacheDir(fromPath) {
58
+ if (!fromPath)
59
+ return null;
60
+ // A file:// URL is not a path, and its percent-encoding (ANDRII%7E1.HER) does
61
+ // not name a real directory. Passing one here once produced a confident
62
+ // "Removed the broken cache directory file:///C:/..." for a removal that
63
+ // never happened, so this refuses rather than guesses: callers convert with
64
+ // fileURLToPath first.
65
+ if (/^[a-z][a-z0-9+.-]*:\/\//i.test(fromPath))
66
+ return null;
67
+ const parts = fromPath.split(BACKSLASH).join("/").split("/");
68
+ const i = parts.indexOf("_npx");
69
+ if (i < 0)
70
+ return null;
71
+ const hash = parts[i + 1];
72
+ if (!hash || !/^[0-9a-f]{8,}$/i.test(hash))
73
+ return null;
74
+ // Slice the ORIGINAL string: splitting and joining on single characters
75
+ // preserves length, so this index maps back exactly and the caller's own
76
+ // separators survive untouched.
77
+ return fromPath.slice(0, parts.slice(0, i + 2).join("/").length);
78
+ }
79
+ /**
80
+ * Decide what to do about a startup failure. Pure — it reads nothing and
81
+ * removes nothing, so every branch is testable without a filesystem.
82
+ */
83
+ export function planRepair(o) {
84
+ const missing = missingModule(o.error);
85
+ if (!missing) {
86
+ const e = o.error;
87
+ return {
88
+ dir: null,
89
+ message: `fatal startup error: ${e instanceof Error ? e.stack || e.message : String(e)}`,
90
+ };
91
+ }
92
+ // The error names the file it could not resolve, which locates the broken
93
+ // install better than our own path does. Fall back to where we are running.
94
+ const dir = npxCacheDir(missing) ?? npxCacheDir(o.selfPath);
95
+ const head = `the installed package is INCOMPLETE — could not resolve ${missing}. ` +
96
+ "This is a truncated download, not a server fault.";
97
+ if (!dir) {
98
+ return {
99
+ dir: null,
100
+ message: head +
101
+ " Not running from an npx cache, so nothing was removed. Reinstall the package " +
102
+ "(npm install suppa-mcp-2 --force) and start it again.",
103
+ };
104
+ }
105
+ if (o.disabled) {
106
+ return {
107
+ dir: null,
108
+ message: head + ` Automatic repair is off (SUPPA_MCP_NO_CACHE_REPAIR). Remove ${dir} by hand.`,
109
+ };
110
+ }
111
+ if (o.lastRepairAt !== null && o.now - o.lastRepairAt < REPAIR_COOLDOWN_MS) {
112
+ // A host that restarts a failing server would otherwise re-download the
113
+ // whole package every few seconds, forever.
114
+ return {
115
+ dir: null,
116
+ message: head +
117
+ ` A repair was made moments ago and did not help, so ${dir} was left alone. ` +
118
+ "Something other than the cache is wrong — check disk space and permissions.",
119
+ };
120
+ }
121
+ return {
122
+ dir,
123
+ message: head +
124
+ ` Removed the broken cache directory ${dir}. Restart the server: the next launch ` +
125
+ "downloads it again cleanly.",
126
+ };
127
+ }
128
+ /** The moment of the last repair, or null when there is none we can trust. */
129
+ export function readStamp(path) {
130
+ try {
131
+ const n = Number(readFileSync(path, "utf-8").trim());
132
+ return Number.isFinite(n) ? n : null;
133
+ }
134
+ catch {
135
+ return null;
136
+ }
137
+ }
138
+ /**
139
+ * Carry out a plan. A failed removal is neither fatal nor hidden: on Windows a
140
+ * file belonging to the running process can be locked, and the operator still
141
+ * needs the path so they can remove it themselves.
142
+ */
143
+ export function applyRepair(plan, stamp = stampPath(),
144
+ // Injectable so the "removal did not take" branch is reachable from a test.
145
+ // On Windows that branch is a locked file, which cannot be staged reliably.
146
+ io = { rm: rmSync, exists: existsSync }) {
147
+ if (!plan.dir)
148
+ return plan.message;
149
+ if (!io.exists(plan.dir)) {
150
+ // rmSync({force: true}) treats a missing path as success, so without this
151
+ // the server would report a removal it did not perform — which is how a
152
+ // file:// URL slipped through review once.
153
+ return (`the installed package is INCOMPLETE and ${plan.dir} is not a directory on ` +
154
+ "this machine, so nothing was removed. Reinstall the package and start again.");
155
+ }
156
+ try {
157
+ io.rm(plan.dir, { recursive: true, force: true });
158
+ }
159
+ catch (e) {
160
+ return (`the installed package is INCOMPLETE. Could not remove ${plan.dir} ` +
161
+ `(${e instanceof Error ? e.message : String(e)}). Remove it by hand and start again.`);
162
+ }
163
+ if (io.exists(plan.dir)) {
164
+ return (`the installed package is INCOMPLETE and ${plan.dir} could not be fully removed ` +
165
+ "(files still in use). Remove it by hand and start the server again.");
166
+ }
167
+ try {
168
+ mkdirSync(dirname(stamp), { recursive: true });
169
+ writeFileSync(stamp, String(Date.now()), "utf-8");
170
+ }
171
+ catch {
172
+ // A missing stamp costs only the loop guard; the repair itself is done.
173
+ }
174
+ return plan.message;
175
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "suppa-mcp-2",
3
- "version": "1.27.1",
3
+ "version": "1.28.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": {