@openparachute/vault 0.7.3-rc.13 → 0.7.3-rc.3

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.
Files changed (45) hide show
  1. package/README.md +3 -5
  2. package/core/src/content-range.test.ts +0 -127
  3. package/core/src/content-range.ts +0 -100
  4. package/core/src/core.test.ts +4 -66
  5. package/core/src/expand.ts +3 -11
  6. package/core/src/mcp.ts +6 -577
  7. package/core/src/notes.ts +4 -201
  8. package/core/src/search-fts-v25.test.ts +1 -9
  9. package/core/src/search-query.test.ts +0 -42
  10. package/core/src/search-query.ts +0 -27
  11. package/core/src/seed-packs.test.ts +1 -117
  12. package/core/src/seed-packs.ts +1 -217
  13. package/core/src/types.ts +0 -6
  14. package/core/src/vault-projection.ts +0 -55
  15. package/package.json +1 -1
  16. package/src/add-pack.test.ts +0 -32
  17. package/src/auth-hub-jwt.test.ts +1 -118
  18. package/src/auth.ts +0 -64
  19. package/src/cli.ts +1 -5
  20. package/src/mcp-http.ts +4 -33
  21. package/src/mcp-tools.ts +14 -61
  22. package/src/oauth-discovery.ts +0 -31
  23. package/src/onboarding-seed.test.ts +0 -64
  24. package/src/routes.ts +95 -92
  25. package/src/routing.test.ts +4 -229
  26. package/src/routing.ts +23 -152
  27. package/src/scopes.ts +0 -22
  28. package/src/server.ts +0 -7
  29. package/src/storage.test.ts +1 -200
  30. package/src/transcription-worker.test.ts +0 -151
  31. package/src/transcription-worker.ts +52 -113
  32. package/src/vault.test.ts +11 -48
  33. package/core/src/attachment/bytes-provider.ts +0 -65
  34. package/core/src/attachment/policy.test.ts +0 -66
  35. package/core/src/attachment/policy.ts +0 -131
  36. package/core/src/attachment/tickets.test.ts +0 -45
  37. package/core/src/attachment/tickets.ts +0 -117
  38. package/core/src/attachment-tickets-tool.test.ts +0 -286
  39. package/core/src/display-title.test.ts +0 -190
  40. package/core/src/lede.test.ts +0 -96
  41. package/core/src/search-title-boost.test.ts +0 -125
  42. package/src/attachment-bytes.ts +0 -68
  43. package/src/attachment-tickets.test.ts +0 -475
  44. package/src/attachment-tickets.ts +0 -340
  45. package/src/read-attachment.test.ts +0 -436
@@ -10,10 +10,8 @@
10
10
  * 3. `/vault/<name>/...` — per-vault routing (OAuth, MCP, view, API).
11
11
  * 4. The RFC 9728 WWW-Authenticate challenge that decorates MCP 401s.
12
12
  *
13
- * No unscoped `/api/*` or `/oauth/*` routes exist — those per-vault resources
14
- * must name the vault in the URL. The canonical root `/mcp` (U1) is the one
15
- * vault-agnostic exception: it derives the target vault from the token, then
16
- * re-dispatches through the same per-vault machinery.
13
+ * No unscoped `/mcp`, `/api/*`, `/oauth/*` routes exist — every per-vault
14
+ * resource must name the vault it targets.
17
15
  *
18
16
  * Uses PARACHUTE_HOME override so each test's vaults live in a tmp dir and
19
17
  * never touch ~/.parachute.
@@ -708,27 +706,14 @@ describe("per-vault routing under /vault/<name>/", () => {
708
706
  }
709
707
  });
710
708
 
711
- test("no /api, /oauth unscoped routes — all 404", async () => {
709
+ test("no /mcp, /api, /oauth unscoped routes — all 404", async () => {
712
710
  createVault("journal");
713
- for (const path of ["/api/notes", "/oauth/register", "/oauth/authorize"]) {
711
+ for (const path of ["/mcp", "/api/notes", "/oauth/register", "/oauth/authorize"]) {
714
712
  const res = await route(new Request(`http://localhost:1940${path}`), path);
715
713
  expect(res.status).toBe(404);
716
714
  }
717
715
  });
718
716
 
719
- test("root /mcp is NOT a 404 — it's the token-derived MCP endpoint (401 unauthenticated)", async () => {
720
- // The old unscoped `/mcp` compat prefix returned 404. The canonical root
721
- // `/mcp` (U1) replaces it: unauthenticated it 401s + carries the root PRM
722
- // challenge, exactly like the per-vault endpoint but pointed at the root
723
- // metadata document.
724
- createVault("journal");
725
- const res = await route(new Request("http://localhost:1940/mcp", { method: "POST" }), "/mcp");
726
- expect(res.status).toBe(401);
727
- expect(res.headers.get("WWW-Authenticate")).toBe(
728
- 'Bearer resource_metadata="http://localhost:1940/.well-known/oauth-protected-resource/mcp"',
729
- );
730
- });
731
-
732
717
  test("bare /vault/<name> returns metadata for authenticated callers", async () => {
733
718
  createVault("journal", "My journal vault");
734
719
  const path = "/vault/journal";
@@ -825,216 +810,6 @@ describe("MCP 401 WWW-Authenticate challenge (RFC 9728)", () => {
825
810
  });
826
811
  });
827
812
 
828
- // ---------------------------------------------------------------------------
829
- // Canonical root MCP endpoint — token-derived vault dispatch (U1).
830
- //
831
- // `/mcp` at the origin root derives the target vault from the TOKEN, not the
832
- // URL, then re-dispatches through the identical per-vault machinery. These
833
- // tests pin the two load-bearing properties: (1) a token for vault A behaves
834
- // byte-identically at root `/mcp` and at `/vault/A/mcp` (equivalence), and
835
- // (2) the vault is ALWAYS the one the token names — a token can't be steered
836
- // to another vault, and an unnameable/ambiguous token is refused with the root
837
- // discovery challenge rather than a guess (derivation honesty).
838
- // ---------------------------------------------------------------------------
839
-
840
- describe("canonical root /mcp — token-derived vault dispatch (U1)", () => {
841
- const INIT = {
842
- jsonrpc: "2.0",
843
- id: 1,
844
- method: "initialize",
845
- params: {
846
- protocolVersion: "2025-06-18",
847
- capabilities: {},
848
- clientInfo: { name: "u1-test", version: "0" },
849
- },
850
- };
851
- const LIST = { jsonrpc: "2.0", id: 2, method: "tools/list" };
852
- const QUERY = {
853
- jsonrpc: "2.0",
854
- id: 3,
855
- method: "tools/call",
856
- params: { name: "query-notes", arguments: {} },
857
- };
858
-
859
- async function mcpPost(path: string, token: string | null, rpc: object): Promise<Response> {
860
- const headers: Record<string, string> = {
861
- "content-type": "application/json",
862
- accept: "application/json, text/event-stream",
863
- };
864
- if (token) headers.authorization = `Bearer ${token}`;
865
- return route(
866
- new Request(`http://localhost:1940${path}`, {
867
- method: "POST",
868
- headers,
869
- body: JSON.stringify(rpc),
870
- }),
871
- path,
872
- );
873
- }
874
-
875
- /** Parse a JSON-RPC frame off an MCP response, tolerating either the JSON
876
- * body (enableJsonResponse) or an SSE `data:` framing. */
877
- async function rpcFrame(res: Response): Promise<unknown> {
878
- const text = await res.text();
879
- const trimmed = text.trimStart();
880
- if (trimmed.startsWith("{") || trimmed.startsWith("[")) return JSON.parse(trimmed);
881
- const dataLine = text.split("\n").find((l) => l.startsWith("data:"));
882
- return JSON.parse(dataLine!.slice("data:".length).trim());
883
- }
884
-
885
- /** Sign a token directly (bypassing `mintJwt`'s `aud=vault.<name>` coupling)
886
- * so a test can craft a token whose sources name no vault, or disagree. */
887
- async function signRaw(payload: Record<string, unknown>, aud: string): Promise<string> {
888
- const iat = Math.floor(Date.now() / 1000);
889
- return new SignJWT({ client_id: "u1-test", ...payload })
890
- .setProtectedHeader({ alg: "RS256", kid: KID })
891
- .setIssuer(`http://127.0.0.1:${hubServer.port}`)
892
- .setSubject("u1-test-user")
893
- .setAudience(aud)
894
- .setIssuedAt(iat)
895
- .setExpirationTime(iat + 60)
896
- .setJti(`jti-${Math.random().toString(36).slice(2)}`)
897
- .sign(signingKey);
898
- }
899
-
900
- test("root PRM mirrors the per-vault PRM shape (root resource, un-narrowed scopes)", async () => {
901
- createVault("journal");
902
- const path = "/.well-known/oauth-protected-resource/mcp";
903
- const res = await route(new Request(`http://localhost:1940${path}`), path);
904
- expect(res.status).toBe(200);
905
- const prm = (await res.json()) as {
906
- resource: string;
907
- authorization_servers: string[];
908
- scopes_supported: string[];
909
- bearer_methods_supported: string[];
910
- };
911
- expect(prm.resource).toBe("http://localhost:1940/mcp");
912
- // Un-narrowed forms — the hub's consent picker narrows them to a vault.
913
- expect(prm.scopes_supported).toEqual(["vault:read", "vault:write"]);
914
- expect(prm.bearer_methods_supported).toEqual(["header"]);
915
- expect(Array.isArray(prm.authorization_servers)).toBe(true);
916
- expect(prm.authorization_servers.length).toBe(1);
917
- });
918
-
919
- test("unauthenticated → 401 whose challenge names the root PRM (self-consistent pointer)", async () => {
920
- createVault("journal");
921
- const res = await mcpPost("/mcp", null, INIT);
922
- expect(res.status).toBe(401);
923
- const header = res.headers.get("WWW-Authenticate")!;
924
- const prmUrl = header.match(/resource_metadata="([^"]+)"/)![1]!;
925
- const prmPath = new URL(prmUrl).pathname;
926
- expect(prmPath).toBe("/.well-known/oauth-protected-resource/mcp");
927
- // The pointer resolves to a real PRM that names the root resource.
928
- const prmRes = await route(new Request(prmUrl), prmPath);
929
- expect(prmRes.status).toBe(200);
930
- expect(((await prmRes.json()) as { resource: string }).resource).toBe(
931
- "http://localhost:1940/mcp",
932
- );
933
- });
934
-
935
- test("x-forwarded-* shape the root challenge URL", async () => {
936
- createVault("journal");
937
- const req = new Request("http://127.0.0.1:1940/mcp", {
938
- method: "POST",
939
- headers: {
940
- "x-forwarded-host": "vault.example.com",
941
- "x-forwarded-proto": "https",
942
- },
943
- });
944
- const res = await route(req, "/mcp");
945
- expect(res.status).toBe(401);
946
- expect(res.headers.get("WWW-Authenticate")).toBe(
947
- 'Bearer resource_metadata="https://vault.example.com/.well-known/oauth-protected-resource/mcp"',
948
- );
949
- });
950
-
951
- test("token for A at root /mcp === same token at /vault/A/mcp (initialize, tools/list, tool call)", async () => {
952
- createVault("journal");
953
- const store = getVaultStore("journal");
954
- await store.createNote("root-mcp equivalence fixture", { path: "fixture", tags: ["probe"] });
955
- const token = await mintJwt({ vaultName: "journal", scopes: ["vault:journal:admin"] });
956
-
957
- for (const rpc of [INIT, LIST, QUERY]) {
958
- const rootRes = await mcpPost("/mcp", token, rpc);
959
- const perVaultRes = await mcpPost("/vault/journal/mcp", token, rpc);
960
- expect(rootRes.status).toBe(200);
961
- expect(perVaultRes.status).toBe(200);
962
- // Byte-identical JSON-RPC frame — the root path derived `journal`, so the
963
- // server name, tool list, and tool result all match the URL-addressed
964
- // endpoint exactly.
965
- expect(await rpcFrame(rootRes)).toEqual(await rpcFrame(perVaultRes));
966
- }
967
- });
968
-
969
- test("a read-only token narrows the root tool list identically to the per-vault endpoint", async () => {
970
- createVault("journal");
971
- getVaultStore("journal");
972
- const readToken = await mintJwt({ vaultName: "journal", scopes: ["vault:journal:read"] });
973
- const rootList = await rpcFrame(await mcpPost("/mcp", readToken, LIST));
974
- const perVaultList = await rpcFrame(await mcpPost("/vault/journal/mcp", readToken, LIST));
975
- expect(rootList).toEqual(perVaultList);
976
- // Sanity: a read token sees the read tools but not write/admin ones.
977
- const names = (rootList as { result: { tools: { name: string }[] } }).result.tools.map(
978
- (t) => t.name,
979
- );
980
- expect(names).toContain("query-notes");
981
- expect(names).not.toContain("create-note");
982
- expect(names).not.toContain("manage-token");
983
- });
984
-
985
- test("a vault-A token ALWAYS lands in A through root /mcp — never another vault's data", async () => {
986
- createVault("alpha");
987
- createVault("beta");
988
- // Seed a secret in BETA. A token minted for ALPHA must never surface it.
989
- await getVaultStore("beta").createNote("BETA-ONLY-SECRET", { path: "secret" });
990
- getVaultStore("alpha");
991
- const alphaToken = await mintJwt({ vaultName: "alpha", scopes: ["vault:alpha:admin"] });
992
-
993
- const res = await mcpPost("/mcp", alphaToken, QUERY);
994
- expect(res.status).toBe(200);
995
- // The vault is derived from the token (alpha); beta's note is unreachable.
996
- expect(JSON.stringify(await rpcFrame(res))).not.toContain("BETA-ONLY-SECRET");
997
- });
998
-
999
- test("scope and aud naming DIFFERENT vaults → 401 (disagreement, never guess)", async () => {
1000
- createVault("alpha");
1001
- createVault("beta");
1002
- // aud=vault.alpha (from vaultName) but the scope names beta — two sources
1003
- // disagree, so derivation refuses rather than picking a winner.
1004
- const token = await mintJwt({ vaultName: "alpha", scopes: ["vault:beta:admin"] });
1005
- const res = await mcpPost("/mcp", token, INIT);
1006
- expect(res.status).toBe(401);
1007
- expect(res.headers.get("WWW-Authenticate")).toContain("/.well-known/oauth-protected-resource/mcp");
1008
- });
1009
-
1010
- test("a token that names NO vault (broad scope, non-vault aud, no vault_scope) → 401", async () => {
1011
- createVault("journal");
1012
- // Broad `vault:read` names no vault; aud is not `vault.<name>`; no
1013
- // vault_scope claim. Nothing derives a target → the discovery challenge.
1014
- const token = await signRaw({ scope: "vault:read" }, "some-other-resource");
1015
- const res = await mcpPost("/mcp", token, INIT);
1016
- expect(res.status).toBe(401);
1017
- expect(res.headers.get("WWW-Authenticate")).toContain("/.well-known/oauth-protected-resource/mcp");
1018
- });
1019
-
1020
- test("an invalid / unverifiable token → 401 + root challenge (no vault leaked)", async () => {
1021
- createVault("journal");
1022
- const res = await mcpPost("/mcp", "not-a-real-jwt", INIT);
1023
- expect(res.status).toBe(401);
1024
- expect(res.headers.get("WWW-Authenticate")).toContain("/.well-known/oauth-protected-resource/mcp");
1025
- });
1026
-
1027
- test("a validly-signed token naming a vault absent on THIS server → 404 (mirrors per-vault)", async () => {
1028
- createVault("journal");
1029
- // Token is real (signed by the fixture hub) but names a vault that isn't
1030
- // installed here. The per-vault URL 404s for a missing vault; root matches.
1031
- const token = await mintJwt({ vaultName: "ghost", scopes: ["vault:ghost:admin"] });
1032
- const res = await mcpPost("/mcp", token, INIT);
1033
- expect(res.status).toBe(404);
1034
- expect(((await res.json()) as { error: string }).error).toBe("Vault not found");
1035
- });
1036
- });
1037
-
1038
813
  // ---------------------------------------------------------------------------
1039
814
  // Per-vault OAuth discovery (RFC 8414 / RFC 9728, path-append form).
1040
815
  //
package/src/routing.ts CHANGED
@@ -1,23 +1,15 @@
1
1
  /**
2
2
  * HTTP request router for the multi-vault server.
3
3
  *
4
- * Per-vault resources live under `/vault/<name>/...` a request names the
5
- * vault it targets in the URL. The one exception is the canonical root `/mcp`
6
- * endpoint (U1), which is vault-AGNOSTIC: the vault is derived from the token,
7
- * not the URL. A fresh install creates a vault named `default`, so
8
- * `/vault/default/...` is the baseline URL for single-vault deployments.
4
+ * All per-vault resources live under `/vault/<name>/...`. There is no
5
+ * unscoped fallback a request must name the vault it targets. A fresh
6
+ * install creates a vault named `default`, so `/vault/default/...` is the
7
+ * baseline URL for single-vault deployments.
9
8
  *
10
9
  * Dispatch shape:
11
10
  *
12
11
  * /.well-known/parachute.json — NOT served here (CLI owns it at
13
12
  * origin root; vault never handles it)
14
- * /.well-known/oauth-protected-resource/mcp
15
- * — root-endpoint PRM (RFC 9728) for the
16
- * canonical `/mcp` below (U1)
17
- * /mcp[/*] — canonical root MCP endpoint; the vault
18
- * is DERIVED FROM THE TOKEN, then the
19
- * request re-dispatches through the same
20
- * per-vault machinery (U1)
21
13
  * /health — liveness ping, vault names leaked
22
14
  * only to authenticated callers
23
15
  * /vaults/list — public vault-name discovery (can be
@@ -29,20 +21,15 @@
29
21
  * /vault/<name>/.well-known/oauth-* — discovery forwarder; metadata names
30
22
  * the hub as the authorization server
31
23
  * (vault is resource-server only)
32
- * /vault/<name>/mcp[/*] — MCP endpoint (Bearer auth); the
33
- * URL-addressed twin of root `/mcp`,
34
- * untouched and permanent
24
+ * /vault/<name>/mcp[/*] — MCP endpoint (Bearer auth)
35
25
  * /vault/<name>/view/<idOrPath> — auth-aware HTML view
36
26
  * /vault/<name>/public/<noteId> — legacy alias → /view redirect
37
27
  * /vault/<name> — vault metadata + stats (auth)
38
28
  * /vault/<name>/api/... — REST surface (auth)
39
29
  *
40
- * The root `/mcp` here is NOT the old unscoped compat prefix (that pre-0.5.0
41
- * `/mcp` an alias for a single default vault — is gone). It's the token-
42
- * derived door: same auth machinery as the per-vault endpoint, reached without
43
- * the client having to know the vault's URL name. There is still deliberately
44
- * no compat for the old `/api/*`, `/oauth/*`, `/view/*`, or `/vaults/<name>/*`
45
- * prefixes; clients using those must re-authenticate and point at the new URLs.
30
+ * There is deliberately no compat for the old `/api/*`, `/mcp`, `/oauth/*`,
31
+ * `/view/*`, or `/vaults/<name>/*` prefixes. Clients must re-authenticate
32
+ * after the upgrade and point at the new URLs.
46
33
  *
47
34
  * **No standalone OAuth issuer.** vault does not mint OAuth tokens or
48
35
  * render a consent UI. Hub is the issuer; vault validates hub-signed
@@ -63,7 +50,6 @@ import {
63
50
  import {
64
51
  authenticateVaultRequest,
65
52
  authenticateGlobalRequest,
66
- deriveVaultFromToken,
67
53
  extractApiKey,
68
54
  } from "./auth.ts";
69
55
  import { hasScopeForVault, hasMigrateScopeForVault, SCOPE_ADMIN, SCOPE_READ, scopeForMethod, verbForMethod } from "./scopes.ts";
@@ -92,7 +78,6 @@ import { handleTriggers } from "./triggers-api.ts";
92
78
  import { expandTokenTagScope } from "./tag-scope.ts";
93
79
  import {
94
80
  handleProtectedResource,
95
- handleRootProtectedResource,
96
81
  handleAuthorizationServer,
97
82
  getBaseUrl,
98
83
  } from "./oauth-discovery.ts";
@@ -118,7 +103,6 @@ import {
118
103
  } from "./mirror-routes.ts";
119
104
  import { getMirrorManager } from "./mirror-registry.ts";
120
105
  import { buildUsageReport } from "./usage.ts";
121
- import { handleTicketSpend } from "./attachment-tickets.ts";
122
106
 
123
107
  /**
124
108
  * Decorate a 401 response from the MCP endpoint with the RFC 9728 challenge
@@ -150,58 +134,6 @@ async function withMcpChallenge(
150
134
  return new Response(body, { status: 401, headers });
151
135
  }
152
136
 
153
- /**
154
- * 401 + RFC 9728 challenge for the canonical ROOT `/mcp` endpoint (U1), when
155
- * the token is absent / invalid / names no single vault. Mirrors
156
- * `withMcpChallenge` but points at the ROOT protected-resource metadata
157
- * (`/.well-known/oauth-protected-resource/mcp`) rather than a vault-scoped one,
158
- * since the root resource is vault-agnostic. A spec-following MCP client
159
- * follows this pointer to the root PRM, discovers the hub, and requests the
160
- * un-narrowed scopes the hub's consent picker narrows to a chosen vault.
161
- */
162
- function rootMcpChallenge(req: Request): Response {
163
- const base = getBaseUrl(req);
164
- return Response.json(
165
- { error: "Unauthorized", message: "API key required" },
166
- {
167
- status: 401,
168
- headers: {
169
- "WWW-Authenticate": `Bearer resource_metadata="${base}/.well-known/oauth-protected-resource/mcp"`,
170
- },
171
- },
172
- );
173
- }
174
-
175
- /**
176
- * Shared scoped-MCP dispatch tail used by BOTH the URL-addressed per-vault
177
- * endpoint (`/vault/<name>/mcp`) and the token-derived root endpoint (`/mcp`,
178
- * U1). Runs the FULL per-vault auth machinery via `authenticateVaultRequest`
179
- * (audience strict-check, `vault_scope` pin, broad-scope rejection, tag-scope
180
- * parse) and, on success, hands off to `handleScopedMcp`. A 401 is decorated
181
- * with the per-vault RFC 9728 challenge. Because both entry points funnel
182
- * through this one function, a root request behaves byte-identically to the
183
- * same token at the per-vault URL — and, for the root, the re-validation here
184
- * is the authoritative gate: a derivation that named the wrong vault fails the
185
- * audience strict-check, it never bypasses it.
186
- */
187
- async function dispatchScopedMcp(
188
- req: Request,
189
- vaultName: string,
190
- vaultConfig: VaultConfig,
191
- ): Promise<Response> {
192
- const auth = await authenticateVaultRequest(req, vaultConfig);
193
- if ("error" in auth) return withMcpChallenge(auth.error, req, vaultName);
194
- // Thread the RAW caller bearer (the exact credential the session presented)
195
- // into the MCP layer so the manage-token tool can forward it to hub's
196
- // mint-token attenuation proxy (vault#403, MGT). Only the raw validated
197
- // bearer — never a fabricated one. extractApiKey returns the same value
198
- // `authenticateVaultRequest` validated above; non-forwardable credentials
199
- // (env-var secret, legacy pvt_*) are handled by manage-token itself (it only
200
- // forwards JWT-shaped bearers).
201
- const callerBearer = extractApiKey(req);
202
- return handleScopedMcp(req, vaultName, auth, callerBearer);
203
- }
204
-
205
137
  /**
206
138
  * Check if a /view request has a valid API key (header or ?key= query param).
207
139
  * Returns true if authenticated, false if not. Never rejects — unauthenticated
@@ -327,18 +259,6 @@ export async function route(
327
259
  return handleAuthorizationServer(req, vaultName);
328
260
  }
329
261
 
330
- // Root-endpoint protected-resource metadata (RFC 9728 §3.1 path-insertion)
331
- // for the canonical `/mcp` endpoint (U1). The root resource is vault-
332
- // agnostic — the target vault is derived from the token, not the URL — so
333
- // this document carries no vault name and advertises the un-narrowed
334
- // `vault:read` / `vault:write` scopes; the hub's consent picker narrows them
335
- // to a chosen vault at authorization time. Distinct from the per-vault
336
- // insertion shapes above (which require a `/vault/<name>` segment), so there
337
- // is no overlap. The `rootMcpChallenge` 401 points clients here.
338
- if (path === "/.well-known/oauth-protected-resource/mcp") {
339
- return handleRootProtectedResource(req);
340
- }
341
-
342
262
  // ---------------------------------------------------------------------
343
263
  // Cross-vault / origin-root endpoints
344
264
  // ---------------------------------------------------------------------
@@ -447,43 +367,6 @@ export async function route(
447
367
  return Response.json({ vaults });
448
368
  }
449
369
 
450
- // ---------------------------------------------------------------------
451
- // Canonical root MCP endpoint — token-derived vault dispatch (U1).
452
- //
453
- // `POST /mcp` (plus the GET/DELETE the streamable-HTTP transport uses, and
454
- // the `/mcp/*` subpaths) is the vault-AGNOSTIC entry point: the target vault
455
- // is derived from the TOKEN, not the URL. We validate the bearer with the
456
- // same scope-guard trust kernel the per-vault path uses, read the vault name
457
- // from the token's claims, then RE-DISPATCH through the identical per-vault
458
- // machinery (`dispatchScopedMcp` → `authenticateVaultRequest` →
459
- // `handleScopedMcp`). The re-dispatch re-validates WITH the audience pin, so
460
- // a bad derivation fails the inner check rather than bypassing it (defense in
461
- // depth). The legacy URL-addressed `/vault/<name>/mcp` is untouched and lives
462
- // forever.
463
- //
464
- // No / invalid / unnameable token → 401 whose `WWW-Authenticate` points at
465
- // the ROOT PRM (`/.well-known/oauth-protected-resource/mcp`), so a spec-
466
- // following MCP client discovers the hub and requests the un-narrowed scopes
467
- // its consent picker narrows to a chosen vault.
468
- if (path === "/mcp" || path.startsWith("/mcp/")) {
469
- const derived = await deriveVaultFromToken(req);
470
- if ("error" in derived) {
471
- return rootMcpChallenge(req);
472
- }
473
- const vaultConfig = readVaultConfig(derived.vaultName);
474
- if (!vaultConfig) {
475
- // A validly-signed hub token naming a vault that isn't on THIS server.
476
- // Mirror the per-vault 404 (`/vault/<name>/mcp` on a missing vault) — the
477
- // holder knows which vault their token targets; nothing is leaked to an
478
- // anonymous caller (they never validate a token to reach here).
479
- return Response.json(
480
- { error: "Vault not found", vault: derived.vaultName },
481
- { status: 404 },
482
- );
483
- }
484
- return dispatchScopedMcp(req, derived.vaultName, vaultConfig);
485
- }
486
-
487
370
  // ---------------------------------------------------------------------
488
371
  // Per-vault routing: /vault/<name>/...
489
372
  // ---------------------------------------------------------------------
@@ -607,40 +490,28 @@ export async function route(
607
490
  return handleAuthorizationServer(req, vaultName);
608
491
  }
609
492
 
610
- // Attachment ticket spend (attachment-tickets design, Wave 1) — no auth
611
- // beyond the ticket itself; the ticket IS the credential (single-use,
612
- // short TTL, scoped to exactly one upload slot or one attachment's
613
- // bytes). Deliberately placed BEFORE `authenticateVaultRequest` below —
614
- // a bearer-less runtime (a bare `curl`) must be able to spend a ticket
615
- // without ever holding this vault's API key. The path segment
616
- // (`/tickets/<id>`) is outside the authed `/api` tree and outside
617
- // `/mcp` on purpose — see `request-attachment-upload`/`-download`
618
- // (core/src/mcp.ts) for where tickets are minted.
619
- const vaultTicketMatch = subpath.match(/^\/tickets\/([^/]+)$/);
620
- if (vaultTicketMatch) {
621
- const ticketId = decodeURIComponent(vaultTicketMatch[1]!);
622
- const store = getVaultStore(vaultName);
623
- return handleTicketSpend(req, ticketId, vaultName, store);
624
- }
625
-
626
- // MCP (per-vault, single-vault session) — the URL names the vault. Shares
627
- // the exact dispatch tail with the token-derived root `/mcp` (U1), so both
628
- // behave identically. Handled BEFORE the shared authenticated-surface auth
629
- // below because `dispatchScopedMcp` owns its own auth + 401 challenge; a
630
- // non-MCP 401 further down never carries the RFC 9728 challenge (it's
631
- // MCP-only).
632
- if (subpath === "/mcp" || subpath.startsWith("/mcp/")) {
633
- return dispatchScopedMcp(req, vaultName, vaultConfig);
634
- }
635
-
636
493
  // ---------------------------------------------------------------------
637
494
  // Authenticated surface
638
495
  // ---------------------------------------------------------------------
639
496
 
640
497
  const store = getVaultStore(vaultName);
641
498
  const auth = await authenticateVaultRequest(req, vaultConfig);
499
+ const isScopedMcp = subpath === "/mcp" || subpath.startsWith("/mcp/");
642
500
  if ("error" in auth) {
643
- return auth.error;
501
+ return isScopedMcp ? withMcpChallenge(auth.error, req, vaultName) : auth.error;
502
+ }
503
+
504
+ // MCP (per-vault, single-vault session).
505
+ if (isScopedMcp) {
506
+ // Thread the RAW caller bearer (the exact credential the session
507
+ // presented) into the MCP layer so the manage-token tool can forward it
508
+ // to hub's mint-token attenuation proxy (vault#403, MGT). Only the raw
509
+ // validated bearer — never a fabricated one. extractApiKey returns the
510
+ // same value `authenticateVaultRequest` validated above; non-forwardable
511
+ // credentials (env-var secret, legacy pvt_*) are handled by manage-token
512
+ // itself (it only forwards JWT-shaped bearers).
513
+ const callerBearer = extractApiKey(req);
514
+ return handleScopedMcp(req, vaultName, auth, callerBearer);
644
515
  }
645
516
 
646
517
  // Bare `/vault/<name>` — single-vault root. Returns name, description,
package/src/scopes.ts CHANGED
@@ -263,28 +263,6 @@ export function validateMintedScopes(
263
263
  return { ok: true };
264
264
  }
265
265
 
266
- /**
267
- * Distinct vault names NAMED by narrowed `vault:<name>:<verb>` scopes in the
268
- * granted list. Broad `vault:<verb>` scopes name no vault (a legacy/operator
269
- * shape) and are skipped; the `migrate` axis isn't a read/write/admin verb so
270
- * `decomposeVaultScope` returns null for it and it too is skipped.
271
- *
272
- * Used by the canonical root `/mcp` endpoint (U1) to read the target vault
273
- * from a token's scope claim — one of three agreeing sources (scope / `aud` /
274
- * single-element `vault_scope`) that `deriveVaultFromToken` cross-checks. A
275
- * hub-minted token carries scopes for exactly one vault, so this returns a
276
- * single-element list in practice; a return of length ≠ 1 signals a
277
- * malformed/multi-vault scope set the derivation treats as a disagreement.
278
- */
279
- export function narrowedVaultNames(granted: string[]): string[] {
280
- const names = new Set<string>();
281
- for (const s of granted) {
282
- const d = decomposeVaultScope(s);
283
- if (d && d.vault !== null) names.add(d.vault);
284
- }
285
- return [...names];
286
- }
287
-
288
266
  /**
289
267
  * Detect a broad `vault:<verb>` scope in a granted list. Hub-issued JWTs
290
268
  * must NOT carry broad vault scopes — the hub mints `vault:<name>:<verb>` so
package/src/server.ts CHANGED
@@ -21,7 +21,6 @@ import { migrateVaultKeys } from "./token-store.ts";
21
21
  import { resolveFirstBootVaultName, reservedNameSquatWarnings } from "./vault-name.ts";
22
22
  import { getVaultStore, getVaultNameForStore, getSharedEmbeddingProvider } from "./vault-store.ts";
23
23
  import { EmbeddingWorker, registerEmbeddingHook } from "./embedding-worker.ts";
24
- import { startAttachmentTicketSweep, stopAttachmentTicketSweep } from "./attachment-tickets.ts";
25
24
  import { seedOnboardingNotesBestEffort } from "./onboarding-seed.ts";
26
25
  import { defaultHookRegistry } from "../core/src/hooks.ts";
27
26
  import { registerTriggers } from "./triggers.ts";
@@ -238,11 +237,6 @@ const embeddingWorker = new EmbeddingWorker({
238
237
  registerEmbeddingHook(defaultHookRegistry, embeddingWorker, (store) => getVaultNameForStore(store as never));
239
238
  embeddingWorker.start();
240
239
 
241
- // Attachment-ticket sweep (vault#612) — drops expired-unspent tickets from
242
- // the in-process store so an abandoned mint (an agent that never curls)
243
- // doesn't sit in memory forever. See src/attachment-tickets.ts.
244
- startAttachmentTicketSweep();
245
-
246
240
  if (process.env.VAULT_AUTH_TOKEN?.trim()) {
247
241
  console.log("[auth] VAULT_AUTH_TOKEN set — server-wide operator bearer active");
248
242
  }
@@ -625,7 +619,6 @@ async function shutdown(signal: string): Promise<void> {
625
619
  // Then drain hooks + stop the transcription/embedding workers in
626
620
  // parallel.
627
621
  embeddingWorker.stop();
628
- stopAttachmentTicketSweep();
629
622
  await Promise.all([
630
623
  defaultHookRegistry.drain(),
631
624
  transcriptionWorker?.stop() ?? Promise.resolve(),