@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.cjs CHANGED
@@ -522,7 +522,7 @@ function outcomeCode(result) {
522
522
  // package.json
523
523
  var package_default = {
524
524
  name: "@tinycloud/mcp",
525
- version: "0.8.7-beta.2",
525
+ version: "0.9.0-beta.4",
526
526
  description: "TinyCloud delegated operations MCP server",
527
527
  keywords: [
528
528
  "tinycloud",
@@ -831,12 +831,14 @@ function firstString(...values) {
831
831
  var import_node_crypto = require("crypto");
832
832
  var import_jose2 = require("jose");
833
833
  var ACTIVE_CACHE_MS = 3e4;
834
+ var UNKNOWN_TOLERANCE_MS = 6e4;
834
835
  var DEFAULT_TOKEN_LIFETIME_SECONDS = 300;
835
836
  var DEFAULT_TIMEOUT_MS = 3e3;
836
837
  var MAX_CACHED_TOKENS = 1e4;
837
838
  function createTokenStatusChecker(config) {
838
839
  const doFetch = config.fetch ?? fetch;
839
840
  const clock = config.now ?? Date.now;
841
+ const monotonic = config.monotonicNow ?? (config.now ?? (() => performance.now()));
840
842
  const log = config.log ?? ((message) => void process.stderr.write(`[tinycloud-mcp-http] ${message}
841
843
  `));
842
844
  const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
@@ -879,6 +881,7 @@ function createTokenStatusChecker(config) {
879
881
  }
880
882
  }
881
883
  async function query(key, token) {
884
+ const askedAt = monotonic();
882
885
  const outcome = await introspect(token);
883
886
  if (outcome === "credentials") {
884
887
  if (!credentialsRefused) {
@@ -889,7 +892,7 @@ function createTokenStatusChecker(config) {
889
892
  }
890
893
  if (outcome === "unknown") return outcome;
891
894
  const now = clock();
892
- prune(now);
895
+ prune(now, monotonic());
893
896
  if (outcome === "mismatch") {
894
897
  inactive.set(key, tokenExpiryMs(token, now));
895
898
  confirmedActive.delete(key);
@@ -900,16 +903,18 @@ function createTokenStatusChecker(config) {
900
903
  inactive.set(key, tokenExpiryMs(token, now));
901
904
  confirmedActive.delete(key);
902
905
  } else {
903
- active.set(key, now + ACTIVE_CACHE_MS);
906
+ active.set(key, askedAt + ACTIVE_CACHE_MS);
904
907
  confirmedActive.delete(key);
905
- confirmedActive.set(key, { at: now, expiresAt: tokenExpiryMs(token, now) });
908
+ confirmedActive.set(key, { at: askedAt, expiresAt: tokenExpiryMs(token, now) });
906
909
  }
907
910
  return outcome;
908
911
  }
909
- function prune(now) {
910
- for (const [key, until] of active) if (until <= now) active.delete(key);
912
+ function prune(now, elapsed) {
913
+ for (const [key, until] of active) if (until <= elapsed) active.delete(key);
911
914
  for (const [key, until] of inactive) if (until <= now) inactive.delete(key);
912
- for (const [key, entry] of confirmedActive) if (entry.expiresAt <= now) confirmedActive.delete(key);
915
+ for (const [key, entry] of confirmedActive) {
916
+ if (entry.expiresAt <= now || elapsed - entry.at >= UNKNOWN_TOLERANCE_MS) confirmedActive.delete(key);
917
+ }
913
918
  for (const cache of [active, inactive, confirmedActive]) {
914
919
  while (cache.size >= MAX_CACHED_TOKENS) {
915
920
  const oldest = cache.keys().next().value;
@@ -922,10 +927,11 @@ function createTokenStatusChecker(config) {
922
927
  async tokenStatus(token) {
923
928
  const key = (0, import_node_crypto.createHash)("sha256").update(token).digest("hex");
924
929
  const now = clock();
930
+ const elapsed = monotonic();
925
931
  const inactiveUntil = inactive.get(key);
926
932
  if (inactiveUntil !== void 0 && inactiveUntil > now) return "inactive";
927
933
  const activeUntil = active.get(key);
928
- if (activeUntil !== void 0 && activeUntil > now) return "active";
934
+ if (activeUntil !== void 0 && activeUntil > elapsed) return "active";
929
935
  const pending = inFlight.get(key);
930
936
  if (pending !== void 0) return pending;
931
937
  const asking = query(key, token).finally(() => inFlight.delete(key));
@@ -936,8 +942,7 @@ function createTokenStatusChecker(config) {
936
942
  if (credentialsRefused) return false;
937
943
  const confirmed = confirmedActive.get((0, import_node_crypto.createHash)("sha256").update(token).digest("hex"));
938
944
  if (confirmed === void 0) return false;
939
- const now = clock();
940
- return confirmed.expiresAt > now && now - confirmed.at < tokenLifetimeSeconds(token) * 1e3;
945
+ return confirmed.expiresAt > clock() && monotonic() - confirmed.at < UNKNOWN_TOLERANCE_MS;
941
946
  }
942
947
  };
943
948
  }
@@ -959,10 +964,6 @@ function tokenExpiryMs(token, now) {
959
964
  const exp = claims(token).exp;
960
965
  return typeof exp === "number" ? exp * 1e3 : now + DEFAULT_TOKEN_LIFETIME_SECONDS * 1e3;
961
966
  }
962
- function tokenLifetimeSeconds(token) {
963
- const { exp, iat } = claims(token);
964
- return typeof exp === "number" && typeof iat === "number" && exp > iat ? exp - iat : DEFAULT_TOKEN_LIFETIME_SECONDS;
965
- }
966
967
  function claims(token) {
967
968
  try {
968
969
  return (0, import_jose2.decodeJwt)(token);
@@ -2199,6 +2200,7 @@ function samePendingRequest(pending, expected) {
2199
2200
 
2200
2201
  // src/http.ts
2201
2202
  var REQUIRED_SCOPE = "tinycloud:mcp";
2203
+ var MCP_CORS_ALLOWED_HEADERS = "Authorization, Content-Type, Accept, Mcp-Protocol-Version, Mcp-Session-Id, Last-Event-ID, Mcp-Method, Mcp-Name";
2202
2204
  var LOCAL_CLI_PACKAGE = ["@tinycloud", "cli"].join("/");
2203
2205
  var LOCAL_MCP_PACKAGE = ["@tinycloud", "mcp"].join("/");
2204
2206
  function createHostedMcpApp(config) {
@@ -2519,8 +2521,8 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2519
2521
  if (url.pathname === callbackPath) {
2520
2522
  if (request.method === "OPTIONS") return callbackCors(new Response(null, { status: 204 }), config.openkeyHost);
2521
2523
  if (request.method !== "POST") return methodNotAllowed("POST");
2522
- const originFailure2 = validateCallbackOrigin(request, config.openkeyHost);
2523
- if (originFailure2 !== void 0) return callbackCors(originFailure2, config.openkeyHost);
2524
+ const originFailure = validateCallbackOrigin(request, config.openkeyHost);
2525
+ if (originFailure !== void 0) return callbackCors(originFailure, config.openkeyHost);
2524
2526
  try {
2525
2527
  await store.completeApproval(requiredState(url), request);
2526
2528
  return callbackCors(json({ success: true }), config.openkeyHost);
@@ -2614,11 +2616,9 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2614
2616
  });
2615
2617
  }
2616
2618
  if (url.pathname !== resourceUrl.pathname) return json({ error: "Not found" }, 404);
2617
- const originFailure = validateOrigin(request, config.allowedOrigins);
2618
- if (originFailure !== void 0) return originFailure;
2619
+ if (request.method === "OPTIONS") return mcpCors(new Response(null, { status: 204 }), request);
2619
2620
  const auth = await authenticate(request);
2620
- if (auth instanceof Response) return auth;
2621
- return handler.fetch(request, { authInfo: auth });
2621
+ return mcpCors(auth instanceof Response ? auth : await handler.fetch(request, { authInfo: auth }), request);
2622
2622
  },
2623
2623
  close: () => handler.close()
2624
2624
  };
@@ -2706,11 +2706,16 @@ function requiredState(url) {
2706
2706
  if (state.length > 512) throw new ApprovalRequestError("state_invalid", "Approval state is invalid.");
2707
2707
  return state;
2708
2708
  }
2709
- function validateOrigin(request, allowed) {
2710
- const origin = request.headers.get("origin");
2711
- if (origin === null) return void 0;
2712
- const allowlist = allowed ?? [];
2713
- return allowlist.includes(origin) ? void 0 : json({ error: "Origin is not allowed." }, 403);
2709
+ function mcpCors(response, request) {
2710
+ const headers = new Headers(response.headers);
2711
+ headers.append("vary", "Origin");
2712
+ if (request.headers.has("origin")) {
2713
+ headers.set("access-control-allow-origin", "*");
2714
+ headers.set("access-control-allow-methods", "GET, POST, DELETE, OPTIONS");
2715
+ headers.set("access-control-expose-headers", "WWW-Authenticate");
2716
+ if (request.method === "OPTIONS") headers.set("access-control-allow-headers", MCP_CORS_ALLOWED_HEADERS);
2717
+ }
2718
+ return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
2714
2719
  }
2715
2720
  function validateCallbackOrigin(request, openkeyHost) {
2716
2721
  const origin = request.headers.get("origin");