@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.d.cts CHANGED
@@ -10,9 +10,14 @@ interface TokenStatusConfig {
10
10
  /** How the confidential client authenticates: `client_secret_basic` or `client_secret_post`. */
11
11
  readonly authMethod: "basic" | "post";
12
12
  readonly timeoutMs?: number;
13
- /** Test seams. */
13
+ /**
14
+ * Test seams. `now` is the wall clock, compared with the token's `exp`;
15
+ * `monotonicNow` measures the 30 s and 60 s windows and defaults to
16
+ * `performance.now` (to `now` when only `now` is given).
17
+ */
14
18
  readonly fetch?: typeof fetch;
15
19
  readonly now?: () => number;
20
+ readonly monotonicNow?: () => number;
16
21
  readonly log?: (message: string) => void;
17
22
  }
18
23
 
@@ -32,7 +37,6 @@ interface RemoteStoreConfig {
32
37
 
33
38
  interface HostedMcpConfig extends RemoteStoreConfig {
34
39
  readonly oauthMetadata: OAuthMetadata;
35
- readonly allowedOrigins?: readonly string[];
36
40
  /**
37
41
  * OpenKey token introspection client (`TC_MCP_INTROSPECTION_URL` and client
38
42
  * credentials). The hosted server always sets this; without it only the
package/dist/http.d.ts CHANGED
@@ -10,9 +10,14 @@ interface TokenStatusConfig {
10
10
  /** How the confidential client authenticates: `client_secret_basic` or `client_secret_post`. */
11
11
  readonly authMethod: "basic" | "post";
12
12
  readonly timeoutMs?: number;
13
- /** Test seams. */
13
+ /**
14
+ * Test seams. `now` is the wall clock, compared with the token's `exp`;
15
+ * `monotonicNow` measures the 30 s and 60 s windows and defaults to
16
+ * `performance.now` (to `now` when only `now` is given).
17
+ */
14
18
  readonly fetch?: typeof fetch;
15
19
  readonly now?: () => number;
20
+ readonly monotonicNow?: () => number;
16
21
  readonly log?: (message: string) => void;
17
22
  }
18
23
 
@@ -32,7 +37,6 @@ interface RemoteStoreConfig {
32
37
 
33
38
  interface HostedMcpConfig extends RemoteStoreConfig {
34
39
  readonly oauthMetadata: OAuthMetadata;
35
- readonly allowedOrigins?: readonly string[];
36
40
  /**
37
41
  * OpenKey token introspection client (`TC_MCP_INTROSPECTION_URL` and client
38
42
  * credentials). The hosted server always sets this; without it only the
package/dist/http.js CHANGED
@@ -107,12 +107,14 @@ function firstString(...values) {
107
107
  import { createHash } from "crypto";
108
108
  import { decodeJwt } from "jose";
109
109
  var ACTIVE_CACHE_MS = 3e4;
110
+ var UNKNOWN_TOLERANCE_MS = 6e4;
110
111
  var DEFAULT_TOKEN_LIFETIME_SECONDS = 300;
111
112
  var DEFAULT_TIMEOUT_MS = 3e3;
112
113
  var MAX_CACHED_TOKENS = 1e4;
113
114
  function createTokenStatusChecker(config) {
114
115
  const doFetch = config.fetch ?? fetch;
115
116
  const clock = config.now ?? Date.now;
117
+ const monotonic = config.monotonicNow ?? (config.now ?? (() => performance.now()));
116
118
  const log = config.log ?? ((message) => void process.stderr.write(`[tinycloud-mcp-http] ${message}
117
119
  `));
118
120
  const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
@@ -155,6 +157,7 @@ function createTokenStatusChecker(config) {
155
157
  }
156
158
  }
157
159
  async function query(key, token) {
160
+ const askedAt = monotonic();
158
161
  const outcome = await introspect(token);
159
162
  if (outcome === "credentials") {
160
163
  if (!credentialsRefused) {
@@ -165,7 +168,7 @@ function createTokenStatusChecker(config) {
165
168
  }
166
169
  if (outcome === "unknown") return outcome;
167
170
  const now = clock();
168
- prune(now);
171
+ prune(now, monotonic());
169
172
  if (outcome === "mismatch") {
170
173
  inactive.set(key, tokenExpiryMs(token, now));
171
174
  confirmedActive.delete(key);
@@ -176,16 +179,18 @@ function createTokenStatusChecker(config) {
176
179
  inactive.set(key, tokenExpiryMs(token, now));
177
180
  confirmedActive.delete(key);
178
181
  } else {
179
- active.set(key, now + ACTIVE_CACHE_MS);
182
+ active.set(key, askedAt + ACTIVE_CACHE_MS);
180
183
  confirmedActive.delete(key);
181
- confirmedActive.set(key, { at: now, expiresAt: tokenExpiryMs(token, now) });
184
+ confirmedActive.set(key, { at: askedAt, expiresAt: tokenExpiryMs(token, now) });
182
185
  }
183
186
  return outcome;
184
187
  }
185
- function prune(now) {
186
- for (const [key, until] of active) if (until <= now) active.delete(key);
188
+ function prune(now, elapsed) {
189
+ for (const [key, until] of active) if (until <= elapsed) active.delete(key);
187
190
  for (const [key, until] of inactive) if (until <= now) inactive.delete(key);
188
- for (const [key, entry] of confirmedActive) if (entry.expiresAt <= now) confirmedActive.delete(key);
191
+ for (const [key, entry] of confirmedActive) {
192
+ if (entry.expiresAt <= now || elapsed - entry.at >= UNKNOWN_TOLERANCE_MS) confirmedActive.delete(key);
193
+ }
189
194
  for (const cache of [active, inactive, confirmedActive]) {
190
195
  while (cache.size >= MAX_CACHED_TOKENS) {
191
196
  const oldest = cache.keys().next().value;
@@ -198,10 +203,11 @@ function createTokenStatusChecker(config) {
198
203
  async tokenStatus(token) {
199
204
  const key = createHash("sha256").update(token).digest("hex");
200
205
  const now = clock();
206
+ const elapsed = monotonic();
201
207
  const inactiveUntil = inactive.get(key);
202
208
  if (inactiveUntil !== void 0 && inactiveUntil > now) return "inactive";
203
209
  const activeUntil = active.get(key);
204
- if (activeUntil !== void 0 && activeUntil > now) return "active";
210
+ if (activeUntil !== void 0 && activeUntil > elapsed) return "active";
205
211
  const pending = inFlight.get(key);
206
212
  if (pending !== void 0) return pending;
207
213
  const asking = query(key, token).finally(() => inFlight.delete(key));
@@ -212,8 +218,7 @@ function createTokenStatusChecker(config) {
212
218
  if (credentialsRefused) return false;
213
219
  const confirmed = confirmedActive.get(createHash("sha256").update(token).digest("hex"));
214
220
  if (confirmed === void 0) return false;
215
- const now = clock();
216
- return confirmed.expiresAt > now && now - confirmed.at < tokenLifetimeSeconds(token) * 1e3;
221
+ return confirmed.expiresAt > clock() && monotonic() - confirmed.at < UNKNOWN_TOLERANCE_MS;
217
222
  }
218
223
  };
219
224
  }
@@ -235,10 +240,6 @@ function tokenExpiryMs(token, now) {
235
240
  const exp = claims(token).exp;
236
241
  return typeof exp === "number" ? exp * 1e3 : now + DEFAULT_TOKEN_LIFETIME_SECONDS * 1e3;
237
242
  }
238
- function tokenLifetimeSeconds(token) {
239
- const { exp, iat } = claims(token);
240
- return typeof exp === "number" && typeof iat === "number" && exp > iat ? exp - iat : DEFAULT_TOKEN_LIFETIME_SECONDS;
241
- }
242
243
  function claims(token) {
243
244
  try {
244
245
  return decodeJwt(token);
@@ -1940,7 +1941,7 @@ function outcomeCode(result) {
1940
1941
  // package.json
1941
1942
  var package_default = {
1942
1943
  name: "@tinycloud/mcp",
1943
- version: "0.8.7-beta.2",
1944
+ version: "0.8.7",
1944
1945
  description: "TinyCloud delegated operations MCP server",
1945
1946
  keywords: [
1946
1947
  "tinycloud",
@@ -2011,9 +2012,9 @@ var package_default = {
2011
2012
  dependencies: {
2012
2013
  "@modelcontextprotocol/node": "2.0.0-beta.5",
2013
2014
  "@modelcontextprotocol/server": "2.0.0-beta.5",
2014
- "@tinycloud/node-sdk": "3.11.0-beta.2",
2015
+ "@tinycloud/node-sdk": "3.10.0",
2015
2016
  "@tinycloud/node-sdk-wasm": "1.7.8",
2016
- "@tinycloud/operations": "0.7.11-beta.2",
2017
+ "@tinycloud/operations": "0.7.10",
2017
2018
  jose: "6.2.4"
2018
2019
  },
2019
2020
  devDependencies: {
@@ -2037,6 +2038,7 @@ var MCP_SERVER_NAME = "tinycloud-mcp";
2037
2038
 
2038
2039
  // src/http.ts
2039
2040
  var REQUIRED_SCOPE = "tinycloud:mcp";
2041
+ var MCP_CORS_ALLOWED_HEADERS = "Authorization, Content-Type, Accept, Mcp-Protocol-Version, Mcp-Session-Id, Last-Event-ID, Mcp-Method, Mcp-Name";
2040
2042
  var LOCAL_CLI_PACKAGE = ["@tinycloud", "cli"].join("/");
2041
2043
  var LOCAL_MCP_PACKAGE = ["@tinycloud", "mcp"].join("/");
2042
2044
  function createHostedMcpApp(config) {
@@ -2357,8 +2359,8 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2357
2359
  if (url.pathname === callbackPath) {
2358
2360
  if (request.method === "OPTIONS") return callbackCors(new Response(null, { status: 204 }), config.openkeyHost);
2359
2361
  if (request.method !== "POST") return methodNotAllowed("POST");
2360
- const originFailure2 = validateCallbackOrigin(request, config.openkeyHost);
2361
- if (originFailure2 !== void 0) return callbackCors(originFailure2, config.openkeyHost);
2362
+ const originFailure = validateCallbackOrigin(request, config.openkeyHost);
2363
+ if (originFailure !== void 0) return callbackCors(originFailure, config.openkeyHost);
2362
2364
  try {
2363
2365
  await store.completeApproval(requiredState(url), request);
2364
2366
  return callbackCors(json({ success: true }), config.openkeyHost);
@@ -2452,11 +2454,9 @@ Local alternative: install \`${LOCAL_CLI_PACKAGE}\` and \`${LOCAL_MCP_PACKAGE}\`
2452
2454
  });
2453
2455
  }
2454
2456
  if (url.pathname !== resourceUrl.pathname) return json({ error: "Not found" }, 404);
2455
- const originFailure = validateOrigin(request, config.allowedOrigins);
2456
- if (originFailure !== void 0) return originFailure;
2457
+ if (request.method === "OPTIONS") return mcpCors(new Response(null, { status: 204 }), request);
2457
2458
  const auth = await authenticate(request);
2458
- if (auth instanceof Response) return auth;
2459
- return handler.fetch(request, { authInfo: auth });
2459
+ return mcpCors(auth instanceof Response ? auth : await handler.fetch(request, { authInfo: auth }), request);
2460
2460
  },
2461
2461
  close: () => handler.close()
2462
2462
  };
@@ -2544,11 +2544,16 @@ function requiredState(url) {
2544
2544
  if (state.length > 512) throw new ApprovalRequestError("state_invalid", "Approval state is invalid.");
2545
2545
  return state;
2546
2546
  }
2547
- function validateOrigin(request, allowed) {
2548
- const origin = request.headers.get("origin");
2549
- if (origin === null) return void 0;
2550
- const allowlist = allowed ?? [];
2551
- return allowlist.includes(origin) ? void 0 : json({ error: "Origin is not allowed." }, 403);
2547
+ function mcpCors(response, request) {
2548
+ const headers = new Headers(response.headers);
2549
+ headers.append("vary", "Origin");
2550
+ if (request.headers.has("origin")) {
2551
+ headers.set("access-control-allow-origin", "*");
2552
+ headers.set("access-control-allow-methods", "GET, POST, DELETE, OPTIONS");
2553
+ headers.set("access-control-expose-headers", "WWW-Authenticate");
2554
+ if (request.method === "OPTIONS") headers.set("access-control-allow-headers", MCP_CORS_ALLOWED_HEADERS);
2555
+ }
2556
+ return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
2552
2557
  }
2553
2558
  function validateCallbackOrigin(request, openkeyHost) {
2554
2559
  const origin = request.headers.get("origin");