@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/index.js CHANGED
@@ -485,7 +485,7 @@ function outcomeCode(result) {
485
485
  // package.json
486
486
  var package_default = {
487
487
  name: "@tinycloud/mcp",
488
- version: "0.8.7-beta.2",
488
+ version: "0.9.0-beta.4",
489
489
  description: "TinyCloud delegated operations MCP server",
490
490
  keywords: [
491
491
  "tinycloud",
@@ -805,12 +805,14 @@ function firstString(...values) {
805
805
  import { createHash } from "crypto";
806
806
  import { decodeJwt } from "jose";
807
807
  var ACTIVE_CACHE_MS = 3e4;
808
+ var UNKNOWN_TOLERANCE_MS = 6e4;
808
809
  var DEFAULT_TOKEN_LIFETIME_SECONDS = 300;
809
810
  var DEFAULT_TIMEOUT_MS = 3e3;
810
811
  var MAX_CACHED_TOKENS = 1e4;
811
812
  function createTokenStatusChecker(config) {
812
813
  const doFetch = config.fetch ?? fetch;
813
814
  const clock = config.now ?? Date.now;
815
+ const monotonic = config.monotonicNow ?? (config.now ?? (() => performance.now()));
814
816
  const log = config.log ?? ((message) => void process.stderr.write(`[tinycloud-mcp-http] ${message}
815
817
  `));
816
818
  const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
@@ -853,6 +855,7 @@ function createTokenStatusChecker(config) {
853
855
  }
854
856
  }
855
857
  async function query(key, token) {
858
+ const askedAt = monotonic();
856
859
  const outcome = await introspect(token);
857
860
  if (outcome === "credentials") {
858
861
  if (!credentialsRefused) {
@@ -863,7 +866,7 @@ function createTokenStatusChecker(config) {
863
866
  }
864
867
  if (outcome === "unknown") return outcome;
865
868
  const now = clock();
866
- prune(now);
869
+ prune(now, monotonic());
867
870
  if (outcome === "mismatch") {
868
871
  inactive.set(key, tokenExpiryMs(token, now));
869
872
  confirmedActive.delete(key);
@@ -874,16 +877,18 @@ function createTokenStatusChecker(config) {
874
877
  inactive.set(key, tokenExpiryMs(token, now));
875
878
  confirmedActive.delete(key);
876
879
  } else {
877
- active.set(key, now + ACTIVE_CACHE_MS);
880
+ active.set(key, askedAt + ACTIVE_CACHE_MS);
878
881
  confirmedActive.delete(key);
879
- confirmedActive.set(key, { at: now, expiresAt: tokenExpiryMs(token, now) });
882
+ confirmedActive.set(key, { at: askedAt, expiresAt: tokenExpiryMs(token, now) });
880
883
  }
881
884
  return outcome;
882
885
  }
883
- function prune(now) {
884
- for (const [key, until] of active) if (until <= now) active.delete(key);
886
+ function prune(now, elapsed) {
887
+ for (const [key, until] of active) if (until <= elapsed) active.delete(key);
885
888
  for (const [key, until] of inactive) if (until <= now) inactive.delete(key);
886
- for (const [key, entry] of confirmedActive) if (entry.expiresAt <= now) confirmedActive.delete(key);
889
+ for (const [key, entry] of confirmedActive) {
890
+ if (entry.expiresAt <= now || elapsed - entry.at >= UNKNOWN_TOLERANCE_MS) confirmedActive.delete(key);
891
+ }
887
892
  for (const cache of [active, inactive, confirmedActive]) {
888
893
  while (cache.size >= MAX_CACHED_TOKENS) {
889
894
  const oldest = cache.keys().next().value;
@@ -896,10 +901,11 @@ function createTokenStatusChecker(config) {
896
901
  async tokenStatus(token) {
897
902
  const key = createHash("sha256").update(token).digest("hex");
898
903
  const now = clock();
904
+ const elapsed = monotonic();
899
905
  const inactiveUntil = inactive.get(key);
900
906
  if (inactiveUntil !== void 0 && inactiveUntil > now) return "inactive";
901
907
  const activeUntil = active.get(key);
902
- if (activeUntil !== void 0 && activeUntil > now) return "active";
908
+ if (activeUntil !== void 0 && activeUntil > elapsed) return "active";
903
909
  const pending = inFlight.get(key);
904
910
  if (pending !== void 0) return pending;
905
911
  const asking = query(key, token).finally(() => inFlight.delete(key));
@@ -910,8 +916,7 @@ function createTokenStatusChecker(config) {
910
916
  if (credentialsRefused) return false;
911
917
  const confirmed = confirmedActive.get(createHash("sha256").update(token).digest("hex"));
912
918
  if (confirmed === void 0) return false;
913
- const now = clock();
914
- return confirmed.expiresAt > now && now - confirmed.at < tokenLifetimeSeconds(token) * 1e3;
919
+ return confirmed.expiresAt > clock() && monotonic() - confirmed.at < UNKNOWN_TOLERANCE_MS;
915
920
  }
916
921
  };
917
922
  }
@@ -933,10 +938,6 @@ function tokenExpiryMs(token, now) {
933
938
  const exp = claims(token).exp;
934
939
  return typeof exp === "number" ? exp * 1e3 : now + DEFAULT_TOKEN_LIFETIME_SECONDS * 1e3;
935
940
  }
936
- function tokenLifetimeSeconds(token) {
937
- const { exp, iat } = claims(token);
938
- return typeof exp === "number" && typeof iat === "number" && exp > iat ? exp - iat : DEFAULT_TOKEN_LIFETIME_SECONDS;
939
- }
940
941
  function claims(token) {
941
942
  try {
942
943
  return decodeJwt(token);
@@ -2185,6 +2186,7 @@ function samePendingRequest(pending, expected) {
2185
2186
 
2186
2187
  // src/http.ts
2187
2188
  var REQUIRED_SCOPE = "tinycloud:mcp";
2189
+ var MCP_CORS_ALLOWED_HEADERS = "Authorization, Content-Type, Accept, Mcp-Protocol-Version, Mcp-Session-Id, Last-Event-ID, Mcp-Method, Mcp-Name";
2188
2190
  var LOCAL_CLI_PACKAGE = ["@tinycloud", "cli"].join("/");
2189
2191
  var LOCAL_MCP_PACKAGE = ["@tinycloud", "mcp"].join("/");
2190
2192
  function createHostedMcpApp(config) {
@@ -2505,8 +2507,8 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2505
2507
  if (url.pathname === callbackPath) {
2506
2508
  if (request.method === "OPTIONS") return callbackCors(new Response(null, { status: 204 }), config.openkeyHost);
2507
2509
  if (request.method !== "POST") return methodNotAllowed("POST");
2508
- const originFailure2 = validateCallbackOrigin(request, config.openkeyHost);
2509
- if (originFailure2 !== void 0) return callbackCors(originFailure2, config.openkeyHost);
2510
+ const originFailure = validateCallbackOrigin(request, config.openkeyHost);
2511
+ if (originFailure !== void 0) return callbackCors(originFailure, config.openkeyHost);
2510
2512
  try {
2511
2513
  await store.completeApproval(requiredState(url), request);
2512
2514
  return callbackCors(json({ success: true }), config.openkeyHost);
@@ -2600,11 +2602,9 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2600
2602
  });
2601
2603
  }
2602
2604
  if (url.pathname !== resourceUrl.pathname) return json({ error: "Not found" }, 404);
2603
- const originFailure = validateOrigin(request, config.allowedOrigins);
2604
- if (originFailure !== void 0) return originFailure;
2605
+ if (request.method === "OPTIONS") return mcpCors(new Response(null, { status: 204 }), request);
2605
2606
  const auth = await authenticate(request);
2606
- if (auth instanceof Response) return auth;
2607
- return handler.fetch(request, { authInfo: auth });
2607
+ return mcpCors(auth instanceof Response ? auth : await handler.fetch(request, { authInfo: auth }), request);
2608
2608
  },
2609
2609
  close: () => handler.close()
2610
2610
  };
@@ -2692,11 +2692,16 @@ function requiredState(url) {
2692
2692
  if (state.length > 512) throw new ApprovalRequestError("state_invalid", "Approval state is invalid.");
2693
2693
  return state;
2694
2694
  }
2695
- function validateOrigin(request, allowed) {
2696
- const origin = request.headers.get("origin");
2697
- if (origin === null) return void 0;
2698
- const allowlist = allowed ?? [];
2699
- return allowlist.includes(origin) ? void 0 : json({ error: "Origin is not allowed." }, 403);
2695
+ function mcpCors(response, request) {
2696
+ const headers = new Headers(response.headers);
2697
+ headers.append("vary", "Origin");
2698
+ if (request.headers.has("origin")) {
2699
+ headers.set("access-control-allow-origin", "*");
2700
+ headers.set("access-control-allow-methods", "GET, POST, DELETE, OPTIONS");
2701
+ headers.set("access-control-expose-headers", "WWW-Authenticate");
2702
+ if (request.method === "OPTIONS") headers.set("access-control-allow-headers", MCP_CORS_ALLOWED_HEADERS);
2703
+ }
2704
+ return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
2700
2705
  }
2701
2706
  function validateCallbackOrigin(request, openkeyHost) {
2702
2707
  const origin = request.headers.get("origin");