@tinycloud/mcp 0.8.7-beta.2 → 0.8.7

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-cli.cjs CHANGED
@@ -137,12 +137,14 @@ function firstString(...values) {
137
137
  var import_node_crypto = require("crypto");
138
138
  var import_jose2 = require("jose");
139
139
  var ACTIVE_CACHE_MS = 3e4;
140
+ var UNKNOWN_TOLERANCE_MS = 6e4;
140
141
  var DEFAULT_TOKEN_LIFETIME_SECONDS = 300;
141
142
  var DEFAULT_TIMEOUT_MS = 3e3;
142
143
  var MAX_CACHED_TOKENS = 1e4;
143
144
  function createTokenStatusChecker(config) {
144
145
  const doFetch = config.fetch ?? fetch;
145
146
  const clock = config.now ?? Date.now;
147
+ const monotonic = config.monotonicNow ?? (config.now ?? (() => performance.now()));
146
148
  const log = config.log ?? ((message) => void process.stderr.write(`[tinycloud-mcp-http] ${message}
147
149
  `));
148
150
  const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
@@ -185,6 +187,7 @@ function createTokenStatusChecker(config) {
185
187
  }
186
188
  }
187
189
  async function query(key, token) {
190
+ const askedAt = monotonic();
188
191
  const outcome = await introspect(token);
189
192
  if (outcome === "credentials") {
190
193
  if (!credentialsRefused) {
@@ -195,7 +198,7 @@ function createTokenStatusChecker(config) {
195
198
  }
196
199
  if (outcome === "unknown") return outcome;
197
200
  const now = clock();
198
- prune(now);
201
+ prune(now, monotonic());
199
202
  if (outcome === "mismatch") {
200
203
  inactive.set(key, tokenExpiryMs(token, now));
201
204
  confirmedActive.delete(key);
@@ -206,16 +209,18 @@ function createTokenStatusChecker(config) {
206
209
  inactive.set(key, tokenExpiryMs(token, now));
207
210
  confirmedActive.delete(key);
208
211
  } else {
209
- active.set(key, now + ACTIVE_CACHE_MS);
212
+ active.set(key, askedAt + ACTIVE_CACHE_MS);
210
213
  confirmedActive.delete(key);
211
- confirmedActive.set(key, { at: now, expiresAt: tokenExpiryMs(token, now) });
214
+ confirmedActive.set(key, { at: askedAt, expiresAt: tokenExpiryMs(token, now) });
212
215
  }
213
216
  return outcome;
214
217
  }
215
- function prune(now) {
216
- for (const [key, until] of active) if (until <= now) active.delete(key);
218
+ function prune(now, elapsed) {
219
+ for (const [key, until] of active) if (until <= elapsed) active.delete(key);
217
220
  for (const [key, until] of inactive) if (until <= now) inactive.delete(key);
218
- for (const [key, entry] of confirmedActive) if (entry.expiresAt <= now) confirmedActive.delete(key);
221
+ for (const [key, entry] of confirmedActive) {
222
+ if (entry.expiresAt <= now || elapsed - entry.at >= UNKNOWN_TOLERANCE_MS) confirmedActive.delete(key);
223
+ }
219
224
  for (const cache of [active, inactive, confirmedActive]) {
220
225
  while (cache.size >= MAX_CACHED_TOKENS) {
221
226
  const oldest = cache.keys().next().value;
@@ -228,10 +233,11 @@ function createTokenStatusChecker(config) {
228
233
  async tokenStatus(token) {
229
234
  const key = (0, import_node_crypto.createHash)("sha256").update(token).digest("hex");
230
235
  const now = clock();
236
+ const elapsed = monotonic();
231
237
  const inactiveUntil = inactive.get(key);
232
238
  if (inactiveUntil !== void 0 && inactiveUntil > now) return "inactive";
233
239
  const activeUntil = active.get(key);
234
- if (activeUntil !== void 0 && activeUntil > now) return "active";
240
+ if (activeUntil !== void 0 && activeUntil > elapsed) return "active";
235
241
  const pending = inFlight.get(key);
236
242
  if (pending !== void 0) return pending;
237
243
  const asking = query(key, token).finally(() => inFlight.delete(key));
@@ -242,8 +248,7 @@ function createTokenStatusChecker(config) {
242
248
  if (credentialsRefused) return false;
243
249
  const confirmed = confirmedActive.get((0, import_node_crypto.createHash)("sha256").update(token).digest("hex"));
244
250
  if (confirmed === void 0) return false;
245
- const now = clock();
246
- return confirmed.expiresAt > now && now - confirmed.at < tokenLifetimeSeconds(token) * 1e3;
251
+ return confirmed.expiresAt > clock() && monotonic() - confirmed.at < UNKNOWN_TOLERANCE_MS;
247
252
  }
248
253
  };
249
254
  }
@@ -265,10 +270,6 @@ function tokenExpiryMs(token, now) {
265
270
  const exp = claims(token).exp;
266
271
  return typeof exp === "number" ? exp * 1e3 : now + DEFAULT_TOKEN_LIFETIME_SECONDS * 1e3;
267
272
  }
268
- function tokenLifetimeSeconds(token) {
269
- const { exp, iat } = claims(token);
270
- return typeof exp === "number" && typeof iat === "number" && exp > iat ? exp - iat : DEFAULT_TOKEN_LIFETIME_SECONDS;
271
- }
272
273
  function claims(token) {
273
274
  try {
274
275
  return (0, import_jose2.decodeJwt)(token);
@@ -1959,7 +1960,7 @@ function outcomeCode(result) {
1959
1960
  // package.json
1960
1961
  var package_default = {
1961
1962
  name: "@tinycloud/mcp",
1962
- version: "0.8.7-beta.2",
1963
+ version: "0.8.7",
1963
1964
  description: "TinyCloud delegated operations MCP server",
1964
1965
  keywords: [
1965
1966
  "tinycloud",
@@ -2030,9 +2031,9 @@ var package_default = {
2030
2031
  dependencies: {
2031
2032
  "@modelcontextprotocol/node": "2.0.0-beta.5",
2032
2033
  "@modelcontextprotocol/server": "2.0.0-beta.5",
2033
- "@tinycloud/node-sdk": "3.11.0-beta.2",
2034
+ "@tinycloud/node-sdk": "3.10.0",
2034
2035
  "@tinycloud/node-sdk-wasm": "1.7.8",
2035
- "@tinycloud/operations": "0.7.11-beta.2",
2036
+ "@tinycloud/operations": "0.7.10",
2036
2037
  jose: "6.2.4"
2037
2038
  },
2038
2039
  devDependencies: {
@@ -2056,6 +2057,7 @@ var MCP_SERVER_NAME = "tinycloud-mcp";
2056
2057
 
2057
2058
  // src/http.ts
2058
2059
  var REQUIRED_SCOPE = "tinycloud:mcp";
2060
+ var MCP_CORS_ALLOWED_HEADERS = "Authorization, Content-Type, Accept, Mcp-Protocol-Version, Mcp-Session-Id, Last-Event-ID, Mcp-Method, Mcp-Name";
2059
2061
  var LOCAL_CLI_PACKAGE = ["@tinycloud", "cli"].join("/");
2060
2062
  var LOCAL_MCP_PACKAGE = ["@tinycloud", "mcp"].join("/");
2061
2063
  function createHostedMcpApp(config) {
@@ -2376,8 +2378,8 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2376
2378
  if (url.pathname === callbackPath) {
2377
2379
  if (request.method === "OPTIONS") return callbackCors(new Response(null, { status: 204 }), config.openkeyHost);
2378
2380
  if (request.method !== "POST") return methodNotAllowed("POST");
2379
- const originFailure2 = validateCallbackOrigin(request, config.openkeyHost);
2380
- if (originFailure2 !== void 0) return callbackCors(originFailure2, config.openkeyHost);
2381
+ const originFailure = validateCallbackOrigin(request, config.openkeyHost);
2382
+ if (originFailure !== void 0) return callbackCors(originFailure, config.openkeyHost);
2381
2383
  try {
2382
2384
  await store.completeApproval(requiredState(url), request);
2383
2385
  return callbackCors(json({ success: true }), config.openkeyHost);
@@ -2471,11 +2473,9 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2471
2473
  });
2472
2474
  }
2473
2475
  if (url.pathname !== resourceUrl.pathname) return json({ error: "Not found" }, 404);
2474
- const originFailure = validateOrigin(request, config.allowedOrigins);
2475
- if (originFailure !== void 0) return originFailure;
2476
+ if (request.method === "OPTIONS") return mcpCors(new Response(null, { status: 204 }), request);
2476
2477
  const auth = await authenticate(request);
2477
- if (auth instanceof Response) return auth;
2478
- return handler.fetch(request, { authInfo: auth });
2478
+ return mcpCors(auth instanceof Response ? auth : await handler.fetch(request, { authInfo: auth }), request);
2479
2479
  },
2480
2480
  close: () => handler.close()
2481
2481
  };
@@ -2563,11 +2563,16 @@ function requiredState(url) {
2563
2563
  if (state.length > 512) throw new ApprovalRequestError("state_invalid", "Approval state is invalid.");
2564
2564
  return state;
2565
2565
  }
2566
- function validateOrigin(request, allowed) {
2567
- const origin = request.headers.get("origin");
2568
- if (origin === null) return void 0;
2569
- const allowlist = allowed ?? [];
2570
- return allowlist.includes(origin) ? void 0 : json({ error: "Origin is not allowed." }, 403);
2566
+ function mcpCors(response, request) {
2567
+ const headers = new Headers(response.headers);
2568
+ headers.append("vary", "Origin");
2569
+ if (request.headers.has("origin")) {
2570
+ headers.set("access-control-allow-origin", "*");
2571
+ headers.set("access-control-allow-methods", "GET, POST, DELETE, OPTIONS");
2572
+ headers.set("access-control-expose-headers", "WWW-Authenticate");
2573
+ if (request.method === "OPTIONS") headers.set("access-control-allow-headers", MCP_CORS_ALLOWED_HEADERS);
2574
+ }
2575
+ return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
2571
2576
  }
2572
2577
  function validateCallbackOrigin(request, openkeyHost) {
2573
2578
  const origin = request.headers.get("origin");
@@ -2680,7 +2685,6 @@ async function main() {
2680
2685
  openkeyHost,
2681
2686
  approvalTtlSeconds: positiveInteger(process.env.TC_MCP_APPROVAL_TTL_SECONDS, 300),
2682
2687
  delegationExpiry: process.env.TC_MCP_DELEGATION_EXPIRY ?? "1h",
2683
- allowedOrigins: commaList(process.env.TC_MCP_ALLOWED_ORIGINS),
2684
2688
  ...process.env.TC_MCP_NATIVE_SERVICE_CREDENTIAL === void 0 ? {} : { nativeServiceCredential: process.env.TC_MCP_NATIVE_SERVICE_CREDENTIAL },
2685
2689
  oauthMetadata,
2686
2690
  introspection: {
@@ -2732,10 +2736,6 @@ function positiveInteger(value, fallback) {
2732
2736
  if (!Number.isInteger(parsed) || parsed <= 0) throw new Error("Expected a positive integer environment value.");
2733
2737
  return parsed;
2734
2738
  }
2735
- function commaList(value) {
2736
- if (value === void 0) return void 0;
2737
- return value.split(",").map((entry) => entry.trim()).filter(Boolean);
2738
- }
2739
2739
  void main().catch(() => {
2740
2740
  process.stderr.write("[tinycloud-mcp-http] Startup failed.\n");
2741
2741
  process.exitCode = 1;