@vvedantb/server 0.7.0 → 0.7.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.1
4
+
5
+ Docs fix: do not import `mcpTokenCacheTable` or `mcpTokenCacheFields` in `convex/schema.ts`. A Convex schema cannot import runtime code from a package, so `convex deploy` fails with `NoImportModuleInSchema`. No runtime change; every export stays.
6
+
7
+ ### Changed
8
+
9
+ - `docs/convex.md`: define the cache table locally in `convex/schema.ts` (same fields, indexes `by_token_hash` and `by_expires`), and check it against `mcpTokenCacheFields` and `mcpTokenCacheTable` in an app test. Both have an example.
10
+ - JSDoc: `mcpTokenCacheFields` and `mcpTokenCacheTable` say not to import them in `convex/schema.ts` and to use them in tests. `mcpTokenCacheTable` is marked `@deprecated` for schema use; it still works.
11
+ - The 0.7.0 entry below wrongly said the two are "for the schema".
12
+ - Tests: the test app in `tests/convex/mcpTokenCacheApp/schema.ts` defines its tables locally, and `tests/convex/mcpTokenCache.test.ts` checks they match `mcpTokenCacheFields` and the indexes from `mcpTokenCacheTable`.
13
+
3
14
  ## 0.7.0
4
15
 
5
16
  Additive: MCP bearer-token cache for Convex backends. Nothing in 0.6.1 changes. See `docs/convex.md`.
@@ -6,7 +6,8 @@
6
6
  * `MCP_TOKEN_CACHE_TTL_MS`).
7
7
  *
8
8
  * The package cannot own a Convex table and never imports `convex` at
9
- * runtime, so the app passes in `v`, `defineTable` and its function builders.
9
+ * runtime, so the app defines the table in its own schema and passes in `v`
10
+ * and its function builders.
10
11
  */
11
12
  import type { defineTable as convexDefineTable, FunctionReference, IndexRange, RegisteredMutation, RegisteredQuery } from "convex/server";
12
13
  import type { v as convexV, GenericId, Validator } from "convex/values";
@@ -37,15 +38,23 @@ export declare function jwtExpiryMs(token: string): number | null;
37
38
  export declare function tokenCacheExpiresAt(token: string, nowMs: number): number | null;
38
39
  /** The cache key: SHA-256 of the token, as lowercase hex. */
39
40
  export declare function hashMcpToken(token: string): Promise<string>;
40
- /** The table's fields, for `defineTable` or your own table definition. */
41
+ /**
42
+ * The table's fields: `tokenHash`, `clerkUserId` (strings) and `expiresAt` (number).
43
+ *
44
+ * Do not import in `convex/schema.ts` (NoImportModuleInSchema). Use in tests to
45
+ * check your local table matches. See `docs/convex.md`.
46
+ */
41
47
  export declare function mcpTokenCacheFields(v: ConvexV): {
42
48
  tokenHash: import("convex/values").VString<string, "required">;
43
49
  clerkUserId: import("convex/values").VString<string, "required">;
44
50
  expiresAt: import("convex/values").VFloat64<number, "required">;
45
51
  };
46
52
  /**
47
- * The cache table with both indexes. In your schema:
48
- * `mcpTokenCache: mcpTokenCacheTable(defineTable, v)`.
53
+ * The cache table with both indexes (`by_token_hash`, `by_expires`).
54
+ *
55
+ * @deprecated Do not call in `convex/schema.ts`: a schema cannot import the
56
+ * package (NoImportModuleInSchema). Define the table locally and use this in
57
+ * tests to check your local table matches. See `docs/convex.md`.
49
58
  */
50
59
  export declare function mcpTokenCacheTable(defineTable: typeof convexDefineTable, v: ConvexV): import("convex/server").TableDefinition<import("convex/values").VObject<{
51
60
  tokenHash: string;
@@ -34,13 +34,21 @@ export function tokenCacheExpiresAt(token, nowMs) {
34
34
  export function hashMcpToken(token) {
35
35
  return sha256Hex(token);
36
36
  }
37
- /** The table's fields, for `defineTable` or your own table definition. */
37
+ /**
38
+ * The table's fields: `tokenHash`, `clerkUserId` (strings) and `expiresAt` (number).
39
+ *
40
+ * Do not import in `convex/schema.ts` (NoImportModuleInSchema). Use in tests to
41
+ * check your local table matches. See `docs/convex.md`.
42
+ */
38
43
  export function mcpTokenCacheFields(v) {
39
44
  return { tokenHash: v.string(), clerkUserId: v.string(), expiresAt: v.number() };
40
45
  }
41
46
  /**
42
- * The cache table with both indexes. In your schema:
43
- * `mcpTokenCache: mcpTokenCacheTable(defineTable, v)`.
47
+ * The cache table with both indexes (`by_token_hash`, `by_expires`).
48
+ *
49
+ * @deprecated Do not call in `convex/schema.ts`: a schema cannot import the
50
+ * package (NoImportModuleInSchema). Define the table locally and use this in
51
+ * tests to check your local table matches. See `docs/convex.md`.
44
52
  */
45
53
  export function mcpTokenCacheTable(defineTable, v) {
46
54
  return defineTable(mcpTokenCacheFields(v))
package/docs/convex.md CHANGED
@@ -236,16 +236,37 @@ Stays in the apps: legacy email lookups, migration-table lookups, API-key actors
236
236
 
237
237
  Caches verified MCP bearer tokens, so most requests skip the verify step (for Clerk: a network call, often from a cold Node action). Rows hold the SHA-256 hex of the token (`hashMcpToken`), never the token. Only successful verifications are cached, so a revoked token keeps working until its row expires: at most `MCP_TOKEN_CACHE_TTL_MS` (5 minutes).
238
238
 
239
- The package cannot own a table, so add it to your schema and register the three internal functions in one module:
239
+ The package cannot own a table, so define it in your schema and register the three internal functions in one module.
240
+
241
+ Define the table locally. `convex/schema.ts` cannot import runtime code from a package: `convex deploy` fails with `NoImportModuleInSchema`. So do not call `mcpTokenCacheTable` or `mcpTokenCacheFields` there.
240
242
 
241
243
  ```ts
242
244
  // convex/schema.ts
243
245
  import { defineSchema, defineTable } from "convex/server";
244
246
  import { v } from "convex/values";
245
- import { mcpTokenCacheTable } from "@vvedantb/server";
246
247
 
247
248
  export default defineSchema({
248
- mcpTokenCache: mcpTokenCacheTable(defineTable, v), // indexes by_token_hash and by_expires
249
+ // the fields of mcpTokenCacheFields; schema.test.ts checks they match
250
+ mcpTokenCache: defineTable({ tokenHash: v.string(), clerkUserId: v.string(), expiresAt: v.number() })
251
+ .index("by_token_hash", ["tokenHash"])
252
+ .index("by_expires", ["expiresAt"]),
253
+ });
254
+ ```
255
+
256
+ Check the local table against the package in a test, outside `convex/schema.ts`, so a change to the fields or indexes fails the test:
257
+
258
+ ```ts
259
+ // convex/schema.test.ts
260
+ import { defineTable } from "convex/server";
261
+ import { v } from "convex/values";
262
+ import { expect, test } from "vitest";
263
+ import { mcpTokenCacheFields, mcpTokenCacheTable } from "@vvedantb/server";
264
+ import schema from "./schema";
265
+
266
+ test("mcpTokenCache matches @vvedantb/server", () => {
267
+ const table = schema.tables.mcpTokenCache;
268
+ expect(table.validator).toEqual(v.object(mcpTokenCacheFields(v)));
269
+ expect(table[" indexes"]()).toEqual(mcpTokenCacheTable(defineTable, v)[" indexes"]());
249
270
  });
250
271
  ```
251
272
 
@@ -290,7 +311,7 @@ crons.hourly("purge MCP token cache", { minuteUTC: 0 }, internal.mcp.tokenCache.
290
311
  | `getInternal` | Internal query `{ tokenHash }` → `{ clerkUserId, expiresAt } \| null` via `by_token_hash`. Does not check expiry, so the query stays cacheable. |
291
312
  | `putInternal` | Internal mutation. Inserts a new row; patches an existing one only when `clerkUserId` differs or the new `expiresAt` is later. |
292
313
  | `purgeExpired` | Internal mutation. Deletes up to `MCP_TOKEN_CACHE_PURGE_BATCH` (500) rows with `expiresAt < now` via `by_expires`, and reschedules itself (`runAfter(0)`) when it deleted a full batch. Returns the count. |
293
- | `mcpTokenCacheFields(v)`, `mcpTokenCacheTable(defineTable, v)` | The fields `tokenHash`, `clerkUserId` (strings) and `expiresAt` (number), and the table with `MCP_TOKEN_CACHE_HASH_INDEX` (`by_token_hash`) and `MCP_TOKEN_CACHE_EXPIRES_INDEX` (`by_expires`). |
314
+ | `mcpTokenCacheFields(v)`, `mcpTokenCacheTable(defineTable, v)` | The fields `tokenHash`, `clerkUserId` (strings) and `expiresAt` (number), and the table with `MCP_TOKEN_CACHE_HASH_INDEX` (`by_token_hash`) and `MCP_TOKEN_CACHE_EXPIRES_INDEX` (`by_expires`). For tests only: do not import them in `convex/schema.ts` (`NoImportModuleInSchema`). `mcpTokenCacheTable` is `@deprecated` to flag schema use. |
294
315
 
295
316
  The ctx types are structural, so the factory checks that the table exists and has both indexes. `getInternal`, `putInternal` and `purgeExpired` are real registered functions, so `ctx.runQuery(internal.mcp.tokenCache.getInternal, …)` is typed. `tests/convex/mcpTokenCache.types.ts` checks this against a codegen-shaped app with `convex/server` 1.47.
296
317
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/server",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "license": "MIT",
5
5
  "description": "Shared Convex backend helpers, MCP server scaffolding and Node-side MCP OAuth client helpers from one root import: query-limit errors, Clerk auth and token verification, an MCP bearer-token cache, Clerk-user lookups and ensure-user for Convex functions, bearer tokens, env, CORS, JSON responses, MCP route registration, a bearer-auth MCP HTTP handler, OAuth metadata, a JSON-RPC dispatcher, explorer tools, WebCrypto primitives, Convex optimistic-update helpers, MCP OAuth client helpers (provider, sign-in, refresh, revocation, loopback redirect server) for desktop apps, a QuickJS code sandbox that bridges script tool calls to host handlers, and a pure artifacts core (Zod contract, state limits and ops, quiz and flashcards pages, a sandboxed document wrapper with CSP and a guest state bridge).",
6
6
  "type": "module",