@zackbart/connecta 0.24.2 → 0.24.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 (57) hide show
  1. package/CHANGELOG.md +141 -0
  2. package/dist/auth/bearer.js +2 -0
  3. package/dist/auth/downstream-oauth.d.ts +12 -1
  4. package/dist/auth/downstream-oauth.js +147 -35
  5. package/dist/call-admission.d.ts +4 -0
  6. package/dist/call-admission.js +26 -0
  7. package/dist/catalog-drift.js +9 -4
  8. package/dist/catalog-service.d.ts +2 -0
  9. package/dist/catalog-service.js +25 -8
  10. package/dist/catalog.d.ts +2 -0
  11. package/dist/catalog.js +246 -121
  12. package/dist/connectors/api.js +11 -1
  13. package/dist/connectors/guarded-fetch.d.ts +1 -1
  14. package/dist/connectors/guarded-fetch.js +27 -20
  15. package/dist/connectors/remote-mcp.js +84 -53
  16. package/dist/errors.d.ts +17 -0
  17. package/dist/errors.js +58 -0
  18. package/dist/execute.js +85 -23
  19. package/dist/executor-result.js +3 -1
  20. package/dist/executors/quickjs-child.js +5 -1
  21. package/dist/executors/quickjs-protocol.d.ts +4 -0
  22. package/dist/executors/quickjs-runtime.d.ts +1 -1
  23. package/dist/executors/quickjs-runtime.js +38 -21
  24. package/dist/executors/quickjs.js +68 -27
  25. package/dist/index.d.ts +14 -0
  26. package/dist/index.js +24 -3
  27. package/dist/invocation.js +134 -93
  28. package/dist/mcp-result.js +3 -2
  29. package/dist/meta-tools.js +118 -39
  30. package/dist/registry.d.ts +14 -2
  31. package/dist/registry.js +87 -13
  32. package/dist/routes/mcp.d.ts +4 -1
  33. package/dist/routes/mcp.js +84 -13
  34. package/dist/routes/oauth.js +4 -0
  35. package/dist/routes/shared.d.ts +1 -0
  36. package/dist/routes/shared.js +4 -4
  37. package/dist/server.js +15 -3
  38. package/dist/skills.js +6 -5
  39. package/dist/storage/file.d.ts +6 -2
  40. package/dist/storage/file.js +312 -34
  41. package/dist/storage/memory.js +12 -1
  42. package/dist/validate.js +3 -3
  43. package/dist/version.d.ts +1 -1
  44. package/dist/version.js +1 -1
  45. package/documentation/architecture.md +22 -6
  46. package/documentation/auth.md +42 -9
  47. package/documentation/call-admission.md +24 -8
  48. package/documentation/code-mode.md +34 -22
  49. package/documentation/connectors.md +47 -5
  50. package/documentation/meta-tools.md +74 -6
  51. package/documentation/operations.md +19 -19
  52. package/documentation/provider-conventions.md +7 -0
  53. package/documentation/request-admission.md +38 -4
  54. package/documentation/storage-and-credentials.md +54 -1
  55. package/documentation/upgrading.md +18 -4
  56. package/package.json +1 -1
  57. package/templates/node/package.json +1 -1
@@ -80,30 +80,43 @@ export function normalizeCode(code) {
80
80
  * and fail closed there.
81
81
  */
82
82
  function setupScript(providers) {
83
+ // X11: raw replies carry an authenticated frame. Capture the bridge and
84
+ // codec before guest code runs, and expose only the decoded provider calls.
83
85
  const lines = [
84
- `globalThis.console = (() => {
85
- const fmt = (x) => { if (typeof x === "string") return x; try { return JSON.stringify(x); } catch { return String(x); } };
86
- const emit = (...a) => __log(a.map(fmt).join(" "));
87
- return { log: emit, info: emit, warn: emit, error: emit, debug: emit };
88
- })();`,
89
- `globalThis.__invoke = async (ns, fn, args) => {
90
- const r = JSON.parse(await __call(ns, fn, JSON.stringify(args)));
91
- if (!r.ok) throw new Error(r.error);
92
- return r.value;
93
- };`,
94
- `globalThis.__namespace = (ns) => Object.freeze(new Proxy(Object.create(null), {
95
- get: (_target, key) => typeof key === "string" ? (...args) => __invoke(ns, key, args) : undefined
96
- }));`,
86
+ `(() => {
87
+ const call = globalThis.__call;
88
+ const log = globalThis.__log;
89
+ const parse = JSON.parse;
90
+ const stringify = JSON.stringify;
91
+ const freeze = Object.freeze;
92
+ const ProxyConstructor = Proxy;
93
+ const create = Object.create;
94
+ delete globalThis.__call;
95
+ delete globalThis.__log;
96
+ globalThis.console = (() => {
97
+ const fmt = (x) => { if (typeof x === "string") return x; try { return stringify(x); } catch { return String(x); } };
98
+ const emit = (...a) => log(a.map(fmt).join(" "));
99
+ return { log: emit, info: emit, warn: emit, error: emit, debug: emit };
100
+ })();
101
+ const invoke = async (ns, fn, args) => {
102
+ const r = parse(await call(ns, fn, stringify(args)));
103
+ if (!r.ok) throw new GuestError(r.error);
104
+ return r.value;
105
+ };
106
+ const namespace = (ns) => freeze(new ProxyConstructor(create(null), {
107
+ get: (_target, key) => typeof key === "string" ? (...args) => invoke(ns, key, args) : undefined
108
+ }));`,
97
109
  ];
98
110
  for (const p of providers) {
99
111
  const ns = JSON.stringify(p.name);
100
- lines.push(`globalThis[${ns}] = __namespace(${ns});`);
112
+ lines.push(`globalThis[${ns}] = namespace(${ns});`);
101
113
  }
102
114
  for (const p of providers) {
103
115
  if (p.prelude)
104
116
  lines.push(p.prelude);
105
117
  }
106
- lines.push(`delete globalThis.__namespace;`);
118
+ lines.push(`const GuestError = globalThis.Error;
119
+ })();`);
107
120
  return lines.join("\n");
108
121
  }
109
122
  /** True when a dumped error is QuickJS's deadline-interrupt signal. */
@@ -145,7 +158,7 @@ function waitForHostOrDeadline(waitForSettle, remainingMs) {
145
158
  });
146
159
  });
147
160
  }
148
- function installBridge(ctx, providers, logs) {
161
+ function installBridge(ctx, providers, logs, onLog) {
149
162
  const bridge = {
150
163
  pending: 0,
151
164
  aborted: false,
@@ -156,13 +169,17 @@ function installBridge(ctx, providers, logs) {
156
169
  // Keep both the in-memory character budget and the twice-JSON-encoded
157
170
  // transport budget. Reserve enough byte budget for whichever truncation
158
171
  // marker ends the stream.
172
+ const captureLog = (entry) => {
173
+ logs.push(entry);
174
+ onLog?.(entry);
175
+ };
159
176
  let logTotalChars = 0;
160
177
  let logTotalTransportBytes = 0;
161
178
  let logBudgetSpent = false;
162
179
  const logFn = ctx.newFunction("__log", (h) => {
163
180
  if (logs.length >= MAX_LOG_ENTRIES) {
164
181
  if (logs.length === MAX_LOG_ENTRIES) {
165
- logs.push(LOG_ENTRY_LIMIT_MARKER);
182
+ captureLog(LOG_ENTRY_LIMIT_MARKER);
166
183
  }
167
184
  return;
168
185
  }
@@ -178,11 +195,11 @@ function installBridge(ctx, providers, logs) {
178
195
  if (logTotalChars + entry.length > MAX_LOG_TOTAL_CHARS ||
179
196
  logTotalTransportBytes + entryTransportBytes >
180
197
  MAX_QUICKJS_LOG_TRANSPORT_BYTES - MAX_LOG_MARKER_TRANSPORT_BYTES) {
181
- logs.push(LOG_SIZE_LIMIT_MARKER);
198
+ captureLog(LOG_SIZE_LIMIT_MARKER);
182
199
  logBudgetSpent = true;
183
200
  return;
184
201
  }
185
- logs.push(entry);
202
+ captureLog(entry);
186
203
  logTotalChars += entry.length;
187
204
  logTotalTransportBytes += entryTransportBytes;
188
205
  });
@@ -248,7 +265,7 @@ function installBridge(ctx, providers, logs) {
248
265
  * CPU accumulates only while evalCode/executePendingJobs is synchronously
249
266
  * driving QuickJS, so a slow downstream does not consume the short CPU budget.
250
267
  */
251
- export async function executeQuickJs(code, providers, options) {
268
+ export async function executeQuickJs(code, providers, options, onLog) {
252
269
  const { timeoutMs, cpuTimeMs, memoryLimitBytes, maxStackSizeBytes } = options;
253
270
  const QuickJS = await getQuickJS();
254
271
  const ctx = QuickJS.newContext();
@@ -282,7 +299,7 @@ export async function executeQuickJs(code, providers, options) {
282
299
  return wallInterrupted || cpuInterrupted;
283
300
  });
284
301
  const logs = [];
285
- const bridge = installBridge(ctx, providers, logs);
302
+ const bridge = installBridge(ctx, providers, logs, onLog);
286
303
  const finish = (r) => {
287
304
  bridge.aborted = true;
288
305
  // Outstanding host calls still hold deferred-promise handles; their
@@ -7,6 +7,7 @@ import { existsSync } from "node:fs";
7
7
  import { fileURLToPath } from "node:url";
8
8
  import { AdmissionController, ExecutorAdmissionError, ExecutorExecutionError, } from "../executor-admission.js";
9
9
  import { msg } from "../errors.js";
10
+ import { MAX_EXECUTE_LOG_CHARS } from "../executor-result.js";
10
11
  import { hostCallLabel, MAX_QUICKJS_IPC_BYTES, MAX_QUICKJS_HOST_RPC_BYTES, serializedBytes, stringifyBounded, } from "./quickjs-protocol.js";
11
12
  export { normalizeCode } from "./quickjs-runtime.js";
12
13
  const DEFAULT_TIMEOUT_MS = 30_000;
@@ -55,9 +56,14 @@ function nonNegativeWhole(value, fallback, name) {
55
56
  return resolved;
56
57
  }
57
58
  function errorPayload(error) {
59
+ // X11: a partial authenticated frame exposes its secret instead of decoding.
60
+ // Refuse it whole if a host ever bypasses execute.ts's framing bound.
61
+ const message = error.length > MAX_ERROR_CHARS && error.includes("\u001econnecta-error:")
62
+ ? `Host failure exceeded the ${MAX_ERROR_CHARS}-character bridge limit.`
63
+ : error.slice(0, MAX_ERROR_CHARS);
58
64
  return JSON.stringify({
59
65
  ok: false,
60
- error: error.slice(0, MAX_ERROR_CHARS),
66
+ error: message,
61
67
  });
62
68
  }
63
69
  function delay(ms, signal) {
@@ -217,14 +223,13 @@ class QuickJsChildPool {
217
223
  child.channel?.ref();
218
224
  return new Promise((resolve, reject) => {
219
225
  const wallTimer = setTimeout(() => {
220
- this.resolveActive(slot, {
221
- result: undefined,
222
- error: `QuickJS child exceeded the ${this.runtimeOptions.timeoutMs}ms wall budget and was terminated.`,
223
- });
226
+ this.rejectActive(slot, new Error(`QuickJS child exceeded the ${this.runtimeOptions.timeoutMs}ms wall budget and was terminated.`));
224
227
  this.recycle(slot);
225
228
  }, this.runtimeOptions.timeoutMs + CHILD_EXIT_GRACE_MS);
226
229
  const active = {
227
230
  id,
231
+ logs: [],
232
+ logChars: 0,
228
233
  providers: providerMap,
229
234
  resolve,
230
235
  reject,
@@ -328,7 +333,7 @@ class QuickJsChildPool {
328
333
  return;
329
334
  this.recordCrash(slot);
330
335
  const error = childExitError(`QuickJS child exited unexpectedly (${exitDescription}).`, stderrTail);
331
- this.resolveActive(slot, { result: undefined, error: error.message });
336
+ this.rejectActive(slot, error);
332
337
  });
333
338
  child.unref();
334
339
  child.channel?.unref();
@@ -350,19 +355,39 @@ class QuickJsChildPool {
350
355
  return;
351
356
  if (typeof message.payloadJson !== "string")
352
357
  return;
358
+ if (message.type === "log") {
359
+ if (message.jobId !== active.id)
360
+ return;
361
+ // Keep one extra character so the presentation layer can signal loss.
362
+ // The final reply owns complete successful logs; this prefix survives
363
+ // only when that reply cannot arrive. Bound even a compromised child.
364
+ if (active.logChars >= MAX_EXECUTE_LOG_CHARS + 1)
365
+ return;
366
+ if (serializedBytes(message.payloadJson) > MAX_QUICKJS_IPC_BYTES)
367
+ return;
368
+ try {
369
+ stringifyBounded(message, "QuickJS log IPC envelope");
370
+ const entry = JSON.parse(message.payloadJson);
371
+ if (typeof entry !== "string")
372
+ return;
373
+ const separator = active.logs.length > 0 ? 1 : 0;
374
+ const retained = entry.slice(0, MAX_EXECUTE_LOG_CHARS + 1 - active.logChars - separator);
375
+ active.logs.push(retained);
376
+ active.logChars += separator + retained.length;
377
+ }
378
+ catch { /* Malformed log messages carry no output. */ }
379
+ return;
380
+ }
353
381
  if (message.type === "host-call") {
354
382
  if (message.jobId !== active.id)
355
383
  return;
356
- await this.handleHostCall(child, active, message);
384
+ await this.handleHostCall(slot, child, active, message);
357
385
  return;
358
386
  }
359
387
  if (message.type !== "result" || message.jobId !== active.id)
360
388
  return;
361
389
  if (serializedBytes(message.payloadJson) > MAX_QUICKJS_IPC_BYTES) {
362
- this.resolveActive(slot, {
363
- result: undefined,
364
- error: "QuickJS execution result exceeded the IPC limit.",
365
- });
390
+ this.rejectActive(slot, new Error("QuickJS execution result exceeded the IPC limit."));
366
391
  this.recycle(slot);
367
392
  return;
368
393
  }
@@ -380,14 +405,11 @@ class QuickJsChildPool {
380
405
  }
381
406
  }
382
407
  catch (err) {
383
- this.resolveActive(slot, {
384
- result: undefined,
385
- error: `QuickJS child returned an invalid result: ${msg(err)}`,
386
- });
408
+ this.rejectActive(slot, new Error(`QuickJS child returned an invalid result: ${msg(err)}`));
387
409
  this.recycle(slot);
388
410
  }
389
411
  }
390
- async handleHostCall(child, active, message) {
412
+ async handleHostCall(slot, child, active, message) {
391
413
  let payloadJson;
392
414
  try {
393
415
  if (serializedBytes(message.payloadJson) >
@@ -422,35 +444,54 @@ class QuickJsChildPool {
422
444
  catch (err) {
423
445
  payloadJson = errorPayload(msg(err));
424
446
  }
425
- if (!child.connected)
447
+ if (!child.connected || slot.active !== active)
426
448
  return;
449
+ const response = {
450
+ type: "host-result",
451
+ jobId: message.jobId,
452
+ callId: message.callId,
453
+ payloadJson,
454
+ };
427
455
  try {
428
- const response = {
429
- type: "host-result",
430
- jobId: message.jobId,
431
- callId: message.callId,
432
- payloadJson,
433
- };
434
456
  stringifyBounded(response, "QuickJS host-result IPC envelope");
435
- child.send(response);
436
457
  }
437
458
  catch {
438
- // The execution has already ended or the child is exiting. Its exit
439
- // handler owns the structured failure for any still-active job.
459
+ // L6/X10: settle the rejected call even if the outer encoding overflows.
460
+ response.payloadJson = errorPayload(`QuickJS host-result IPC envelope could not be serialized within the ${MAX_QUICKJS_IPC_BYTES}-byte IPC limit.`);
461
+ }
462
+ const failedSend = (error) => {
463
+ if (!error || slot.active !== active)
464
+ return;
465
+ this.rejectActive(slot, error);
466
+ this.recycle(slot);
467
+ };
468
+ try {
469
+ stringifyBounded(response, "QuickJS host-result IPC envelope");
470
+ child.send(response, failedSend);
471
+ }
472
+ catch (err) {
473
+ failedSend(err instanceof Error ? err : new Error(msg(err)));
440
474
  }
441
475
  }
442
476
  resolveActive(slot, outcome) {
443
477
  const active = slot.active;
444
478
  if (!active)
445
479
  return;
480
+ // Never concatenate the stream with the final reply: those entries are
481
+ // the same logs. A normal reply retains its existing full log contract.
482
+ const resolved = outcome.logs === undefined && active.logs.length > 0
483
+ ? { ...outcome, logs: active.logs }
484
+ : outcome;
446
485
  this.clearActive(slot, active);
447
- active.resolve(outcome);
486
+ active.resolve(resolved);
448
487
  }
449
488
  rejectActive(slot, error) {
450
489
  const active = slot.active;
451
490
  if (!active)
452
491
  return;
453
492
  this.clearActive(slot, active);
493
+ if (active.logs.length > 0)
494
+ Object.assign(error, { logs: active.logs });
454
495
  active.reject(error);
455
496
  }
456
497
  clearActive(slot, active) {
package/dist/index.d.ts CHANGED
@@ -51,6 +51,13 @@ export interface ConnectaCallsConfig {
51
51
  */
52
52
  maxResultBytes?: number;
53
53
  }
54
+ /** Runtime-wide bounds for transient direct-call result paging. */
55
+ export interface ConnectaResultsConfig {
56
+ /** Stored bytes, including the paging envelope. Default 8 MiB. Zero disables stashing. */
57
+ maxStashBytes?: number;
58
+ /** Stored or in-flight entries. Default 64. Zero disables stashing. */
59
+ maxStashEntries?: number;
60
+ }
54
61
  /** Budgets for execute_code programs: host calls and rich output (`connecta.emit`). */
55
62
  export interface ConnectaExecuteConfig {
56
63
  /**
@@ -149,6 +156,11 @@ export interface ConnectaConfig {
149
156
  * HTTPS URL also redirects matching inbound HTTP requests to HTTPS.
150
157
  */
151
158
  publicUrl?: string;
159
+ /**
160
+ * Exact browser MCP origins, or "*". Defaults to publicUrl's origin and
161
+ * HTTP(S) loopback origins at any port. Originless clients are admitted.
162
+ */
163
+ allowedOrigins?: readonly string[] | "*";
152
164
  /** Optional recorder and reader, created by activityHistory() from /activity. */
153
165
  activity?: ActivityModule;
154
166
  /** Replaceable owner-partitioned credential storage. Omit for config-owned secrets. */
@@ -159,6 +171,8 @@ export interface ConnectaConfig {
159
171
  discovery?: ConnectaDiscoveryConfig;
160
172
  /** Deployment-wide call deadlines and result paging threshold. */
161
173
  calls?: ConnectaCallsConfig;
174
+ /** Runtime-wide transient result stash limits, shared across subjects. */
175
+ results?: ConnectaResultsConfig;
162
176
  /** Budgets for the `connecta.emit` rich-output channel in execute_code. */
163
177
  execute?: ConnectaExecuteConfig;
164
178
  /** Bounded MCP and fallback code-mode admission. */
package/dist/index.js CHANGED
@@ -60,6 +60,7 @@ const CONFIG_SCHEMA = {
60
60
  pools: null,
61
61
  storage: null,
62
62
  publicUrl: null,
63
+ allowedOrigins: null,
63
64
  activity: null,
64
65
  vault: null,
65
66
  ui: null,
@@ -70,6 +71,10 @@ const CONFIG_SCHEMA = {
70
71
  staleCatalogSeconds: null,
71
72
  probeTimeoutMs: null,
72
73
  },
74
+ results: {
75
+ maxStashBytes: null,
76
+ maxStashEntries: null,
77
+ },
73
78
  calls: {
74
79
  defaultTimeoutMs: null,
75
80
  maxResultBytes: null,
@@ -135,6 +140,17 @@ function rejectUnknownOptions(paths) {
135
140
  /** Reject JavaScript typos and removed options before construction does work. */
136
141
  function assertKnownConfig(config) {
137
142
  rejectUnknownOptions(unknownOptionPaths(config, "ConnectaConfig", CONFIG_SCHEMA));
143
+ if (config.results !== undefined) {
144
+ if (!config.results || typeof config.results !== "object" || Array.isArray(config.results)) {
145
+ throw new Error("ConnectaConfig.results must be an object");
146
+ }
147
+ for (const key of ["maxStashBytes", "maxStashEntries"]) {
148
+ const value = config.results[key];
149
+ if (value !== undefined && (!Number.isSafeInteger(value) || value < 0)) {
150
+ throw new Error(`ConnectaConfig.results.${key} must be a non-negative safe integer`);
151
+ }
152
+ }
153
+ }
138
154
  if (config.vault && ["get", "getAll", "set", "setAll", "metadata", "delete"].some(key => typeof config.vault[key] !== "function"))
139
155
  throw new Error("ConnectaConfig.vault must implement CredentialVault");
140
156
  if (config.ui && (typeof config.ui.handle !== "function" || typeof config.ui.credentialHandoffUrl !== "function" || !Array.isArray(config.ui.reservedPaths)))
@@ -217,12 +233,15 @@ function resolvePools(pools, registry) {
217
233
  function warnInsecureConfig(config, inboundAuth, logger) {
218
234
  const oauthConnectors = config.connectors.filter((c) => c.finishAuth);
219
235
  const hasCredentialConnector = config.connectors.some((c) => c.credential);
220
- // Open mode (no inbound auth) with connectors that expose credentials or
221
- // downstream OAuth: any caller reaches everything, including the vault.
236
+ // Static API headers can carry secrets without declaring credential hooks.
237
+ // Any configured connector warrants the open-deployment warning.
222
238
  if (inboundAuth.length === 0 &&
223
- (hasCredentialConnector || oauthConnectors.length > 0)) {
239
+ config.connectors.length > 0) {
224
240
  logger.warn("[connecta] running with no inbound authentication: any caller can " +
225
241
  "invoke every shared connector. " +
242
+ (hasCredentialConnector || oauthConnectors.length > 0
243
+ ? "Configured credentials and downstream OAuth grants are exposed to those calls. "
244
+ : "") +
226
245
  "Configure `auth` (for example bearerToken(...) or Clerk) to gate access.");
227
246
  }
228
247
  // Unset publicUrl with OAuth connectors: the downstream redirect_uri is
@@ -330,6 +349,7 @@ export function createConnecta(config) {
330
349
  persistToolCatalog: config.discovery?.persistCatalog,
331
350
  toolCatalogStaleSeconds: config.discovery?.staleCatalogSeconds,
332
351
  maxResultBytes: config.calls?.maxResultBytes,
352
+ results: config.results,
333
353
  });
334
354
  const inboundAuth = configuredAuth;
335
355
  const pools = resolvePools(config.pools, registry);
@@ -356,6 +376,7 @@ export function createConnecta(config) {
356
376
  identity: config.identity,
357
377
  pools,
358
378
  publicUrl: config.publicUrl,
379
+ allowedOrigins: config.allowedOrigins,
359
380
  serverInfo,
360
381
  logger,
361
382
  activity: config.activity?.store,