gogcli-mcp 2.21.0 → 2.21.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/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/dist/index.js +321 -54
- package/dist/lib.js +322 -55
- package/manifest.json +1 -1
- package/package.json +1 -1
- package/server.json +2 -2
- package/src/auth-log.ts +147 -0
- package/src/connector-runtime.ts +579 -79
- package/src/google-token.ts +181 -15
- package/src/runner.ts +67 -2
- package/src/tools/utils.ts +70 -5
- package/src/worker.ts +12 -1
- package/tests/auth-log.test.ts +508 -0
- package/tests/connector-runtime.test.ts +675 -2
- package/tests/google-token.test.ts +125 -4
- package/tests/runner.test.ts +21 -1
- package/tests/tools/auth-401-context.test.ts +42 -0
- package/tests/tools/auth-401-shapes.test.ts +50 -0
- package/tests/tools/utils.test.ts +55 -2
package/dist/lib.js
CHANGED
|
@@ -23018,6 +23018,21 @@ import { delimiter, join } from "node:path";
|
|
|
23018
23018
|
function isGogFileArg(arg) {
|
|
23019
23019
|
return typeof arg !== "string";
|
|
23020
23020
|
}
|
|
23021
|
+
var RUNNER_TRANSPORT_BRAND = /* @__PURE__ */ Symbol.for("gogcli.RunnerTransportError");
|
|
23022
|
+
var RunnerTransportError = class extends Error {
|
|
23023
|
+
kind;
|
|
23024
|
+
status;
|
|
23025
|
+
constructor(message, kind, status) {
|
|
23026
|
+
super(message);
|
|
23027
|
+
this.name = "RunnerTransportError";
|
|
23028
|
+
this.kind = kind;
|
|
23029
|
+
this.status = status;
|
|
23030
|
+
Object.defineProperty(this, RUNNER_TRANSPORT_BRAND, { value: true });
|
|
23031
|
+
}
|
|
23032
|
+
};
|
|
23033
|
+
function isRunnerTransportError(err) {
|
|
23034
|
+
return err instanceof Error && err[RUNNER_TRANSPORT_BRAND] === true;
|
|
23035
|
+
}
|
|
23021
23036
|
var runExecutor = new AsyncLocalStorage();
|
|
23022
23037
|
var defaultExecutor;
|
|
23023
23038
|
function setDefaultGogExecutor(executor) {
|
|
@@ -23027,7 +23042,7 @@ function activeExecutor() {
|
|
|
23027
23042
|
return runExecutor.getStore() ?? defaultExecutor;
|
|
23028
23043
|
}
|
|
23029
23044
|
var TIMEOUT_MS = 3e4;
|
|
23030
|
-
var MIN_GOG_VERSION = "0.
|
|
23045
|
+
var MIN_GOG_VERSION = "0.35.0";
|
|
23031
23046
|
function readonlyEnvEnabled() {
|
|
23032
23047
|
return readEnvVar("GOG_READONLY") !== void 0 && parseBoolEnv("GOG_READONLY", { default: true });
|
|
23033
23048
|
}
|
|
@@ -23199,7 +23214,11 @@ async function run(args, options = {}) {
|
|
|
23199
23214
|
}
|
|
23200
23215
|
return redact(output);
|
|
23201
23216
|
} catch (err) {
|
|
23202
|
-
|
|
23217
|
+
const message = redact(err instanceof Error ? err.message : String(err));
|
|
23218
|
+
if (isRunnerTransportError(err)) {
|
|
23219
|
+
throw new RunnerTransportError(message, err.kind, err.status);
|
|
23220
|
+
}
|
|
23221
|
+
throw new Error(message);
|
|
23203
23222
|
}
|
|
23204
23223
|
}
|
|
23205
23224
|
async function runBinary(args, options = {}) {
|
|
@@ -23473,7 +23492,7 @@ function registerRunTool(server, options) {
|
|
|
23473
23492
|
function errorText(err) {
|
|
23474
23493
|
return err instanceof Error ? `Error: ${err.message}` : String(err);
|
|
23475
23494
|
}
|
|
23476
|
-
var DEFINITE_AUTH_PATTERN = /\b(
|
|
23495
|
+
var DEFINITE_AUTH_PATTERN = /\b(?:unauthorized|invalid_grant)\b|\b(?:error|status|code|http|responded|response)["']?[\s:=(,]{0,4}401\b/i;
|
|
23477
23496
|
var STALE_TOKEN_PATTERN = /\b(?:access[ _-]?)?token\b[^.;\n]{0,40}\b(?:has\s+)?(?:been\s+)?(?:expired|revoked)\b|\b(?:expired|revoked)\s+(?:access[ _-]?)?token\b/i;
|
|
23478
23497
|
var AUTH_ERROR_PATTERN = new RegExp(`${DEFINITE_AUTH_PATTERN.source}|${STALE_TOKEN_PATTERN.source}`, "i");
|
|
23479
23498
|
var INVALID_GRANT_PATTERN = /invalid_grant|token has been expired or revoked/i;
|
|
@@ -23483,6 +23502,14 @@ var AUTH_HINT = "\n\nAuthentication may have expired. Use gog_auth_add to re-aut
|
|
|
23483
23502
|
var INVALID_GRANT_HINT = '\n\nThe stored refresh token was rejected (invalid_grant): it has expired or been revoked, so the whole account is signed out and re-authorization is required. The most common cause is the 7-day refresh-token limit Google applies to OAuth apps whose consent screen is still in "Testing" mode. Re-authorize with gog_auth_add (opens a browser) or gog_auth_add_url + gog_auth_add_complete (remote/headless). To stop this recurring, publish the OAuth consent screen to "In production" in the Google Cloud project that owns the OAuth client. Ask the user if they would like to re-authenticate.';
|
|
23484
23503
|
var TRANSIENT_HINT = "\n\nThis error is often transient. Retry the same call before trying a different approach (do not fall back to smaller writes or row-by-row operations).";
|
|
23485
23504
|
var GRID_LIMIT_HINT = "\n\nThe target range is outside the sheet's current grid. Add the missing rows or columns first with gog_sheets_insert (dimension: rows or cols), then retry the write.";
|
|
23505
|
+
var RUNNER_TRANSPORT_AUTH_HINT = "\n\nThis is the CONNECTOR's own transport auth failing, not your Google sign-in. The gog-runner backend rejected the bearer token this server sent, so the request never reached gog and no Google credential was checked \u2014 the Google account is not the problem and re-authorizing it cannot fix this. An operator must make the Worker secret GOG_RUNNER_KEY equal RUNNER_KEY on the Fly app (wrangler secret put GOG_RUNNER_KEY / fly secrets set RUNNER_KEY), then retry.";
|
|
23506
|
+
var RUNNER_TRANSPORT_HINTS = {
|
|
23507
|
+
"transport-auth": RUNNER_TRANSPORT_AUTH_HINT,
|
|
23508
|
+
// The request itself was malformed, so the runner will refuse it identically
|
|
23509
|
+
// every time. Nothing to advise beyond the message the runner already gave.
|
|
23510
|
+
"transport-request": "",
|
|
23511
|
+
"transport-retryable": TRANSIENT_HINT
|
|
23512
|
+
};
|
|
23486
23513
|
function formatAccountList(raw) {
|
|
23487
23514
|
try {
|
|
23488
23515
|
const parsed = JSON.parse(raw);
|
|
@@ -23495,11 +23522,12 @@ function formatAccountList(raw) {
|
|
|
23495
23522
|
}
|
|
23496
23523
|
async function diagnose(err) {
|
|
23497
23524
|
const errText = errorText(err);
|
|
23525
|
+
const transportHint = isRunnerTransportError(err) ? RUNNER_TRANSPORT_HINTS[err.kind] : void 0;
|
|
23498
23526
|
const isInvalidGrant = INVALID_GRANT_PATTERN.test(errText);
|
|
23499
23527
|
const isTransientError = !DEFINITE_AUTH_PATTERN.test(errText) && TRANSIENT_ERROR_PATTERN.test(errText);
|
|
23500
23528
|
const isAuthError = !isTransientError && AUTH_ERROR_PATTERN.test(errText);
|
|
23501
23529
|
const isGridLimitError = GRID_LIMIT_ERROR_PATTERN.test(errText);
|
|
23502
|
-
const hint = isInvalidGrant ? INVALID_GRANT_HINT : isAuthError ? AUTH_HINT : isTransientError ? TRANSIENT_HINT : isGridLimitError ? GRID_LIMIT_HINT : "";
|
|
23530
|
+
const hint = transportHint ?? (isInvalidGrant ? INVALID_GRANT_HINT : isAuthError ? AUTH_HINT : isTransientError ? TRANSIENT_HINT : isGridLimitError ? GRID_LIMIT_HINT : "");
|
|
23503
23531
|
try {
|
|
23504
23532
|
const accounts = formatAccountList(await run(["auth", "list"]));
|
|
23505
23533
|
return errorResult(`${errText}
|
|
@@ -24990,7 +25018,7 @@ function registerTasksTools(server) {
|
|
|
24990
25018
|
}
|
|
24991
25019
|
|
|
24992
25020
|
// src/server.ts
|
|
24993
|
-
var VERSION = true ? "2.21.
|
|
25021
|
+
var VERSION = true ? "2.21.1" : "0.0.0";
|
|
24994
25022
|
var BASE_TOOL_REGISTRARS = [
|
|
24995
25023
|
registerApiTools,
|
|
24996
25024
|
registerAuthTools,
|
|
@@ -25005,6 +25033,24 @@ var BASE_TOOL_REGISTRARS = [
|
|
|
25005
25033
|
registerTasksTools
|
|
25006
25034
|
];
|
|
25007
25035
|
|
|
25036
|
+
// src/auth-log.ts
|
|
25037
|
+
var FAILURES = /* @__PURE__ */ new Set([
|
|
25038
|
+
"token.mint-failed",
|
|
25039
|
+
"grant.dead",
|
|
25040
|
+
"replay.failed",
|
|
25041
|
+
"runner.auth-failed"
|
|
25042
|
+
]);
|
|
25043
|
+
var PREFIX = "gog-auth ";
|
|
25044
|
+
var TAG_CHARS = 12;
|
|
25045
|
+
function credentialTag(cacheKeyHash) {
|
|
25046
|
+
return cacheKeyHash.slice(0, TAG_CHARS);
|
|
25047
|
+
}
|
|
25048
|
+
function logAuthTransition(event, context) {
|
|
25049
|
+
const record2 = JSON.stringify({ at: (/* @__PURE__ */ new Date()).toISOString(), event, ...context });
|
|
25050
|
+
const write = FAILURES.has(event) ? console.error : console.warn;
|
|
25051
|
+
write(PREFIX + redactSecrets2(record2));
|
|
25052
|
+
}
|
|
25053
|
+
|
|
25008
25054
|
// src/google-token.ts
|
|
25009
25055
|
var TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token";
|
|
25010
25056
|
var EXPIRY_MARGIN_MS = 12e4;
|
|
@@ -25030,22 +25076,73 @@ function makeAccessTokenSource(env) {
|
|
|
25030
25076
|
);
|
|
25031
25077
|
};
|
|
25032
25078
|
}
|
|
25033
|
-
|
|
25034
|
-
|
|
25035
|
-
|
|
25036
|
-
|
|
25037
|
-
|
|
25079
|
+
let keyPromise;
|
|
25080
|
+
const key = () => keyPromise ??= cacheKey(refreshToken, clientId);
|
|
25081
|
+
const logCacheHits = parseBoolEnv("GOG_AUTH_LOG_CACHE_HITS", { env });
|
|
25082
|
+
const read = async () => {
|
|
25083
|
+
const k = await key();
|
|
25084
|
+
const hit = cache.get(k);
|
|
25085
|
+
if (hit && hit.expiresAt - EXPIRY_MARGIN_MS > Date.now()) {
|
|
25086
|
+
if (logCacheHits) logAuthTransition("token.cache-hit", { credential: credentialTag(k) });
|
|
25087
|
+
return hit.accessToken;
|
|
25088
|
+
}
|
|
25089
|
+
let pending = inFlight.get(k);
|
|
25038
25090
|
if (!pending) {
|
|
25039
25091
|
pending = exchange(refreshToken, clientId, clientSecret).then((minted2) => {
|
|
25040
|
-
cache.set(
|
|
25092
|
+
cache.set(k, minted2);
|
|
25093
|
+
logAuthTransition("token.minted", {
|
|
25094
|
+
credential: credentialTag(k),
|
|
25095
|
+
reason: `valid for ${Math.round((minted2.expiresAt - Date.now()) / 1e3)}s`
|
|
25096
|
+
});
|
|
25041
25097
|
return minted2;
|
|
25042
|
-
}).
|
|
25043
|
-
|
|
25098
|
+
}).catch((err) => {
|
|
25099
|
+
logAuthTransition(err.grantDead ? "grant.dead" : "token.mint-failed", {
|
|
25100
|
+
credential: credentialTag(k),
|
|
25101
|
+
reason: err.message
|
|
25102
|
+
});
|
|
25103
|
+
throw err;
|
|
25104
|
+
}).finally(() => inFlight.delete(k));
|
|
25105
|
+
inFlight.set(k, pending);
|
|
25044
25106
|
}
|
|
25045
25107
|
const minted = await pending;
|
|
25046
25108
|
return minted.accessToken;
|
|
25047
25109
|
};
|
|
25110
|
+
const invalidate = async (rejected) => {
|
|
25111
|
+
const k = await key();
|
|
25112
|
+
const hit = cache.get(k);
|
|
25113
|
+
if (!hit || hit.accessToken !== rejected) {
|
|
25114
|
+
logAuthTransition("token.evict-noop", {
|
|
25115
|
+
credential: credentialTag(k),
|
|
25116
|
+
reason: hit ? "a concurrent caller had already replaced this credential\u2019s token" : "no token was cached for this credential"
|
|
25117
|
+
});
|
|
25118
|
+
return false;
|
|
25119
|
+
}
|
|
25120
|
+
cache.delete(k);
|
|
25121
|
+
logAuthTransition("token.evicted", {
|
|
25122
|
+
credential: credentialTag(k),
|
|
25123
|
+
reason: "Google rejected this access token; the next read will mint a new one"
|
|
25124
|
+
});
|
|
25125
|
+
return true;
|
|
25126
|
+
};
|
|
25127
|
+
return Object.assign(read, {
|
|
25128
|
+
invalidate,
|
|
25129
|
+
credentialId: async () => credentialTag(await key())
|
|
25130
|
+
});
|
|
25048
25131
|
}
|
|
25132
|
+
var TokenExchangeError = class extends Error {
|
|
25133
|
+
/**
|
|
25134
|
+
* The REFRESH token is dead (Google's `invalid_grant`), not merely the access
|
|
25135
|
+
* token. Carried as a flag rather than re-read from the message, because
|
|
25136
|
+
* inferring the author of a failure from prose several authors can produce is
|
|
25137
|
+
* precisely the mistake this branch exists to undo. `instanceof` is safe: the
|
|
25138
|
+
* class is thrown and caught inside this one module.
|
|
25139
|
+
*/
|
|
25140
|
+
grantDead;
|
|
25141
|
+
constructor(message, grantDead) {
|
|
25142
|
+
super(message);
|
|
25143
|
+
this.grantDead = grantDead;
|
|
25144
|
+
}
|
|
25145
|
+
};
|
|
25049
25146
|
async function exchange(refreshToken, clientId, clientSecret) {
|
|
25050
25147
|
let res;
|
|
25051
25148
|
try {
|
|
@@ -25060,23 +25157,29 @@ async function exchange(refreshToken, clientId, clientSecret) {
|
|
|
25060
25157
|
}).toString()
|
|
25061
25158
|
});
|
|
25062
25159
|
} catch (err) {
|
|
25063
|
-
throw new
|
|
25064
|
-
`the Google token exchange could not be reached: ${err instanceof Error ? err.message : String(err)}
|
|
25160
|
+
throw new TokenExchangeError(
|
|
25161
|
+
`the Google token exchange could not be reached: ${err instanceof Error ? err.message : String(err)}`,
|
|
25162
|
+
false
|
|
25065
25163
|
);
|
|
25066
25164
|
}
|
|
25067
25165
|
const body = await res.json().catch(() => ({}));
|
|
25068
25166
|
if (!res.ok) {
|
|
25069
25167
|
if (body.error === "invalid_grant") {
|
|
25070
|
-
throw new
|
|
25071
|
-
'the stored refresh token has expired or been revoked, so this account must be re-authorized (commonly the 7-day limit on OAuth consent screens still in "Testing" mode). Re-enrol with gog_auth_add_url + gog_auth_add_complete and store the new refresh token.'
|
|
25168
|
+
throw new TokenExchangeError(
|
|
25169
|
+
'the stored refresh token was rejected (invalid_grant): it has expired or been revoked, so this account must be re-authorized (commonly the 7-day limit on OAuth consent screens still in "Testing" mode). Re-enrol with gog_auth_add_url + gog_auth_add_complete and store the new refresh token.',
|
|
25170
|
+
true
|
|
25072
25171
|
);
|
|
25073
25172
|
}
|
|
25074
|
-
throw new
|
|
25075
|
-
`the access token could not be refreshed (HTTP ${res.status}${body.error ? `, ${body.error}` : ""})
|
|
25173
|
+
throw new TokenExchangeError(
|
|
25174
|
+
`the access token could not be refreshed (HTTP ${res.status}${body.error ? `, ${body.error}` : ""})`,
|
|
25175
|
+
false
|
|
25076
25176
|
);
|
|
25077
25177
|
}
|
|
25078
25178
|
if (!body.access_token) {
|
|
25079
|
-
throw new
|
|
25179
|
+
throw new TokenExchangeError(
|
|
25180
|
+
"the access token could not be refreshed: Google returned no access_token",
|
|
25181
|
+
false
|
|
25182
|
+
);
|
|
25080
25183
|
}
|
|
25081
25184
|
const expiresInMs = (body.expires_in ?? 3600) * 1e3;
|
|
25082
25185
|
return { accessToken: body.access_token, expiresAt: Date.now() + expiresInMs };
|
|
@@ -25085,54 +25188,218 @@ async function exchange(refreshToken, clientId, clientSecret) {
|
|
|
25085
25188
|
// src/connector-runtime.ts
|
|
25086
25189
|
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
25087
25190
|
var DEADLINE_GRACE_MS = 5e3;
|
|
25191
|
+
var MIN_REPLAY_BUDGET_MS = 1e3;
|
|
25088
25192
|
var RUNNER_GOG_FAILED = 422;
|
|
25089
25193
|
var RUNNER_DRAINING = 503;
|
|
25194
|
+
var RUNNER_BAD_REQUEST = 400;
|
|
25195
|
+
var RUNNER_BAD_KEY = 401;
|
|
25196
|
+
var GogFailedError = class extends Error {
|
|
25197
|
+
/** gog's stderr alone, with no echoed argv mixed in. */
|
|
25198
|
+
stderr;
|
|
25199
|
+
constructor(message, stderr) {
|
|
25200
|
+
super(message);
|
|
25201
|
+
this.stderr = stderr;
|
|
25202
|
+
}
|
|
25203
|
+
};
|
|
25204
|
+
var GOOGLE_TOKEN_REJECTED_PATTERN = /Google API error \(401\b|invalid[ _]authentication[ _]credentials|\bACCESS_TOKEN_EXPIRED\b|\binvalid_token\b/i;
|
|
25205
|
+
var REFRESH_TOKEN_DEAD_PATTERN = /\binvalid_grant\b/i;
|
|
25206
|
+
var READ_ONLY_SUBCOMMANDS = /* @__PURE__ */ new Set([
|
|
25207
|
+
"cat",
|
|
25208
|
+
"describe",
|
|
25209
|
+
"get",
|
|
25210
|
+
"info",
|
|
25211
|
+
"list",
|
|
25212
|
+
"list-slides",
|
|
25213
|
+
"ls",
|
|
25214
|
+
"metadata",
|
|
25215
|
+
"read-slide",
|
|
25216
|
+
"search",
|
|
25217
|
+
"services",
|
|
25218
|
+
"status",
|
|
25219
|
+
"structure"
|
|
25220
|
+
]);
|
|
25221
|
+
function gogTarget(args) {
|
|
25222
|
+
const words = args.filter((arg) => typeof arg === "string");
|
|
25223
|
+
let service;
|
|
25224
|
+
for (let i = 0; i < words.length; i += 1) {
|
|
25225
|
+
const word = words[i];
|
|
25226
|
+
if (word.startsWith("-")) {
|
|
25227
|
+
if (word === "--account") i += 1;
|
|
25228
|
+
continue;
|
|
25229
|
+
}
|
|
25230
|
+
if (service === void 0) {
|
|
25231
|
+
service = word;
|
|
25232
|
+
continue;
|
|
25233
|
+
}
|
|
25234
|
+
return { service, subcommand: word };
|
|
25235
|
+
}
|
|
25236
|
+
return { service };
|
|
25237
|
+
}
|
|
25238
|
+
async function remintAfterGoogleRejection(err, used, args, readAccessToken, deadlineAt) {
|
|
25239
|
+
if (!(err instanceof GogFailedError)) return void 0;
|
|
25240
|
+
const grantDead = REFRESH_TOKEN_DEAD_PATTERN.test(err.stderr);
|
|
25241
|
+
if (!grantDead && !GOOGLE_TOKEN_REJECTED_PATTERN.test(err.stderr)) return void 0;
|
|
25242
|
+
const { service, subcommand } = gogTarget(args);
|
|
25243
|
+
const credential = await readAccessToken?.credentialId?.();
|
|
25244
|
+
const where = { credential, service };
|
|
25245
|
+
if (grantDead) {
|
|
25246
|
+
logAuthTransition("grant.dead", {
|
|
25247
|
+
...where,
|
|
25248
|
+
reason: "gog reported invalid_grant: the stored refresh token is dead, so no token can be minted and this account must be re-authorized"
|
|
25249
|
+
});
|
|
25250
|
+
return void 0;
|
|
25251
|
+
}
|
|
25252
|
+
if (!used) {
|
|
25253
|
+
logAuthTransition("replay.declined", {
|
|
25254
|
+
...where,
|
|
25255
|
+
reason: "no access token was supplied with the call, so gog acted as the backend volume\u2019s own identity"
|
|
25256
|
+
});
|
|
25257
|
+
return void 0;
|
|
25258
|
+
}
|
|
25259
|
+
if (!readAccessToken?.invalidate) {
|
|
25260
|
+
logAuthTransition("replay.declined", {
|
|
25261
|
+
...where,
|
|
25262
|
+
reason: "this token source cannot mint a replacement, so a replay would resend the rejected token"
|
|
25263
|
+
});
|
|
25264
|
+
return void 0;
|
|
25265
|
+
}
|
|
25266
|
+
const evicted = await readAccessToken.invalidate(used);
|
|
25267
|
+
if (subcommand === void 0 || !READ_ONLY_SUBCOMMANDS.has(subcommand)) {
|
|
25268
|
+
logAuthTransition("replay.declined", {
|
|
25269
|
+
...where,
|
|
25270
|
+
reason: `not replayable: '${subcommand ?? "(none)"}' is not a known read-only subcommand and a write could double-apply`
|
|
25271
|
+
});
|
|
25272
|
+
return void 0;
|
|
25273
|
+
}
|
|
25274
|
+
if (!evicted) {
|
|
25275
|
+
logAuthTransition("replay.declined", {
|
|
25276
|
+
...where,
|
|
25277
|
+
reason: "the rejected token was already superseded, so the cache holds the token a replay would send"
|
|
25278
|
+
});
|
|
25279
|
+
return void 0;
|
|
25280
|
+
}
|
|
25281
|
+
const fresh = await readAccessToken();
|
|
25282
|
+
if (!fresh) {
|
|
25283
|
+
logAuthTransition("replay.declined", {
|
|
25284
|
+
...where,
|
|
25285
|
+
reason: "the token source produced no token after eviction; replaying without one would act as the backend"
|
|
25286
|
+
});
|
|
25287
|
+
return void 0;
|
|
25288
|
+
}
|
|
25289
|
+
const budgetMs = deadlineAt - Date.now();
|
|
25290
|
+
if (budgetMs < MIN_REPLAY_BUDGET_MS) {
|
|
25291
|
+
logAuthTransition("replay.declined", {
|
|
25292
|
+
...where,
|
|
25293
|
+
reason: `only ${budgetMs}ms of the call\u2019s deadline remained, so a replay could only time out; the rejected token was evicted, so the next call mints a fresh one`
|
|
25294
|
+
});
|
|
25295
|
+
return void 0;
|
|
25296
|
+
}
|
|
25297
|
+
return { token: fresh, budgetMs, invalidate: readAccessToken.invalidate, ...where };
|
|
25298
|
+
}
|
|
25090
25299
|
function makeFlyExecutor(endpoint, key, readAccessToken) {
|
|
25091
25300
|
return async (args, opts) => {
|
|
25092
25301
|
const deadlineMs = (opts?.timeout ?? DEFAULT_TIMEOUT_MS) + DEADLINE_GRACE_MS;
|
|
25093
25302
|
const accessToken = await readAccessToken?.();
|
|
25094
|
-
|
|
25303
|
+
const deadlineAt = Date.now() + deadlineMs;
|
|
25095
25304
|
try {
|
|
25096
|
-
|
|
25097
|
-
method: "POST",
|
|
25098
|
-
headers: {
|
|
25099
|
-
Authorization: "Bearer " + key,
|
|
25100
|
-
"Content-Type": "application/json"
|
|
25101
|
-
},
|
|
25102
|
-
body: JSON.stringify(accessToken ? { args, accessToken } : { args }),
|
|
25103
|
-
signal: AbortSignal.timeout(deadlineMs)
|
|
25104
|
-
});
|
|
25305
|
+
return await attempt(endpoint, key, args, accessToken, deadlineMs);
|
|
25105
25306
|
} catch (err) {
|
|
25106
|
-
const
|
|
25107
|
-
|
|
25108
|
-
|
|
25109
|
-
|
|
25110
|
-
|
|
25307
|
+
const replay = await remintAfterGoogleRejection(
|
|
25308
|
+
err,
|
|
25309
|
+
accessToken,
|
|
25310
|
+
args,
|
|
25311
|
+
readAccessToken,
|
|
25312
|
+
deadlineAt
|
|
25313
|
+
);
|
|
25314
|
+
if (replay === void 0) throw err;
|
|
25315
|
+
const where = { credential: replay.credential, service: replay.service, endpoint };
|
|
25316
|
+
logAuthTransition("replay.attempted", {
|
|
25317
|
+
...where,
|
|
25318
|
+
reason: "Google rejected the access token; replaying this read once with a freshly minted one"
|
|
25319
|
+
});
|
|
25320
|
+
try {
|
|
25321
|
+
const stdout = await attempt(endpoint, key, args, replay.token, replay.budgetMs);
|
|
25322
|
+
logAuthTransition("replay.succeeded", where);
|
|
25323
|
+
return stdout;
|
|
25324
|
+
} catch (replayErr) {
|
|
25325
|
+
logAuthTransition("replay.failed", { ...where, reason: String(replayErr) });
|
|
25326
|
+
if (replayErr instanceof GogFailedError && GOOGLE_TOKEN_REJECTED_PATTERN.test(replayErr.stderr)) {
|
|
25327
|
+
await replay.invalidate(replay.token);
|
|
25328
|
+
}
|
|
25329
|
+
throw replayErr;
|
|
25111
25330
|
}
|
|
25112
|
-
throw err;
|
|
25113
25331
|
}
|
|
25114
|
-
|
|
25115
|
-
|
|
25116
|
-
|
|
25332
|
+
};
|
|
25333
|
+
}
|
|
25334
|
+
async function attempt(endpoint, key, args, accessToken, deadlineMs) {
|
|
25335
|
+
let res;
|
|
25336
|
+
try {
|
|
25337
|
+
res = await fetch(endpoint + "/run", {
|
|
25338
|
+
method: "POST",
|
|
25339
|
+
headers: {
|
|
25340
|
+
Authorization: "Bearer " + key,
|
|
25341
|
+
"Content-Type": "application/json"
|
|
25342
|
+
},
|
|
25343
|
+
body: JSON.stringify(accessToken ? { args, accessToken } : { args }),
|
|
25344
|
+
signal: AbortSignal.timeout(deadlineMs)
|
|
25345
|
+
});
|
|
25346
|
+
} catch (err) {
|
|
25347
|
+
const name = err instanceof Error ? err.name : "";
|
|
25348
|
+
if (name === "TimeoutError" || name === "AbortError") {
|
|
25349
|
+
throw new RunnerTransportError(
|
|
25350
|
+
`gog-runner did not respond within ${deadlineMs}ms (${endpoint}) \u2014 the Fly backend may be cold or wedged`,
|
|
25351
|
+
"transport-retryable"
|
|
25352
|
+
);
|
|
25353
|
+
}
|
|
25354
|
+
throw err;
|
|
25355
|
+
}
|
|
25356
|
+
if (!res.ok) {
|
|
25357
|
+
const body = await res.json().catch(() => null);
|
|
25358
|
+
const detail = body && typeof body.error === "string" ? body.stderr && body.stderr.trim() && body.stderr.trim() !== body.error.trim() ? `${body.error}
|
|
25117
25359
|
${body.stderr}` : body.error : "";
|
|
25118
|
-
|
|
25119
|
-
|
|
25120
|
-
|
|
25121
|
-
|
|
25122
|
-
throw new Error(
|
|
25123
|
-
`gog-runner is restarting; retry this call.${detail ? ` ${detail}` : ""}`
|
|
25124
|
-
);
|
|
25125
|
-
}
|
|
25126
|
-
if (detail) {
|
|
25127
|
-
throw new Error(detail);
|
|
25128
|
-
}
|
|
25129
|
-
throw new Error(
|
|
25130
|
-
`gog-runner HTTP ${res.status}: the response did not come from the runner, so the request never reached gog. The backend Machine was most likely starting or shutting down \u2014 this is transient, retry the same call.`
|
|
25360
|
+
if (res.status === RUNNER_GOG_FAILED) {
|
|
25361
|
+
throw new GogFailedError(
|
|
25362
|
+
detail || "gog failed on the runner (no detail supplied)",
|
|
25363
|
+
typeof body?.stderr === "string" ? body.stderr : ""
|
|
25131
25364
|
);
|
|
25132
25365
|
}
|
|
25133
|
-
|
|
25134
|
-
|
|
25135
|
-
|
|
25366
|
+
if (res.status === RUNNER_BAD_KEY) {
|
|
25367
|
+
logAuthTransition("runner.auth-failed", {
|
|
25368
|
+
service: gogTarget(args).service,
|
|
25369
|
+
endpoint,
|
|
25370
|
+
reason: "the gog-runner rejected the connector\u2019s bearer token, so gog never ran and no Google credential was read; GOG_RUNNER_KEY does not match the Fly app\u2019s RUNNER_KEY"
|
|
25371
|
+
});
|
|
25372
|
+
throw new RunnerTransportError(
|
|
25373
|
+
"gog-runner rejected the connector's bearer token, so the request never reached gog and no Google credential was involved. The Worker secret GOG_RUNNER_KEY no longer matches RUNNER_KEY on the Fly app; set them to the same value (wrangler secret put GOG_RUNNER_KEY / fly secrets set RUNNER_KEY) and retry.",
|
|
25374
|
+
"transport-auth",
|
|
25375
|
+
res.status
|
|
25376
|
+
);
|
|
25377
|
+
}
|
|
25378
|
+
if (res.status === RUNNER_BAD_REQUEST) {
|
|
25379
|
+
throw new RunnerTransportError(
|
|
25380
|
+
detail || "gog-runner rejected the request (no detail supplied)",
|
|
25381
|
+
"transport-request",
|
|
25382
|
+
res.status
|
|
25383
|
+
);
|
|
25384
|
+
}
|
|
25385
|
+
if (res.status === RUNNER_DRAINING || body?.retryable === true) {
|
|
25386
|
+
throw new RunnerTransportError(
|
|
25387
|
+
`gog-runner is restarting; retry this call.${detail ? ` ${detail}` : ""}`,
|
|
25388
|
+
"transport-retryable",
|
|
25389
|
+
res.status
|
|
25390
|
+
);
|
|
25391
|
+
}
|
|
25392
|
+
if (detail) {
|
|
25393
|
+
throw new Error(detail);
|
|
25394
|
+
}
|
|
25395
|
+
throw new RunnerTransportError(
|
|
25396
|
+
`gog-runner HTTP ${res.status}: the response did not come from the runner, so the request never reached gog. The backend Machine was most likely starting or shutting down \u2014 this is transient, retry the same call.`,
|
|
25397
|
+
"transport-retryable",
|
|
25398
|
+
res.status
|
|
25399
|
+
);
|
|
25400
|
+
}
|
|
25401
|
+
const { stdout } = await res.json();
|
|
25402
|
+
return stdout;
|
|
25136
25403
|
}
|
|
25137
25404
|
|
|
25138
25405
|
// src/remote-runner.ts
|
package/manifest.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"manifest_version": "0.3",
|
|
4
4
|
"name": "gogcli-mcp",
|
|
5
5
|
"display_name": "gogcli",
|
|
6
|
-
"version": "2.21.
|
|
6
|
+
"version": "2.21.1",
|
|
7
7
|
"description": "Google Sheets (and more) for Claude via gogcli — read, write, and manage spreadsheets",
|
|
8
8
|
"author": {
|
|
9
9
|
"name": "Chris Hall",
|
package/package.json
CHANGED
package/server.json
CHANGED
|
@@ -7,12 +7,12 @@
|
|
|
7
7
|
"source": "github",
|
|
8
8
|
"subfolder": "packages/gogcli-mcp"
|
|
9
9
|
},
|
|
10
|
-
"version": "2.21.
|
|
10
|
+
"version": "2.21.1",
|
|
11
11
|
"packages": [
|
|
12
12
|
{
|
|
13
13
|
"registryType": "npm",
|
|
14
14
|
"identifier": "gogcli-mcp",
|
|
15
|
-
"version": "2.21.
|
|
15
|
+
"version": "2.21.1",
|
|
16
16
|
"transport": {
|
|
17
17
|
"type": "stdio"
|
|
18
18
|
},
|
package/src/auth-log.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { redactSecrets } from './runner.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One line per auth-state transition, on the paths where a Google credential is
|
|
5
|
+
* minted, served, evicted, replaced or refused.
|
|
6
|
+
*
|
|
7
|
+
* ## Why this exists
|
|
8
|
+
*
|
|
9
|
+
* The incident that produced this branch could not be investigated. `gog`
|
|
10
|
+
* connectors told a user all session to re-authorize a Google account whose
|
|
11
|
+
* grant was healthy, and nothing anywhere had recorded a single auth-state
|
|
12
|
+
* transition: not the runner's transport 401, not a mint, not a cache hit, not
|
|
13
|
+
* a dead grant. `wrangler.jsonc` has set `observability.enabled = true` since
|
|
14
|
+
* the Worker shipped, so a Workers Logs sink was live the whole time with
|
|
15
|
+
* nothing writing to it.
|
|
16
|
+
*
|
|
17
|
+
* The other three fixes on this branch made those outcomes DISTINGUISHABLE —
|
|
18
|
+
* a transport 401 is now a typed RunnerTransportError, a rejected access token
|
|
19
|
+
* is evicted and re-minted, `invalid_grant` is separated from a token that
|
|
20
|
+
* merely expired. This one makes them VISIBLE, which is what turns "the user
|
|
21
|
+
* says it was broken yesterday" into a query.
|
|
22
|
+
*
|
|
23
|
+
* ## Why console, and why never console.log
|
|
24
|
+
*
|
|
25
|
+
* Workers Logs captures `console.*` and nothing else — there is no other sink
|
|
26
|
+
* available to a Worker without adding a dependency and a network hop to the
|
|
27
|
+
* request path.
|
|
28
|
+
*
|
|
29
|
+
* But this module also loads in the stdio servers (`useRemoteGogRunner` wires
|
|
30
|
+
* the same executor and the same token source into a plain Node process), and
|
|
31
|
+
* there STDOUT IS THE JSON-RPC CHANNEL. Node routes `console.log`, `.info`,
|
|
32
|
+
* `.debug` and `.trace` to fd 1, so any one of them would interleave a log line
|
|
33
|
+
* with the protocol frames and break the session. Only `.warn` and `.error` go
|
|
34
|
+
* to fd 2. That is the whole reason routine transitions are emitted at `warn`
|
|
35
|
+
* rather than at `info` where their severity belongs: `warn` is the least-severe
|
|
36
|
+
* console method that Node does not send down the wire. The record carries its
|
|
37
|
+
* own `event` name, so a log consumer classifies on that rather than on level.
|
|
38
|
+
*
|
|
39
|
+
* ## Why the whole line is redacted
|
|
40
|
+
*
|
|
41
|
+
* `reason` is built from text this layer did not author — gog's stderr, Google's
|
|
42
|
+
* error body, a rejected fetch's message — and any of those can quote a token
|
|
43
|
+
* verbatim. Redacting per-field would leave the next field someone adds
|
|
44
|
+
* unprotected, so the SERIALIZED record goes through the repo's existing
|
|
45
|
+
* `redactSecrets` (the shared mcp-utils redactor plus this repo's Google
|
|
46
|
+
* `ya29.…` / `1//…` shapes) as one string. A credential can therefore only
|
|
47
|
+
* appear in a log line if it survives the same redactor that guards every error
|
|
48
|
+
* this repo returns to a client.
|
|
49
|
+
*
|
|
50
|
+
* Credentials are never IDENTIFIED by value either: `credentialTag` derives the
|
|
51
|
+
* identifier from the SHA-256 that already keys the token cache, which is the
|
|
52
|
+
* hash-keying precedent google-token.ts set for exactly this reason.
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* What changed. Named after the transition rather than after the code site, so
|
|
57
|
+
* a query reads as a story about a credential rather than about a call stack.
|
|
58
|
+
*/
|
|
59
|
+
export type AuthTransition =
|
|
60
|
+
/** A fresh access token was obtained from the refresh token. */
|
|
61
|
+
| 'token.minted'
|
|
62
|
+
/** An unexpired cached access token was served without contacting Google. */
|
|
63
|
+
| 'token.cache-hit'
|
|
64
|
+
/** The exchange failed for a reason that is not a dead grant. */
|
|
65
|
+
| 'token.mint-failed'
|
|
66
|
+
/** A rejected access token was dropped, so the next read mints a new one. */
|
|
67
|
+
| 'token.evicted'
|
|
68
|
+
/** Nothing was dropped: the credential holds no token, or a different one. */
|
|
69
|
+
| 'token.evict-noop'
|
|
70
|
+
/** The REFRESH token is gone. Nothing can be minted; a human must re-enrol. */
|
|
71
|
+
| 'grant.dead'
|
|
72
|
+
/** Google refused our token, but this call is not one we may replay. */
|
|
73
|
+
| 'replay.declined'
|
|
74
|
+
/** Replaying the call once with a freshly minted token. */
|
|
75
|
+
| 'replay.attempted'
|
|
76
|
+
/** The replay succeeded — the caller saw no error at all. */
|
|
77
|
+
| 'replay.succeeded'
|
|
78
|
+
/** The replay failed too; the original failure reaches the caller. */
|
|
79
|
+
| 'replay.failed'
|
|
80
|
+
/** The RUNNER rejected our bearer. gog never ran; no Google credential read. */
|
|
81
|
+
| 'runner.auth-failed';
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Which credential, which service, which backend, and why — every field
|
|
85
|
+
* optional because each call site honestly knows a different subset. A
|
|
86
|
+
* transport 401 has no credential to name; a mint has no service.
|
|
87
|
+
*/
|
|
88
|
+
export interface AuthContext {
|
|
89
|
+
/** `credentialTag` of the hash that keys the token cache. Never a token. */
|
|
90
|
+
credential?: string;
|
|
91
|
+
/** The `gog` service word (gmail, drive, sheets…), when there is one. */
|
|
92
|
+
service?: string;
|
|
93
|
+
/** The gog-runner base URL. Configuration, not a secret. */
|
|
94
|
+
endpoint?: string;
|
|
95
|
+
/** Prose. Redacted with everything else — see the module note. */
|
|
96
|
+
reason?: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Transitions that describe something going WRONG, routed to `console.error`.
|
|
101
|
+
* Everything else is routine and goes to `console.warn` (see the module note on
|
|
102
|
+
* why not `console.info`).
|
|
103
|
+
*
|
|
104
|
+
* `token.evicted` is deliberately NOT here: an eviction is the repair working.
|
|
105
|
+
* `replay.declined` is not either — declining is usually the correct, safe
|
|
106
|
+
* answer (a write, a superseded token), and only reads as a failure alongside
|
|
107
|
+
* the record that follows it.
|
|
108
|
+
*/
|
|
109
|
+
const FAILURES: ReadonlySet<AuthTransition> = new Set<AuthTransition>([
|
|
110
|
+
'token.mint-failed',
|
|
111
|
+
'grant.dead',
|
|
112
|
+
'replay.failed',
|
|
113
|
+
'runner.auth-failed',
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
/** Marker so one `grep gog-auth` finds every record and nothing else. */
|
|
117
|
+
const PREFIX = 'gog-auth ';
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* How many hex characters of the cache key identify a credential in a log.
|
|
121
|
+
*
|
|
122
|
+
* 12 hex characters is 48 bits — far past collision risk for the handful of
|
|
123
|
+
* credentials one deployment holds, and short enough to read across lines. The
|
|
124
|
+
* input is already a SHA-256 of (clientId, refreshToken), so truncating it can
|
|
125
|
+
* only ever remove information.
|
|
126
|
+
*/
|
|
127
|
+
const TAG_CHARS = 12;
|
|
128
|
+
|
|
129
|
+
/** The log-safe name for a credential, from the hash that already keys it. */
|
|
130
|
+
export function credentialTag(cacheKeyHash: string): string {
|
|
131
|
+
return cacheKeyHash.slice(0, TAG_CHARS);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Emit one record. Deliberately synchronous, allocation-light and
|
|
136
|
+
* exception-free by construction: this sits on the request path, and an
|
|
137
|
+
* observability call that can fail or block would be worse than the silence it
|
|
138
|
+
* replaces.
|
|
139
|
+
*/
|
|
140
|
+
export function logAuthTransition(event: AuthTransition, context: AuthContext): void {
|
|
141
|
+
// `at` and `event` first so the line reads left to right; JSON.stringify
|
|
142
|
+
// drops the context keys whose value is undefined, which is why an absent
|
|
143
|
+
// credential produces no `"credential":null` noise.
|
|
144
|
+
const record = JSON.stringify({ at: new Date().toISOString(), event, ...context });
|
|
145
|
+
const write = FAILURES.has(event) ? console.error : console.warn;
|
|
146
|
+
write(PREFIX + redactSecrets(record));
|
|
147
|
+
}
|