@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.
- package/.turbo/turbo-build.log +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -2
- package/dist/index.js.map +1 -1
- package/dist/proof/generator.d.ts +8 -6
- package/dist/proof/generator.d.ts.map +1 -1
- package/dist/proof/generator.js +7 -24
- package/dist/proof/generator.js.map +1 -1
- package/dist/runtime/audit.d.ts.map +1 -1
- package/dist/runtime/audit.js +66 -18
- package/dist/runtime/audit.js.map +1 -1
- package/dist/runtime/base.d.ts +6 -1
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js +65 -17
- package/dist/runtime/base.js.map +1 -1
- package/dist/services/access-control.service.d.ts.map +1 -1
- package/dist/services/access-control.service.js +12 -5
- package/dist/services/access-control.service.js.map +1 -1
- package/dist/services/proof-verifier.d.ts +13 -35
- package/dist/services/proof-verifier.d.ts.map +1 -1
- package/dist/services/proof-verifier.js +16 -74
- package/dist/services/proof-verifier.js.map +1 -1
- package/dist/services/tool-context-builder.d.ts.map +1 -1
- package/dist/services/tool-context-builder.js +16 -4
- package/dist/services/tool-context-builder.js.map +1 -1
- package/dist/services/tool-protection.service.d.ts +7 -0
- package/dist/services/tool-protection.service.d.ts.map +1 -1
- package/dist/services/tool-protection.service.js +34 -15
- package/dist/services/tool-protection.service.js.map +1 -1
- package/dist/types/tool-protection.d.ts +94 -0
- package/dist/types/tool-protection.d.ts.map +1 -1
- package/dist/types/tool-protection.js +113 -1
- package/dist/types/tool-protection.js.map +1 -1
- package/package.json +4 -3
- package/src/__tests__/runtime/amount-limit-enforcement.test.ts +269 -0
- package/src/__tests__/runtime/amount-limit-result.test.ts +61 -0
- package/src/__tests__/runtime/audit-contracts-compat.test.ts +20 -5
- package/src/__tests__/runtime/audit.test.ts +255 -53
- package/src/__tests__/runtime/process-tool-call-security.test.ts +97 -13
- package/src/__tests__/runtime/provider-selection.test.ts +4 -6
- package/src/__tests__/runtime/route-interception.test.ts +215 -214
- package/src/__tests__/runtime/tool-protection-enforcement.test.ts +89 -6
- package/src/__tests__/services/tool-protection-amount.test.ts +107 -0
- package/src/__tests__/services/tool-protection-failsafe.test.ts +71 -4
- package/src/__tests__/utils/tool-protection-runtime.ts +3 -0
- package/src/index.ts +15 -1
- package/src/proof/__tests__/proof-generator.test.ts +35 -12
- package/src/proof/generator.ts +18 -27
- package/src/runtime/__tests__/ext-apps-consent.test.ts +87 -2
- package/src/runtime/audit.ts +155 -62
- package/src/runtime/base.ts +95 -31
- package/src/services/__tests__/access-control.service.test.ts +44 -0
- package/src/services/__tests__/proof-verifier.test.ts +283 -195
- package/src/services/__tests__/tool-context-builder-authtype.test.ts +79 -0
- package/src/services/__tests__/tool-context-builder.test.ts +4 -7
- package/src/services/__tests__/tool-protection-served-revision.test.ts +102 -0
- package/src/services/access-control.service.ts +11 -5
- package/src/services/proof-verifier.ts +18 -79
- package/src/services/tool-context-builder.ts +19 -4
- package/src/services/tool-protection.service.ts +62 -19
- package/src/types/tool-protection.ts +161 -0
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import type { ToolProtection, ToolProtectionMap } from "@kya-os/contracts/tool-protection";
|
|
11
11
|
import type { DelegationDenial } from "../runtime/delegation-denial.js";
|
|
12
|
+
import { type AmountLimitCode, type AmountLimitViolation } from "@kya-os/contracts/delegation";
|
|
12
13
|
export type { ToolProtection };
|
|
13
14
|
/**
|
|
14
15
|
* Complete tool protection configuration for a project
|
|
@@ -19,7 +20,25 @@ export interface ToolProtectionConfig {
|
|
|
19
20
|
* Map of tool names to their protection settings
|
|
20
21
|
*/
|
|
21
22
|
toolProtections: ToolProtectionMap;
|
|
23
|
+
/**
|
|
24
|
+
* The revision of the tool protection Checkpoint served for this config:
|
|
25
|
+
* SHA-256 (lowercase hex) of the RFC 8785 canonical JSON of the served
|
|
26
|
+
* `config.toolProtection.tools` object, exactly as received. Checkpoint
|
|
27
|
+
* recomputes it from what it serves. Absent for local, fallback and
|
|
28
|
+
* deny-all configs, which Checkpoint did not serve.
|
|
29
|
+
*/
|
|
30
|
+
servedRevision?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The wildcard protection the service stands in with when no config can be
|
|
34
|
+
* read and none is cached. Every tool resolves to it, and the runtime refuses
|
|
35
|
+
* it until a config arrives. It is never cached.
|
|
36
|
+
*/
|
|
37
|
+
export interface UnavailableToolProtection extends ToolProtection {
|
|
38
|
+
readonly unavailable: true;
|
|
22
39
|
}
|
|
40
|
+
export declare function unavailableToolProtection(): UnavailableToolProtection;
|
|
41
|
+
export declare function isUnavailableToolProtection(protection: ToolProtection): protection is UnavailableToolProtection;
|
|
23
42
|
/**
|
|
24
43
|
* Configuration for the ToolProtectionService
|
|
25
44
|
*/
|
|
@@ -131,6 +150,81 @@ export declare class DelegationRequiredError extends Error {
|
|
|
131
150
|
denial?: DelegationDenial;
|
|
132
151
|
constructor(toolName: string, requiredScopes: string[], consentUrl?: string | undefined, interceptedCall?: InterceptedToolCall, resumeToken?: string, denial?: DelegationDenial);
|
|
133
152
|
}
|
|
153
|
+
export declare const TOOL_PROTECTION_UNENFORCEABLE: "tool_protection_unenforceable";
|
|
154
|
+
/**
|
|
155
|
+
* A protected tool was called where its protection can't be enforced: the
|
|
156
|
+
* deployment lacks what the protection service needs, the protection names no
|
|
157
|
+
* scope to check a delegation against, or no protection config can be read.
|
|
158
|
+
* Refused before the handler runs.
|
|
159
|
+
*/
|
|
160
|
+
export declare class ToolProtectionUnenforceableError extends Error {
|
|
161
|
+
readonly toolName: string;
|
|
162
|
+
/**
|
|
163
|
+
* What enforcement lacks: the deployment's CHECKPOINT_API_KEY and/or
|
|
164
|
+
* TOOL_PROTECTION_KV, the protection's requiredScopes, or the
|
|
165
|
+
* toolProtectionConfig itself.
|
|
166
|
+
*/
|
|
167
|
+
readonly missing: readonly string[];
|
|
168
|
+
readonly code: "tool_protection_unenforceable";
|
|
169
|
+
constructor(toolName: string,
|
|
170
|
+
/**
|
|
171
|
+
* What enforcement lacks: the deployment's CHECKPOINT_API_KEY and/or
|
|
172
|
+
* TOOL_PROTECTION_KV, the protection's requiredScopes, or the
|
|
173
|
+
* toolProtectionConfig itself.
|
|
174
|
+
*/
|
|
175
|
+
missing: readonly string[], message?: string);
|
|
176
|
+
toJSON(): {
|
|
177
|
+
error: "tool_protection_unenforceable";
|
|
178
|
+
message: string;
|
|
179
|
+
toolName: string;
|
|
180
|
+
missing: string[];
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
export declare function isToolProtectionUnenforceableError(error: unknown): error is ToolProtectionUnenforceableError;
|
|
184
|
+
/**
|
|
185
|
+
* Whether a protection's required scopes name at least one scope. With none
|
|
186
|
+
* there is nothing to check a delegation against, so the protection can't be
|
|
187
|
+
* enforced: an empty list never counts as satisfied.
|
|
188
|
+
*/
|
|
189
|
+
export declare function namesRequiredScope(requiredScopes: readonly unknown[] | undefined): boolean;
|
|
190
|
+
/** The refusal for a protected tool whose protection names no scope. */
|
|
191
|
+
export declare function unscopedToolProtectionError(toolName: string): ToolProtectionUnenforceableError;
|
|
192
|
+
/** The refusal while no tool protection config can be read. */
|
|
193
|
+
export declare function unavailableToolProtectionError(toolName: string): ToolProtectionUnenforceableError;
|
|
194
|
+
export declare class AmountLimitError extends Error {
|
|
195
|
+
readonly toolName: string;
|
|
196
|
+
readonly violation: AmountLimitViolation;
|
|
197
|
+
readonly code: AmountLimitCode;
|
|
198
|
+
constructor(toolName: string, violation: AmountLimitViolation);
|
|
199
|
+
toJSON(): {
|
|
200
|
+
scope: string;
|
|
201
|
+
error: "amount_limit_exceeded" | "amount_missing" | "amount_malformed" | "amount_currency_mismatch" | "amount_limit_malformed" | "amount_config_invalid";
|
|
202
|
+
message: string;
|
|
203
|
+
toolName: string;
|
|
204
|
+
} | {
|
|
205
|
+
scope: string;
|
|
206
|
+
limit: import("@kya-os/contracts/delegation").AmountLimit;
|
|
207
|
+
error: "amount_limit_exceeded" | "amount_missing" | "amount_malformed" | "amount_currency_mismatch" | "amount_limit_malformed" | "amount_config_invalid";
|
|
208
|
+
message: string;
|
|
209
|
+
toolName: string;
|
|
210
|
+
} | {
|
|
211
|
+
scope: string;
|
|
212
|
+
limit: import("@kya-os/contracts/delegation").AmountLimit;
|
|
213
|
+
amount: string;
|
|
214
|
+
error: "amount_limit_exceeded" | "amount_missing" | "amount_malformed" | "amount_currency_mismatch" | "amount_limit_malformed" | "amount_config_invalid";
|
|
215
|
+
message: string;
|
|
216
|
+
toolName: string;
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
export declare function amountRefusalCode(code: AmountLimitCode): string;
|
|
220
|
+
export declare function amountLimitToolResult(error: AmountLimitError): {
|
|
221
|
+
content: {
|
|
222
|
+
type: "text";
|
|
223
|
+
text: string;
|
|
224
|
+
}[];
|
|
225
|
+
isError: true;
|
|
226
|
+
_meta: import("@kya-os/contracts/config").ToolRefusalMeta;
|
|
227
|
+
};
|
|
134
228
|
/**
|
|
135
229
|
* Context for an intercepted tool call that needs authorization
|
|
136
230
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-protection.d.ts","sourceRoot":"","sources":["../../src/types/tool-protection.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EACV,cAAc,EACd,iBAAiB,EAClB,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"tool-protection.d.ts","sourceRoot":"","sources":["../../src/types/tool-protection.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EACV,cAAc,EACd,iBAAiB,EAClB,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;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QAAQ,EAAE,MAAM,GACf,gCAAgC,CAQlC;AAED,qBAAa,gBAAiB,SAAQ,KAAK;aAIvB,QAAQ,EAAE,MAAM;aAChB,SAAS,EAAE,oBAAoB;IAJjD,SAAgB,IAAI,EAAE,eAAe,CAAC;gBAGpB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,oBAAoB;IAOjD,MAAM;;;;;;;;;;;;;;;;;;;CASP;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,eAAe,GAAG,MAAM,CAE/D;AAED,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,gBAAgB;;;;;;;EAM5D;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,GAAG,CAAC;IACV,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,qBAAa,0BAA2B,SAAQ,KAAK;IAG1C,KAAK,CAAC,EAAE,KAAK;gBADpB,OAAO,EAAE,MAAM,EACR,KAAK,CAAC,EAAE,KAAK,YAAA;CAKvB"}
|
|
@@ -9,7 +9,23 @@
|
|
|
9
9
|
* @version 1.0.0
|
|
10
10
|
*/
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.ToolProtectionServiceError = exports.DelegationRequiredError = void 0;
|
|
12
|
+
exports.ToolProtectionServiceError = exports.AmountLimitError = exports.ToolProtectionUnenforceableError = exports.TOOL_PROTECTION_UNENFORCEABLE = exports.DelegationRequiredError = void 0;
|
|
13
|
+
exports.unavailableToolProtection = unavailableToolProtection;
|
|
14
|
+
exports.isUnavailableToolProtection = isUnavailableToolProtection;
|
|
15
|
+
exports.isToolProtectionUnenforceableError = isToolProtectionUnenforceableError;
|
|
16
|
+
exports.namesRequiredScope = namesRequiredScope;
|
|
17
|
+
exports.unscopedToolProtectionError = unscopedToolProtectionError;
|
|
18
|
+
exports.unavailableToolProtectionError = unavailableToolProtectionError;
|
|
19
|
+
exports.amountRefusalCode = amountRefusalCode;
|
|
20
|
+
exports.amountLimitToolResult = amountLimitToolResult;
|
|
21
|
+
const config_1 = require("@kya-os/contracts/config");
|
|
22
|
+
const delegation_1 = require("@kya-os/contracts/delegation");
|
|
23
|
+
function unavailableToolProtection() {
|
|
24
|
+
return { requiresDelegation: true, requiredScopes: [], unavailable: true };
|
|
25
|
+
}
|
|
26
|
+
function isUnavailableToolProtection(protection) {
|
|
27
|
+
return protection.unavailable === true;
|
|
28
|
+
}
|
|
13
29
|
/**
|
|
14
30
|
* Error thrown when a tool requires delegation but none is provided
|
|
15
31
|
*
|
|
@@ -40,6 +56,102 @@ class DelegationRequiredError extends Error {
|
|
|
40
56
|
}
|
|
41
57
|
}
|
|
42
58
|
exports.DelegationRequiredError = DelegationRequiredError;
|
|
59
|
+
exports.TOOL_PROTECTION_UNENFORCEABLE = "tool_protection_unenforceable";
|
|
60
|
+
/**
|
|
61
|
+
* A protected tool was called where its protection can't be enforced: the
|
|
62
|
+
* deployment lacks what the protection service needs, the protection names no
|
|
63
|
+
* scope to check a delegation against, or no protection config can be read.
|
|
64
|
+
* Refused before the handler runs.
|
|
65
|
+
*/
|
|
66
|
+
class ToolProtectionUnenforceableError extends Error {
|
|
67
|
+
toolName;
|
|
68
|
+
missing;
|
|
69
|
+
code = exports.TOOL_PROTECTION_UNENFORCEABLE;
|
|
70
|
+
constructor(toolName,
|
|
71
|
+
/**
|
|
72
|
+
* What enforcement lacks: the deployment's CHECKPOINT_API_KEY and/or
|
|
73
|
+
* TOOL_PROTECTION_KV, the protection's requiredScopes, or the
|
|
74
|
+
* toolProtectionConfig itself.
|
|
75
|
+
*/
|
|
76
|
+
missing, message) {
|
|
77
|
+
super(message ??
|
|
78
|
+
`Tool "${toolName}" is protected, but this deployment can't enforce its ` +
|
|
79
|
+
`protection without ${missing.join(" and ")}. Configure ` +
|
|
80
|
+
`${missing.length === 1 ? "it" : "them"} to call this tool.`);
|
|
81
|
+
this.toolName = toolName;
|
|
82
|
+
this.missing = missing;
|
|
83
|
+
this.name = "ToolProtectionUnenforceableError";
|
|
84
|
+
}
|
|
85
|
+
toJSON() {
|
|
86
|
+
return {
|
|
87
|
+
error: this.code,
|
|
88
|
+
message: this.message,
|
|
89
|
+
toolName: this.toolName,
|
|
90
|
+
missing: [...this.missing],
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.ToolProtectionUnenforceableError = ToolProtectionUnenforceableError;
|
|
95
|
+
// By shape, like the other refusal checks: the error may come from another
|
|
96
|
+
// copy of this package.
|
|
97
|
+
function isToolProtectionUnenforceableError(error) {
|
|
98
|
+
return (error instanceof Error &&
|
|
99
|
+
error.name === "ToolProtectionUnenforceableError" &&
|
|
100
|
+
error.code === exports.TOOL_PROTECTION_UNENFORCEABLE);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Whether a protection's required scopes name at least one scope. With none
|
|
104
|
+
* there is nothing to check a delegation against, so the protection can't be
|
|
105
|
+
* enforced: an empty list never counts as satisfied.
|
|
106
|
+
*/
|
|
107
|
+
function namesRequiredScope(requiredScopes) {
|
|
108
|
+
return (Array.isArray(requiredScopes) &&
|
|
109
|
+
requiredScopes.some((scope) => typeof scope === "string" && scope !== ""));
|
|
110
|
+
}
|
|
111
|
+
/** The refusal for a protected tool whose protection names no scope. */
|
|
112
|
+
function unscopedToolProtectionError(toolName) {
|
|
113
|
+
return new ToolProtectionUnenforceableError(toolName, ["requiredScopes"], `Tool "${toolName}" is protected, but its protection names no required ` +
|
|
114
|
+
`scope, so no delegation can be checked against it. Give it a scope to ` +
|
|
115
|
+
`call this tool.`);
|
|
116
|
+
}
|
|
117
|
+
/** The refusal while no tool protection config can be read. */
|
|
118
|
+
function unavailableToolProtectionError(toolName) {
|
|
119
|
+
return new ToolProtectionUnenforceableError(toolName, ["toolProtectionConfig"], `Tool "${toolName}" can't be called while its protection is unavailable: ` +
|
|
120
|
+
`the tool protection config can't be read and none is cached. It is ` +
|
|
121
|
+
`refused until the config can be read.`);
|
|
122
|
+
}
|
|
123
|
+
class AmountLimitError extends Error {
|
|
124
|
+
toolName;
|
|
125
|
+
violation;
|
|
126
|
+
code;
|
|
127
|
+
constructor(toolName, violation) {
|
|
128
|
+
super((0, delegation_1.describeAmountLimitViolation)(toolName, violation));
|
|
129
|
+
this.toolName = toolName;
|
|
130
|
+
this.violation = violation;
|
|
131
|
+
this.name = "AmountLimitError";
|
|
132
|
+
this.code = violation.code;
|
|
133
|
+
}
|
|
134
|
+
toJSON() {
|
|
135
|
+
const { code, ...detail } = this.violation;
|
|
136
|
+
return {
|
|
137
|
+
error: code,
|
|
138
|
+
message: this.message,
|
|
139
|
+
toolName: this.toolName,
|
|
140
|
+
...detail,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
exports.AmountLimitError = AmountLimitError;
|
|
145
|
+
function amountRefusalCode(code) {
|
|
146
|
+
return code.toUpperCase();
|
|
147
|
+
}
|
|
148
|
+
function amountLimitToolResult(error) {
|
|
149
|
+
return {
|
|
150
|
+
content: [{ type: "text", text: JSON.stringify(error) }],
|
|
151
|
+
isError: true,
|
|
152
|
+
_meta: (0, config_1.markToolRefusal)(amountRefusalCode(error.code)),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
43
155
|
/**
|
|
44
156
|
* Error thrown when the tool protection service fails
|
|
45
157
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-protection.js","sourceRoot":"","sources":["../../src/types/tool-protection.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;
|
|
1
|
+
{"version":3,"file":"tool-protection.js","sourceRoot":"","sources":["../../src/types/tool-protection.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;AA8CH,8DAEC;AAED,kEAIC;AAmLD,gFAQC;AAOD,gDAOC;AAGD,kEAUC;AAGD,wEAUC;AAyBD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package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kya-os/mcp-i-runtime",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "Shared product-runtime layer for the MCP-I framework (Node + Cloudflare adapters). Protocol types/values live in @kya-os/mcp (DIF).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Dylan Hobbs",
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "tsc",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
54
55
|
"test": "vitest run",
|
|
55
56
|
"test:coverage": "vitest run --coverage",
|
|
56
57
|
"test:watch": "vitest",
|
|
@@ -59,8 +60,8 @@
|
|
|
59
60
|
"prepublishOnly": "npm run build && node ../create-mcpi-app/scripts/validate-no-workspace.js"
|
|
60
61
|
},
|
|
61
62
|
"dependencies": {
|
|
62
|
-
"@kya-os/contracts": "^1.13.
|
|
63
|
-
"@kya-os/mcp": "1.
|
|
63
|
+
"@kya-os/contracts": "^1.13.1",
|
|
64
|
+
"@kya-os/mcp": "1.20.0",
|
|
64
65
|
"jose": "^5.6.3",
|
|
65
66
|
"json-canonicalize": "2.0.0",
|
|
66
67
|
"zod": "^3.25.67"
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
2
|
+
import type { MCPIRuntimeBase } from "../../runtime/base";
|
|
3
|
+
import { createToolProtectionRuntime } from "../utils/tool-protection-runtime";
|
|
4
|
+
import {
|
|
5
|
+
AmountLimitError,
|
|
6
|
+
DelegationRequiredError,
|
|
7
|
+
isToolProtectionUnenforceableError,
|
|
8
|
+
type ToolProtectionUnenforceableError,
|
|
9
|
+
} from "../../types/tool-protection";
|
|
10
|
+
import { terminalForToolError } from "../../runtime/audit";
|
|
11
|
+
|
|
12
|
+
const SCOPE = "payments:send";
|
|
13
|
+
const LIMIT = { value: "50.00", currency: "USD" };
|
|
14
|
+
const AMOUNT_CONFIG = { argument: "amount", currency: "USD" };
|
|
15
|
+
|
|
16
|
+
const session = {
|
|
17
|
+
id: "session123",
|
|
18
|
+
audience: "https://client.example.com",
|
|
19
|
+
nonce: "test-nonce",
|
|
20
|
+
delegationToken: "delegation-token",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const verifyResponse = (constraints?: Record<string, unknown>) => ({
|
|
24
|
+
success: true,
|
|
25
|
+
data: {
|
|
26
|
+
valid: true,
|
|
27
|
+
credential: {
|
|
28
|
+
agent_did: "did:key:zmock123",
|
|
29
|
+
scopes: [SCOPE],
|
|
30
|
+
...(constraints === undefined ? {} : { constraints }),
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const grantWithLimit = (maxAmount: unknown = LIMIT) =>
|
|
36
|
+
verifyResponse({
|
|
37
|
+
crisp: {
|
|
38
|
+
scopes: [{ resource: SCOPE, matcher: "exact", maxAmount }],
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("amount limit enforcement", () => {
|
|
43
|
+
let runtime: MCPIRuntimeBase;
|
|
44
|
+
let protectionService: { checkToolProtection: ReturnType<typeof vi.fn> };
|
|
45
|
+
let accessControl: { verifyDelegation: ReturnType<typeof vi.fn> };
|
|
46
|
+
const handler = vi.fn().mockResolvedValue({ result: "success" });
|
|
47
|
+
|
|
48
|
+
const protect = (amount?: unknown) =>
|
|
49
|
+
protectionService.checkToolProtection.mockResolvedValue({
|
|
50
|
+
requiresDelegation: true,
|
|
51
|
+
requiredScopes: [SCOPE],
|
|
52
|
+
...(amount === undefined ? {} : { amount }),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const call = (args: unknown) =>
|
|
56
|
+
runtime.processToolCall("send_payment", args, handler, session);
|
|
57
|
+
|
|
58
|
+
const failure = async (args: unknown) =>
|
|
59
|
+
(await call(args).catch((error: unknown) => error)) as AmountLimitError;
|
|
60
|
+
|
|
61
|
+
beforeEach(async () => {
|
|
62
|
+
vi.clearAllMocks();
|
|
63
|
+
({
|
|
64
|
+
runtime,
|
|
65
|
+
mockToolProtectionService: protectionService,
|
|
66
|
+
mockAccessControlService: accessControl,
|
|
67
|
+
} = await createToolProtectionRuntime());
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("leaves a tool with no amount config untouched even when the grant carries a limit", async () => {
|
|
71
|
+
protect();
|
|
72
|
+
accessControl.verifyDelegation.mockResolvedValue(grantWithLimit());
|
|
73
|
+
await expect(call({ amount: "1000000.00" })).resolves.toEqual({
|
|
74
|
+
result: "success",
|
|
75
|
+
});
|
|
76
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
77
|
+
expect(handler.mock.calls[0][1]).toMatchObject({
|
|
78
|
+
constraints: { crisp: { scopes: [{ maxAmount: LIMIT }] } },
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it.each(["50.00", "50", "49.99", 50, "0"])(
|
|
83
|
+
"passes an amount of %j against a 50.00 USD limit",
|
|
84
|
+
async (amount) => {
|
|
85
|
+
protect(AMOUNT_CONFIG);
|
|
86
|
+
accessControl.verifyDelegation.mockResolvedValue(grantWithLimit());
|
|
87
|
+
await expect(call({ amount })).resolves.toEqual({ result: "success" });
|
|
88
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
it.each(["50.01", 50.01])(
|
|
93
|
+
"refuses an amount of %j against a 50.00 USD limit and names the limit",
|
|
94
|
+
async (amount) => {
|
|
95
|
+
protect(AMOUNT_CONFIG);
|
|
96
|
+
accessControl.verifyDelegation.mockResolvedValue(grantWithLimit());
|
|
97
|
+
const error = await failure({ amount });
|
|
98
|
+
expect(error).toBeInstanceOf(AmountLimitError);
|
|
99
|
+
expect(error).not.toBeInstanceOf(DelegationRequiredError);
|
|
100
|
+
expect(error.code).toBe("amount_limit_exceeded");
|
|
101
|
+
expect(error.toolName).toBe("send_payment");
|
|
102
|
+
expect(error.violation).toEqual({
|
|
103
|
+
code: "amount_limit_exceeded",
|
|
104
|
+
scope: SCOPE,
|
|
105
|
+
limit: LIMIT,
|
|
106
|
+
amount: "50.01",
|
|
107
|
+
});
|
|
108
|
+
expect(error.message).toBe(
|
|
109
|
+
'Tool "send_payment" refused under scope "payments:send": amount 50.01 USD exceeds the delegated limit of 50.00 USD. Nothing was executed.'
|
|
110
|
+
);
|
|
111
|
+
expect(error.toJSON()).toEqual({
|
|
112
|
+
error: "amount_limit_exceeded",
|
|
113
|
+
message: error.message,
|
|
114
|
+
toolName: "send_payment",
|
|
115
|
+
scope: SCOPE,
|
|
116
|
+
limit: LIMIT,
|
|
117
|
+
amount: "50.01",
|
|
118
|
+
});
|
|
119
|
+
expect(handler).not.toHaveBeenCalled();
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
it("reads the limit from the delegation record when the verifier returns no credential", async () => {
|
|
124
|
+
protect(AMOUNT_CONFIG);
|
|
125
|
+
accessControl.verifyDelegation.mockResolvedValue({
|
|
126
|
+
success: true,
|
|
127
|
+
data: {
|
|
128
|
+
valid: true,
|
|
129
|
+
delegation: {
|
|
130
|
+
constraints: {
|
|
131
|
+
crisp: {
|
|
132
|
+
scopes: [{ resource: SCOPE, matcher: "exact", maxAmount: LIMIT }],
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
expect((await failure({ amount: "50.01" })).code).toBe(
|
|
139
|
+
"amount_limit_exceeded"
|
|
140
|
+
);
|
|
141
|
+
expect(handler).not.toHaveBeenCalled();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("refuses a configured tool that requires no scope before any amount check", async () => {
|
|
145
|
+
protectionService.checkToolProtection.mockResolvedValue({
|
|
146
|
+
requiresDelegation: true,
|
|
147
|
+
requiredScopes: [],
|
|
148
|
+
amount: AMOUNT_CONFIG,
|
|
149
|
+
});
|
|
150
|
+
accessControl.verifyDelegation.mockResolvedValue(grantWithLimit());
|
|
151
|
+
const error = await call({ amount: "1.00" }).catch((e: unknown) => e);
|
|
152
|
+
expect(isToolProtectionUnenforceableError(error)).toBe(true);
|
|
153
|
+
expect((error as ToolProtectionUnenforceableError).missing).toEqual([
|
|
154
|
+
"requiredScopes",
|
|
155
|
+
]);
|
|
156
|
+
expect(accessControl.verifyDelegation).not.toHaveBeenCalled();
|
|
157
|
+
expect(handler).not.toHaveBeenCalled();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("refuses a currency mismatch from the tool config", async () => {
|
|
161
|
+
protect({ argument: "amount", currency: "EUR" });
|
|
162
|
+
accessControl.verifyDelegation.mockResolvedValue(grantWithLimit());
|
|
163
|
+
const error = await failure({ amount: "1.00" });
|
|
164
|
+
expect(error.code).toBe("amount_currency_mismatch");
|
|
165
|
+
expect(handler).not.toHaveBeenCalled();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("refuses a currency mismatch read from the call arguments", async () => {
|
|
169
|
+
protect({ argument: "amount", currency: { argument: "ccy" } });
|
|
170
|
+
accessControl.verifyDelegation.mockResolvedValue(grantWithLimit());
|
|
171
|
+
await expect(call({ amount: "1.00", ccy: "USD" })).resolves.toEqual({
|
|
172
|
+
result: "success",
|
|
173
|
+
});
|
|
174
|
+
const error = await failure({ amount: "1.00", ccy: "EUR" });
|
|
175
|
+
expect(error.code).toBe("amount_currency_mismatch");
|
|
176
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it.each([
|
|
180
|
+
["abc", "amount_malformed"],
|
|
181
|
+
["-5", "amount_malformed"],
|
|
182
|
+
["1e3", "amount_malformed"],
|
|
183
|
+
[null, "amount_malformed"],
|
|
184
|
+
[undefined, "amount_missing"],
|
|
185
|
+
])("refuses an amount of %j when configured", async (amount, code) => {
|
|
186
|
+
protect(AMOUNT_CONFIG);
|
|
187
|
+
accessControl.verifyDelegation.mockResolvedValue(grantWithLimit());
|
|
188
|
+
const error = await failure({ amount });
|
|
189
|
+
expect(error.code).toBe(code);
|
|
190
|
+
expect(handler).not.toHaveBeenCalled();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("refuses a call with no arguments at all when configured", async () => {
|
|
194
|
+
protect(AMOUNT_CONFIG);
|
|
195
|
+
accessControl.verifyDelegation.mockResolvedValue(grantWithLimit());
|
|
196
|
+
const error = await failure(undefined);
|
|
197
|
+
expect(error.code).toBe("amount_missing");
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it.each([
|
|
201
|
+
["no constraints on the credential", verifyResponse()],
|
|
202
|
+
["constraints without crisp", verifyResponse({ not_after: 1 })],
|
|
203
|
+
[
|
|
204
|
+
"a crisp scope without maxAmount",
|
|
205
|
+
verifyResponse({
|
|
206
|
+
crisp: { scopes: [{ resource: SCOPE, matcher: "exact" }] },
|
|
207
|
+
}),
|
|
208
|
+
],
|
|
209
|
+
[
|
|
210
|
+
"a maxAmount on a different scope",
|
|
211
|
+
verifyResponse({
|
|
212
|
+
crisp: {
|
|
213
|
+
scopes: [
|
|
214
|
+
{ resource: "other:scope", matcher: "exact", maxAmount: LIMIT },
|
|
215
|
+
],
|
|
216
|
+
},
|
|
217
|
+
}),
|
|
218
|
+
],
|
|
219
|
+
["no credential at all", { success: true, data: { valid: true } }],
|
|
220
|
+
])(
|
|
221
|
+
"passes a configured tool when the grant has %s",
|
|
222
|
+
async (_name, response) => {
|
|
223
|
+
protect(AMOUNT_CONFIG);
|
|
224
|
+
accessControl.verifyDelegation.mockResolvedValue(response);
|
|
225
|
+
await expect(call({ amount: "999999.00" })).resolves.toEqual({
|
|
226
|
+
result: "success",
|
|
227
|
+
});
|
|
228
|
+
await expect(call({})).resolves.toEqual({ result: "success" });
|
|
229
|
+
}
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
it("refuses when the delegated limit is malformed", async () => {
|
|
233
|
+
protect(AMOUNT_CONFIG);
|
|
234
|
+
accessControl.verifyDelegation.mockResolvedValue(
|
|
235
|
+
grantWithLimit({ value: "-5", currency: "USD" })
|
|
236
|
+
);
|
|
237
|
+
const error = await failure({ amount: "1.00" });
|
|
238
|
+
expect(error.code).toBe("amount_limit_malformed");
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("refuses when the tool amount config is invalid and a limit applies", async () => {
|
|
242
|
+
protect({ argument: "", currency: "USD" });
|
|
243
|
+
accessControl.verifyDelegation.mockResolvedValue(grantWithLimit());
|
|
244
|
+
const error = await failure({ amount: "1.00" });
|
|
245
|
+
expect(error.code).toBe("amount_config_invalid");
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("does not consult the amount when the delegation itself is refused", async () => {
|
|
249
|
+
protect(AMOUNT_CONFIG);
|
|
250
|
+
accessControl.verifyDelegation.mockResolvedValue({
|
|
251
|
+
success: true,
|
|
252
|
+
data: { valid: false, error: { code: "delegation_revoked" } },
|
|
253
|
+
});
|
|
254
|
+
await expect(call({ amount: "1000.00" })).rejects.toBeInstanceOf(
|
|
255
|
+
DelegationRequiredError
|
|
256
|
+
);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("records the refusal as a denied terminal event", async () => {
|
|
260
|
+
protect(AMOUNT_CONFIG);
|
|
261
|
+
accessControl.verifyDelegation.mockResolvedValue(grantWithLimit());
|
|
262
|
+
const error = await failure({ amount: "50.01" });
|
|
263
|
+
expect(terminalForToolError(error)).toEqual({
|
|
264
|
+
phase: "denied",
|
|
265
|
+
outcome: "denied",
|
|
266
|
+
reasonCode: "AMOUNT_LIMIT_EXCEEDED",
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { AMOUNT_LIMIT_CODES } from "@kya-os/contracts/delegation";
|
|
3
|
+
import { readToolRefusal } from "@kya-os/contracts/config";
|
|
4
|
+
import {
|
|
5
|
+
AmountLimitError,
|
|
6
|
+
amountLimitToolResult,
|
|
7
|
+
amountRefusalCode,
|
|
8
|
+
} from "../../types/tool-protection";
|
|
9
|
+
|
|
10
|
+
const exceeded = () =>
|
|
11
|
+
new AmountLimitError("send_payment", {
|
|
12
|
+
code: "amount_limit_exceeded",
|
|
13
|
+
scope: "payments:send",
|
|
14
|
+
limit: { value: "50.00", currency: "USD" },
|
|
15
|
+
amount: "50.01",
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("amountLimitToolResult", () => {
|
|
19
|
+
it("is an isError result whose text is the error JSON, plus the refusal marker", () => {
|
|
20
|
+
expect(amountLimitToolResult(exceeded())).toEqual({
|
|
21
|
+
content: [
|
|
22
|
+
{
|
|
23
|
+
type: "text",
|
|
24
|
+
text: JSON.stringify({
|
|
25
|
+
error: "amount_limit_exceeded",
|
|
26
|
+
message:
|
|
27
|
+
'Tool "send_payment" refused under scope "payments:send": amount 50.01 USD exceeds the delegated limit of 50.00 USD. Nothing was executed.',
|
|
28
|
+
toolName: "send_payment",
|
|
29
|
+
scope: "payments:send",
|
|
30
|
+
limit: { value: "50.00", currency: "USD" },
|
|
31
|
+
amount: "50.01",
|
|
32
|
+
}),
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
isError: true,
|
|
36
|
+
_meta: { "ai.kya-os/refusal": { code: "AMOUNT_LIMIT_EXCEEDED" } },
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("adds only a marker to the text and isError fields", () => {
|
|
41
|
+
expect(Object.keys(amountLimitToolResult(exceeded())).sort()).toEqual([
|
|
42
|
+
"_meta",
|
|
43
|
+
"content",
|
|
44
|
+
"isError",
|
|
45
|
+
]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it.each(AMOUNT_LIMIT_CODES)("marks %s with its upper-case code", (code) => {
|
|
49
|
+
const error = new AmountLimitError("send_payment", {
|
|
50
|
+
code,
|
|
51
|
+
scope: "payments:send",
|
|
52
|
+
limit: { value: "50.00", currency: "USD" },
|
|
53
|
+
amount: "1.00",
|
|
54
|
+
} as never);
|
|
55
|
+
|
|
56
|
+
expect(amountRefusalCode(code)).toBe(code.toUpperCase());
|
|
57
|
+
expect(readToolRefusal(amountLimitToolResult(error))).toEqual({
|
|
58
|
+
code: code.toUpperCase(),
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -15,25 +15,40 @@ vi.mock("@kya-os/contracts/config", async (importOriginal) => {
|
|
|
15
15
|
});
|
|
16
16
|
|
|
17
17
|
const { terminalForToolError } = await import("../../runtime/audit");
|
|
18
|
-
const { DelegationRequiredError } = await import(
|
|
19
|
-
"../../types/tool-protection"
|
|
20
|
-
);
|
|
18
|
+
const { DelegationRequiredError } = await import("../../types/tool-protection");
|
|
21
19
|
const { toDelegationDenial } = await import("../../runtime/delegation-denial");
|
|
22
20
|
|
|
23
21
|
describe("audit denial mapping against an older contracts release", () => {
|
|
24
|
-
it("still records a verifier denial as denied", () => {
|
|
22
|
+
it("still records a stateful verifier denial as denied", () => {
|
|
25
23
|
const error = new DelegationRequiredError(
|
|
26
24
|
"orders.place",
|
|
27
25
|
["orders:place"],
|
|
28
26
|
"https://example.com/consent",
|
|
29
27
|
undefined,
|
|
30
28
|
"resume_token",
|
|
31
|
-
toDelegationDenial("
|
|
29
|
+
toDelegationDenial("max_calls_exceeded")
|
|
32
30
|
);
|
|
33
31
|
|
|
34
32
|
expect(terminalForToolError(error)).toEqual({
|
|
35
33
|
phase: "denied",
|
|
36
34
|
outcome: "denied",
|
|
35
|
+
reasonCode: "BUDGET_EXCEEDED",
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("still records a refused grant a new grant could fix as challenged", () => {
|
|
40
|
+
const error = new DelegationRequiredError(
|
|
41
|
+
"orders.place",
|
|
42
|
+
["orders:place"],
|
|
43
|
+
"https://example.com/consent",
|
|
44
|
+
undefined,
|
|
45
|
+
"resume_token",
|
|
46
|
+
toDelegationDenial("delegation_revoked")
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
expect(terminalForToolError(error)).toEqual({
|
|
50
|
+
phase: "challenged",
|
|
51
|
+
outcome: "challenged",
|
|
37
52
|
reasonCode: "DELEGATION_REVOKED",
|
|
38
53
|
});
|
|
39
54
|
});
|