@tinycloud/mcp 0.8.7-beta.2 → 0.9.0-beta.4

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/http.cjs CHANGED
@@ -120,12 +120,14 @@ function firstString(...values) {
120
120
  var import_node_crypto = require("crypto");
121
121
  var import_jose2 = require("jose");
122
122
  var ACTIVE_CACHE_MS = 3e4;
123
+ var UNKNOWN_TOLERANCE_MS = 6e4;
123
124
  var DEFAULT_TOKEN_LIFETIME_SECONDS = 300;
124
125
  var DEFAULT_TIMEOUT_MS = 3e3;
125
126
  var MAX_CACHED_TOKENS = 1e4;
126
127
  function createTokenStatusChecker(config) {
127
128
  const doFetch = config.fetch ?? fetch;
128
129
  const clock = config.now ?? Date.now;
130
+ const monotonic = config.monotonicNow ?? (config.now ?? (() => performance.now()));
129
131
  const log = config.log ?? ((message) => void process.stderr.write(`[tinycloud-mcp-http] ${message}
130
132
  `));
131
133
  const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
@@ -168,6 +170,7 @@ function createTokenStatusChecker(config) {
168
170
  }
169
171
  }
170
172
  async function query(key, token) {
173
+ const askedAt = monotonic();
171
174
  const outcome = await introspect(token);
172
175
  if (outcome === "credentials") {
173
176
  if (!credentialsRefused) {
@@ -178,7 +181,7 @@ function createTokenStatusChecker(config) {
178
181
  }
179
182
  if (outcome === "unknown") return outcome;
180
183
  const now = clock();
181
- prune(now);
184
+ prune(now, monotonic());
182
185
  if (outcome === "mismatch") {
183
186
  inactive.set(key, tokenExpiryMs(token, now));
184
187
  confirmedActive.delete(key);
@@ -189,16 +192,18 @@ function createTokenStatusChecker(config) {
189
192
  inactive.set(key, tokenExpiryMs(token, now));
190
193
  confirmedActive.delete(key);
191
194
  } else {
192
- active.set(key, now + ACTIVE_CACHE_MS);
195
+ active.set(key, askedAt + ACTIVE_CACHE_MS);
193
196
  confirmedActive.delete(key);
194
- confirmedActive.set(key, { at: now, expiresAt: tokenExpiryMs(token, now) });
197
+ confirmedActive.set(key, { at: askedAt, expiresAt: tokenExpiryMs(token, now) });
195
198
  }
196
199
  return outcome;
197
200
  }
198
- function prune(now) {
199
- for (const [key, until] of active) if (until <= now) active.delete(key);
201
+ function prune(now, elapsed) {
202
+ for (const [key, until] of active) if (until <= elapsed) active.delete(key);
200
203
  for (const [key, until] of inactive) if (until <= now) inactive.delete(key);
201
- for (const [key, entry] of confirmedActive) if (entry.expiresAt <= now) confirmedActive.delete(key);
204
+ for (const [key, entry] of confirmedActive) {
205
+ if (entry.expiresAt <= now || elapsed - entry.at >= UNKNOWN_TOLERANCE_MS) confirmedActive.delete(key);
206
+ }
202
207
  for (const cache of [active, inactive, confirmedActive]) {
203
208
  while (cache.size >= MAX_CACHED_TOKENS) {
204
209
  const oldest = cache.keys().next().value;
@@ -211,10 +216,11 @@ function createTokenStatusChecker(config) {
211
216
  async tokenStatus(token) {
212
217
  const key = (0, import_node_crypto.createHash)("sha256").update(token).digest("hex");
213
218
  const now = clock();
219
+ const elapsed = monotonic();
214
220
  const inactiveUntil = inactive.get(key);
215
221
  if (inactiveUntil !== void 0 && inactiveUntil > now) return "inactive";
216
222
  const activeUntil = active.get(key);
217
- if (activeUntil !== void 0 && activeUntil > now) return "active";
223
+ if (activeUntil !== void 0 && activeUntil > elapsed) return "active";
218
224
  const pending = inFlight.get(key);
219
225
  if (pending !== void 0) return pending;
220
226
  const asking = query(key, token).finally(() => inFlight.delete(key));
@@ -225,8 +231,7 @@ function createTokenStatusChecker(config) {
225
231
  if (credentialsRefused) return false;
226
232
  const confirmed = confirmedActive.get((0, import_node_crypto.createHash)("sha256").update(token).digest("hex"));
227
233
  if (confirmed === void 0) return false;
228
- const now = clock();
229
- return confirmed.expiresAt > now && now - confirmed.at < tokenLifetimeSeconds(token) * 1e3;
234
+ return confirmed.expiresAt > clock() && monotonic() - confirmed.at < UNKNOWN_TOLERANCE_MS;
230
235
  }
231
236
  };
232
237
  }
@@ -248,10 +253,6 @@ function tokenExpiryMs(token, now) {
248
253
  const exp = claims(token).exp;
249
254
  return typeof exp === "number" ? exp * 1e3 : now + DEFAULT_TOKEN_LIFETIME_SECONDS * 1e3;
250
255
  }
251
- function tokenLifetimeSeconds(token) {
252
- const { exp, iat } = claims(token);
253
- return typeof exp === "number" && typeof iat === "number" && exp > iat ? exp - iat : DEFAULT_TOKEN_LIFETIME_SECONDS;
254
- }
255
256
  function claims(token) {
256
257
  try {
257
258
  return (0, import_jose2.decodeJwt)(token);
@@ -1942,7 +1943,7 @@ function outcomeCode(result) {
1942
1943
  // package.json
1943
1944
  var package_default = {
1944
1945
  name: "@tinycloud/mcp",
1945
- version: "0.8.7-beta.2",
1946
+ version: "0.9.0-beta.4",
1946
1947
  description: "TinyCloud delegated operations MCP server",
1947
1948
  keywords: [
1948
1949
  "tinycloud",
@@ -2039,6 +2040,7 @@ var MCP_SERVER_NAME = "tinycloud-mcp";
2039
2040
 
2040
2041
  // src/http.ts
2041
2042
  var REQUIRED_SCOPE = "tinycloud:mcp";
2043
+ var MCP_CORS_ALLOWED_HEADERS = "Authorization, Content-Type, Accept, Mcp-Protocol-Version, Mcp-Session-Id, Last-Event-ID, Mcp-Method, Mcp-Name";
2042
2044
  var LOCAL_CLI_PACKAGE = ["@tinycloud", "cli"].join("/");
2043
2045
  var LOCAL_MCP_PACKAGE = ["@tinycloud", "mcp"].join("/");
2044
2046
  function createHostedMcpApp(config) {
@@ -2359,8 +2361,8 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2359
2361
  if (url.pathname === callbackPath) {
2360
2362
  if (request.method === "OPTIONS") return callbackCors(new Response(null, { status: 204 }), config.openkeyHost);
2361
2363
  if (request.method !== "POST") return methodNotAllowed("POST");
2362
- const originFailure2 = validateCallbackOrigin(request, config.openkeyHost);
2363
- if (originFailure2 !== void 0) return callbackCors(originFailure2, config.openkeyHost);
2364
+ const originFailure = validateCallbackOrigin(request, config.openkeyHost);
2365
+ if (originFailure !== void 0) return callbackCors(originFailure, config.openkeyHost);
2364
2366
  try {
2365
2367
  await store.completeApproval(requiredState(url), request);
2366
2368
  return callbackCors(json({ success: true }), config.openkeyHost);
@@ -2454,11 +2456,9 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2454
2456
  });
2455
2457
  }
2456
2458
  if (url.pathname !== resourceUrl.pathname) return json({ error: "Not found" }, 404);
2457
- const originFailure = validateOrigin(request, config.allowedOrigins);
2458
- if (originFailure !== void 0) return originFailure;
2459
+ if (request.method === "OPTIONS") return mcpCors(new Response(null, { status: 204 }), request);
2459
2460
  const auth = await authenticate(request);
2460
- if (auth instanceof Response) return auth;
2461
- return handler.fetch(request, { authInfo: auth });
2461
+ return mcpCors(auth instanceof Response ? auth : await handler.fetch(request, { authInfo: auth }), request);
2462
2462
  },
2463
2463
  close: () => handler.close()
2464
2464
  };
@@ -2546,11 +2546,16 @@ function requiredState(url) {
2546
2546
  if (state.length > 512) throw new ApprovalRequestError("state_invalid", "Approval state is invalid.");
2547
2547
  return state;
2548
2548
  }
2549
- function validateOrigin(request, allowed) {
2550
- const origin = request.headers.get("origin");
2551
- if (origin === null) return void 0;
2552
- const allowlist = allowed ?? [];
2553
- return allowlist.includes(origin) ? void 0 : json({ error: "Origin is not allowed." }, 403);
2549
+ function mcpCors(response, request) {
2550
+ const headers = new Headers(response.headers);
2551
+ headers.append("vary", "Origin");
2552
+ if (request.headers.has("origin")) {
2553
+ headers.set("access-control-allow-origin", "*");
2554
+ headers.set("access-control-allow-methods", "GET, POST, DELETE, OPTIONS");
2555
+ headers.set("access-control-expose-headers", "WWW-Authenticate");
2556
+ if (request.method === "OPTIONS") headers.set("access-control-allow-headers", MCP_CORS_ALLOWED_HEADERS);
2557
+ }
2558
+ return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
2554
2559
  }
2555
2560
  function validateCallbackOrigin(request, openkeyHost) {
2556
2561
  const origin = request.headers.get("origin");