suppa-mcp-2 1.22.0 → 1.23.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/dist/auth.js +116 -8
- package/dist/httpClient.js +16 -4
- package/dist/sessionDiag.js +29 -0
- package/package.json +1 -1
package/dist/auth.js
CHANGED
|
@@ -28,6 +28,7 @@ import { readFileSync, statSync, writeFileSync } from "node:fs";
|
|
|
28
28
|
import { config } from "./config.js";
|
|
29
29
|
import * as store from "./store.js";
|
|
30
30
|
import * as tenants from "./tenants.js";
|
|
31
|
+
import { recordRefreshFailure, clearRefreshFailure } from "./sessionDiag.js";
|
|
31
32
|
export const REFRESH_PATH = "/api/auth/accounts/refresh-token";
|
|
32
33
|
export const OAUTH2_PATH = "/api/auth/accounts/oauth2";
|
|
33
34
|
export const EXPIRY_SKEW = 30.0; // seconds — refresh this long before the token actually expires
|
|
@@ -175,6 +176,28 @@ function readSetCookies(headers) {
|
|
|
175
176
|
}
|
|
176
177
|
return [];
|
|
177
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* A non-2xx answer from an auth endpoint, carrying enough to diagnose it.
|
|
181
|
+
*
|
|
182
|
+
* The message is unchanged from the plain Error this replaces; the fields are
|
|
183
|
+
* what lets the refresh layer tell "this route does not exist" (404/405 — the
|
|
184
|
+
* platform moved it) apart from "this token is no good" (401/403 — the session
|
|
185
|
+
* aged out). Those two need opposite actions from the user.
|
|
186
|
+
*/
|
|
187
|
+
export class AuthHttpError extends Error {
|
|
188
|
+
status;
|
|
189
|
+
method;
|
|
190
|
+
url;
|
|
191
|
+
body;
|
|
192
|
+
constructor(status, statusText, method, url, body) {
|
|
193
|
+
super(`HTTP ${status} ${statusText}: ${body}`);
|
|
194
|
+
this.name = "AuthHttpError";
|
|
195
|
+
this.status = status;
|
|
196
|
+
this.method = method;
|
|
197
|
+
this.url = url;
|
|
198
|
+
this.body = body;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
178
201
|
const rawPost = async (url, opts) => {
|
|
179
202
|
const { cookie = "", body = null, timeout = 60, method = "POST" } = opts;
|
|
180
203
|
const headers = {
|
|
@@ -210,7 +233,7 @@ const rawPost = async (url, opts) => {
|
|
|
210
233
|
clearTimeout(timer);
|
|
211
234
|
const raw = await resp.text();
|
|
212
235
|
if (!resp.ok) {
|
|
213
|
-
throw new
|
|
236
|
+
throw new AuthHttpError(resp.status, resp.statusText, method, url, raw);
|
|
214
237
|
}
|
|
215
238
|
const cookies = readSetCookies(resp.headers);
|
|
216
239
|
const parsed = raw.trim() ? JSON.parse(raw) : {};
|
|
@@ -315,6 +338,23 @@ function refreshDead(creds) {
|
|
|
315
338
|
const exp = creds.refreshExp || jwtExp(creds.refreshToken);
|
|
316
339
|
return !creds.refreshToken || (Boolean(exp) && Date.now() / 1000 >= exp);
|
|
317
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* Record the one refresh failure that never reaches the network: the stored
|
|
343
|
+
* refresh token is already past its expiry. This IS the case where signing in
|
|
344
|
+
* again is the right answer, and saying the date makes that obvious instead of
|
|
345
|
+
* leaving "session expired" to compete with every other cause.
|
|
346
|
+
*/
|
|
347
|
+
function noteExpiredRefresh(host, creds) {
|
|
348
|
+
const exp = creds.refreshExp || jwtExp(creds.refreshToken);
|
|
349
|
+
if (!creds.refreshToken) {
|
|
350
|
+
recordRefreshFailure(host, "no refresh token is stored for this tenant, so the session could not be extended");
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
recordRefreshFailure(host, exp
|
|
354
|
+
? `the stored refresh token expired on ${readableDate(exp)} — a Suppa session lasts ` +
|
|
355
|
+
"about ten days, so this one simply aged out and suppa_login starts a new one"
|
|
356
|
+
: "the stored refresh token is no longer valid; suppa_login starts a new session");
|
|
357
|
+
}
|
|
318
358
|
/** Refresh `ctx`'s access token if needed. No-op when there's no session. */
|
|
319
359
|
export async function maybeRefresh(ctx) {
|
|
320
360
|
let creds = store.load(ctx.host);
|
|
@@ -333,6 +373,7 @@ export async function maybeRefresh(ctx) {
|
|
|
333
373
|
return;
|
|
334
374
|
}
|
|
335
375
|
if (refreshDead(fresh)) {
|
|
376
|
+
noteExpiredRefresh(ctx.host, fresh);
|
|
336
377
|
throw new ReloginRequired(ctx.host);
|
|
337
378
|
}
|
|
338
379
|
await doRefresh(ctx.baseUrl, ctx.host, fresh);
|
|
@@ -355,6 +396,9 @@ export async function maybeRefresh(ctx) {
|
|
|
355
396
|
export async function forceRefresh(ctx) {
|
|
356
397
|
const creds = store.load(ctx.host);
|
|
357
398
|
if (!creds || refreshDead(creds)) {
|
|
399
|
+
if (creds) {
|
|
400
|
+
noteExpiredRefresh(ctx.host, creds);
|
|
401
|
+
}
|
|
358
402
|
return false;
|
|
359
403
|
}
|
|
360
404
|
let promise = inflight.get(ctx.host);
|
|
@@ -362,6 +406,9 @@ export async function forceRefresh(ctx) {
|
|
|
362
406
|
promise = (async () => {
|
|
363
407
|
const fresh = store.load(ctx.host);
|
|
364
408
|
if (!fresh || refreshDead(fresh)) {
|
|
409
|
+
if (fresh) {
|
|
410
|
+
noteExpiredRefresh(ctx.host, fresh);
|
|
411
|
+
}
|
|
365
412
|
throw new ReloginRequired(ctx.host);
|
|
366
413
|
}
|
|
367
414
|
await doRefresh(ctx.baseUrl, ctx.host, fresh);
|
|
@@ -380,16 +427,76 @@ export async function forceRefresh(ctx) {
|
|
|
380
427
|
return false;
|
|
381
428
|
}
|
|
382
429
|
}
|
|
430
|
+
/** Statuses that mean "this route does not answer that verb" — not "no". */
|
|
431
|
+
const VERB_MISMATCH = new Set([404, 405]);
|
|
432
|
+
/** The verb that last worked for a host's refresh route. */
|
|
433
|
+
const refreshVerb = new Map();
|
|
434
|
+
/** A date a person can read, from epoch seconds. */
|
|
435
|
+
function readableDate(epochSeconds) {
|
|
436
|
+
return new Date(epochSeconds * 1000).toISOString().replace("T", " ").slice(0, 16) + " UTC";
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Call the refresh route, surviving a change of verb.
|
|
440
|
+
*
|
|
441
|
+
* The platform has moved this route once already — it used to accept POST and
|
|
442
|
+
* now accepts only GET — and the whole session dies the moment the server
|
|
443
|
+
* guesses wrong. A 404/405 means nothing happened, so trying the other verb
|
|
444
|
+
* once costs one wasted request and saves the session; the verb that works is
|
|
445
|
+
* then remembered per host, so the mistake is paid once, not on every refresh.
|
|
446
|
+
*/
|
|
447
|
+
async function callRefresh(baseUrl, host, creds) {
|
|
448
|
+
const url = baseUrl + REFRESH_PATH;
|
|
449
|
+
const cookie = `refreshToken=${creds.refreshToken}`;
|
|
450
|
+
const first = refreshVerb.get(host) ?? "GET";
|
|
451
|
+
const second = first === "GET" ? "POST" : "GET";
|
|
452
|
+
try {
|
|
453
|
+
const result = await authPost(url, { cookie, method: first });
|
|
454
|
+
refreshVerb.set(host, first);
|
|
455
|
+
return result;
|
|
456
|
+
}
|
|
457
|
+
catch (e) {
|
|
458
|
+
if (!(e instanceof AuthHttpError) || !VERB_MISMATCH.has(e.status)) {
|
|
459
|
+
throw e; // the route answered on the merits — asking again as a verb it
|
|
460
|
+
// does accept would just be a second failed auth attempt
|
|
461
|
+
}
|
|
462
|
+
const result = await authPost(url, { cookie, method: second });
|
|
463
|
+
refreshVerb.set(host, second);
|
|
464
|
+
return result;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
/** Turn a refresh failure into a sentence that names the actual problem. */
|
|
468
|
+
function refreshFailureReason(e) {
|
|
469
|
+
if (e instanceof AuthHttpError) {
|
|
470
|
+
if (VERB_MISMATCH.has(e.status)) {
|
|
471
|
+
return (`the tenant answered HTTP ${e.status} for both GET and POST ${REFRESH_PATH} — ` +
|
|
472
|
+
"the platform's auth API has moved, so signing in again will only buy one more " +
|
|
473
|
+
"access-token lifetime. Install a newer suppa-mcp-2 (npm i -g suppa-mcp-2@latest) " +
|
|
474
|
+
"or report this if none exists yet");
|
|
475
|
+
}
|
|
476
|
+
if (e.status === 401 || e.status === 403) {
|
|
477
|
+
return (`the platform rejected the stored refresh token (HTTP ${e.status}) — the refresh ` +
|
|
478
|
+
"window has run out or the session was revoked; suppa_login starts a new one");
|
|
479
|
+
}
|
|
480
|
+
return `the refresh endpoint answered HTTP ${e.status}: ${e.body.slice(0, 200)}`;
|
|
481
|
+
}
|
|
482
|
+
if (e instanceof ReloginRequired) {
|
|
483
|
+
return e.message;
|
|
484
|
+
}
|
|
485
|
+
return `the refresh request failed: ${e instanceof Error ? e.message : String(e)}`;
|
|
486
|
+
}
|
|
383
487
|
export async function doRefresh(baseUrl, host, creds) {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
488
|
+
let body;
|
|
489
|
+
let cookies;
|
|
490
|
+
try {
|
|
491
|
+
({ body, cookies } = await callRefresh(baseUrl, host, creds));
|
|
492
|
+
}
|
|
493
|
+
catch (e) {
|
|
494
|
+
recordRefreshFailure(host, refreshFailureReason(e));
|
|
495
|
+
throw e;
|
|
496
|
+
}
|
|
391
497
|
const fresh = extractCredentials(body, cookies);
|
|
392
498
|
if (!fresh.accessToken) {
|
|
499
|
+
recordRefreshFailure(host, `the refresh endpoint returned HTTP 200 with no access token in it (${REFRESH_PATH})`);
|
|
393
500
|
throw new ReloginRequired(host);
|
|
394
501
|
}
|
|
395
502
|
if (!fresh.refreshToken) {
|
|
@@ -398,6 +505,7 @@ export async function doRefresh(baseUrl, host, creds) {
|
|
|
398
505
|
fresh.refreshExp = creds.refreshExp;
|
|
399
506
|
}
|
|
400
507
|
store.save(host, fresh);
|
|
508
|
+
clearRefreshFailure(host);
|
|
401
509
|
writeTokensToEnv(host, fresh.accessToken, fresh.refreshToken); // Persist both tokens to .env
|
|
402
510
|
return fresh;
|
|
403
511
|
}
|
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 { lastRefreshError } from "./sessionDiag.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) {
|
|
@@ -386,10 +387,21 @@ async function readResponse(res, rc) {
|
|
|
386
387
|
// ignore — the status code is the message
|
|
387
388
|
}
|
|
388
389
|
if (code === 401) {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
390
|
+
// The refresh that failed knows WHY it failed; without this the sentence
|
|
391
|
+
// was the same whether the session had aged out or the endpoint had
|
|
392
|
+
// moved — and only the first of those is fixed by signing in again.
|
|
393
|
+
// Each recorded reason already ends with ITS action — sign in again, or
|
|
394
|
+
// install a newer server. Appending the generic "Run suppa_login" to a
|
|
395
|
+
// diagnosis that just said signing in will not help is how a user ends up
|
|
396
|
+
// re-authenticating every fifteen minutes.
|
|
397
|
+
const why = lastRefreshError(ctx.host);
|
|
398
|
+
throw new Error(`HTTP 401 for tenant '${ctx.host}': the session could not be refreshed — ` +
|
|
399
|
+
(why
|
|
400
|
+
? why
|
|
401
|
+
: "there is no stored session to refresh, or the platform rejected the " +
|
|
402
|
+
"refresh. Run suppa_login — or, if this tenant is configured through " +
|
|
403
|
+
"SUPPA_API_KEY, set a valid key") +
|
|
404
|
+
`. ${errorBody}`);
|
|
393
405
|
}
|
|
394
406
|
throw new Error(`HTTP ${code} ${safeStatusText(res)}: ${errorBody}`);
|
|
395
407
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why the last token refresh for a tenant failed.
|
|
3
|
+
*
|
|
4
|
+
* A refresh failure is swallowed on purpose — an unhandled rejection out of
|
|
5
|
+
* `ensureValid` kills the stdio server — so the only thing a user ever saw was
|
|
6
|
+
* the 401 that came next: "the session could not be refreshed … run
|
|
7
|
+
* suppa_login". When the platform moved this route from POST to GET, that
|
|
8
|
+
* message sent people to re-login every fifteen minutes and never once named
|
|
9
|
+
* the 404 that was the actual cause. Someone had to read the web client's
|
|
10
|
+
* source to find it.
|
|
11
|
+
*
|
|
12
|
+
* This module is the one place that remembers the reason. It is kept
|
|
13
|
+
* dependency-free so the auth layer that produces it and the transport that
|
|
14
|
+
* reports it can both import it without an import cycle.
|
|
15
|
+
*/
|
|
16
|
+
/** host -> why its last refresh attempt failed. */
|
|
17
|
+
const reasons = new Map();
|
|
18
|
+
/** Remember why `host` could not refresh, for the 401 that is about to follow. */
|
|
19
|
+
export function recordRefreshFailure(host, reason) {
|
|
20
|
+
reasons.set(host, reason);
|
|
21
|
+
}
|
|
22
|
+
/** Forget the last failure for `host` — called when a refresh succeeds. */
|
|
23
|
+
export function clearRefreshFailure(host) {
|
|
24
|
+
reasons.delete(host);
|
|
25
|
+
}
|
|
26
|
+
/** The last refresh failure for `host`, or null if the last attempt worked. */
|
|
27
|
+
export function lastRefreshError(host) {
|
|
28
|
+
return reasons.get(host) ?? null;
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "suppa-mcp-2",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.23.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": {
|