suppa-mcp-2 1.19.0 → 1.20.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 +14 -0
- package/dist/httpClient.js +24 -1
- package/dist/schemaCache.js +112 -0
- package/dist/tools/entity.js +46 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -51,6 +51,7 @@ All variables are optional — the server works out of the box and authenticates
|
|
|
51
51
|
| `SUPPA_MCP_LOGIN_PORT` | `8080` | Local loopback port for the browser login callback |
|
|
52
52
|
| `SUPPA_MCP_HOME` | `~/.suppa-mcp` | Directory for the tenant registry |
|
|
53
53
|
| `SUPPA_HTTP_TIMEOUT` | `60` | Per-request timeout, seconds |
|
|
54
|
+
| `SUPPA_SCHEMA_CACHE_TTL` | `30` | Seconds a schema read stays cached, per tenant. `0` disables the cache. |
|
|
54
55
|
| `SUPPA_MCP_DEBUG` | (off) | `1` logs each request line to stderr |
|
|
55
56
|
|
|
56
57
|
---
|
|
@@ -178,6 +179,19 @@ A tool result is not paid once: the whole conversation is re-sent on every turn,
|
|
|
178
179
|
|
|
179
180
|
The key is always kept, so "the field was written" is never in doubt. A marker is not the stored text: quote the value to the user from your own arguments.
|
|
180
181
|
|
|
182
|
+
### What the server does not ask twice
|
|
183
|
+
|
|
184
|
+
Every record write reads the schema before it does anything: once to resolve the entity name, once to check the field names against real columns. Those reads are what make the write path safe — the platform silently **drops** unknown keys, so an unvalidated write creates a record quietly missing its data. They are not optional, only repetitive: ten sequential `suppa_create_record` calls fetched the identical schema twenty times.
|
|
185
|
+
|
|
186
|
+
Schema reads are now cached per tenant for `SUPPA_SCHEMA_CACHE_TTL` seconds (default 30). Ten sequential creates: **30 requests → 12**.
|
|
187
|
+
|
|
188
|
+
A stale schema is not a slow answer, it is a wrong one, so three things bound it:
|
|
189
|
+
|
|
190
|
+
- **Anything that changes a schema clears the cache**, from inside `makeRequest`, where every `/api/core/builder/` request must pass. No call site can forget, because no call site does it. Clearing is per-tenant and deliberately coarse: a relation with a back-reference adds a column to an entity nobody named in the URL.
|
|
191
|
+
- **The TTL bounds a change made elsewhere** — another session, the UI, or the platform converging the schema on application start.
|
|
192
|
+
- **The one path where staleness could produce a wrong answer re-reads before it acts.** Refusing a write because a field "does not exist", or an entity name because it "is not on this tenant", costs one fresh read from the platform first. An agent is never told a field is missing because this server was holding an old copy.
|
|
193
|
+
|
|
194
|
+
|
|
181
195
|
|
|
182
196
|
### Deletes are reversible — permanent delete is blocked
|
|
183
197
|
|
package/dist/httpClient.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { config } from "./config.js";
|
|
8
8
|
import { assertMergeLists, elideEchoedResponse, stripAccessMetadata } from "./utils.js";
|
|
9
9
|
import * as tenants from "./tenants.js";
|
|
10
|
+
import * as schemaCache from "./schemaCache.js";
|
|
10
11
|
// HTTP statuses that signal a transient upstream problem worth one retry.
|
|
11
12
|
const TRANSIENT_CODES = new Set([429, 502, 503, 504]);
|
|
12
13
|
function debug(msg) {
|
|
@@ -268,6 +269,25 @@ async function sendWithReauth(ctx, buildHeaders, method, url, data, timeout, opt
|
|
|
268
269
|
if (isPermanentDeletePath(url)) {
|
|
269
270
|
throw permanentDeleteRefusal(url);
|
|
270
271
|
}
|
|
272
|
+
// A schema read repeated inside one burst of work is the same schema. The
|
|
273
|
+
// lookup sits here rather than at the twelve call sites, so a new caller gets
|
|
274
|
+
// the benefit without knowing the cache exists. See schemaCache.
|
|
275
|
+
// sendWithReauth is handed an absolute URL; the cache is keyed by the path so
|
|
276
|
+
// the same entity on two tenants cannot collide.
|
|
277
|
+
const path = url.startsWith(ctx.baseUrl) ? url.slice(ctx.baseUrl.length) : url;
|
|
278
|
+
const cacheable = schemaCache.isCacheableSchemaRead(method, path);
|
|
279
|
+
if (cacheable && !opts.fresh) {
|
|
280
|
+
const hit = schemaCache.get(ctx.host, path);
|
|
281
|
+
if (hit !== schemaCache.MISS) {
|
|
282
|
+
debug(`${method} ${url} -> schema cache hit`);
|
|
283
|
+
return opts.unwrap ? unwrapEnvelope(hit) : hit;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
// Likewise the invalidation: every builder write passes through here, so no
|
|
287
|
+
// call site can change a schema and forget to say so.
|
|
288
|
+
if (schemaCache.invalidatesSchema(method, path)) {
|
|
289
|
+
schemaCache.invalidate(ctx.host);
|
|
290
|
+
}
|
|
271
291
|
let refreshed = false;
|
|
272
292
|
let transientRetried = false;
|
|
273
293
|
const retriable = isSafeRequest(method, url);
|
|
@@ -360,6 +380,9 @@ async function sendWithReauth(ctx, buildHeaders, method, url, data, timeout, opt
|
|
|
360
380
|
// the only place a backstop can cover the endpoints whose access flag is
|
|
361
381
|
// undocumented as well as the ones where it is set. See stripAccessMetadata.
|
|
362
382
|
const result = stripAccessMetadata(JSON.parse(raw));
|
|
383
|
+
if (cacheable) {
|
|
384
|
+
schemaCache.set(ctx.host, path, result);
|
|
385
|
+
}
|
|
363
386
|
return opts.unwrap ? unwrapEnvelope(result) : result;
|
|
364
387
|
}
|
|
365
388
|
}
|
|
@@ -382,7 +405,7 @@ export async function makeRequest(method, path, body = null, opts = {}) {
|
|
|
382
405
|
data = JSON.stringify(body);
|
|
383
406
|
extra["Content-Type"] = "application/json; charset=UTF-8";
|
|
384
407
|
}
|
|
385
|
-
return sendWithReauth(ctx, (t) => headers(ctx, t, extra), method, url, data, timeout, { unwrap: !opts.raw });
|
|
408
|
+
return sendWithReauth(ctx, (t) => headers(ctx, t, extra), method, url, data, timeout, { unwrap: !opts.raw, fresh: opts.fresh });
|
|
386
409
|
}
|
|
387
410
|
/** Make a raw-bytes request (for multipart uploads). */
|
|
388
411
|
export async function makeRequestRawBytes(method, path, data, contentType, opts = {}) {
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A short-lived, per-tenant cache for entity schema reads.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS EXISTS
|
|
5
|
+
*
|
|
6
|
+
* Every record write pays for the schema twice before it does any work:
|
|
7
|
+
* `GET /api/core/schema` to resolve the entity name, then
|
|
8
|
+
* `GET /api/core/schema/<entity>` to check the field names against the real
|
|
9
|
+
* columns. Measured on suppa_create_record and suppa_update_record, that is two
|
|
10
|
+
* of three requests — and the second one averages ~24 KB. An agent creating ten
|
|
11
|
+
* records in a row fetched the identical schema twenty times.
|
|
12
|
+
*
|
|
13
|
+
* Those two reads are what make the write path SAFE (the platform silently drops
|
|
14
|
+
* unknown keys, so a write that skipped validation would create a record quietly
|
|
15
|
+
* missing its data). They are not optional. They are just repetitive.
|
|
16
|
+
*
|
|
17
|
+
* WHY IT IS SAFE
|
|
18
|
+
*
|
|
19
|
+
* A cache trades freshness for speed, and a stale schema is not a harmless
|
|
20
|
+
* slowdown — it is a wrong answer. Three things bound that:
|
|
21
|
+
*
|
|
22
|
+
* 1. Anything this server does that CHANGES a schema clears the tenant's cache,
|
|
23
|
+
* and the clearing happens in makeRequest, where every builder request must
|
|
24
|
+
* pass. No call site can forget to invalidate, because no call site does it.
|
|
25
|
+
* 2. A short TTL bounds a change made ELSEWHERE — another session, the UI, or
|
|
26
|
+
* the platform's own convergence on application start.
|
|
27
|
+
* 3. The one place staleness could produce a wrong ANSWER rather than a slow
|
|
28
|
+
* one — refusing a write because a field looks unknown — re-reads with
|
|
29
|
+
* `fresh` before it refuses. See _validateRecordWrites.
|
|
30
|
+
*
|
|
31
|
+
* Set SUPPA_SCHEMA_CACHE_TTL=0 to switch it off entirely.
|
|
32
|
+
*/
|
|
33
|
+
/** Seconds a cached schema read stays usable. 0 disables the cache. */
|
|
34
|
+
export const SCHEMA_CACHE_TTL = (() => {
|
|
35
|
+
const raw = process.env.SUPPA_SCHEMA_CACHE_TTL;
|
|
36
|
+
if (raw === undefined || raw === "") {
|
|
37
|
+
return 30;
|
|
38
|
+
}
|
|
39
|
+
const n = Number.parseInt(raw, 10);
|
|
40
|
+
return Number.isFinite(n) && n >= 0 ? n : 30;
|
|
41
|
+
})();
|
|
42
|
+
/** host → (path → entry). Keyed by tenant, because suppa_use_tenant switches. */
|
|
43
|
+
const store = new Map();
|
|
44
|
+
/**
|
|
45
|
+
* Schema READS, and only reads.
|
|
46
|
+
*
|
|
47
|
+
* `/api/core/schema`, `/api/core/schema/<entity>` and the tabular-parts listing
|
|
48
|
+
* hanging off it. Deliberately not `/api/core/builder/...`: those are writes,
|
|
49
|
+
* and a write is what invalidates.
|
|
50
|
+
*/
|
|
51
|
+
export function isCacheableSchemaRead(method, path) {
|
|
52
|
+
if (method.toUpperCase() !== "GET") {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return /^\/api\/core\/schema(\/|$|\?)/.test(path);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Does this request change what a schema read would return?
|
|
59
|
+
*
|
|
60
|
+
* The entity builder is the whole surface: add-fields, update-field,
|
|
61
|
+
* create-entity, update-options, remove. Data writes (`/api/core/data/...`) move
|
|
62
|
+
* rows, not columns, and leave the schema alone.
|
|
63
|
+
*/
|
|
64
|
+
export function invalidatesSchema(method, path) {
|
|
65
|
+
if (method.toUpperCase() === "GET") {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return path.includes("/api/core/builder/");
|
|
69
|
+
}
|
|
70
|
+
/** A cached value, or MISS. `undefined` is a legal cached value, so use MISS. */
|
|
71
|
+
export const MISS = Symbol("schema-cache-miss");
|
|
72
|
+
export function get(host, path) {
|
|
73
|
+
if (SCHEMA_CACHE_TTL <= 0) {
|
|
74
|
+
return MISS;
|
|
75
|
+
}
|
|
76
|
+
const byPath = store.get(host);
|
|
77
|
+
const entry = byPath?.get(path);
|
|
78
|
+
if (!entry) {
|
|
79
|
+
return MISS;
|
|
80
|
+
}
|
|
81
|
+
if (Date.now() >= entry.expires) {
|
|
82
|
+
byPath.delete(path);
|
|
83
|
+
return MISS;
|
|
84
|
+
}
|
|
85
|
+
return entry.value;
|
|
86
|
+
}
|
|
87
|
+
export function set(host, path, value) {
|
|
88
|
+
if (SCHEMA_CACHE_TTL <= 0) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
let byPath = store.get(host);
|
|
92
|
+
if (!byPath) {
|
|
93
|
+
byPath = new Map();
|
|
94
|
+
store.set(host, byPath);
|
|
95
|
+
}
|
|
96
|
+
byPath.set(path, { value, expires: Date.now() + SCHEMA_CACHE_TTL * 1000 });
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Drop everything cached for one tenant — or, with no argument, for all of them.
|
|
100
|
+
*
|
|
101
|
+
* Coarse on purpose. A builder call can change an entity this server never named
|
|
102
|
+
* (a back-reference lands a column on the OTHER side of a relation), so clearing
|
|
103
|
+
* only the entity in the URL would leave a stale entry behind that nothing would
|
|
104
|
+
* ever correct. The cache refills in one request.
|
|
105
|
+
*/
|
|
106
|
+
export function invalidate(host) {
|
|
107
|
+
if (host === undefined) {
|
|
108
|
+
store.clear();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
store.delete(host);
|
|
112
|
+
}
|
package/dist/tools/entity.js
CHANGED
|
@@ -1861,9 +1861,9 @@ function _truncJson(val) {
|
|
|
1861
1861
|
return jsonResponse(val).slice(0, 60);
|
|
1862
1862
|
}
|
|
1863
1863
|
/** Live entity names on this tenant, or null if the schema list is unreadable. */
|
|
1864
|
-
async function _liveEntityList() {
|
|
1864
|
+
async function _liveEntityList(fresh = false) {
|
|
1865
1865
|
try {
|
|
1866
|
-
const listing = await makeRequest("GET", "/api/core/schema");
|
|
1866
|
+
const listing = await makeRequest("GET", "/api/core/schema", null, { fresh });
|
|
1867
1867
|
if (!Array.isArray(listing)) {
|
|
1868
1868
|
return null;
|
|
1869
1869
|
}
|
|
@@ -1884,17 +1884,31 @@ async function _liveEntityList() {
|
|
|
1884
1884
|
* returns an error with near-matches.
|
|
1885
1885
|
*/
|
|
1886
1886
|
async function _resolveEntity(name) {
|
|
1887
|
-
|
|
1887
|
+
let names = await _liveEntityList();
|
|
1888
1888
|
if (names === null || names.length === 0) {
|
|
1889
1889
|
return { name };
|
|
1890
1890
|
}
|
|
1891
1891
|
if (names.includes(name)) {
|
|
1892
1892
|
return { name };
|
|
1893
1893
|
}
|
|
1894
|
-
|
|
1894
|
+
let ci = names.find((n) => n.toLowerCase() === name.toLowerCase());
|
|
1895
1895
|
if (ci) {
|
|
1896
1896
|
return { name: ci };
|
|
1897
1897
|
}
|
|
1898
|
+
// About to refuse a name for not existing. The list may have come from the
|
|
1899
|
+
// cache, and an entity created elsewhere since would be refused wrongly — so
|
|
1900
|
+
// the refusal is only allowed to stand against a list read from the platform.
|
|
1901
|
+
const latest = await _liveEntityList(true);
|
|
1902
|
+
if (latest !== null && latest.length > 0) {
|
|
1903
|
+
names = latest;
|
|
1904
|
+
if (names.includes(name)) {
|
|
1905
|
+
return { name };
|
|
1906
|
+
}
|
|
1907
|
+
ci = names.find((n) => n.toLowerCase() === name.toLowerCase());
|
|
1908
|
+
if (ci) {
|
|
1909
|
+
return { name: ci };
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1898
1912
|
const nl = name.toLowerCase();
|
|
1899
1913
|
const near = names
|
|
1900
1914
|
.filter((n) => n.toLowerCase().includes(nl) || nl.includes(n.toLowerCase()))
|
|
@@ -1937,20 +1951,36 @@ async function _validateRecordWrites(entityName, records) {
|
|
|
1937
1951
|
return { entityName, error: ent.error };
|
|
1938
1952
|
}
|
|
1939
1953
|
entityName = ent.name;
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
}
|
|
1954
|
+
const readDefs = async (fresh) => {
|
|
1955
|
+
try {
|
|
1956
|
+
return _extractFields(await makeRequest("GET", `/api/core/schema/${entityName}`, null, { fresh }));
|
|
1957
|
+
}
|
|
1958
|
+
catch {
|
|
1959
|
+
return null; // unreadable → can't validate fields; proceed
|
|
1960
|
+
}
|
|
1961
|
+
};
|
|
1962
|
+
const unknownIn = (names) => [
|
|
1963
|
+
...new Set(records.flatMap((r) => Object.keys(r).filter((k) => !names.has(k)))),
|
|
1964
|
+
];
|
|
1965
|
+
const namesOf = (list) => new Set(list.map((f) => f.name).filter((n) => Boolean(n)));
|
|
1966
|
+
let defs = await readDefs(false);
|
|
1948
1967
|
if (defs && defs.length > 0) {
|
|
1949
1968
|
const many = records.length > 1;
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1969
|
+
let fieldNames = namesOf(defs);
|
|
1970
|
+
let unknown = unknownIn(fieldNames);
|
|
1971
|
+
if (unknown.length > 0) {
|
|
1972
|
+
// The schema read above may have been served from the cache, and the one
|
|
1973
|
+
// way a stale schema turns into a WRONG answer rather than a slow one is
|
|
1974
|
+
// right here: refusing a field that somebody added since. Re-read from the
|
|
1975
|
+
// platform before refusing. Costs a request only on the path that was
|
|
1976
|
+
// about to fail anyway.
|
|
1977
|
+
const latest = await readDefs(true);
|
|
1978
|
+
if (latest && latest.length > 0) {
|
|
1979
|
+
defs = latest;
|
|
1980
|
+
fieldNames = namesOf(latest);
|
|
1981
|
+
unknown = unknownIn(fieldNames);
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1954
1984
|
if (unknown.length > 0) {
|
|
1955
1985
|
const body = {
|
|
1956
1986
|
error: `Refused: field(s) [${unknown.join(", ")}] do not exist on '${entityName}'. ` +
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "suppa-mcp-2",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.20.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": {
|