@kya-os/mcp-i-runtime 1.3.0 → 1.3.2

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 (62) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/index.d.ts +2 -1
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +13 -2
  5. package/dist/index.js.map +1 -1
  6. package/dist/proof/generator.d.ts +8 -6
  7. package/dist/proof/generator.d.ts.map +1 -1
  8. package/dist/proof/generator.js +7 -24
  9. package/dist/proof/generator.js.map +1 -1
  10. package/dist/runtime/audit.d.ts.map +1 -1
  11. package/dist/runtime/audit.js +66 -18
  12. package/dist/runtime/audit.js.map +1 -1
  13. package/dist/runtime/base.d.ts +6 -1
  14. package/dist/runtime/base.d.ts.map +1 -1
  15. package/dist/runtime/base.js +65 -17
  16. package/dist/runtime/base.js.map +1 -1
  17. package/dist/services/access-control.service.d.ts.map +1 -1
  18. package/dist/services/access-control.service.js +12 -5
  19. package/dist/services/access-control.service.js.map +1 -1
  20. package/dist/services/proof-verifier.d.ts +13 -35
  21. package/dist/services/proof-verifier.d.ts.map +1 -1
  22. package/dist/services/proof-verifier.js +16 -74
  23. package/dist/services/proof-verifier.js.map +1 -1
  24. package/dist/services/tool-context-builder.d.ts.map +1 -1
  25. package/dist/services/tool-context-builder.js +16 -4
  26. package/dist/services/tool-context-builder.js.map +1 -1
  27. package/dist/services/tool-protection.service.d.ts +7 -0
  28. package/dist/services/tool-protection.service.d.ts.map +1 -1
  29. package/dist/services/tool-protection.service.js +34 -15
  30. package/dist/services/tool-protection.service.js.map +1 -1
  31. package/dist/types/tool-protection.d.ts +94 -0
  32. package/dist/types/tool-protection.d.ts.map +1 -1
  33. package/dist/types/tool-protection.js +113 -1
  34. package/dist/types/tool-protection.js.map +1 -1
  35. package/package.json +4 -3
  36. package/src/__tests__/runtime/amount-limit-enforcement.test.ts +269 -0
  37. package/src/__tests__/runtime/amount-limit-result.test.ts +61 -0
  38. package/src/__tests__/runtime/audit-contracts-compat.test.ts +20 -5
  39. package/src/__tests__/runtime/audit.test.ts +255 -53
  40. package/src/__tests__/runtime/process-tool-call-security.test.ts +97 -13
  41. package/src/__tests__/runtime/provider-selection.test.ts +4 -6
  42. package/src/__tests__/runtime/route-interception.test.ts +215 -214
  43. package/src/__tests__/runtime/tool-protection-enforcement.test.ts +89 -6
  44. package/src/__tests__/services/tool-protection-amount.test.ts +107 -0
  45. package/src/__tests__/services/tool-protection-failsafe.test.ts +71 -4
  46. package/src/__tests__/utils/tool-protection-runtime.ts +3 -0
  47. package/src/index.ts +15 -1
  48. package/src/proof/__tests__/proof-generator.test.ts +35 -12
  49. package/src/proof/generator.ts +18 -27
  50. package/src/runtime/__tests__/ext-apps-consent.test.ts +87 -2
  51. package/src/runtime/audit.ts +155 -62
  52. package/src/runtime/base.ts +95 -31
  53. package/src/services/__tests__/access-control.service.test.ts +44 -0
  54. package/src/services/__tests__/proof-verifier.test.ts +283 -195
  55. package/src/services/__tests__/tool-context-builder-authtype.test.ts +79 -0
  56. package/src/services/__tests__/tool-context-builder.test.ts +4 -7
  57. package/src/services/__tests__/tool-protection-served-revision.test.ts +102 -0
  58. package/src/services/access-control.service.ts +11 -5
  59. package/src/services/proof-verifier.ts +18 -79
  60. package/src/services/tool-context-builder.ts +19 -4
  61. package/src/services/tool-protection.service.ts +62 -19
  62. package/src/types/tool-protection.ts +161 -0
@@ -14,6 +14,12 @@ import type {
14
14
  ToolProtectionMap,
15
15
  } from "@kya-os/contracts/tool-protection";
16
16
  import type { DelegationDenial } from "../runtime/delegation-denial.js";
17
+ import { markToolRefusal } from "@kya-os/contracts/config";
18
+ import {
19
+ describeAmountLimitViolation,
20
+ type AmountLimitCode,
21
+ type AmountLimitViolation,
22
+ } from "@kya-os/contracts/delegation";
17
23
 
18
24
  // Re-export for backward compatibility
19
25
  export type { ToolProtection };
@@ -27,6 +33,33 @@ export interface ToolProtectionConfig {
27
33
  * Map of tool names to their protection settings
28
34
  */
29
35
  toolProtections: ToolProtectionMap;
36
+ /**
37
+ * The revision of the tool protection Checkpoint served for this config:
38
+ * SHA-256 (lowercase hex) of the RFC 8785 canonical JSON of the served
39
+ * `config.toolProtection.tools` object, exactly as received. Checkpoint
40
+ * recomputes it from what it serves. Absent for local, fallback and
41
+ * deny-all configs, which Checkpoint did not serve.
42
+ */
43
+ servedRevision?: string;
44
+ }
45
+
46
+ /**
47
+ * The wildcard protection the service stands in with when no config can be
48
+ * read and none is cached. Every tool resolves to it, and the runtime refuses
49
+ * it until a config arrives. It is never cached.
50
+ */
51
+ export interface UnavailableToolProtection extends ToolProtection {
52
+ readonly unavailable: true;
53
+ }
54
+
55
+ export function unavailableToolProtection(): UnavailableToolProtection {
56
+ return { requiresDelegation: true, requiredScopes: [], unavailable: true };
57
+ }
58
+
59
+ export function isUnavailableToolProtection(
60
+ protection: ToolProtection
61
+ ): protection is UnavailableToolProtection {
62
+ return (protection as { unavailable?: unknown }).unavailable === true;
30
63
  }
31
64
 
32
65
  /**
@@ -163,6 +196,134 @@ export class DelegationRequiredError extends Error {
163
196
  }
164
197
  }
165
198
 
199
+ export const TOOL_PROTECTION_UNENFORCEABLE =
200
+ "tool_protection_unenforceable" as const;
201
+
202
+ /**
203
+ * A protected tool was called where its protection can't be enforced: the
204
+ * deployment lacks what the protection service needs, the protection names no
205
+ * scope to check a delegation against, or no protection config can be read.
206
+ * Refused before the handler runs.
207
+ */
208
+ export class ToolProtectionUnenforceableError extends Error {
209
+ readonly code = TOOL_PROTECTION_UNENFORCEABLE;
210
+
211
+ constructor(
212
+ readonly toolName: string,
213
+ /**
214
+ * What enforcement lacks: the deployment's CHECKPOINT_API_KEY and/or
215
+ * TOOL_PROTECTION_KV, the protection's requiredScopes, or the
216
+ * toolProtectionConfig itself.
217
+ */
218
+ readonly missing: readonly string[],
219
+ message?: string
220
+ ) {
221
+ super(
222
+ message ??
223
+ `Tool "${toolName}" is protected, but this deployment can't enforce its ` +
224
+ `protection without ${missing.join(" and ")}. Configure ` +
225
+ `${missing.length === 1 ? "it" : "them"} to call this tool.`
226
+ );
227
+ this.name = "ToolProtectionUnenforceableError";
228
+ }
229
+
230
+ toJSON() {
231
+ return {
232
+ error: this.code,
233
+ message: this.message,
234
+ toolName: this.toolName,
235
+ missing: [...this.missing],
236
+ };
237
+ }
238
+ }
239
+
240
+ // By shape, like the other refusal checks: the error may come from another
241
+ // copy of this package.
242
+ export function isToolProtectionUnenforceableError(
243
+ error: unknown
244
+ ): error is ToolProtectionUnenforceableError {
245
+ return (
246
+ error instanceof Error &&
247
+ error.name === "ToolProtectionUnenforceableError" &&
248
+ (error as { code?: unknown }).code === TOOL_PROTECTION_UNENFORCEABLE
249
+ );
250
+ }
251
+
252
+ /**
253
+ * Whether a protection's required scopes name at least one scope. With none
254
+ * there is nothing to check a delegation against, so the protection can't be
255
+ * enforced: an empty list never counts as satisfied.
256
+ */
257
+ export function namesRequiredScope(
258
+ requiredScopes: readonly unknown[] | undefined
259
+ ): boolean {
260
+ return (
261
+ Array.isArray(requiredScopes) &&
262
+ requiredScopes.some((scope) => typeof scope === "string" && scope !== "")
263
+ );
264
+ }
265
+
266
+ /** The refusal for a protected tool whose protection names no scope. */
267
+ export function unscopedToolProtectionError(
268
+ toolName: string
269
+ ): ToolProtectionUnenforceableError {
270
+ return new ToolProtectionUnenforceableError(
271
+ toolName,
272
+ ["requiredScopes"],
273
+ `Tool "${toolName}" is protected, but its protection names no required ` +
274
+ `scope, so no delegation can be checked against it. Give it a scope to ` +
275
+ `call this tool.`
276
+ );
277
+ }
278
+
279
+ /** The refusal while no tool protection config can be read. */
280
+ export function unavailableToolProtectionError(
281
+ toolName: string
282
+ ): ToolProtectionUnenforceableError {
283
+ return new ToolProtectionUnenforceableError(
284
+ toolName,
285
+ ["toolProtectionConfig"],
286
+ `Tool "${toolName}" can't be called while its protection is unavailable: ` +
287
+ `the tool protection config can't be read and none is cached. It is ` +
288
+ `refused until the config can be read.`
289
+ );
290
+ }
291
+
292
+ export class AmountLimitError extends Error {
293
+ public readonly code: AmountLimitCode;
294
+
295
+ constructor(
296
+ public readonly toolName: string,
297
+ public readonly violation: AmountLimitViolation
298
+ ) {
299
+ super(describeAmountLimitViolation(toolName, violation));
300
+ this.name = "AmountLimitError";
301
+ this.code = violation.code;
302
+ }
303
+
304
+ toJSON() {
305
+ const { code, ...detail } = this.violation;
306
+ return {
307
+ error: code,
308
+ message: this.message,
309
+ toolName: this.toolName,
310
+ ...detail,
311
+ };
312
+ }
313
+ }
314
+
315
+ export function amountRefusalCode(code: AmountLimitCode): string {
316
+ return code.toUpperCase();
317
+ }
318
+
319
+ export function amountLimitToolResult(error: AmountLimitError) {
320
+ return {
321
+ content: [{ type: "text" as const, text: JSON.stringify(error) }],
322
+ isError: true as const,
323
+ _meta: markToolRefusal(amountRefusalCode(error.code)),
324
+ };
325
+ }
326
+
166
327
  /**
167
328
  * Context for an intercepted tool call that needs authorization
168
329
  */