@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-cli.js CHANGED
@@ -124,12 +124,14 @@ function firstString(...values) {
124
124
  import { createHash } from "crypto";
125
125
  import { decodeJwt } from "jose";
126
126
  var ACTIVE_CACHE_MS = 3e4;
127
+ var UNKNOWN_TOLERANCE_MS = 6e4;
127
128
  var DEFAULT_TOKEN_LIFETIME_SECONDS = 300;
128
129
  var DEFAULT_TIMEOUT_MS = 3e3;
129
130
  var MAX_CACHED_TOKENS = 1e4;
130
131
  function createTokenStatusChecker(config) {
131
132
  const doFetch = config.fetch ?? fetch;
132
133
  const clock = config.now ?? Date.now;
134
+ const monotonic = config.monotonicNow ?? (config.now ?? (() => performance.now()));
133
135
  const log = config.log ?? ((message) => void process.stderr.write(`[tinycloud-mcp-http] ${message}
134
136
  `));
135
137
  const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
@@ -172,6 +174,7 @@ function createTokenStatusChecker(config) {
172
174
  }
173
175
  }
174
176
  async function query(key, token) {
177
+ const askedAt = monotonic();
175
178
  const outcome = await introspect(token);
176
179
  if (outcome === "credentials") {
177
180
  if (!credentialsRefused) {
@@ -182,7 +185,7 @@ function createTokenStatusChecker(config) {
182
185
  }
183
186
  if (outcome === "unknown") return outcome;
184
187
  const now = clock();
185
- prune(now);
188
+ prune(now, monotonic());
186
189
  if (outcome === "mismatch") {
187
190
  inactive.set(key, tokenExpiryMs(token, now));
188
191
  confirmedActive.delete(key);
@@ -193,16 +196,18 @@ function createTokenStatusChecker(config) {
193
196
  inactive.set(key, tokenExpiryMs(token, now));
194
197
  confirmedActive.delete(key);
195
198
  } else {
196
- active.set(key, now + ACTIVE_CACHE_MS);
199
+ active.set(key, askedAt + ACTIVE_CACHE_MS);
197
200
  confirmedActive.delete(key);
198
- confirmedActive.set(key, { at: now, expiresAt: tokenExpiryMs(token, now) });
201
+ confirmedActive.set(key, { at: askedAt, expiresAt: tokenExpiryMs(token, now) });
199
202
  }
200
203
  return outcome;
201
204
  }
202
- function prune(now) {
203
- for (const [key, until] of active) if (until <= now) active.delete(key);
205
+ function prune(now, elapsed) {
206
+ for (const [key, until] of active) if (until <= elapsed) active.delete(key);
204
207
  for (const [key, until] of inactive) if (until <= now) inactive.delete(key);
205
- for (const [key, entry] of confirmedActive) if (entry.expiresAt <= now) confirmedActive.delete(key);
208
+ for (const [key, entry] of confirmedActive) {
209
+ if (entry.expiresAt <= now || elapsed - entry.at >= UNKNOWN_TOLERANCE_MS) confirmedActive.delete(key);
210
+ }
206
211
  for (const cache of [active, inactive, confirmedActive]) {
207
212
  while (cache.size >= MAX_CACHED_TOKENS) {
208
213
  const oldest = cache.keys().next().value;
@@ -215,10 +220,11 @@ function createTokenStatusChecker(config) {
215
220
  async tokenStatus(token) {
216
221
  const key = createHash("sha256").update(token).digest("hex");
217
222
  const now = clock();
223
+ const elapsed = monotonic();
218
224
  const inactiveUntil = inactive.get(key);
219
225
  if (inactiveUntil !== void 0 && inactiveUntil > now) return "inactive";
220
226
  const activeUntil = active.get(key);
221
- if (activeUntil !== void 0 && activeUntil > now) return "active";
227
+ if (activeUntil !== void 0 && activeUntil > elapsed) return "active";
222
228
  const pending = inFlight.get(key);
223
229
  if (pending !== void 0) return pending;
224
230
  const asking = query(key, token).finally(() => inFlight.delete(key));
@@ -229,8 +235,7 @@ function createTokenStatusChecker(config) {
229
235
  if (credentialsRefused) return false;
230
236
  const confirmed = confirmedActive.get(createHash("sha256").update(token).digest("hex"));
231
237
  if (confirmed === void 0) return false;
232
- const now = clock();
233
- return confirmed.expiresAt > now && now - confirmed.at < tokenLifetimeSeconds(token) * 1e3;
238
+ return confirmed.expiresAt > clock() && monotonic() - confirmed.at < UNKNOWN_TOLERANCE_MS;
234
239
  }
235
240
  };
236
241
  }
@@ -252,10 +257,6 @@ function tokenExpiryMs(token, now) {
252
257
  const exp = claims(token).exp;
253
258
  return typeof exp === "number" ? exp * 1e3 : now + DEFAULT_TOKEN_LIFETIME_SECONDS * 1e3;
254
259
  }
255
- function tokenLifetimeSeconds(token) {
256
- const { exp, iat } = claims(token);
257
- return typeof exp === "number" && typeof iat === "number" && exp > iat ? exp - iat : DEFAULT_TOKEN_LIFETIME_SECONDS;
258
- }
259
260
  function claims(token) {
260
261
  try {
261
262
  return decodeJwt(token);
@@ -1957,7 +1958,7 @@ function outcomeCode(result) {
1957
1958
  // package.json
1958
1959
  var package_default = {
1959
1960
  name: "@tinycloud/mcp",
1960
- version: "0.8.7-beta.2",
1961
+ version: "0.9.0-beta.4",
1961
1962
  description: "TinyCloud delegated operations MCP server",
1962
1963
  keywords: [
1963
1964
  "tinycloud",
@@ -2054,6 +2055,7 @@ var MCP_SERVER_NAME = "tinycloud-mcp";
2054
2055
 
2055
2056
  // src/http.ts
2056
2057
  var REQUIRED_SCOPE = "tinycloud:mcp";
2058
+ var MCP_CORS_ALLOWED_HEADERS = "Authorization, Content-Type, Accept, Mcp-Protocol-Version, Mcp-Session-Id, Last-Event-ID, Mcp-Method, Mcp-Name";
2057
2059
  var LOCAL_CLI_PACKAGE = ["@tinycloud", "cli"].join("/");
2058
2060
  var LOCAL_MCP_PACKAGE = ["@tinycloud", "mcp"].join("/");
2059
2061
  function createHostedMcpApp(config) {
@@ -2374,8 +2376,8 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2374
2376
  if (url.pathname === callbackPath) {
2375
2377
  if (request.method === "OPTIONS") return callbackCors(new Response(null, { status: 204 }), config.openkeyHost);
2376
2378
  if (request.method !== "POST") return methodNotAllowed("POST");
2377
- const originFailure2 = validateCallbackOrigin(request, config.openkeyHost);
2378
- if (originFailure2 !== void 0) return callbackCors(originFailure2, config.openkeyHost);
2379
+ const originFailure = validateCallbackOrigin(request, config.openkeyHost);
2380
+ if (originFailure !== void 0) return callbackCors(originFailure, config.openkeyHost);
2379
2381
  try {
2380
2382
  await store.completeApproval(requiredState(url), request);
2381
2383
  return callbackCors(json({ success: true }), config.openkeyHost);
@@ -2469,11 +2471,9 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2469
2471
  });
2470
2472
  }
2471
2473
  if (url.pathname !== resourceUrl.pathname) return json({ error: "Not found" }, 404);
2472
- const originFailure = validateOrigin(request, config.allowedOrigins);
2473
- if (originFailure !== void 0) return originFailure;
2474
+ if (request.method === "OPTIONS") return mcpCors(new Response(null, { status: 204 }), request);
2474
2475
  const auth = await authenticate(request);
2475
- if (auth instanceof Response) return auth;
2476
- return handler.fetch(request, { authInfo: auth });
2476
+ return mcpCors(auth instanceof Response ? auth : await handler.fetch(request, { authInfo: auth }), request);
2477
2477
  },
2478
2478
  close: () => handler.close()
2479
2479
  };
@@ -2561,11 +2561,16 @@ function requiredState(url) {
2561
2561
  if (state.length > 512) throw new ApprovalRequestError("state_invalid", "Approval state is invalid.");
2562
2562
  return state;
2563
2563
  }
2564
- function validateOrigin(request, allowed) {
2565
- const origin = request.headers.get("origin");
2566
- if (origin === null) return void 0;
2567
- const allowlist = allowed ?? [];
2568
- return allowlist.includes(origin) ? void 0 : json({ error: "Origin is not allowed." }, 403);
2564
+ function mcpCors(response, request) {
2565
+ const headers = new Headers(response.headers);
2566
+ headers.append("vary", "Origin");
2567
+ if (request.headers.has("origin")) {
2568
+ headers.set("access-control-allow-origin", "*");
2569
+ headers.set("access-control-allow-methods", "GET, POST, DELETE, OPTIONS");
2570
+ headers.set("access-control-expose-headers", "WWW-Authenticate");
2571
+ if (request.method === "OPTIONS") headers.set("access-control-allow-headers", MCP_CORS_ALLOWED_HEADERS);
2572
+ }
2573
+ return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
2569
2574
  }
2570
2575
  function validateCallbackOrigin(request, openkeyHost) {
2571
2576
  const origin = request.headers.get("origin");
@@ -2678,7 +2683,6 @@ async function main() {
2678
2683
  openkeyHost,
2679
2684
  approvalTtlSeconds: positiveInteger(process.env.TC_MCP_APPROVAL_TTL_SECONDS, 300),
2680
2685
  delegationExpiry: process.env.TC_MCP_DELEGATION_EXPIRY ?? "1h",
2681
- allowedOrigins: commaList(process.env.TC_MCP_ALLOWED_ORIGINS),
2682
2686
  ...process.env.TC_MCP_NATIVE_SERVICE_CREDENTIAL === void 0 ? {} : { nativeServiceCredential: process.env.TC_MCP_NATIVE_SERVICE_CREDENTIAL },
2683
2687
  oauthMetadata,
2684
2688
  introspection: {
@@ -2730,10 +2734,6 @@ function positiveInteger(value, fallback) {
2730
2734
  if (!Number.isInteger(parsed) || parsed <= 0) throw new Error("Expected a positive integer environment value.");
2731
2735
  return parsed;
2732
2736
  }
2733
- function commaList(value) {
2734
- if (value === void 0) return void 0;
2735
- return value.split(",").map((entry) => entry.trim()).filter(Boolean);
2736
- }
2737
2737
  void main().catch(() => {
2738
2738
  process.stderr.write("[tinycloud-mcp-http] Startup failed.\n");
2739
2739
  process.exitCode = 1;