@metamynd/agentsafe-mcp-guard 0.3.6 → 0.4.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.
- package/package.json +1 -1
- package/policy-core.mjs +66 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamynd/agentsafe-mcp-guard",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Zero-dependency trustless governance for the SERVICE side. An MCP server or API re-verifies a calling agent's signed request against the agent's own published policy \u00e2\u20ac\u201d so an agent that ignores its own guard still cannot make your service act.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./agentsafe-mcp-guard.mjs",
|
package/policy-core.mjs
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
// src/policy-core/atom-registry.ts
|
|
4
4
|
var RISK_RANK = { low: 0, medium: 1, high: 2, critical: 3 };
|
|
5
|
+
function currencyOutOfScope(ctx, cfgCurrency) {
|
|
6
|
+
if (cfgCurrency === void 0 || cfgCurrency === null) return false;
|
|
7
|
+
const allowed = Array.isArray(cfgCurrency) ? cfgCurrency : [cfgCurrency];
|
|
8
|
+
if (allowed.length === 0) return false;
|
|
9
|
+
const currency = ctx.currency;
|
|
10
|
+
const matches = typeof currency === "string" && allowed.some((u) => typeof u === "string" && u.toUpperCase() === currency.toUpperCase());
|
|
11
|
+
return !matches;
|
|
12
|
+
}
|
|
5
13
|
var ATOM_REGISTRY = {
|
|
6
14
|
"data-source-not-approved": (c, cfg) => !!c.dataSourceId && !(cfg?.approved ?? []).includes(String(c.dataSourceId)),
|
|
7
15
|
"consent-missing": (c) => c.consent === false,
|
|
@@ -10,7 +18,11 @@ var ATOM_REGISTRY = {
|
|
|
10
18
|
const need = RISK_RANK[String(cfg?.level ?? "high")];
|
|
11
19
|
return have !== void 0 && need !== void 0 && have >= need;
|
|
12
20
|
},
|
|
13
|
-
"amount-over": (c, cfg) =>
|
|
21
|
+
"amount-over": (c, cfg) => {
|
|
22
|
+
if (typeof c.amount !== "number") return false;
|
|
23
|
+
if (currencyOutOfScope(c, cfg?.currency)) return true;
|
|
24
|
+
return c.amount > Number(cfg?.limit ?? 0);
|
|
25
|
+
},
|
|
14
26
|
// Deny-by-default primitive for value-moving actions. Fires on ABSENCE (like the
|
|
15
27
|
// evidence atoms below, and unlike `amount-over`) OR on a NEGATIVE amount: true when
|
|
16
28
|
// the context carries no usable amount, or one that cannot be trusted for capping —
|
|
@@ -31,7 +43,12 @@ var ATOM_REGISTRY = {
|
|
|
31
43
|
"amount-unknown": (c) => !(typeof c.amount === "number" && Number.isFinite(c.amount) && c.amount >= 0),
|
|
32
44
|
// Total budget: cumulativeSpend is a SERVER-derived, signed-last context field (never
|
|
33
45
|
// shadowable by the agent's itinerary), so this compares already-spent + this amount.
|
|
34
|
-
|
|
46
|
+
// See `currencyOutOfScope` above: a configured currency scope that this request's
|
|
47
|
+
// currency doesn't match fires the cap outright, same fail-closed reasoning as `amount-over`.
|
|
48
|
+
"cumulative-over": (c, cfg) => {
|
|
49
|
+
if (currencyOutOfScope(c, cfg?.currency)) return true;
|
|
50
|
+
return Number(c.cumulativeSpend ?? 0) + Number(c.amount ?? 0) > Number(cfg?.limit ?? 0);
|
|
51
|
+
},
|
|
35
52
|
// Fires if any configured term appears in the prompt and/or output text.
|
|
36
53
|
// Used to govern agent responses on content (prohibited claims, sensitive advice).
|
|
37
54
|
"text-matches": (c, cfg) => {
|
|
@@ -87,7 +104,25 @@ var ATOM_SPECS = [
|
|
|
87
104
|
predicate: "amount-over",
|
|
88
105
|
label: "Per-transaction amount over limit",
|
|
89
106
|
description: "Fires when a single action amount exceeds a configured limit (per-transaction cap).",
|
|
90
|
-
config: [
|
|
107
|
+
config: [
|
|
108
|
+
{ key: "limit", type: "number", required: true, description: "Maximum allowed amount for one transaction" },
|
|
109
|
+
{
|
|
110
|
+
key: "currency",
|
|
111
|
+
type: "string[]",
|
|
112
|
+
required: false,
|
|
113
|
+
description: `Optional currency scope for the limit (e.g. ['USD'], or ['USD','GBP'] for several). Leave empty to keep the limit currency-blind \u2014 the historical default: the raw number is compared regardless of currency. Once set, a request in a currency outside this list \u2014 or with none supplied at all \u2014 fires this atom regardless of amount (unverifiable is treated as unsafe, not as "smaller"), so the cap can't be cleared by naming a cheaper-looking currency (e.g. 200 JPY vs 200 USD).`
|
|
114
|
+
}
|
|
115
|
+
],
|
|
116
|
+
// `currency` is NOT listed here even though the executable atom conditionally reads it:
|
|
117
|
+
// unlike `limit`, the `currency` config is OPTIONAL per atom instance, so whether an agent
|
|
118
|
+
// needs to supply it depends on how a given molecule configures this atom — something
|
|
119
|
+
// `requiredContextFor`'s per-predicate (not per-instance) model can't express. Every
|
|
120
|
+
// authorize request already carries `currency` unconditionally regardless (see
|
|
121
|
+
// AuthorizeInput), so nothing is actually left unfed by omitting it here — this only
|
|
122
|
+
// controls the Scenario Bank simulate form / docs "context contract" surfacing, and
|
|
123
|
+
// forcing it onto every amount-over molecule would spuriously mark scenarios that never
|
|
124
|
+
// configure a currency scope as unexercised (see cumulative-over-atom.test.ts's sibling
|
|
125
|
+
// comment below for the same reasoning applied there).
|
|
91
126
|
requiredContext: ["amount"]
|
|
92
127
|
},
|
|
93
128
|
{
|
|
@@ -101,8 +136,32 @@ var ATOM_SPECS = [
|
|
|
101
136
|
predicate: "cumulative-over",
|
|
102
137
|
label: "Total budget over limit",
|
|
103
138
|
description: "Fires when cumulative spend (already-spent + this transaction) exceeds a configured total budget.",
|
|
104
|
-
config: [
|
|
105
|
-
|
|
139
|
+
config: [
|
|
140
|
+
{ key: "limit", type: "number", required: true, description: "Maximum total budget across all transactions" },
|
|
141
|
+
{
|
|
142
|
+
key: "currency",
|
|
143
|
+
type: "string[]",
|
|
144
|
+
required: false,
|
|
145
|
+
description: "Optional currency scope for the budget (e.g. ['USD'], or ['USD','GBP'] for several). Leave empty to keep it currency-blind \u2014 the historical default. Once set, a request in a currency outside this list \u2014 or with none supplied at all \u2014 fires this atom regardless of amount, same fail-closed design as amount-over's currency scope."
|
|
146
|
+
}
|
|
147
|
+
],
|
|
148
|
+
// The executable atom (atom-registry.ts) reads BOTH fields: `cumulativeSpend + amount >
|
|
149
|
+
// limit`. Omitting `cumulativeSpend` here silently broke two downstream consumers this
|
|
150
|
+
// catalog is the single source of truth for (see file header): the Scenario Bank's
|
|
151
|
+
// simulate form never rendered an "already spent" field for any set using this atom —
|
|
152
|
+
// including its own seeded preset, which supplied `cumulativeSpend` for a form field
|
|
153
|
+
// that didn't exist — so the control could never actually be exercised from the UI; and
|
|
154
|
+
// the integration docs' generated "context contract" told real SDK integrators this
|
|
155
|
+
// atom only needs `amount`, so an agent that never sends `cumulativeSpend` gets it
|
|
156
|
+
// silently treated as 0 and the total-budget cap never fires in production either.
|
|
157
|
+
//
|
|
158
|
+
// `currency`, by contrast, is deliberately NOT added here even though the executable atom
|
|
159
|
+
// conditionally reads it — see the sibling comment on `amount-over`'s currency config
|
|
160
|
+
// above: it is optional PER ATOM INSTANCE (only read when a molecule configures a
|
|
161
|
+
// currency scope), so unlike `cumulativeSpend` (always read), a static per-predicate
|
|
162
|
+
// requiredContext can't represent it without forcing every set using this atom to demand
|
|
163
|
+
// a currency it may never need.
|
|
164
|
+
requiredContext: ["amount", "cumulativeSpend"]
|
|
106
165
|
},
|
|
107
166
|
{
|
|
108
167
|
predicate: "risk-at-or-above",
|
|
@@ -357,7 +416,8 @@ function constraintSatisfied(c, req, strict) {
|
|
|
357
416
|
const left = Object.prototype.hasOwnProperty.call(req.values, c.leftOperand) ? req.values[c.leftOperand] : void 0;
|
|
358
417
|
if (!c.unit) return op(left, c.rightOperand);
|
|
359
418
|
const currency = req.values["mm:currency"];
|
|
360
|
-
const
|
|
419
|
+
const allowedUnits = Array.isArray(c.unit) ? c.unit : [c.unit];
|
|
420
|
+
const unitMatches = typeof currency === "string" && allowedUnits.some((u) => u.toUpperCase() === currency.toUpperCase());
|
|
361
421
|
return unitMatches ? op(left, c.rightOperand) : !strict;
|
|
362
422
|
}
|
|
363
423
|
function targetOf(rule, mandate) {
|