@parcha/agentrun-serve 0.1.0-beta.54 → 0.1.0-beta.56

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 (234) hide show
  1. package/CHANGELOG.md +142 -2
  2. package/README.md +310 -98
  3. package/dist/adapters.d.ts +11 -0
  4. package/dist/adapters.js +49 -0
  5. package/dist/agent-package.d.ts +3 -3
  6. package/dist/agent-package.js +6 -13
  7. package/dist/agents-store.js +6 -5
  8. package/dist/answer-json.d.ts +6 -2
  9. package/dist/answer-json.js +22 -4
  10. package/dist/api-only.d.ts +16 -4
  11. package/dist/api-only.js +23 -7
  12. package/dist/calendar/tools.js +3 -2
  13. package/dist/chatgpt-plan.d.ts +16 -1
  14. package/dist/chatgpt-plan.js +116 -12
  15. package/dist/claude-login.d.ts +34 -5
  16. package/dist/claude-login.js +31 -7
  17. package/dist/claude-sign-in.d.ts +35 -0
  18. package/dist/claude-sign-in.js +90 -0
  19. package/dist/cli.js +5 -3
  20. package/dist/composio-made.d.ts +26 -1
  21. package/dist/composio-made.js +39 -2
  22. package/dist/composio-preset.d.ts +3 -2
  23. package/dist/composio-preset.js +46 -14
  24. package/dist/computer-held-routes.js +2 -3
  25. package/dist/computer-setting.d.ts +4 -2
  26. package/dist/computer-setting.js +0 -2
  27. package/dist/computers-running.d.ts +8 -0
  28. package/dist/computers-running.js +27 -0
  29. package/dist/console/assets/backend-Boh7tC4i.js +166 -0
  30. package/dist/console/assets/index-CQ3cJRzI.js +746 -0
  31. package/dist/console/assets/index-DKkaS3a0.css +1 -0
  32. package/dist/console/index.html +2 -2
  33. package/dist/docker-host-addresses.d.ts +23 -3
  34. package/dist/docker-host-addresses.js +90 -18
  35. package/dist/exa-keyless.d.ts +2 -3
  36. package/dist/exa-keyless.js +43 -127
  37. package/dist/firecrawl-keyless.d.ts +41 -0
  38. package/dist/firecrawl-keyless.js +152 -0
  39. package/dist/handed-secret.d.ts +3 -1
  40. package/dist/handed-secret.js +4 -2
  41. package/dist/harness-keys.d.ts +98 -0
  42. package/dist/harness-keys.js +247 -0
  43. package/dist/harness-recipe-entry.d.ts +14 -9
  44. package/dist/harness-recipe-entry.js +25 -14
  45. package/dist/harness-table.d.ts +16 -1
  46. package/dist/harness-table.js +19 -7
  47. package/dist/harness.d.ts +4 -0
  48. package/dist/harness.js +12 -1
  49. package/dist/host-check.d.ts +6 -0
  50. package/dist/host-check.js +10 -0
  51. package/dist/import-summary.d.ts +1 -1
  52. package/dist/import-summary.js +1 -1
  53. package/dist/import.d.ts +5 -5
  54. package/dist/import.js +54 -67
  55. package/dist/key-refusal.d.ts +21 -5
  56. package/dist/key-refusal.js +231 -37
  57. package/dist/keyless-mcp.d.ts +36 -0
  58. package/dist/keyless-mcp.js +127 -0
  59. package/dist/learn/audit.d.ts +3 -3
  60. package/dist/learn/audit.js +3 -3
  61. package/dist/learn/commit.d.ts +8 -7
  62. package/dist/learn/commit.js +60 -20
  63. package/dist/learn/compile.d.ts +4 -3
  64. package/dist/learn/compile.js +7 -5
  65. package/dist/learn/folder.d.ts +16 -3
  66. package/dist/learn/folder.js +46 -13
  67. package/dist/learn/promote.d.ts +37 -13
  68. package/dist/learn/promote.js +112 -40
  69. package/dist/learn/secrets.d.ts +20 -10
  70. package/dist/learn/secrets.js +44 -176
  71. package/dist/learning-routes.d.ts +22 -17
  72. package/dist/learning-routes.js +40 -39
  73. package/dist/learning.js +2 -2
  74. package/dist/llms.txt +1 -1
  75. package/dist/manifest.d.ts +10 -0
  76. package/dist/manifest.js +30 -0
  77. package/dist/mcp-pin-first.d.ts +4 -2
  78. package/dist/mcp-pin-first.js +5 -2
  79. package/dist/mcp-secret-writes.d.ts +16 -7
  80. package/dist/mcp-secret-writes.js +35 -27
  81. package/dist/mcp-servers-store.d.ts +30 -6
  82. package/dist/mcp-servers-store.js +112 -28
  83. package/dist/mcp-sources.d.ts +10 -2
  84. package/dist/mcp-sources.js +9 -2
  85. package/dist/mcp-store.d.ts +13 -7
  86. package/dist/mcp-store.js +12 -9
  87. package/dist/model-form.d.ts +3 -2
  88. package/dist/model-form.js +11 -7
  89. package/dist/model.d.ts +20 -4
  90. package/dist/model.js +68 -9
  91. package/dist/native/history.d.ts +1 -1
  92. package/dist/native/history.js +2 -2
  93. package/dist/ollama-provider.d.ts +92 -0
  94. package/dist/ollama-provider.js +495 -0
  95. package/dist/openapi/agentrun-v2.openapi.json +2 -2
  96. package/dist/openapi.d.ts +3 -3
  97. package/dist/openapi.js +197 -55
  98. package/dist/page.d.ts +11 -3
  99. package/dist/page.js +22 -7
  100. package/dist/place-offers.d.ts +25 -1
  101. package/dist/place-offers.js +62 -8
  102. package/dist/print.js +1 -1
  103. package/dist/purged-workflow.d.ts +35 -7
  104. package/dist/purged-workflow.js +73 -26
  105. package/dist/redact/match.d.ts +74 -0
  106. package/dist/redact/match.js +771 -0
  107. package/dist/run-computer.d.ts +2 -0
  108. package/dist/run-computer.js +2 -0
  109. package/dist/run-files.d.ts +4 -0
  110. package/dist/run-files.js +4 -0
  111. package/dist/run-keys.d.ts +2 -4
  112. package/dist/run-keys.js +6 -7
  113. package/dist/run-launch.d.ts +4 -0
  114. package/dist/run-launch.js +4 -0
  115. package/dist/run-reads-over-store.d.ts +3 -2
  116. package/dist/run-reads-over-store.js +66 -2
  117. package/dist/run-secrets.js +5 -2
  118. package/dist/run-timeline-served.d.ts +1 -1
  119. package/dist/run-timeline-served.js +4 -4
  120. package/dist/runner/adapter-loop.d.ts +9 -3
  121. package/dist/runner/adapter-loop.js +34 -15
  122. package/dist/runner/adapter-run-spec.d.ts +3 -0
  123. package/dist/runner/adapter-run-spec.js +9 -4
  124. package/dist/runner/audit.js +8 -4
  125. package/dist/runner/beside.d.ts +2 -1
  126. package/dist/runner/beside.js +13 -5
  127. package/dist/runner/claim-secrets.d.ts +5 -8
  128. package/dist/runner/claim-secrets.js +6 -0
  129. package/dist/runner/cli-run-spec.d.ts +1 -1
  130. package/dist/runner/cli-run-spec.js +41 -7
  131. package/dist/runner/harness-loops.d.ts +22 -9
  132. package/dist/runner/harness-loops.js +21 -11
  133. package/dist/runner/held.d.ts +3 -0
  134. package/dist/runner/learning-end.d.ts +1 -1
  135. package/dist/runner/learning-end.js +8 -3
  136. package/dist/runner/loop-contract.d.ts +9 -4
  137. package/dist/runner/main.js +12 -6
  138. package/dist/runner/mcp-claim.d.ts +3 -0
  139. package/dist/runner/mcp-claim.js +26 -2
  140. package/dist/runner/mcp-run.d.ts +3 -0
  141. package/dist/runner/mcp-run.js +2 -1
  142. package/dist/runner/metrics.d.ts +10 -10
  143. package/dist/runner/metrics.js +19 -38
  144. package/dist/runner/model-route.d.ts +3 -0
  145. package/dist/runner/model-route.js +6 -2
  146. package/dist/runner/presence.d.ts +4 -3
  147. package/dist/runner/presence.js +4 -2
  148. package/dist/runner/promotion.d.ts +15 -9
  149. package/dist/runner/promotion.js +25 -15
  150. package/dist/runner/run-child-host.d.ts +2 -0
  151. package/dist/runner/run-child-host.js +19 -9
  152. package/dist/runner/run-child.js +3 -1
  153. package/dist/runner/runner.d.ts +4 -0
  154. package/dist/runner/runner.js +33 -3
  155. package/dist/runner/sdk-loop.js +2 -0
  156. package/dist/runner/workflow-loop.d.ts +2 -2
  157. package/dist/runner/workflow-loop.js +1 -2
  158. package/dist/runner/workflow-spec.d.ts +1 -1
  159. package/dist/runner/workflow-spec.js +6 -6
  160. package/dist/sdk/check.d.ts +4 -1
  161. package/dist/sdk/check.js +2 -1
  162. package/dist/sdk/runs.d.ts +2 -2
  163. package/dist/sdk/runs.js +8 -6
  164. package/dist/secret-cut.js +2 -1
  165. package/dist/secret-writes.d.ts +3 -2
  166. package/dist/secret-writes.js +1 -1
  167. package/dist/server.d.ts +49 -10
  168. package/dist/server.js +324 -118
  169. package/dist/standard-webhooks.d.ts +11 -0
  170. package/dist/standard-webhooks.js +20 -2
  171. package/dist/store/dialect.d.ts +3 -0
  172. package/dist/store/dialect.js +1 -0
  173. package/dist/store/index.d.ts +2 -2
  174. package/dist/store/index.js +1 -1
  175. package/dist/store/postgres/dialect.js +3 -0
  176. package/dist/store/schema.js +5 -2
  177. package/dist/store/sqlite/store.js +133 -47
  178. package/dist/store/store.d.ts +64 -15
  179. package/dist/store/store.js +7 -0
  180. package/dist/store-reads/journal.d.ts +7 -2
  181. package/dist/store-reads/journal.js +9 -6
  182. package/dist/store-reads/learning.d.ts +12 -9
  183. package/dist/store-reads/learning.js +18 -24
  184. package/dist/store-view.d.ts +0 -5
  185. package/dist/store-view.js +10 -9
  186. package/dist/template-eval.d.ts +51 -6
  187. package/dist/template-eval.js +177 -53
  188. package/dist/template-import.js +4 -2
  189. package/dist/templates.d.ts +5 -3
  190. package/dist/templates.js +19 -40
  191. package/dist/timeline/index.d.ts +1 -1
  192. package/dist/timeline/index.js +1 -1
  193. package/dist/timeline/labels.d.ts +2 -2
  194. package/dist/timeline/labels.js +2 -2
  195. package/dist/timeline/timeline.d.ts +14 -9
  196. package/dist/timeline/timeline.js +15 -7
  197. package/dist/tool-gate.js +3 -1
  198. package/dist/tool-plane.d.ts +17 -9
  199. package/dist/tool-plane.js +38 -17
  200. package/dist/tools.js +2 -2
  201. package/dist/trigger-launch-hook.d.ts +16 -8
  202. package/dist/trigger-launch-hook.js +32 -23
  203. package/dist/trigger-targets.d.ts +18 -8
  204. package/dist/trigger-targets.js +42 -19
  205. package/dist/triggers.d.ts +85 -23
  206. package/dist/triggers.js +303 -96
  207. package/dist/validate.js +1 -1
  208. package/dist/web-fake.js +3 -3
  209. package/dist/web-fetch-direct.d.ts +4 -0
  210. package/dist/web-fetch-direct.js +8 -1
  211. package/dist/web-fetch.d.ts +12 -3
  212. package/dist/web-fetch.js +47 -14
  213. package/dist/web-search.d.ts +33 -4
  214. package/dist/web-search.js +100 -41
  215. package/dist/webhooks.js +4 -5
  216. package/dist/workspace/computer-deps.d.ts +2 -1
  217. package/dist/workspace/computer-deps.js +5 -4
  218. package/dist/workspace/computer-for-claim.d.ts +8 -2
  219. package/dist/workspace/computer-for-claim.js +28 -9
  220. package/dist/workspace/computer-janitor.d.ts +15 -7
  221. package/dist/workspace/computer-janitor.js +33 -8
  222. package/dist/workspace/computer-kept.d.ts +52 -4
  223. package/dist/workspace/computer-kept.js +118 -6
  224. package/dist/workspace/run-end-computers.d.ts +5 -1
  225. package/dist/workspace/run-end-computers.js +27 -1
  226. package/dist/wrapped.d.ts +57 -0
  227. package/dist/wrapped.js +51 -0
  228. package/launcher/agentrun-serve +5 -1
  229. package/package.json +17 -13
  230. package/templates/README.md +10 -9
  231. package/dist/console/assets/index-B_85An2t.js +0 -745
  232. package/dist/console/assets/index-n1YsbnYh.css +0 -1
  233. package/dist/import-sources.d.ts +0 -116
  234. package/dist/import-sources.js +0 -693
@@ -1,10 +1,10 @@
1
1
  import { type Listed, type McpPrices, type McpTool } from "./mcp-probe.js";
2
2
  import { type SecretMeta } from "./secret-writes.js";
3
3
  import { type ClientKind, type OAuthNetwork } from "./mcp-oauth.js";
4
- import { type Cleanup, type ComposioMade } from "./composio-made.js";
4
+ import { type Cleanup, COMPOSIO_KEY_ENV, type ComposioMade } from "./composio-made.js";
5
5
  import { type RecordedRow, type SourceRecord } from "./mcp-sources.js";
6
6
  import { type ToolSettingsMap } from "./tool-plane.js";
7
- import { type McpCredentialMark, type Mcp, type McpOauth, type Settings, type Store } from "./store/index.js";
7
+ import { type McpCredentialMark, type Mcp, type McpOauth, type McpServerRow, type Settings, type Store } from "./store/index.js";
8
8
  import { MCP_ALLOW_LOOPBACK_ENV } from "./start-switches.js";
9
9
  /** The store slice this layer reads and writes; `settings` for the private-network OAuth hosts the owner allows and each
10
10
  * server's source and toolkit (mcp-sources.ts). */
@@ -64,10 +64,15 @@ export declare class McpServersOverStore {
64
64
  /** The one origin a github_app server may have (GITHUB_MCP_ORIGIN; a test's fake through the server's seam): the App's
65
65
  * tokens are minted for, and sent to, nothing else. */
66
66
  appOrigin: string;
67
+ /** The one origin a key read from the environment is sent to (COMPOSIO_ORIGIN; a test's fake through the server's
68
+ * seam): a Composio server saved with api_key_env at any other address is refused (composioEnvKey). */
69
+ composioOrigin: string;
67
70
  private readonly sealKey;
68
71
  private readonly signIns;
69
72
  /** The callbacks under way, which close() waits out before the server lets its data go. */
70
73
  private readonly inflight;
74
+ /** Each server name's save turn (nameTurn): the last save in it, settled either way. */
75
+ private readonly nameTurns;
71
76
  private readonly stopped;
72
77
  /** Whether this server is a local install (attach): only then may an MCP server's URL be on loopback. */
73
78
  private loopback;
@@ -142,6 +147,7 @@ export declare class McpServersOverStore {
142
147
  expires_at?: string | undefined;
143
148
  } | undefined;
144
149
  auth: {
150
+ api_key_env?: "COMPOSIO_API_KEY" | undefined;
145
151
  headers?: string[] | undefined;
146
152
  type: string;
147
153
  };
@@ -184,6 +190,7 @@ export declare class McpServersOverStore {
184
190
  expires_at?: string | undefined;
185
191
  } | undefined;
186
192
  auth: {
193
+ api_key_env?: "COMPOSIO_API_KEY" | undefined;
187
194
  headers?: string[] | undefined;
188
195
  type: string;
189
196
  };
@@ -244,8 +251,11 @@ export declare class McpServersOverStore {
244
251
  * server's record now owns or the caller removes. `guard`, a preset row's: the turn its write and cleanup take, once
245
252
  * the listing is done (never across it), and the check of the triggers that use it, made in that turn before a write
246
253
  * that leaves what they stand on (an ordinary save moving the row off Composio, a preset save as another user or auth
247
- * config): refused, nothing written. */
248
- save(name: string, body: Record<string, unknown>, stop?: AbortSignal, checkedAgainst?: McpCredentialMark, made?: SourceRecord, stands?: ComposioMade, dropped?: (left: ComposioDropped) => void, guard?: SaveGuard): Promise<{
254
+ * config): refused, nothing written. `byToken`: the API token makes the save (a request the secret-write gate read), so
255
+ * a row it makes is recorded as the token's (mcp-sources.ts savedByToken), and it writes over a saved row only while
256
+ * that row still carries the token's mark, compared in the row's own statement (412, nothing written, when an owner's
257
+ * save took it meanwhile). */
258
+ save(name: string, body: Record<string, unknown>, stop?: AbortSignal, checkedAgainst?: McpCredentialMark, made?: SourceRecord, stands?: ComposioMade, dropped?: (left: ComposioDropped) => void, guard?: SaveGuard, byToken?: boolean): Promise<{
249
259
  listed_at: string;
250
260
  updated_at: string;
251
261
  secret: SecretMeta;
@@ -256,6 +266,7 @@ export declare class McpServersOverStore {
256
266
  expires_at?: string | undefined;
257
267
  } | undefined;
258
268
  auth: {
269
+ api_key_env?: "COMPOSIO_API_KEY" | undefined;
259
270
  headers?: string[] | undefined;
260
271
  type: string;
261
272
  };
@@ -286,6 +297,11 @@ export declare class McpServersOverStore {
286
297
  source: "url" | "github" | "composio";
287
298
  toolkit: string | null;
288
299
  }>;
300
+ /** `work` in the turn of the saved server `name`, after every save of that name already in it: a save writes its row and
301
+ * then its source record (mcp-sources.ts), so the token's mark, written after its row, never lands on a row an owner's
302
+ * save wrote between the two. One server holds a store (StoreOwned), so the turn is this process's. A delete takes
303
+ * none: its statement compares the mark itself (Mcp.removeIfCredential). */
304
+ private nameTurn;
289
305
  /** POST refresh: list the saved server's tools again, keep them, and say what was added, removed and changed. */
290
306
  refresh(name: string, stop?: AbortSignal): Promise<{
291
307
  added: string[];
@@ -302,6 +318,7 @@ export declare class McpServersOverStore {
302
318
  expires_at?: string | undefined;
303
319
  } | undefined;
304
320
  auth: {
321
+ api_key_env?: "COMPOSIO_API_KEY" | undefined;
305
322
  headers?: string[] | undefined;
306
323
  type: string;
307
324
  };
@@ -341,8 +358,12 @@ export declare class McpServersOverStore {
341
358
  * none: removing it drops no secret); the row goes only while it is still that, in the store's one statement, so a
342
359
  * credential saved meanwhile is never dropped by it. An OAuth one's sign-in is then let go at its authorization server
343
360
  * (its refresh token revoked, RFC 7009, where its metadata names a revocation_endpoint), behind the answer: best effort,
344
- * never holding or failing the removal. A claimed run sees the removal at its next call (runner/mcp-claim.ts check). */
345
- remove(name: string, checkedAgainst?: McpCredentialMark): Promise<{
361
+ * never holding or failing the removal. A claimed run sees the removal at its next call (runner/mcp-claim.ts check).
362
+ * `byToken`: the API token removes it, which it may only while the row is one the token saved (savedByToken): its
363
+ * credential and its mark are compared in the deleting statement itself (Mcp.removeIfCredential), never in a read
364
+ * before it, so an owner's save that lands after the gate's check (it drops the mark in the transaction that writes
365
+ * its row) is never removed, and nothing is sent to Composio for it. */
366
+ remove(name: string, checkedAgainst?: McpCredentialMark, byToken?: boolean): Promise<{
346
367
  composio?: ComposioHandle;
347
368
  }>;
348
369
  /** Whether `name` is a Composio preset server now (auth headers, source composio): a save of it takes the Composio turn,
@@ -379,6 +400,9 @@ export declare class McpServersOverStore {
379
400
  * read and checked, never the one saved now: a save at another address with a new credential while the caller waited
380
401
  * (its address check, a listing) must never have that credential sent to the old address. */
381
402
  private opened;
403
+ /** The variable a Composio server saved with api_key_env reads its key from (composio-made.ts key_env): a headers row
404
+ * that seals no credential, whose record for this row names it. None for every other row. */
405
+ keyEnvOf(row: McpServerRow): Promise<typeof COMPOSIO_KEY_ENV | undefined>;
382
406
  /** The saved OAuth token set of `row` (the row read and checked, as opened), for a sign-in again that reuses the kept
383
407
  * client. */
384
408
  private openedSet;
@@ -15,9 +15,9 @@ import { checkPrivateHosts, privateOAuthHosts, savePrivateOAuthHosts } from "./m
15
15
  import { credentialMark } from "./mcp-secret-writes.js";
16
16
  import { secretMeta, secretName } from "./secret-writes.js";
17
17
  import { dialAt, issuerOf, LOOPBACK, MAX_PENDING, MCP_SETTINGS_PAGE, mcpServerAt, McpSignIns, reasonOf, SIGN_IN_AGAIN, systemNetwork } from "./mcp-oauth.js";
18
- import { cleanupOf, composioMadeKey, composioPerRun, droppedOf, madeToWrite, recordedMade, releasedOf, storedMade } from "./composio-made.js";
18
+ import { cleanupOf, COMPOSIO_ORIGIN, composioEnvKey, composioKeyFromEnv, composioMadeKey, composioPerRun, droppedOf, madeToWrite, recordedMade, releasedOf, storedMade } from "./composio-made.js";
19
19
  import { COMPOSIO_PER_RUN, COMPOSIO_USER, composioListingAddress, composioServerId } from "./composio-preset.js";
20
- import { mcpSourceKey, recordedForAnother, sourceOf, sourceToWrite, standInGiven, storedSource, toolkitGiven } from "./mcp-sources.js";
20
+ import { mcpSourceKey, recordedForAnother, savedByToken, sourceOf, sourceToWrite, standInGiven, storedSource, toolkitGiven } from "./mcp-sources.js";
21
21
  import { SEAL_KEY_ENV, sealKeyOf } from "./tool-keys.js";
22
22
  import { GITHUB_MCP_ONLY, GITHUB_MCP_ORIGIN, githubMcpUrl } from "./github.js";
23
23
  import { serverGate } from "./tool-plane.js";
@@ -41,9 +41,10 @@ function changedMeanwhile(name, saved) {
41
41
  throw new ApiError("precondition_failed", `The MCP server ${name}'s credential changed while this save ran (another save, a sign-in or a removal): read it again, then save again.`);
42
42
  return saved;
43
43
  }
44
- function composioHandleOf(removed, cleanup) {
44
+ function composioHandleOf(removed, cleanup, keyEnv) {
45
45
  const id = removed ? composioServerId(removed.url) : undefined;
46
- const key = composioKeyOf(removed?.opened);
46
+ // A server saved with api_key_env seals no key: its variable's value, as this process was started with it.
47
+ const key = composioKeyOf(removed?.opened) ?? (keyEnv && composioKeyFromEnv(keyEnv));
47
48
  return { ...(id && key ? { id, key } : { unusable: true }), ...(cleanup ? { cleanup } : {}) };
48
49
  }
49
50
  /** The Composio key a preset row's opened credential holds (its x-api-key header); none when it does not open. */
@@ -100,10 +101,15 @@ export class McpServersOverStore {
100
101
  /** The one origin a github_app server may have (GITHUB_MCP_ORIGIN; a test's fake through the server's seam): the App's
101
102
  * tokens are minted for, and sent to, nothing else. */
102
103
  appOrigin = GITHUB_MCP_ORIGIN;
104
+ /** The one origin a key read from the environment is sent to (COMPOSIO_ORIGIN; a test's fake through the server's
105
+ * seam): a Composio server saved with api_key_env at any other address is refused (composioEnvKey). */
106
+ composioOrigin = COMPOSIO_ORIGIN;
103
107
  sealKey;
104
108
  signIns;
105
109
  /** The callbacks under way, which close() waits out before the server lets its data go. */
106
110
  inflight = new Set();
111
+ /** Each server name's save turn (nameTurn): the last save in it, settled either way. */
112
+ nameTurns = new Map();
107
113
  stopped = new AbortController();
108
114
  /** Whether this server is a local install (attach): only then may an MCP server's URL be on loopback. */
109
115
  loopback = false;
@@ -260,8 +266,11 @@ export class McpServersOverStore {
260
266
  * server's record now owns or the caller removes. `guard`, a preset row's: the turn its write and cleanup take, once
261
267
  * the listing is done (never across it), and the check of the triggers that use it, made in that turn before a write
262
268
  * that leaves what they stand on (an ordinary save moving the row off Composio, a preset save as another user or auth
263
- * config): refused, nothing written. */
264
- async save(name, body, stop, checkedAgainst, made, stands, dropped, guard) {
269
+ * config): refused, nothing written. `byToken`: the API token makes the save (a request the secret-write gate read), so
270
+ * a row it makes is recorded as the token's (mcp-sources.ts savedByToken), and it writes over a saved row only while
271
+ * that row still carries the token's mark, compared in the row's own statement (412, nothing written, when an owner's
272
+ * save took it meanwhile). */
273
+ async save(name, body, stop, checkedAgainst, made, stands, dropped, guard, byToken = false) {
265
274
  nameOf(name);
266
275
  const was = await this.store.mcp.server(name);
267
276
  // Its URL as every read shows it (shownUrl), sent back unchanged: the saved one.
@@ -270,8 +279,10 @@ export class McpServersOverStore {
270
279
  // is now, checked before anything is listed; written once the server is, for the row it wrote.
271
280
  const sourceIssues = [];
272
281
  const wasSource = await this.sourceOf({ name, auth: auth.type, createdAt: was?.createdAt });
273
- // Whether the row was a Composio preset server (auth headers, source composio), read before this save writes its own.
282
+ // Whether the row was a Composio preset server (auth headers, source composio), read before this save writes its own;
283
+ // and the variable its key was read from, when it was saved with api_key_env.
274
284
  const wasPreset = was !== undefined && was.auth === "headers" && (await this.sourceOf(was)).source === "composio";
285
+ const wasKeyEnv = wasPreset ? await this.keyEnvOf(was) : undefined;
275
286
  // An ordinary save that moves a Composio preset row to another address makes it another service's server: it stops being
276
287
  // Composio's, whatever its path looks like, so its delete never sends the key it holds to Composio. It is url's, with the
277
288
  // toolkit the same save names, else none.
@@ -313,11 +324,23 @@ export class McpServersOverStore {
313
324
  throw invalid([credentialMissing(auth.type, `The address moved from ${new URL(was.url).origin} to ${url.origin}: send the credential again, so a saved one never goes to another origin.`)]);
314
325
  const open = await this.opened(name, was, credentialMissing(auth.type, "").field);
315
326
  headers = open.headers;
316
- keeping = true;
327
+ // A key read from the environment is no sealed one to keep: the row is written sealing none again.
328
+ keeping = !open.env;
317
329
  }
318
- else if (auth.type !== "none") {
330
+ else if (auth.type !== "none" && !stands?.key_env) {
331
+ // A Composio key read from the environment (`stands.key_env`) only lists the tools now: the row seals none.
319
332
  credential = { type: "headers", headers: headers };
320
333
  }
334
+ else if (stands?.key_env) {
335
+ // ...and only at Composio's origin: an address Composio answered elsewhere is refused, and the key is not sent there.
336
+ const got = composioEnvKey(name, url.href, stands.key_env, this.composioOrigin);
337
+ if ("refused" in got)
338
+ throw invalid([{ field: "url", reason: got.reason, message: got.refused }]);
339
+ }
340
+ // The token keeps no sealed credential: the secret-write gate lets its save over no sealed row (mcp-secret-writes.ts
341
+ // tokenWriteOverRefused), so one read here is an owner's, saved since. Refused as changed meanwhile, nothing written.
342
+ if (byToken && keeping)
343
+ changedMeanwhile(name, undefined);
321
344
  // A per-run Composio server, made so now (`stands`) or saved so and kept at its address by an ordinary save.
322
345
  const perRun = Boolean(stands?.per_run) || (!made && was !== undefined && auth.type === "headers" && was.url === url.href && (await this.perRunComposio(name, was)));
323
346
  const listed = await this.listServer(url, await this.withAppToken(auth.type, url, headers ?? {}, stop), send, stop, perRun);
@@ -345,16 +368,31 @@ export class McpServersOverStore {
345
368
  if (moved || (made && stands && (!stood || stood.user !== stands.user || Boolean(stood.per_run) !== Boolean(stands.per_run) || (stood.auth_config?.id ?? null) !== (stands.auth_config?.id ?? null))))
346
369
  await guard.inUse();
347
370
  }
371
+ // Who saved the row (mcp-sources.ts `by`): the token marks a row it makes, and keeps the mark on its own; the owner's
372
+ // row stays the owner's under the token's save. The owner's save over a saved row drops the token's mark in the
373
+ // store's transaction that writes the row, once it is written (`owns`, McpOwnersMark): a save refused (412) leaves
374
+ // the row and its record as they were, so the server stays the token's; one that lands is the owner's with no
375
+ // moment between its row and its record, so the token's write or delete, which compares the mark in its own
376
+ // statement, never takes the row the owner saved.
377
+ const recorded = was ? await this.store.settings.get(mcpSourceKey(name)) : undefined;
378
+ const wasToken = was !== undefined && savedByToken(recorded, was.createdAt);
379
+ const mark = byToken && (was === undefined || wasToken);
380
+ const owns = !byToken && was !== undefined ? { record: mcpSourceKey(name) } : undefined;
348
381
  const saved = keeping
349
382
  // Re-applied only onto a row that still holds a credential of this auth type, at the address this save read: never
350
383
  // onto another one's, nor over a row another save moved meanwhile (a Composio save over it, whose cleanup removed the
351
384
  // MCP server at the address read; Composio's share an origin). It writes no credential column, so it never drops or
352
385
  // replaces one.
353
- ? await this.rewriteKeeping(name, was, fields, "save", (fresh) => fresh.auth === auth.type && fresh.sealed !== null && fresh.url === was.url)
354
- // A new credential, or none: written only while the row's credential is still `against`, in the store's one statement.
355
- : changedMeanwhile(name, await saveMcpServerIfCredential(this.store, this.sealKey, { ...fields(was), ...(credential ? { credential } : {}) }, against, { githubMcpOrigin: this.appOrigin }));
386
+ ? await this.rewriteKeeping(name, was, fields, "save", (fresh) => fresh.auth === auth.type && fresh.sealed !== null && fresh.url === was.url, owns)
387
+ // A new credential, or none: written only while the row's credential is still `against`, and the token's over a
388
+ // saved row only while that row still carries its mark (savedByToken), both in the store's one statement: the
389
+ // check and the write are one step, so an owner's save that lands after the gate's read is never written over.
390
+ : changedMeanwhile(name, await saveMcpServerIfCredential(this.store, this.sealKey, { ...fields(was), ...(credential ? { credential } : {}) }, against, { githubMcpOrigin: this.appOrigin, ...(byToken ? { tokens: { record: mcpSourceKey(name) } } : {}), ...(owns ? { owns } : {}) }));
356
391
  if (source)
357
- await this.store.settings.put(mcpSourceKey(name), storedSource(source, saved, !made && source === standIn));
392
+ await this.store.settings.put(mcpSourceKey(name), storedSource(source, saved, !made && source === standIn, mark));
393
+ // A row the token made with no source given: url with none, as an unrecorded row reads (sourceOf), marked.
394
+ else if (mark && !wasToken)
395
+ await this.store.settings.put(mcpSourceKey(name), storedSource({ source: "url", toolkit: null }, saved, false, true));
358
396
  // The Composio MCP server the row named before, when the preset made it (the row was a preset server) and the row names
359
397
  // it no longer: a preset save over it made a new one, or an ordinary save moved the row off Composio.
360
398
  const madeBefore = wasPreset && (made || moved) ? composioServerId(was.url) : undefined;
@@ -401,14 +439,32 @@ export class McpServersOverStore {
401
439
  catch {
402
440
  key = undefined;
403
441
  }
442
+ key ??= wasKeyEnv && composioKeyFromEnv(wasKeyEnv);
404
443
  dropped?.({ ...(key ? { key } : { unusable: true }), cleanup: left ?? { remove: [], kept: [] }, ...(leftMcp ? { mcp: leftMcp } : {}) });
405
444
  }
406
445
  return saved;
407
446
  };
408
- const saved = guard ? await guard.turn(write) : await write();
447
+ // The name's turn (nameTurn) inside the guard's: the row and its record are written with no other save of the name between.
448
+ const saved = guard ? await guard.turn(() => this.nameTurn(name, write)) : await this.nameTurn(name, write);
409
449
  await this.store.flush();
410
450
  return this.view(saved);
411
451
  }
452
+ /** `work` in the turn of the saved server `name`, after every save of that name already in it: a save writes its row and
453
+ * then its source record (mcp-sources.ts), so the token's mark, written after its row, never lands on a row an owner's
454
+ * save wrote between the two. One server holds a store (StoreOwned), so the turn is this process's. A delete takes
455
+ * none: its statement compares the mark itself (Mcp.removeIfCredential). */
456
+ async nameTurn(name, work) {
457
+ const mine = (this.nameTurns.get(name) ?? Promise.resolve()).then(work);
458
+ const held = mine.then(() => undefined, () => undefined);
459
+ this.nameTurns.set(name, held);
460
+ try {
461
+ return await mine;
462
+ }
463
+ finally {
464
+ if (this.nameTurns.get(name) === held)
465
+ this.nameTurns.delete(name);
466
+ }
467
+ }
412
468
  /** POST refresh: list the saved server's tools again, keep them, and say what was added, removed and changed. */
413
469
  async refresh(name, stop) {
414
470
  const was = await this.need(name);
@@ -438,10 +494,10 @@ export class McpServersOverStore {
438
494
  /** Writes the owner's change over the row it read (`was`), its credential left as it is, by a compare-and-set on that
439
495
  * credential (store.mcp.rewrite). A lost one (a run kept a refreshed set meanwhile) reads the row again and re-applies the
440
496
  * change onto it, while `still` holds of it, at most REWRITE_TRIES writes; then it is refused in words, nothing written. */
441
- async rewriteKeeping(name, was, fields, doing, still) {
497
+ async rewriteKeeping(name, was, fields, doing, still, owns) {
442
498
  let row = was;
443
499
  for (let tries = 1;; tries++) {
444
- const saved = await rewriteMcpServer(this.store, fields(row), row.sealed, { githubMcpOrigin: this.appOrigin });
500
+ const saved = await rewriteMcpServer(this.store, fields(row), row.sealed, { githubMcpOrigin: this.appOrigin, ...(owns ? { owns } : {}) });
445
501
  if (saved)
446
502
  return saved;
447
503
  const fresh = await this.store.mcp.server(name);
@@ -461,18 +517,24 @@ export class McpServersOverStore {
461
517
  * none: removing it drops no secret); the row goes only while it is still that, in the store's one statement, so a
462
518
  * credential saved meanwhile is never dropped by it. An OAuth one's sign-in is then let go at its authorization server
463
519
  * (its refresh token revoked, RFC 7009, where its metadata names a revocation_endpoint), behind the answer: best effort,
464
- * never holding or failing the removal. A claimed run sees the removal at its next call (runner/mcp-claim.ts check). */
465
- async remove(name, checkedAgainst) {
520
+ * never holding or failing the removal. A claimed run sees the removal at its next call (runner/mcp-claim.ts check).
521
+ * `byToken`: the API token removes it, which it may only while the row is one the token saved (savedByToken): its
522
+ * credential and its mark are compared in the deleting statement itself (Mcp.removeIfCredential), never in a read
523
+ * before it, so an owner's save that lands after the gate's check (it drops the mark in the transaction that writes
524
+ * its row) is never removed, and nothing is sent to Composio for it. */
525
+ async remove(name, checkedAgainst, byToken = false) {
466
526
  const row = await this.need(name);
527
+ // The credential the row goes against: the gate's, else (a token's removal the gate did not read) the one read here.
528
+ const expected = checkedAgainst !== undefined ? checkedAgainst : byToken ? credentialMark(row) : undefined;
467
529
  // Whether it is a Composio preset server: from its record (source composio), read before the row goes.
468
530
  const preset = row.auth === "headers" && (await this.sourceOf(row)).source === "composio";
469
531
  // What it stands on at Composio, as its save recorded it (none: saved before that was recorded, or not a preset).
470
532
  const stands = preset ? recordedMade(await this.store.settings.get(composioMadeKey(name)), row.createdAt) : undefined;
471
533
  // The credential revoked is the one the deleting statement saw (RETURNING): a run's refresh landing before it is in it,
472
534
  // one landing after finds no row to keep. A sign-in kept as expired has no refresh token left to revoke.
473
- const removed = checkedAgainst === undefined ? await removeMcpServer(this.store, this.sealKey, name)
474
- : checkedAgainst === null ? undefined : await removeMcpServerIfCredential(this.store, this.sealKey, name, checkedAgainst);
475
- if (checkedAgainst !== undefined && !removed)
535
+ const removed = expected === undefined ? await removeMcpServer(this.store, this.sealKey, name)
536
+ : expected === null ? undefined : await removeMcpServerIfCredential(this.store, this.sealKey, name, expected, byToken ? { record: mcpSourceKey(name) } : undefined);
537
+ if (expected !== undefined && !removed)
476
538
  changedMeanwhile(name, undefined);
477
539
  // Its record goes with it, unless a server saved again under the name meanwhile wrote its own: one left behind is
478
540
  // never read for another row (mcp-sources.ts), so a server saved again says what it is afresh.
@@ -494,7 +556,7 @@ export class McpServersOverStore {
494
556
  const credential = open && "credential" in open && open.credential.type === "oauth" && !open.credential.expired ? open.credential : undefined;
495
557
  if (credential && removed)
496
558
  void this.track(this.signIns.revoke(credential.set, removed.url, this.stopped.signal));
497
- return preset ? { composio: composioHandleOf(removed, cleanup) } : {};
559
+ return preset ? { composio: composioHandleOf(removed, cleanup, stands?.key_env) } : {};
498
560
  }
499
561
  /** Whether `name` is a Composio preset server now (auth headers, source composio): a save of it takes the Composio turn,
500
562
  * since one that moves it off Composio removes there what the preset made for it. */
@@ -650,6 +712,17 @@ export class McpServersOverStore {
650
712
  * read and checked, never the one saved now: a save at another address with a new credential while the caller waited
651
713
  * (its address check, a listing) must never have that credential sent to the old address. */
652
714
  async opened(name, row, field) {
715
+ // A Composio server saved with api_key_env: its key is its variable's value as the server was started with it
716
+ // (composioKeyFromEnv), refused by name when unset.
717
+ // Sent to Composio's origin alone (composioEnvKey): a row at any other address is refused by name, and nothing is sent.
718
+ const keyEnv = await this.keyEnvOf(row);
719
+ if (keyEnv) {
720
+ const got = composioEnvKey(name, row.url, keyEnv, this.composioOrigin);
721
+ if ("refused" in got)
722
+ throw invalid([{ field, reason: got.reason, message: got.refused }]);
723
+ const headers = { "x-api-key": got.key };
724
+ return { headers, credential: { type: "headers", headers, origin: new URL(row.url).origin }, env: keyEnv };
725
+ }
653
726
  // No credential kept: none for a server that takes none, nor for a github_app server with no static headers, nor for a
654
727
  // per_run server (each run hands its own).
655
728
  if (row.auth === "none" || row.sealed === null)
@@ -665,6 +738,13 @@ export class McpServersOverStore {
665
738
  }
666
739
  return { headers: credential.headers, credential };
667
740
  }
741
+ /** The variable a Composio server saved with api_key_env reads its key from (composio-made.ts key_env): a headers row
742
+ * that seals no credential, whose record for this row names it. None for every other row. */
743
+ async keyEnvOf(row) {
744
+ if (row.auth !== "headers" || row.sealed !== null)
745
+ return undefined;
746
+ return recordedMade(await this.store.settings.get(composioMadeKey(row.name)), row.createdAt)?.key_env;
747
+ }
668
748
  /** The saved OAuth token set of `row` (the row read and checked, as opened), for a sign-in again that reuses the kept
669
749
  * client. */
670
750
  openedSet(row) {
@@ -712,7 +792,9 @@ export class McpServersOverStore {
712
792
  const prices = (row.prices ?? undefined);
713
793
  const listed = marked(row.url, { protocol: row.protocol ?? "unknown", server_info: row.serverInfo ?? null, ...(typeof row.instructions === "string" ? { instructions: row.instructions } : {}), tools });
714
794
  const unmetered = tools.filter((tool) => !(prices && Object.hasOwn(prices, tool.name))).map((tool) => tool.name);
715
- const opened = row.auth === "none" || row.auth === "per_run" || (row.auth === "github_app" && row.sealed === null) ? null : openMcpRow(this.sealKey, row);
795
+ // A key read from the environment (api_key_env) is no credential kept here: its header is named, its value never read.
796
+ const keyEnv = await this.keyEnvOf(row);
797
+ const opened = keyEnv || row.auth === "none" || row.auth === "per_run" || (row.auth === "github_app" && row.sealed === null) ? null : openMcpRow(this.sealKey, row);
716
798
  const cred = opened && "credential" in opened ? opened.credential : undefined;
717
799
  const set = cred?.type === "oauth" ? cred.set : undefined;
718
800
  const expired = cred?.type === "oauth" && cred.expired === true;
@@ -738,12 +820,12 @@ export class McpServersOverStore {
738
820
  return { ...tool, effect, ...serverGate(mcpToolName(name, tool.name), settings, effect), ...grantProblemsOf(this.env, name, tool) };
739
821
  }),
740
822
  ...(unmetered.length ? { not_metered: unmetered } : {}),
741
- auth: { type: row.auth, ...(row.auth === "headers" || row.auth === "github_app" ? { headers: Object.keys((cred?.type === "headers" ? cred.headers : {})) } : {}) },
823
+ auth: { type: row.auth, ...(row.auth === "headers" || row.auth === "github_app" ? { headers: keyEnv ? ["x-api-key"] : Object.keys((cred?.type === "headers" ? cred.headers : {})) } : {}), ...(keyEnv ? { api_key_env: keyEnv } : {}) },
742
824
  credential: opened === null ? null : !cred ? "unreadable" : expired ? "expired" : "sealed",
743
825
  ...(set ? { oauth: { ...(set.expires_at ? { expires_at: set.expires_at } : {}), scopes: (set.tokens.scope ?? "").split(" ").filter(Boolean), client: set.kind, ...(set.client?.client_id ? { client_id: set.client.client_id } : {}) } } : {}),
744
826
  listed_at: listedAt,
745
827
  updated_at: updatedAt,
746
- secret: secretOf(row),
828
+ secret: secretOf(row, keyEnv),
747
829
  };
748
830
  }
749
831
  /** A body's URL, auth and prices, and a save's source and toolkit (`priced`: a save's body), or 422. */
@@ -806,9 +888,11 @@ export function mcpServersOverStore(store, env, version, network) {
806
888
  return new McpServersOverStore(store, env, version, network);
807
889
  }
808
890
  /** The server's credential as every secret is read (SECRETS-PLAN.md): set or not, how it came ("console": pasted, a
809
- * bearer token or headers; "sign-in": an OAuth sign-in; null: none), and the credential's own last change. Never a value.
810
- * An MCP credential never comes from the server's environment. */
811
- function secretOf(row) {
891
+ * bearer token or headers; "sign-in": an OAuth sign-in; "env": a Composio key read from the variable `keyEnv`, set when
892
+ * the variable is set now, with no time; null: none), and the credential's own last change. Never a value. */
893
+ function secretOf(row, keyEnv) {
894
+ if (keyEnv)
895
+ return secretMeta(secretName("mcp", row.name), { set: composioKeyFromEnv(keyEnv) !== undefined, source: "env", changedAt: null });
812
896
  const set = row.sealed !== null && row.auth !== "none";
813
897
  return secretMeta(secretName("mcp", row.name), { set, source: row.auth === "oauth" ? "sign-in" : "console", changedAt: row.credentialChangedAt === null ? null : new Date(row.credentialChangedAt) });
814
898
  }
@@ -17,13 +17,21 @@ export type RecordedRow = {
17
17
  createdAt?: number;
18
18
  };
19
19
  /** The settings value a record is stored as, bound to the server row it was written for. `standIn`: an eval's fake
20
- * standing in for a need (standInGiven), the one record that may say github. */
20
+ * standing in for a need (standInGiven), the one record that may say github. `byToken`: the API token saved the row. */
21
21
  export declare const storedSource: (record: SourceRecord, row: {
22
22
  createdAt: number;
23
- }, standIn?: boolean) => SourceRecord & {
23
+ }, standIn?: boolean, byToken?: boolean) => SourceRecord & {
24
24
  of: number;
25
25
  stand_in?: true;
26
+ by?: "token";
26
27
  };
28
+ /** Whether the stored record `value`, read for the server row created at `createdAt`, says the API token saved that row.
29
+ * No record, one written for another row, or one with no mark: the owner's. */
30
+ export declare const savedByToken: (value: unknown, createdAt: number | undefined) => boolean;
31
+ /** A stored record as it stands once the owner saves over the token's row: the same, without the token's mark. The store
32
+ * writes it so in the owner's write's own transaction (store/sqlite/store.ts dropTokensMark); tests write it to stand for
33
+ * that save. */
34
+ export declare const ownersRecord: (value: Record<string, unknown>) => Record<string, unknown>;
27
35
  /** Whether the stored value `value` was written for another server row than the one created at `createdAt`: a record left
28
36
  * behind, which a removal of this row leaves for the row it is. */
29
37
  export declare const recordedForAnother: (value: unknown, createdAt: number) => boolean;
@@ -19,8 +19,15 @@ export const mcpSourceKey = (name) => `mcp.source/${name}`;
19
19
  const GITHUB = { source: "github", toolkit: "github" };
20
20
  const NONE = { source: "url", toolkit: null };
21
21
  /** The settings value a record is stored as, bound to the server row it was written for. `standIn`: an eval's fake
22
- * standing in for a need (standInGiven), the one record that may say github. */
23
- export const storedSource = (record, row, standIn = false) => ({ ...record, of: row.createdAt, ...(standIn ? { stand_in: true } : {}) });
22
+ * standing in for a need (standInGiven), the one record that may say github. `byToken`: the API token saved the row. */
23
+ export const storedSource = (record, row, standIn = false, byToken = false) => ({ ...record, of: row.createdAt, ...(standIn ? { stand_in: true } : {}), ...(byToken ? { by: "token" } : {}) });
24
+ /** Whether the stored record `value`, read for the server row created at `createdAt`, says the API token saved that row.
25
+ * No record, one written for another row, or one with no mark: the owner's. */
26
+ export const savedByToken = (value, createdAt) => isObject(value) && createdAt !== undefined && value.of === createdAt && value.by === "token";
27
+ /** A stored record as it stands once the owner saves over the token's row: the same, without the token's mark. The store
28
+ * writes it so in the owner's write's own transaction (store/sqlite/store.ts dropTokensMark); tests write it to stand for
29
+ * that save. */
30
+ export const ownersRecord = (value) => Object.fromEntries(Object.entries(value).filter(([field]) => field !== "by"));
24
31
  /** Whether the stored value `value` was written for another server row than the one created at `createdAt`: a record left
25
32
  * behind, which a removal of this row leaves for the row it is. */
26
33
  export const recordedForAnother = (value, createdAt) => isObject(value) && typeof value.of === "number" && value.of !== createdAt;
@@ -1,4 +1,4 @@
1
- import { type Mcp, type McpCredential, type McpCredentialMark, type McpOauth, type McpServerRow, type OpenedMcpCredential } from "./store/index.js";
1
+ import { type Mcp, type McpCredential, type McpCredentialMark, type McpOauth, type McpOwnersMark, type McpServerRow, type McpTokensMark, type OpenedMcpCredential } from "./store/index.js";
2
2
  /** The most server-supplied text (server_info JSON, instructions) a row holds: both come from the remote server
3
3
  * (untrusted), so they are bounded at save, never stored unbounded. */
4
4
  export declare const MCP_META_MAX_BYTES: number;
@@ -29,15 +29,19 @@ export declare function saveMcpServer(store: {
29
29
  }): Promise<McpServerRow>;
30
30
  /** saveMcpServer, only while the server's credential is still `expected` (its mark as the writer read it: null, no
31
31
  * server by that name), compared and written in one statement (Mcp.putIfCredential). Undefined, writing nothing, when
32
- * another save, a sign-in or a removal changed it since. */
32
+ * another save, a sign-in or a removal changed it since. `tokens`: the API token's save, which writes over a saved row
33
+ * only while that row still carries the token's mark, compared in the same statement. `owns`: the owner's save, which
34
+ * drops the token's mark in the transaction that writes the row, once it is written (McpOwnersMark). */
33
35
  export declare function saveMcpServerIfCredential(store: {
34
36
  mcp: Pick<Mcp, "putIfCredential">;
35
- }, sealKey: Buffer, server: McpServerSave, expected: McpCredentialMark, { githubMcpOrigin }?: {
37
+ }, sealKey: Buffer, server: McpServerSave, expected: McpCredentialMark, { githubMcpOrigin, tokens, owns }?: {
36
38
  githubMcpOrigin?: string;
39
+ tokens?: McpTokensMark;
40
+ owns?: McpOwnersMark;
37
41
  }): Promise<McpServerRow | undefined>;
38
42
  /** Rewrites a server's row the owner read, its sealed credential kept as it is (store.mcp.rewrite): written iff the
39
43
  * credential is still `seen`; undefined, writing nothing, when it changed meanwhile or the row went. Bounded and
40
- * github_app-guarded as saveMcpServer. */
44
+ * github_app-guarded as saveMcpServer. `owns`: as saveMcpServerIfCredential's. */
41
45
  export declare function rewriteMcpServer(store: {
42
46
  mcp: Pick<Mcp, "rewrite">;
43
47
  }, server: {
@@ -51,18 +55,20 @@ export declare function rewriteMcpServer(store: {
51
55
  instructions?: string | null;
52
56
  listedAt?: number;
53
57
  updatedAt?: number;
54
- }, seen: Uint8Array | null, { githubMcpOrigin }?: {
58
+ }, seen: Uint8Array | null, { githubMcpOrigin, owns }?: {
55
59
  githubMcpOrigin?: string;
60
+ owns?: McpOwnersMark;
56
61
  }): Promise<McpServerRow | undefined>;
57
62
  /** The server's row goes; what it held as it went (its address, its credential opened, as openServerCredential does), or
58
63
  * undefined when there was no row. */
59
64
  export declare function removeMcpServer(store: {
60
65
  mcp: Pick<Mcp, "remove">;
61
66
  }, sealKey: Buffer, name: string): Promise<RemovedMcpServer | undefined>;
62
- /** The server's row goes, only while its credential is still `expected` (Mcp.removeIfCredential). Undefined when it is not. */
67
+ /** The server's row goes, only while its credential is still `expected` (Mcp.removeIfCredential), and with `tokens` (the
68
+ * API token's delete) only while it still carries the token's mark, both in the deleting statement. Undefined when not. */
63
69
  export declare function removeMcpServerIfCredential(store: {
64
70
  mcp: Pick<Mcp, "removeIfCredential">;
65
- }, sealKey: Buffer, name: string, expected: NonNullable<McpCredentialMark>): Promise<RemovedMcpServer | undefined>;
71
+ }, sealKey: Buffer, name: string, expected: NonNullable<McpCredentialMark>, tokens?: McpTokensMark): Promise<RemovedMcpServer | undefined>;
66
72
  /** A removed row: its address and its credential opened (§7), the credential the one the deleting statement saw. */
67
73
  export type RemovedMcpServer = {
68
74
  url: string;
package/dist/mcp-store.js CHANGED
@@ -38,9 +38,11 @@ export async function saveMcpServer(store, sealKey, server,
38
38
  }
39
39
  /** saveMcpServer, only while the server's credential is still `expected` (its mark as the writer read it: null, no
40
40
  * server by that name), compared and written in one statement (Mcp.putIfCredential). Undefined, writing nothing, when
41
- * another save, a sign-in or a removal changed it since. */
42
- export async function saveMcpServerIfCredential(store, sealKey, server, expected, { githubMcpOrigin = GITHUB_MCP_ORIGIN } = {}) {
43
- return store.mcp.putIfCredential(serverWrite(sealKey, server, githubMcpOrigin), expected);
41
+ * another save, a sign-in or a removal changed it since. `tokens`: the API token's save, which writes over a saved row
42
+ * only while that row still carries the token's mark, compared in the same statement. `owns`: the owner's save, which
43
+ * drops the token's mark in the transaction that writes the row, once it is written (McpOwnersMark). */
44
+ export async function saveMcpServerIfCredential(store, sealKey, server, expected, { githubMcpOrigin = GITHUB_MCP_ORIGIN, tokens, owns } = {}) {
45
+ return store.mcp.putIfCredential(serverWrite(sealKey, server, githubMcpOrigin), expected, tokens, owns);
44
46
  }
45
47
  /** The store's write of `server`: its credential sealed, its info and instructions bounded. */
46
48
  function serverWrite(sealKey, server, githubMcpOrigin) {
@@ -60,24 +62,25 @@ function serverWrite(sealKey, server, githubMcpOrigin) {
60
62
  }
61
63
  /** Rewrites a server's row the owner read, its sealed credential kept as it is (store.mcp.rewrite): written iff the
62
64
  * credential is still `seen`; undefined, writing nothing, when it changed meanwhile or the row went. Bounded and
63
- * github_app-guarded as saveMcpServer. */
64
- export async function rewriteMcpServer(store, server, seen, { githubMcpOrigin = GITHUB_MCP_ORIGIN } = {}) {
65
+ * github_app-guarded as saveMcpServer. `owns`: as saveMcpServerIfCredential's. */
66
+ export async function rewriteMcpServer(store, server, seen, { githubMcpOrigin = GITHUB_MCP_ORIGIN, owns } = {}) {
65
67
  if (server.auth === "github_app" && !githubMcpUrl(server.url, githubMcpOrigin))
66
68
  throw new Error(`The MCP server ${server.name} is not kept: ${GITHUB_MCP_ONLY}.`);
67
69
  return store.mcp.rewrite({
68
70
  name: server.name, url: server.url, auth: server.auth, protocol: server.protocol ?? null, tools: server.tools, prices: server.prices ?? null,
69
71
  serverInfo: boundedServerInfo(server.serverInfo), instructions: boundedInstructions(server.instructions),
70
72
  ...(server.listedAt !== undefined ? { listedAt: server.listedAt } : {}), ...(server.updatedAt !== undefined ? { updatedAt: server.updatedAt } : {}),
71
- }, seen);
73
+ }, seen, owns);
72
74
  }
73
75
  /** The server's row goes; what it held as it went (its address, its credential opened, as openServerCredential does), or
74
76
  * undefined when there was no row. */
75
77
  export async function removeMcpServer(store, sealKey, name) {
76
78
  return removedServer(sealKey, name, await store.mcp.remove(name));
77
79
  }
78
- /** The server's row goes, only while its credential is still `expected` (Mcp.removeIfCredential). Undefined when it is not. */
79
- export async function removeMcpServerIfCredential(store, sealKey, name, expected) {
80
- return removedServer(sealKey, name, await store.mcp.removeIfCredential(name, expected));
80
+ /** The server's row goes, only while its credential is still `expected` (Mcp.removeIfCredential), and with `tokens` (the
81
+ * API token's delete) only while it still carries the token's mark, both in the deleting statement. Undefined when not. */
82
+ export async function removeMcpServerIfCredential(store, sealKey, name, expected, tokens) {
83
+ return removedServer(sealKey, name, await store.mcp.removeIfCredential(name, expected, tokens));
81
84
  }
82
85
  const removedServer = (sealKey, name, removed) => {
83
86
  if (!removed)
@@ -20,8 +20,9 @@ export type FreeModel = {
20
20
  context_window: number;
21
21
  };
22
22
  /** The free models offered for `harness` on this server, besides pickerCandidates' (harness.ts harnessOffers drops those
23
- * it does not run): OpenRouter's chat models whose catalog price is all zeros (pi-ai's own and serve's ADDED_MODELS), on
24
- * pi with OpenRouter's key, by id. None for an adapter harness (each runs its own wire's vendor), behind a gateway (its
23
+ * it does not run), on pi: the models of the Ollama here (ollama-provider.ts, `ollama/<tag>`, each at $0 on this machine,
24
+ * in Ollama's order), then, with OpenRouter's key, OpenRouter's chat models whose catalog price is all zeros (pi-ai's own
25
+ * and serve's ADDED_MODELS), by id. None for an adapter harness (each runs its own wire's vendor), behind a gateway (its
25
26
  * ids are its own) or with the faux model. */
26
27
  export declare function freeCandidates(env: NodeJS.ProcessEnv, harness: string): Promise<FreeModel[]>;
27
28
  /** The model `given` most likely means, in the form this harness takes, or undefined when none is obvious: the same id
@@ -56,16 +56,20 @@ export function pickerCandidates(env, harness) {
56
56
  return [...plan, ...PICKABLE.filter((p) => vendors.includes(p.vendor)).flatMap((p) => p.ids.map((id) => formOf(env, p.vendor, id)))];
57
57
  }
58
58
  /** The free models offered for `harness` on this server, besides pickerCandidates' (harness.ts harnessOffers drops those
59
- * it does not run): OpenRouter's chat models whose catalog price is all zeros (pi-ai's own and serve's ADDED_MODELS), on
60
- * pi with OpenRouter's key, by id. None for an adapter harness (each runs its own wire's vendor), behind a gateway (its
59
+ * it does not run), on pi: the models of the Ollama here (ollama-provider.ts, `ollama/<tag>`, each at $0 on this machine,
60
+ * in Ollama's order), then, with OpenRouter's key, OpenRouter's chat models whose catalog price is all zeros (pi-ai's own
61
+ * and serve's ADDED_MODELS), by id. None for an adapter harness (each runs its own wire's vendor), behind a gateway (its
61
62
  * ids are its own) or with the faux model. */
62
63
  export async function freeCandidates(env, harness) {
63
- if (env[FAUX_ENV] || env.AGENT_MODEL_BASE_URL || !env.OPENROUTER_API_KEY || factsOf(env, harness))
64
+ if (env[FAUX_ENV] || env.AGENT_MODEL_BASE_URL || factsOf(env, harness))
64
65
  return [];
65
- const [{ builtinModels }, { withLocalModels, isFree }] = await Promise.all([import("@earendil-works/pi-ai/providers/all"), import("./local-models.js")]);
66
- return withLocalModels(builtinModels()).getModels("openrouter").filter(isFree)
67
- .map((model) => ({ id: `openrouter/${model.id}`, context_window: model.contextWindow }))
68
- .sort((a, b) => a.id.localeCompare(b.id));
66
+ const [{ builtinModels }, { withLocalModels, isFree }, { contextOf, OLLAMA_PROVIDER, ollamaModels }] = await Promise.all([import("@earendil-works/pi-ai/providers/all"), import("./local-models.js"), import("./ollama-provider.js")]);
67
+ const local = (await ollamaModels(env)).filter((model) => model.capabilities.includes("completion")).map((model) => ({ id: `${OLLAMA_PROVIDER}/${model.name}`, context_window: contextOf(model, env) }));
68
+ if (!env.OPENROUTER_API_KEY)
69
+ return local;
70
+ return [...local, ...withLocalModels(builtinModels()).getModels("openrouter").filter(isFree)
71
+ .map((model) => ({ id: `openrouter/${model.id}`, context_window: model.contextWindow }))
72
+ .sort((a, b) => a.id.localeCompare(b.id))];
69
73
  }
70
74
  /** A model id with its spelling taken out (case, spaces, dots, dashes), and the provider's segments off. */
71
75
  const spelled = (given) => (given.split("/").pop() ?? "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");