@zackbart/connecta 0.4.1 → 0.6.0

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 (85) hide show
  1. package/CHANGELOG.md +527 -0
  2. package/README.md +83 -7
  3. package/SECURITY.md +10 -6
  4. package/dist/activity.d.ts +8 -0
  5. package/dist/activity.d.ts.map +1 -1
  6. package/dist/activity.js +1 -0
  7. package/dist/activity.js.map +1 -1
  8. package/dist/auth/bearer.d.ts +10 -3
  9. package/dist/auth/bearer.d.ts.map +1 -1
  10. package/dist/auth/bearer.js +21 -0
  11. package/dist/auth/bearer.js.map +1 -1
  12. package/dist/auth/clerk.d.ts +26 -1
  13. package/dist/auth/clerk.d.ts.map +1 -1
  14. package/dist/auth/clerk.js +161 -4
  15. package/dist/auth/clerk.js.map +1 -1
  16. package/dist/connectors/api.d.ts +13 -0
  17. package/dist/connectors/api.d.ts.map +1 -1
  18. package/dist/connectors/api.js +2 -0
  19. package/dist/connectors/api.js.map +1 -1
  20. package/dist/connectors/remote-mcp.d.ts +13 -0
  21. package/dist/connectors/remote-mcp.d.ts.map +1 -1
  22. package/dist/connectors/remote-mcp.js +10 -0
  23. package/dist/connectors/remote-mcp.js.map +1 -1
  24. package/dist/credential-health.d.ts +212 -0
  25. package/dist/credential-health.d.ts.map +1 -0
  26. package/dist/credential-health.js +535 -0
  27. package/dist/credential-health.js.map +1 -0
  28. package/dist/execute.d.ts +4 -4
  29. package/dist/execute.d.ts.map +1 -1
  30. package/dist/execute.js +16 -4
  31. package/dist/execute.js.map +1 -1
  32. package/dist/index.d.ts +77 -2
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +112 -2
  35. package/dist/index.js.map +1 -1
  36. package/dist/meta-tools.d.ts +76 -7
  37. package/dist/meta-tools.d.ts.map +1 -1
  38. package/dist/meta-tools.js +328 -98
  39. package/dist/meta-tools.js.map +1 -1
  40. package/dist/registry.d.ts +245 -2
  41. package/dist/registry.d.ts.map +1 -1
  42. package/dist/registry.js +377 -27
  43. package/dist/registry.js.map +1 -1
  44. package/dist/server.d.ts +7 -1
  45. package/dist/server.d.ts.map +1 -1
  46. package/dist/server.js +342 -27
  47. package/dist/server.js.map +1 -1
  48. package/dist/skills.d.ts +53 -2
  49. package/dist/skills.d.ts.map +1 -1
  50. package/dist/skills.js +162 -2
  51. package/dist/skills.js.map +1 -1
  52. package/dist/timeout.d.ts +16 -0
  53. package/dist/timeout.d.ts.map +1 -0
  54. package/dist/timeout.js +38 -0
  55. package/dist/timeout.js.map +1 -0
  56. package/dist/toolkits.d.ts +138 -0
  57. package/dist/toolkits.d.ts.map +1 -0
  58. package/dist/toolkits.js +319 -0
  59. package/dist/toolkits.js.map +1 -0
  60. package/dist/types.d.ts +90 -1
  61. package/dist/types.d.ts.map +1 -1
  62. package/dist/ui.d.ts +63 -0
  63. package/dist/ui.d.ts.map +1 -1
  64. package/dist/ui.js +176 -11
  65. package/dist/ui.js.map +1 -1
  66. package/dist/version.d.ts +1 -1
  67. package/dist/version.js +1 -1
  68. package/package.json +5 -2
  69. package/src/activity.ts +9 -0
  70. package/src/auth/bearer.ts +35 -1
  71. package/src/auth/clerk.ts +202 -5
  72. package/src/connectors/api.ts +15 -0
  73. package/src/connectors/remote-mcp.ts +24 -0
  74. package/src/credential-health.ts +736 -0
  75. package/src/execute.ts +32 -8
  76. package/src/index.ts +226 -2
  77. package/src/meta-tools.ts +397 -119
  78. package/src/registry.ts +540 -29
  79. package/src/server.ts +431 -25
  80. package/src/skills.ts +185 -2
  81. package/src/timeout.ts +49 -0
  82. package/src/toolkits.ts +450 -0
  83. package/src/types.ts +96 -2
  84. package/src/ui.ts +190 -11
  85. package/src/version.ts +1 -1
@@ -0,0 +1,450 @@
1
+ // Toolkits: named, operator-defined scoped views over one deployment's
2
+ // registry, selected per client connection with `?toolkit=<name>` on /mcp.
3
+ //
4
+ // A connecta deployment belongs to an ORG; a toolkit is the view a GROUP OF
5
+ // TEAM MEMBERS inside that org gets — a "support" toolkit seeing Zendesk and
6
+ // Notion, an "exec" toolkit that also sees Gmail. This module only *defines and
7
+ // validates* scopes and the identity bindings that gate them. Enforcement lives
8
+ // in two places, each with one job:
9
+ //
10
+ // - WHICH toolkit an identity may open: the connect-time binding check in
11
+ // `resolveToolkitScope` (src/server.ts), run after the auth gate and before
12
+ // any scoped registry exists.
13
+ // - WHAT a selected toolkit may see: `ScopedRegistry` (src/registry.ts),
14
+ // which every meta-tool inherits through `RegistryView`.
15
+
16
+ import type { Connector, InboundAuth, ToolkitBinding } from "./types.js";
17
+
18
+ /** Toolkit names share the connector-id grammar: URL-safe, no separators. */
19
+ export const TOOLKIT_NAME_RE = /^[a-z0-9_-]+$/;
20
+
21
+ /** One named scope, declared in `ConnectaConfig.toolkits` (config as code). */
22
+ export interface ToolkitDefinition {
23
+ /** Connector ids this toolkit may see. Required, and at least one. */
24
+ connectors: string[];
25
+ /**
26
+ * Optional finer grain: full tool addresses (`"<connectorId>.<toolName>"`).
27
+ * Naming ANY address of a connector narrows that connector to exactly the
28
+ * addresses named; connectors with no entry here keep their whole tool list.
29
+ */
30
+ includeTools?: string[];
31
+ /** Optional tool addresses to hide, applied after `includeTools`. */
32
+ excludeTools?: string[];
33
+ /** Operator note. Never sent to clients — this is documentation for config. */
34
+ description?: string;
35
+ }
36
+
37
+ /** `ConnectaConfig.toolkits` — toolkit name → definition. */
38
+ export type ToolkitConfig = Record<string, ToolkitDefinition>;
39
+
40
+ /** A validated toolkit: the visibility predicate the scoped registry consults. */
41
+ export interface Toolkit {
42
+ readonly name: string;
43
+ readonly description?: string;
44
+ /** True when `connectorId` is inside this toolkit's scope. */
45
+ hasConnector(connectorId: string): boolean;
46
+ /** True when `<connectorId>.<toolName>` is inside this toolkit's scope. */
47
+ hasTool(connectorId: string, toolName: string): boolean;
48
+ }
49
+
50
+ /**
51
+ * Split `"<connectorId>.<toolName>"` on the FIRST dot — connector ids contain
52
+ * no dots, so a downstream tool name may. Returns null for a malformed address.
53
+ */
54
+ export function splitAddress(
55
+ address: string,
56
+ ): { connectorId: string; toolName: string } | null {
57
+ const dot = address.indexOf(".");
58
+ if (dot <= 0 || dot === address.length - 1) return null;
59
+ return {
60
+ connectorId: address.slice(0, dot),
61
+ toolName: address.slice(dot + 1),
62
+ };
63
+ }
64
+
65
+ /** Group tool addresses by connector id, validating each against the toolkit. */
66
+ function toolFilter(
67
+ name: string,
68
+ addresses: string[] | undefined,
69
+ connectorIds: ReadonlySet<string>,
70
+ staticTools: ReadonlyMap<string, ReadonlySet<string>>,
71
+ field: "includeTools" | "excludeTools",
72
+ ): Map<string, Set<string>> {
73
+ const byConnector = new Map<string, Set<string>>();
74
+ if (addresses !== undefined && !Array.isArray(addresses)) {
75
+ // A bare string would otherwise iterate character by character and produce
76
+ // a stream of confusing address errors; anything else would throw "not
77
+ // iterable" from deep inside the loop. Name the field instead.
78
+ throw new Error(
79
+ `Toolkit "${name}" ${field} must be an array of "<connectorId>.<toolName>" addresses.`,
80
+ );
81
+ }
82
+ if (
83
+ addresses !== undefined &&
84
+ addresses.length === 0 &&
85
+ field === "includeTools"
86
+ ) {
87
+ // An empty allowlist reads as "only these tools" but would behave as "all
88
+ // of them" — the one shape here that fails OPEN. (An empty excludeTools is
89
+ // an honest no-op and is allowed.)
90
+ throw new Error(
91
+ `Toolkit "${name}" has an empty includeTools: remove it to expose every tool, or list the addresses this toolkit may use.`,
92
+ );
93
+ }
94
+ for (const address of addresses ?? []) {
95
+ const parts = splitAddress(address);
96
+ if (!parts) {
97
+ throw new Error(
98
+ `Toolkit "${name}" ${field} entry "${address}" is not a tool address: expected "<connectorId>.<toolName>".`,
99
+ );
100
+ }
101
+ if (!connectorIds.has(parts.connectorId)) {
102
+ // A typo here would silently do nothing, quietly widening the scope the
103
+ // operator believes they wrote. Fail at construction instead.
104
+ throw new Error(
105
+ `Toolkit "${name}" ${field} entry "${address}" names connector "${parts.connectorId}", which is not in this toolkit's connectors list.`,
106
+ );
107
+ }
108
+ // Static-only, exactly like the registry's convention checks: an in-code
109
+ // connector's tool list is known now, so a misspelled name — an exclude
110
+ // that silently excludes nothing — is caught. Remote catalogs are fetched
111
+ // lazily over the network and cannot be checked at construction.
112
+ const known = staticTools.get(parts.connectorId);
113
+ if (known && !known.has(parts.toolName)) {
114
+ throw new Error(
115
+ `Toolkit "${name}" ${field} entry "${address}" names no tool on connector "${parts.connectorId}".`,
116
+ );
117
+ }
118
+ const tools = byConnector.get(parts.connectorId) ?? new Set<string>();
119
+ tools.add(parts.toolName);
120
+ byConnector.set(parts.connectorId, tools);
121
+ }
122
+ return byConnector;
123
+ }
124
+
125
+ /**
126
+ * Validate one toolkit definition against the deployment's connectors.
127
+ *
128
+ * Structural mistakes THROW at construction rather than warn: a typo'd id in
129
+ * an allowlist is a scope the operator did not write, and a scope nobody wrote
130
+ * is not one an operator can reason about. (A definition scopes visibility only;
131
+ * WHICH identity may select it is the separate binding below — see the module
132
+ * header and documentation.md §16.) Tool names are checked only for connectors that expose
133
+ * `staticTools` (i.e. `api()`); a remote connector's catalog is fetched lazily
134
+ * over the network and is unknown at construction time.
135
+ */
136
+ function resolveToolkit(
137
+ name: string,
138
+ definition: ToolkitDefinition,
139
+ known: ReadonlySet<string>,
140
+ staticTools: ReadonlyMap<string, ReadonlySet<string>>,
141
+ ): Toolkit {
142
+ if (!TOOLKIT_NAME_RE.test(name)) {
143
+ throw new Error(
144
+ `Invalid toolkit name "${name}": must match ${TOOLKIT_NAME_RE.source}`,
145
+ );
146
+ }
147
+ if (
148
+ !Array.isArray(definition.connectors) ||
149
+ definition.connectors.length === 0
150
+ ) {
151
+ throw new Error(
152
+ `Toolkit "${name}" selects no connectors: list at least one connector id in "connectors".`,
153
+ );
154
+ }
155
+ const connectorIds = new Set<string>();
156
+ for (const id of definition.connectors) {
157
+ if (!known.has(id)) {
158
+ throw new Error(
159
+ `Toolkit "${name}" references unknown connector "${id}".`,
160
+ );
161
+ }
162
+ connectorIds.add(id);
163
+ }
164
+ const includes = toolFilter(
165
+ name,
166
+ definition.includeTools,
167
+ connectorIds,
168
+ staticTools,
169
+ "includeTools",
170
+ );
171
+ const excludes = toolFilter(
172
+ name,
173
+ definition.excludeTools,
174
+ connectorIds,
175
+ staticTools,
176
+ "excludeTools",
177
+ );
178
+ return {
179
+ name,
180
+ ...(definition.description ? { description: definition.description } : {}),
181
+ hasConnector: (connectorId) => connectorIds.has(connectorId),
182
+ hasTool: (connectorId, toolName) => {
183
+ if (!connectorIds.has(connectorId)) return false;
184
+ const include = includes.get(connectorId);
185
+ if (include && !include.has(toolName)) return false;
186
+ return !excludes.get(connectorId)?.has(toolName);
187
+ },
188
+ };
189
+ }
190
+
191
+ /**
192
+ * Validate every declared toolkit against the connector set. Returns undefined
193
+ * when no toolkits are configured, so an existing deployment keeps exactly its
194
+ * current (unscoped) behavior.
195
+ */
196
+ export function resolveToolkits(
197
+ toolkits: ToolkitConfig | undefined,
198
+ connectors: readonly Connector[],
199
+ ): ReadonlyMap<string, Toolkit> | undefined {
200
+ if (!toolkits) return undefined;
201
+ // Object.entries (not a keyed lookup) so no config key — `__proto__` and
202
+ // friends included — can ever resolve through the prototype chain. Names are
203
+ // then held in a Map, which has no prototype to pollute.
204
+ const entries = Object.entries(toolkits);
205
+ if (entries.length === 0) return undefined;
206
+ const known = new Set(connectors.map((connector) => connector.id));
207
+ const staticTools = new Map<string, ReadonlySet<string>>();
208
+ for (const connector of connectors) {
209
+ if (connector.staticTools) {
210
+ staticTools.set(
211
+ connector.id,
212
+ new Set(connector.staticTools.map((tool) => tool.name)),
213
+ );
214
+ }
215
+ }
216
+ const resolved = new Map<string, Toolkit>();
217
+ for (const [name, definition] of entries) {
218
+ resolved.set(name, resolveToolkit(name, definition, known, staticTools));
219
+ }
220
+ return resolved;
221
+ }
222
+
223
+ /**
224
+ * The binding half of an inbound-auth adapter's options — the shape every
225
+ * shipped adapter (`bearerToken`, `clerkAuth`) mixes into its own options so an
226
+ * operator writes one thing in one style, next to the credential it binds.
227
+ */
228
+ export interface ToolkitBindingOptions {
229
+ /**
230
+ * Toolkit names this credential may select with `?toolkit=<name>`. Present ⇒
231
+ * the identity is BOUND: any other toolkit, and (unless `unscoped`) a
232
+ * connection with no `?toolkit=`, is refused at connect time. Absent ⇒
233
+ * unbound, exactly as before bindings existed.
234
+ */
235
+ toolkits?: readonly string[];
236
+ /**
237
+ * Also allow a connection with no `?toolkit=` (the full registry, and the
238
+ * deployment-wide operator surfaces). Only meaningful beside `toolkits`.
239
+ */
240
+ unscoped?: boolean;
241
+ }
242
+
243
+ /**
244
+ * Validate one adapter's binding options into a `ToolkitBinding`, or undefined
245
+ * when the adapter declares none. Structural mistakes THROW where the operator
246
+ * wrote them (adapter construction), for the same reason toolkit definitions do:
247
+ * a binding that does not say what its author meant is worse than none, because
248
+ * it is invisible until the day it denies — or admits — the wrong caller.
249
+ *
250
+ * Names are only checked against the *grammar* here; cross-checking them
251
+ * against the configured toolkits happens in `validateToolkitBindings`, which
252
+ * runs in `createConnecta` where both halves are finally in scope.
253
+ */
254
+ export function resolveToolkitBinding(
255
+ source: string,
256
+ options: ToolkitBindingOptions,
257
+ ): ToolkitBinding | undefined {
258
+ const { toolkits, unscoped } = options;
259
+ if (toolkits === undefined) {
260
+ if (unscoped !== undefined) {
261
+ // `unscoped` alone reads like a permission but grants nothing an unbound
262
+ // identity does not already have, so it is almost certainly a half-written
263
+ // binding — the one shape here that would silently fail OPEN.
264
+ throw new Error(
265
+ `${source}: \`unscoped\` only means something beside \`toolkits\`. ` +
266
+ "List the toolkits this credential may open, or drop `unscoped` to " +
267
+ "leave the credential unbound.",
268
+ );
269
+ }
270
+ return undefined;
271
+ }
272
+ if (!Array.isArray(toolkits)) {
273
+ throw new Error(
274
+ `${source}: \`toolkits\` must be an array of toolkit names.`,
275
+ );
276
+ }
277
+ const names: string[] = [];
278
+ for (const name of toolkits) {
279
+ if (typeof name !== "string" || !TOOLKIT_NAME_RE.test(name)) {
280
+ // A name outside the grammar can never match a declared toolkit, so this
281
+ // would bind the credential to nothing selectable.
282
+ throw new Error(
283
+ `${source}: \`toolkits\` entry ${JSON.stringify(name)} is not a ` +
284
+ `toolkit name (must match ${TOOLKIT_NAME_RE.source}).`,
285
+ );
286
+ }
287
+ if (!names.includes(name)) names.push(name);
288
+ }
289
+ if (names.length === 0 && unscoped !== true) {
290
+ throw new Error(
291
+ `${source}: binds no toolkits and no unscoped access, so this credential ` +
292
+ "could authenticate but never connect. List at least one toolkit, or " +
293
+ "pass `unscoped: true` to bind it to the full registry only.",
294
+ );
295
+ }
296
+ return Object.freeze({
297
+ toolkits: Object.freeze(names) as readonly string[],
298
+ ...(unscoped === true ? { unscoped: true } : {}),
299
+ });
300
+ }
301
+
302
+ /**
303
+ * Coerce an arbitrary value into a `ToolkitBinding`, or null when it is not one.
304
+ *
305
+ * The shipped adapters build bindings through `resolveToolkitBinding` above, but
306
+ * `InboundAuth` is an open interface and `AuthResult.toolkitBinding` arrives at
307
+ * REQUEST time from code connecta does not own — a custom adapter, or one
308
+ * mapping an IdP claim. Every field is therefore re-checked here rather than
309
+ * trusted from the type, because each way of being wrong fails OPEN if it is
310
+ * merely believed:
311
+ *
312
+ * - `unscoped` is compared to `true` by identity, so a truthy non-boolean (the
313
+ * string `"false"` out of an env var, say) cannot grant the full registry;
314
+ * - `toolkits` must be a real array — a bare string would otherwise reach
315
+ * `String.prototype.includes`, where `?toolkit=sup` would "match" `"support"`
316
+ * by substring;
317
+ * - a missing/!array `toolkits` is not treated as an empty binding, because the
318
+ * caller of a null return refuses the request outright.
319
+ *
320
+ * Returns a frozen, deduplicated copy: nothing downstream can be mutated by the
321
+ * adapter after the check, and every name is known to fit the grammar.
322
+ */
323
+ export function normalizeToolkitBinding(value: unknown): ToolkitBinding | null {
324
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
325
+ return null;
326
+ }
327
+ const { toolkits, unscoped } = value as {
328
+ toolkits?: unknown;
329
+ unscoped?: unknown;
330
+ };
331
+ if (!Array.isArray(toolkits)) return null;
332
+ if (unscoped !== undefined && typeof unscoped !== "boolean") return null;
333
+ const names: string[] = [];
334
+ for (const name of toolkits) {
335
+ if (typeof name !== "string" || !TOOLKIT_NAME_RE.test(name)) return null;
336
+ if (!names.includes(name)) names.push(name);
337
+ }
338
+ return Object.freeze({
339
+ toolkits: Object.freeze(names) as readonly string[],
340
+ ...(unscoped === true ? { unscoped: true } : {}),
341
+ });
342
+ }
343
+
344
+ /**
345
+ * Resolve the binding one admitted identity is actually held to, from the
346
+ * provider's static declaration and whatever its `authorize` returned.
347
+ *
348
+ * - Neither ⇒ unbound (undefined), the pre-binding behavior.
349
+ * - Declaration only ⇒ the declaration.
350
+ * - Per-identity only ⇒ that binding, validated. This is the custom-adapter
351
+ * seam: a provider that declares nothing is asserting it resolves membership
352
+ * itself, so there is nothing to check it against.
353
+ * - Both ⇒ the **intersection**. The declaration is a CEILING, not a default: an
354
+ * adapter that maps a user-writable IdP claim to toolkits must not be able to
355
+ * widen the credential's own binding, which would turn "support token" into
356
+ * "any toolkit, plus the full registry" for anyone who can set that claim.
357
+ * Narrowing is fine and useful (per-user subsets of the team's view).
358
+ *
359
+ * A malformed binding on either side is not silently ignored — it returns
360
+ * `{ ok: false }` and the caller refuses the request, because the alternative
361
+ * (dropping it) is the fail-open reading.
362
+ */
363
+ export function resolveIdentityBinding(
364
+ declared: unknown,
365
+ perIdentity: unknown,
366
+ ):
367
+ | { ok: true; binding?: ToolkitBinding }
368
+ | { ok: false; reason: string } {
369
+ const ceiling =
370
+ declared === undefined ? undefined : normalizeToolkitBinding(declared);
371
+ if (declared !== undefined && !ceiling) {
372
+ return {
373
+ ok: false,
374
+ reason:
375
+ "the toolkit binding declared on the provider is malformed " +
376
+ "(`toolkits` must be an array of toolkit names, `unscoped` a boolean)",
377
+ };
378
+ }
379
+ if (perIdentity === undefined) {
380
+ return ceiling ? { ok: true, binding: ceiling } : { ok: true };
381
+ }
382
+ const identity = normalizeToolkitBinding(perIdentity);
383
+ if (!identity) {
384
+ return {
385
+ ok: false,
386
+ reason:
387
+ "the toolkit binding its authorize() returned for this identity is " +
388
+ "malformed (`toolkits` must be an array of toolkit names, `unscoped` " +
389
+ "a boolean)",
390
+ };
391
+ }
392
+ if (!ceiling) return { ok: true, binding: identity };
393
+ return {
394
+ ok: true,
395
+ binding: Object.freeze({
396
+ toolkits: Object.freeze(
397
+ identity.toolkits.filter((name) => ceiling.toolkits.includes(name)),
398
+ ) as readonly string[],
399
+ ...(identity.unscoped === true && ceiling.unscoped === true
400
+ ? { unscoped: true }
401
+ : {}),
402
+ }),
403
+ };
404
+ }
405
+
406
+ /**
407
+ * Cross-check every statically declared binding against the deployment's
408
+ * toolkits, in `createConnecta`. A name that no toolkit declares is a typo, and
409
+ * a typo here fails CLOSED — the credential would be refused every connection
410
+ * with a 403 the client reads as a transport failure — so it throws at
411
+ * construction rather than becoming a support ticket. A structurally malformed
412
+ * declaration (only reachable from a hand-written `InboundAuth`, since the
413
+ * shipped adapters validate their own options) throws here too, rather than
414
+ * waiting to refuse every request at runtime.
415
+ *
416
+ * Bindings a provider mints per-identity (`AuthResult.toolkitBinding`) do not
417
+ * exist yet and cannot be checked here; they are validated on arrival and capped
418
+ * by the declaration (`resolveIdentityBinding`).
419
+ */
420
+ export function validateToolkitBindings(
421
+ auth: readonly InboundAuth[],
422
+ toolkits: ReadonlyMap<string, Toolkit> | undefined,
423
+ ): void {
424
+ for (const provider of auth) {
425
+ if (provider.toolkitBinding === undefined) continue;
426
+ const binding = normalizeToolkitBinding(provider.toolkitBinding);
427
+ if (!binding) {
428
+ throw new Error(
429
+ `Inbound auth provider "${provider.kind}" declares a malformed ` +
430
+ "toolkitBinding: `toolkits` must be an array of toolkit names " +
431
+ `(matching ${TOOLKIT_NAME_RE.source}) and \`unscoped\` a boolean.`,
432
+ );
433
+ }
434
+ if (!toolkits || toolkits.size === 0) {
435
+ throw new Error(
436
+ `Inbound auth provider "${provider.kind}" binds toolkits ` +
437
+ `(${binding.toolkits.join(", ")}) but this deployment configures no ` +
438
+ "toolkits. Declare them in `toolkits`, or drop the binding.",
439
+ );
440
+ }
441
+ for (const name of binding.toolkits) {
442
+ if (!toolkits.has(name)) {
443
+ throw new Error(
444
+ `Inbound auth provider "${provider.kind}" binds unknown toolkit ` +
445
+ `"${name}". Configured toolkits: ${[...toolkits.keys()].join(", ")}.`,
446
+ );
447
+ }
448
+ }
449
+ }
450
+ }
package/src/types.ts CHANGED
@@ -135,6 +135,23 @@ export interface Connector {
135
135
  /** How call_tool wraps results. "mcp" passes the content array through; anything else is JSON-wrapped. */
136
136
  kind?: "mcp" | "api";
137
137
  description?: string;
138
+ /**
139
+ * Max inline result size (bytes) for this connector's tools before
140
+ * call_tool/batch_call truncate and stash the full text for get_result
141
+ * paging. Overrides the deployment-wide `ConnectaConfig.maxResultBytes`;
142
+ * omit to inherit it (which itself defaults to 50_000). Must be a whole
143
+ * number of bytes >= 1; anything else warns at startup and is ignored, so
144
+ * the connector inherits the deployment-wide cap.
145
+ */
146
+ maxResultBytes?: number;
147
+ /**
148
+ * Optional agent-facing usage guide (markdown) for this connector — preferred
149
+ * tools, address quirks, pagination conventions, rate-limit etiquette, good
150
+ * query patterns. Listed by the `skills` meta-tool as `connector:<id>` and
151
+ * returned verbatim by `skills({ name: "connector:<id>" })`. Keep it concise
152
+ * and imperative; it is read by agents, not operators.
153
+ */
154
+ usageGuide?: string;
138
155
  /** Optional operator-managed credential slot rendered inside this connector's /ui card. */
139
156
  credential?: ConnectorCredentialConfig;
140
157
  /** Optional server-side check used by /ui's Test action. */
@@ -147,6 +164,19 @@ export interface Connector {
147
164
  values: ConnectorCredentialValues,
148
165
  ctx: ConnectorContext,
149
166
  ): Promise<CredentialTestResult>;
167
+ /**
168
+ * Optional: whether this connector currently holds a stored downstream
169
+ * credential — an OAuth grant it persisted, typically. Read only by the
170
+ * credential liveness checks: a connector with nothing stored has no
171
+ * credential whose liveness could be in question, and probing it anyway would
172
+ * start an authorization flow nobody asked for.
173
+ *
174
+ * Implement it on connectors that manage their own credential storage (the
175
+ * shipped `remoteMcp` does, for `auth: { type: "oauth" }`). Connectors whose
176
+ * credential lives in connecta's vault (`credential` above) need not: the
177
+ * vault answers for them. Must not perform downstream I/O.
178
+ */
179
+ hasStoredCredential?(ctx: ConnectorContext): Promise<boolean>;
150
180
  /**
151
181
  * Statically-known tool defs, exposed by in-code connectors (`api()`) for
152
182
  * startup convention checks. Remote connectors omit this — their tools are
@@ -222,15 +252,69 @@ export interface Executor {
222
252
  execute(code: string, providers: ExecutorProvider[]): Promise<ExecuteResult>;
223
253
  }
224
254
 
255
+ /**
256
+ * Which toolkits one inbound identity may open — the membership half of the
257
+ * deployment=org / toolkit=team framing (§16). A mapping, never a policy
258
+ * engine: one identity → the toolkit names it may select, plus whether it may
259
+ * connect with no `?toolkit=` at all.
260
+ *
261
+ * An identity with NO binding is unbound and keeps the pre-binding behavior:
262
+ * any declared toolkit, or the full registry. A binding is enforced at connect
263
+ * time, before any scoped registry is constructed.
264
+ */
265
+ export interface ToolkitBinding {
266
+ /** Toolkit names this identity may select with `?toolkit=<name>`. */
267
+ readonly toolkits: readonly string[];
268
+ /**
269
+ * Whether this identity may also connect with no `?toolkit=` and see the full
270
+ * registry (and read the deployment-wide operator surfaces). Defaults to
271
+ * false: binding a credential to a toolkit means binding it.
272
+ */
273
+ readonly unscoped?: boolean;
274
+ }
275
+
225
276
  /** Result of an inbound-auth check. */
226
277
  export type AuthResult =
227
- | { ok: true; userId?: string; subjectId?: string }
278
+ | {
279
+ ok: true;
280
+ userId?: string;
281
+ subjectId?: string;
282
+ /**
283
+ * Toolkit binding resolved for THIS identity — the seam for an adapter
284
+ * that maps its own users (or an IdP claim) to views. Omit to inherit the
285
+ * provider's `toolkitBinding`.
286
+ *
287
+ * When the provider also declares one, the declaration is a **CEILING**,
288
+ * not a default: connecta intersects the two, and grants `unscoped` only
289
+ * if both do. A per-identity binding can therefore narrow the credential's
290
+ * view but never widen it — otherwise an adapter reading a user-writable
291
+ * claim would let the user name their own toolkits. When the provider
292
+ * declares nothing, this binding is used as given.
293
+ *
294
+ * Validated on arrival (a malformed one refuses the request with 403
295
+ * rather than being ignored), but never checked against the configured
296
+ * toolkits, which is only possible for the static declaration at startup.
297
+ */
298
+ toolkitBinding?: ToolkitBinding;
299
+ }
228
300
  | { ok: false; response: Response };
229
301
 
230
302
  /** Public browser-auth configuration exposed to connecta's status UI. */
231
303
  export type UiAuthConfig = {
232
304
  kind: "clerk";
233
305
  publishableKey: string;
306
+ /**
307
+ * Origin `/ui` fetches its browser sign-in loader from. **Must be an absolute
308
+ * `https:` URL** — the value lands in a `<script src>`, so the gate is
309
+ * stricter than the branding href gate: no `http:`, no loopback exemption, and
310
+ * no root-relative form (a relative path is rejected, not resolved). The
311
+ * shipped `clerkAuth` adapter derives this from the publishable key and
312
+ * Clerk's Frontend API is always https, so nothing legitimate needs a
313
+ * carve-out. A value that fails the gate reaches neither the loader tag nor
314
+ * the page's inline auth config: `/ui` renders without the loader and reports
315
+ * that Clerk could not load, and `createConnecta` names the drop in a startup
316
+ * warning.
317
+ */
234
318
  frontendApiUrl: string;
235
319
  signInUrl?: string;
236
320
  signUpUrl?: string;
@@ -262,7 +346,9 @@ export interface ConnectaBranding {
262
346
  * `/favicon.svg`, `ico` at `/favicon.ico`; omit either to keep the default
263
347
  * for that format. Use `href` instead to point the page at an icon you host
264
348
  * elsewhere (it replaces the `/favicon.svg` link in the page head; the
265
- * `/favicon.*` routes still serve whatever `svg`/`ico` provide).
349
+ * `/favicon.*` routes still serve whatever `svg`/`ico` provide). `href` must
350
+ * be an absolute `http(s)` URL or a root-relative path; anything else falls
351
+ * back to the default mark.
266
352
  */
267
353
  favicon?: {
268
354
  svg?: string;
@@ -281,6 +367,14 @@ export interface InboundAuth {
281
367
  * provider instead of asking the operator to paste a static bearer secret.
282
368
  */
283
369
  uiAuth?: UiAuthConfig;
370
+ /**
371
+ * Optional toolkit binding for every identity this provider admits (§16).
372
+ * Declared statically so `createConnecta` can validate the names against
373
+ * `ConnectaConfig.toolkits` and throw on a typo — a binding nobody wrote is
374
+ * not one an operator can reason about. An `authorize` result may narrow it
375
+ * per identity with its own `toolkitBinding`.
376
+ */
377
+ toolkitBinding?: ToolkitBinding;
284
378
  /** Serve/short-circuit .well-known + OPTIONS. Return null when not handled. */
285
379
  handleMetadata?(
286
380
  request: Request,