hmcp-core 0.2.0 → 0.2.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.
@@ -205,6 +205,17 @@ export declare function ownsRule(rule: Rule, tool: string): {
205
205
  ok: false;
206
206
  why: string;
207
207
  };
208
+ /**
209
+ * The required inputs a rule would catch on every call.
210
+ *
211
+ * With `unknown_args` at deny or approve, an input no bound mentions is
212
+ * caught — and a required one is sent by every call, so bounding `amount` on
213
+ * pay(to*, amount) blocks every call over `to`. The preview's verdict skips
214
+ * bounds and cannot show this, so whoever writes the rule is told by name.
215
+ * A bound anywhere under an input (`items[].qty`) reaches it, so that input
216
+ * is not caught on every call; only one no bound touches at all is listed.
217
+ */
218
+ export declare function requiredInputsCaught(inputSchema: unknown, rule: Rule): string[];
208
219
  /** The rule a draft becomes, with anything it left unsaid carried from the policy. */
209
220
  export declare function ruleFor(policy: Policy, draft: ToolRuleDraft): Rule;
210
221
  /**
@@ -274,14 +274,40 @@ function resolveDraft(policy, draft) {
274
274
  const carry = governing !== undefined && draft.args === undefined;
275
275
  const args = draft.args ?? (carry ? governing.args : undefined);
276
276
  const onViolation = draft.on_violation ?? (carry ? governing.on_violation : undefined);
277
- const unknownArgs = draft.unknown_args ?? (carry ? governing.unknown_args : undefined);
277
+ const bounded = args !== undefined && Object.keys(args).length > 0;
278
+ /*
279
+ * Our own rule, when there is one, decides the tool today: it sits above any
280
+ * pattern rule. So an edit that says nothing about the rest keeps what it
281
+ * says, rather than taking a pattern rule's value and quietly letting more
282
+ * through. Unless this edit leaves its limits behind, whether the draft sent
283
+ * `args: {}` or no `args` at all: "block the rest" with no limits would
284
+ * refuse every call that has any input, so the default below applies and
285
+ * the drop-limits confirmation asks about it. A rule of ours with no limits
286
+ * that lets everything through chose nothing, so it doesn't count either.
287
+ */
288
+ const own = ruleAtOurId(policy, draft.tool);
289
+ const ownLimited = Object.keys(own?.args ?? {}).length > 0;
290
+ const ownChose = own !== undefined && (ownLimited || own.unknown_args !== "allow");
291
+ const removesLast = ownLimited && !bounded;
292
+ const unknownArgs = draft.unknown_args ??
293
+ (ownChose && !removesLast ? own.unknown_args : undefined) ??
294
+ (carry ? governing.unknown_args : undefined);
278
295
  const rule = {
279
296
  id: ruleIdFor(draft.tool),
280
297
  match: draft.tool,
281
298
  effect: draft.effect,
282
299
  decision: draft.decision,
283
300
  on_violation: onViolation ?? "deny",
284
- unknown_args: unknownArgs ?? "allow",
301
+ /*
302
+ * Unsaid, an input no limit mentions is treated like a call outside the
303
+ * limits — but only when there are limits. Someone who limits `amount`
304
+ * means "only calls like this", and letting `force: true` through
305
+ * unchecked would protect less than the rule reads. With no limits every
306
+ * input is unmentioned, so the same default would refuse every call that
307
+ * has one; there it stays `allow`. Not the schema's default, which stays
308
+ * `allow` so no existing policy changes behaviour on upgrade.
309
+ */
310
+ unknown_args: unknownArgs ?? (bounded ? (onViolation ?? "deny") : "allow"),
285
311
  ...(args && Object.keys(args).length > 0 ? { args } : {}),
286
312
  ...(draft.reason ? { reason: draft.reason } : {})
287
313
  };
@@ -293,6 +319,30 @@ function hasBounds(rule) {
293
319
  return false;
294
320
  return Object.keys(rule.args ?? {}).length > 0 || rule.unknown_args !== "allow";
295
321
  }
322
+ /**
323
+ * The required inputs a rule would catch on every call.
324
+ *
325
+ * With `unknown_args` at deny or approve, an input no bound mentions is
326
+ * caught — and a required one is sent by every call, so bounding `amount` on
327
+ * pay(to*, amount) blocks every call over `to`. The preview's verdict skips
328
+ * bounds and cannot show this, so whoever writes the rule is told by name.
329
+ * A bound anywhere under an input (`items[].qty`) reaches it, so that input
330
+ * is not caught on every call; only one no bound touches at all is listed.
331
+ */
332
+ export function requiredInputsCaught(inputSchema, rule) {
333
+ if (rule.unknown_args === "allow")
334
+ return [];
335
+ const schema = typeof inputSchema === "object" && inputSchema !== null ? inputSchema : {};
336
+ const required = Array.isArray(schema["required"]) ? schema["required"].filter((r) => typeof r === "string") : [];
337
+ const reached = new Set();
338
+ for (const spec of Object.keys(rule.args ?? {})) {
339
+ const parsed = parseArgPath(spec);
340
+ const first = parsed.ok ? parsed.segments[0] : undefined;
341
+ if (first?.kind === "key")
342
+ reached.add(first.name);
343
+ }
344
+ return required.filter((name) => !reached.has(name));
345
+ }
296
346
  /** The rule a draft becomes, with anything it left unsaid carried from the policy. */
297
347
  export function ruleFor(policy, draft) {
298
348
  return resolveDraft(policy, draft).rule;
@@ -354,9 +404,17 @@ export function previewToolRule(args) {
354
404
  ? `this tool's name reads as ${named}, not ${draft.effect}. The gateway refuses to let an upstream's ` +
355
405
  `own hint contradict its name for exactly this reason; a person may, but it is worth being sure.`
356
406
  : null;
357
- const currentBounds = boundsOf(governing ?? existing ?? undefined);
407
+ // Ours, when it exists, is what limits the tool today: it sits above the pattern rule.
408
+ const today = existing ?? governing;
409
+ const currentBounds = boundsOf(today ?? undefined);
358
410
  const nextBounds = boundsOf(rule);
359
- const dropsBounds = currentBounds && !nextBounds ? (governing?.id ?? existing?.id ?? null) : null;
411
+ const dropsBounds = currentBounds && !nextBounds ? (today?.id ?? null) : null;
412
+ /*
413
+ * The verdict ignores limits, so it can't see "anything else the tool is
414
+ * sent" going from blocked or held to let through. That lets more through
415
+ * all the same, and is said as loosening.
416
+ */
417
+ const restLoosens = today !== undefined && UNKNOWN_ARGS_RANK[rule.unknown_args] > UNKNOWN_ARGS_RANK[today.unknown_args];
360
418
  return {
361
419
  policy: candidate,
362
420
  rule,
@@ -381,7 +439,7 @@ export function previewToolRule(args) {
381
439
  RANK[draft.decision] > RANK[governing.decision]
382
440
  ? { ruleId: governing.id, decision: governing.decision, reason: governing.reason }
383
441
  : null,
384
- loosens: RANK[verdict.kind] > RANK[current.kind]
442
+ loosens: RANK[verdict.kind] > RANK[current.kind] || restLoosens
385
443
  };
386
444
  }
387
445
  export function previewClearToolRule(args) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hmcp-core",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Policy schema, decision engine, approvals, standing grants, egress guard and hash-chained audit log for hardened-mcp.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Aditya Das",