hmcp-core 0.0.1 → 0.2.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/README.md +19 -3
- package/dist/approvals.d.ts +508 -0
- package/dist/approvals.js +1445 -0
- package/dist/argpath.d.ts +75 -0
- package/dist/argpath.js +189 -0
- package/dist/audit.d.ts +297 -0
- package/dist/audit.js +524 -0
- package/dist/canonical.d.ts +78 -0
- package/dist/canonical.js +146 -0
- package/dist/classify-held.d.ts +47 -0
- package/dist/classify-held.js +60 -0
- package/dist/classify-name.d.ts +55 -0
- package/dist/classify-name.js +255 -0
- package/dist/decide.d.ts +168 -0
- package/dist/decide.js +518 -0
- package/dist/egress.d.ts +123 -0
- package/dist/egress.js +544 -0
- package/dist/enforce.d.ts +78 -0
- package/dist/enforce.js +231 -0
- package/dist/exposure-apply.d.ts +51 -0
- package/dist/exposure-apply.js +69 -0
- package/dist/exposure.d.ts +81 -0
- package/dist/exposure.js +65 -0
- package/dist/file-lock.d.ts +70 -0
- package/dist/file-lock.js +165 -0
- package/dist/glob.d.ts +11 -0
- package/dist/glob.js +55 -0
- package/dist/grants.d.ts +204 -0
- package/dist/grants.js +372 -0
- package/dist/http-util.d.ts +52 -0
- package/dist/http-util.js +81 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +36 -0
- package/dist/inventory.d.ts +100 -0
- package/dist/inventory.js +88 -0
- package/dist/ip.d.ts +15 -0
- package/dist/ip.js +186 -0
- package/dist/jwt.d.ts +57 -0
- package/dist/jwt.js +199 -0
- package/dist/live-state.d.ts +76 -0
- package/dist/live-state.js +57 -0
- package/dist/migrate.d.ts +22 -0
- package/dist/migrate.js +318 -0
- package/dist/pins.d.ts +119 -0
- package/dist/pins.js +125 -0
- package/dist/policy-batch.d.ts +89 -0
- package/dist/policy-batch.js +340 -0
- package/dist/policy-source.d.ts +161 -0
- package/dist/policy-source.js +299 -0
- package/dist/policy-write.d.ts +627 -0
- package/dist/policy-write.js +1697 -0
- package/dist/policy.d.ts +456 -0
- package/dist/policy.js +448 -0
- package/dist/redact.d.ts +65 -0
- package/dist/redact.js +191 -0
- package/dist/registry.d.ts +41 -0
- package/dist/registry.js +121 -0
- package/dist/remember-defaults.d.ts +9 -0
- package/dist/remember-defaults.js +9 -0
- package/dist/remember.d.ts +91 -0
- package/dist/remember.js +386 -0
- package/dist/resolve.d.ts +22 -0
- package/dist/resolve.js +34 -0
- package/dist/runtime-state.d.ts +65 -0
- package/dist/runtime-state.js +35 -0
- package/dist/secrets.d.ts +159 -0
- package/dist/secrets.js +361 -0
- package/dist/tenant.d.ts +83 -0
- package/dist/tenant.js +182 -0
- package/dist/unicode.d.ts +40 -0
- package/dist/unicode.js +103 -0
- package/dist/within-host.d.ts +33 -0
- package/dist/within-host.js +91 -0
- package/dist/within-real.d.ts +7 -0
- package/dist/within-real.js +28 -0
- package/package.json +33 -4
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
/**
|
|
3
|
+
* Deterministic JSON serialization: object keys sorted, no insignificant
|
|
4
|
+
* whitespace, `undefined` properties dropped. Two structurally equal values
|
|
5
|
+
* always produce the same string, which is what makes argument-bound approval
|
|
6
|
+
* grants and the audit hash chain meaningful.
|
|
7
|
+
*/
|
|
8
|
+
export function canonicalJson(value) {
|
|
9
|
+
return JSON.stringify(normalize(value));
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Sets an own property, whatever the key is called.
|
|
13
|
+
*
|
|
14
|
+
* `out[key] = value` is not that. For the key `__proto__` on an ordinary
|
|
15
|
+
* object it calls the inherited setter, which replaces the object's prototype
|
|
16
|
+
* and creates no property at all -- so a copy built with plain assignment
|
|
17
|
+
* drops exactly the key an attacker gets to choose, and `JSON.stringify`
|
|
18
|
+
* agrees with the copy rather than with what was sent. `JSON.parse`, by
|
|
19
|
+
* contrast, does create it as an own property, which is how the key arrives
|
|
20
|
+
* here in the first place.
|
|
21
|
+
*
|
|
22
|
+
* Every function that rebuilds an argument object goes through this, so the
|
|
23
|
+
* binding, the audit hash and the redacted view all describe the arguments
|
|
24
|
+
* that actually reach the upstream.
|
|
25
|
+
*/
|
|
26
|
+
export function setOwn(target, key, value) {
|
|
27
|
+
if (key === "__proto__") {
|
|
28
|
+
Object.defineProperty(target, key, { value, enumerable: true, writable: true, configurable: true });
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
target[key] = value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function normalize(value) {
|
|
35
|
+
if (value === null || typeof value !== "object") {
|
|
36
|
+
if (typeof value === "number" && !Number.isFinite(value))
|
|
37
|
+
return String(value);
|
|
38
|
+
if (typeof value === "bigint")
|
|
39
|
+
return value.toString();
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
if (Array.isArray(value))
|
|
43
|
+
return value.map(normalize);
|
|
44
|
+
if (value instanceof Date)
|
|
45
|
+
return value.toISOString();
|
|
46
|
+
const source = value;
|
|
47
|
+
const out = {};
|
|
48
|
+
for (const key of Object.keys(source).sort()) {
|
|
49
|
+
const v = source[key];
|
|
50
|
+
if (v === undefined)
|
|
51
|
+
continue;
|
|
52
|
+
setOwn(out, key, normalize(v));
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
export function sha256(...parts) {
|
|
57
|
+
const h = createHash("sha256");
|
|
58
|
+
for (const part of parts)
|
|
59
|
+
h.update(part, "utf8");
|
|
60
|
+
return h.digest("hex");
|
|
61
|
+
}
|
|
62
|
+
/** Hash of a value's canonical form. Used for args_hash in the audit log. */
|
|
63
|
+
export function hashValue(value) {
|
|
64
|
+
return sha256(canonicalJson(value));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Values JSON cannot carry, which `normalize` would otherwise turn into a
|
|
68
|
+
* string. `NaN` becoming `"NaN"` means the number and the literal string hash
|
|
69
|
+
* identically, and two different calls sharing one binding is exactly the
|
|
70
|
+
* confusion an argument-bound approval exists to prevent. MCP arguments arrive
|
|
71
|
+
* as parsed JSON so none of these can come off the wire; this is the check
|
|
72
|
+
* that keeps that true if something else ever calls in.
|
|
73
|
+
*/
|
|
74
|
+
function assertJsonSafe(value, path = "arguments") {
|
|
75
|
+
if (typeof value === "number" && !Number.isFinite(value)) {
|
|
76
|
+
throw new Error(`${path} is ${String(value)}, which JSON cannot represent and a binding cannot hash`);
|
|
77
|
+
}
|
|
78
|
+
if (typeof value === "bigint") {
|
|
79
|
+
throw new Error(`${path} is a bigint, which JSON cannot represent and a binding cannot hash`);
|
|
80
|
+
}
|
|
81
|
+
if (value === null || typeof value !== "object")
|
|
82
|
+
return;
|
|
83
|
+
if (value instanceof Date)
|
|
84
|
+
return;
|
|
85
|
+
if (Array.isArray(value)) {
|
|
86
|
+
value.forEach((v, i) => assertJsonSafe(v, `${path}[${i}]`));
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
for (const [key, v] of Object.entries(value)) {
|
|
90
|
+
assertJsonSafe(v, `${path}.${key}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** Framing for `bindingHash`, so a change of scheme can never collide with the old one. */
|
|
94
|
+
const BINDING_SCHEME = "hmcp.binding.v2";
|
|
95
|
+
/**
|
|
96
|
+
* The scheme a newly written approval is bound under, stored on the row.
|
|
97
|
+
*
|
|
98
|
+
* Rows carry it so redemption can be exact rather than hopeful: a digest
|
|
99
|
+
* computed under one scheme is only ever compared against rows written under
|
|
100
|
+
* that same scheme. Without it, supporting an older scheme would mean matching
|
|
101
|
+
* a row on either digest, and a v1 digest could then release a v2 row.
|
|
102
|
+
*/
|
|
103
|
+
export const BINDING_VERSION = 2;
|
|
104
|
+
/**
|
|
105
|
+
* Identity of a specific call: the tool plus its exact arguments. An approval
|
|
106
|
+
* grant is bound to this, so approving `{amount: 10}` cannot release
|
|
107
|
+
* `{amount: 10000}`.
|
|
108
|
+
*
|
|
109
|
+
* The tool and the arguments go through one canonical document rather than
|
|
110
|
+
* being concatenated into the digest. Concatenating them means the boundary
|
|
111
|
+
* between the two is only a convention, and a tool name carrying the separator
|
|
112
|
+
* could stand in for a different (tool, arguments) pair. Tool names are
|
|
113
|
+
* validated elsewhere, but a hash this load-bearing should not depend on a
|
|
114
|
+
* check in another package.
|
|
115
|
+
*
|
|
116
|
+
* The scheme string is inside the hash, so changing this function produces
|
|
117
|
+
* different hashes by construction rather than by hope.
|
|
118
|
+
*
|
|
119
|
+
* Scope stays out: approvals are filtered by `(component, tenant)` in SQL,
|
|
120
|
+
* where the isolation can be read, grepped and tested. See `ScopedApprovals`.
|
|
121
|
+
*/
|
|
122
|
+
export function bindingHash(tool, args) {
|
|
123
|
+
assertJsonSafe(args ?? {});
|
|
124
|
+
return sha256(canonicalJson([BINDING_SCHEME, tool, args ?? {}]));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The previous framing: the tool, a newline, and the canonical arguments, fed
|
|
128
|
+
* to one digest as three updates.
|
|
129
|
+
*
|
|
130
|
+
* Kept only so an approval that was already granted when this build was
|
|
131
|
+
* deployed can still be redeemed. A reviewer who approved a call should not
|
|
132
|
+
* find that their approval evaporated because the binary was upgraded between
|
|
133
|
+
* the approval and the retry -- and the alternative, telling the agent to ask
|
|
134
|
+
* again, trains exactly the behaviour approvals exist to interrupt.
|
|
135
|
+
*
|
|
136
|
+
* It is self-limiting rather than permanent. It is only ever compared against
|
|
137
|
+
* rows stamped `binding_version = 1`, no new row is ever written under it, and
|
|
138
|
+
* approvals expire -- `ttl_seconds`, 300 by default -- so within minutes of an
|
|
139
|
+
* upgrade there is nothing left for it to match.
|
|
140
|
+
*/
|
|
141
|
+
export function legacyBindingHash(tool, args) {
|
|
142
|
+
return sha256(tool, "\n", canonicalJson(args ?? {}));
|
|
143
|
+
}
|
|
144
|
+
export function callBindings(tool, args) {
|
|
145
|
+
return { current: bindingHash(tool, args), legacy: legacyBindingHash(tool, args) };
|
|
146
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { AuditLog } from "./audit.js";
|
|
2
|
+
import { type Effect } from "./policy.js";
|
|
3
|
+
/**
|
|
4
|
+
* Saying what a tool does, at the moment a call to it is waiting.
|
|
5
|
+
*
|
|
6
|
+
* A tool nothing classified is held for a person on every call. Classifying
|
|
7
|
+
* it is the thing that stops that, and the natural time to do it is while the
|
|
8
|
+
* question is already in front of someone -- so this is callable from the
|
|
9
|
+
* approval itself, by `hmcp approve --classify` and by the approval prompt,
|
|
10
|
+
* and writes the same rule `hmcp-gateway rule` would.
|
|
11
|
+
*
|
|
12
|
+
* It is deliberately narrow, so that approving calls cannot turn into a
|
|
13
|
+
* quieter way of editing policy:
|
|
14
|
+
*
|
|
15
|
+
* - only for a call held *because* nothing classified the tool. A call held
|
|
16
|
+
* by a rule or by the posture already has a classification, and changing
|
|
17
|
+
* it is a decision that belongs in the open;
|
|
18
|
+
* - never a refusal. The person is approving this call; a rule written as a
|
|
19
|
+
* side effect of that must not be what blocks the retry;
|
|
20
|
+
* - only where the gateway's config allows policy to be written at all.
|
|
21
|
+
*/
|
|
22
|
+
export interface ClassifyHeldOptions {
|
|
23
|
+
/** The policy file the gateway that held the call is enforcing. */
|
|
24
|
+
readonly policyPath: string;
|
|
25
|
+
/** The held call: which tool, and which rule held it. */
|
|
26
|
+
readonly row: {
|
|
27
|
+
readonly tool: string;
|
|
28
|
+
readonly rule_id: string | null;
|
|
29
|
+
};
|
|
30
|
+
readonly effect: Effect;
|
|
31
|
+
/**
|
|
32
|
+
* What calls to the tool get from now on. `approve` keeps every call in
|
|
33
|
+
* front of a person and only removes "unknown"; `allow` lets them run.
|
|
34
|
+
*/
|
|
35
|
+
readonly decision: "allow" | "approve";
|
|
36
|
+
readonly actor: string;
|
|
37
|
+
/** `false` where the gateway's `policy_writes` is off. */
|
|
38
|
+
readonly writesAllowed?: boolean | undefined;
|
|
39
|
+
/** Where to record the change, when the caller has a log to hand. */
|
|
40
|
+
readonly audit?: AuditLog | undefined;
|
|
41
|
+
readonly tenant?: string | null | undefined;
|
|
42
|
+
/** How this was asked for, in prose: "hmcp approve", "the approval prompt". */
|
|
43
|
+
readonly via?: string | undefined;
|
|
44
|
+
}
|
|
45
|
+
export declare function classifyHeldTool(options: ClassifyHeldOptions): {
|
|
46
|
+
ruleId: string;
|
|
47
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { UNCLASSIFIED_RULE } from "./decide.js";
|
|
2
|
+
import { loadPolicy } from "./policy.js";
|
|
3
|
+
import { TOOL_RULE_SET, parseToolRuleDraft, writeToolRule } from "./policy-write.js";
|
|
4
|
+
import { policyDigest } from "./runtime-state.js";
|
|
5
|
+
export function classifyHeldTool(options) {
|
|
6
|
+
const { row, effect, decision, actor } = options;
|
|
7
|
+
if (decision !== "allow" && decision !== "approve") {
|
|
8
|
+
throw new Error(`a tool can be classified from an approval as allow or approve, not ${JSON.stringify(decision)}: ` +
|
|
9
|
+
`approving a call must not be what blocks it`);
|
|
10
|
+
}
|
|
11
|
+
if (row.rule_id !== UNCLASSIFIED_RULE) {
|
|
12
|
+
throw new Error(`this call was held by rule "${row.rule_id}", not for want of a classification, so classifying ` +
|
|
13
|
+
`the tool would not change what happens to the next one`);
|
|
14
|
+
}
|
|
15
|
+
if (options.writesAllowed === false) {
|
|
16
|
+
throw new Error(`this gateway's config sets policy_writes: false, so its policy is only changed by editing ` +
|
|
17
|
+
`${options.policyPath} by hand`);
|
|
18
|
+
}
|
|
19
|
+
const via = options.via ?? "an approval";
|
|
20
|
+
const policy = loadPolicy(options.policyPath);
|
|
21
|
+
const draft = parseToolRuleDraft({
|
|
22
|
+
tool: row.tool,
|
|
23
|
+
effect,
|
|
24
|
+
decision,
|
|
25
|
+
reason: `classified as ${effect} while approving a held call`,
|
|
26
|
+
actor,
|
|
27
|
+
via
|
|
28
|
+
});
|
|
29
|
+
const result = writeToolRule({
|
|
30
|
+
policyPath: options.policyPath,
|
|
31
|
+
draft,
|
|
32
|
+
expectedDigest: policyDigest(policy),
|
|
33
|
+
// A call to it was just held, so a gateway was serving it.
|
|
34
|
+
knownToLiveSurface: true,
|
|
35
|
+
writesAllowed: true
|
|
36
|
+
});
|
|
37
|
+
if (result.kind !== "written")
|
|
38
|
+
throw new Error(result.why);
|
|
39
|
+
options.audit?.append({
|
|
40
|
+
tool: draft.tool,
|
|
41
|
+
effect: draft.effect,
|
|
42
|
+
decision,
|
|
43
|
+
tenant: options.tenant ?? null,
|
|
44
|
+
rule_id: TOOL_RULE_SET,
|
|
45
|
+
reason: `${draft.tool} classified as ${draft.effect} by ${actor} via ${via}; rule "${result.rule.id}" ` +
|
|
46
|
+
`written to ${options.policyPath}. ` +
|
|
47
|
+
(decision === "approve" ? "Calls still wait for a person." : "Calls now run without asking."),
|
|
48
|
+
outcome: "completed",
|
|
49
|
+
args_redacted: {
|
|
50
|
+
tool: draft.tool,
|
|
51
|
+
effect: draft.effect,
|
|
52
|
+
decision,
|
|
53
|
+
rule_id: result.rule.id,
|
|
54
|
+
origin: via,
|
|
55
|
+
policy_digest_before: result.previousDigest,
|
|
56
|
+
policy_digest_after: result.digest
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return { ruleId: result.rule.id };
|
|
60
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Effect } from "./policy.js";
|
|
2
|
+
/**
|
|
3
|
+
* What a tool's own name says it does.
|
|
4
|
+
*
|
|
5
|
+
* This lives in `core` rather than beside the gateway's `classify()` because
|
|
6
|
+
* two things now ask the question and they must not answer it differently.
|
|
7
|
+
* The gateway uses it as the last resort when nothing else classified a tool,
|
|
8
|
+
* and as the thing that *contradicts* a dishonest `readOnlyHint`. The console
|
|
9
|
+
* uses it to warn a reviewer who is about to mark a tool whose name begins
|
|
10
|
+
* with `delete` as a read -- the same dishonest-annotation move, made by a
|
|
11
|
+
* human instead of an upstream. Two copies of these verb lists is how the two
|
|
12
|
+
* come to disagree about what `purge_all` is.
|
|
13
|
+
*
|
|
14
|
+
* It is a heuristic and is only ever used as one: the gateway treats it as the
|
|
15
|
+
* weakest source of a classification, and the console as a warning a person
|
|
16
|
+
* may override.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Leading verbs that indicate what an upstream tool does, used only as a last
|
|
20
|
+
* resort. Anything these do not match stays unclassified, which under the
|
|
21
|
+
* default posture means denied - a new upstream tool is not reachable until
|
|
22
|
+
* someone classifies it.
|
|
23
|
+
*
|
|
24
|
+
* Matched as the first *word* of a name, not as a literal prefix, because MCP
|
|
25
|
+
* servers in the wild name tools every way at once: `delete_note`,
|
|
26
|
+
* `delete-note`, `deleteNote`, `delete.note`. These were `delete_`-style
|
|
27
|
+
* prefixes, which meant a server using any convention but snake_case got no
|
|
28
|
+
* classification from its name at all -- and that is not merely a missed hint.
|
|
29
|
+
* The name is what contradicts a `readOnlyHint`, so a kebab-case `delete-note`
|
|
30
|
+
* claiming to be read-only was believed, which is exactly the thing this
|
|
31
|
+
* function exists to refuse.
|
|
32
|
+
*/
|
|
33
|
+
export declare const DESTRUCTIVE_VERBS: string[];
|
|
34
|
+
export interface NameOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Whether the name may carry the gateway's own `<upstream>__` prefix.
|
|
37
|
+
*
|
|
38
|
+
* True for a name as the agent sees it. False for the name exactly as the
|
|
39
|
+
* upstream advertised it, where a `__` is the upstream's own choice and
|
|
40
|
+
* nothing in front of it may be discarded.
|
|
41
|
+
*/
|
|
42
|
+
readonly namespaced?: boolean;
|
|
43
|
+
}
|
|
44
|
+
export declare function classifyByName(name: string, options?: NameOptions): Effect | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Whether a tool's name supports its own claim to be read-only.
|
|
47
|
+
*
|
|
48
|
+
* True when some word of the name says "read" and no word says otherwise.
|
|
49
|
+
* Every word is weighed, not only the first: `status_then_delete` has a read
|
|
50
|
+
* word in front and a destructive one behind it, and the one behind it is the
|
|
51
|
+
* one that matters.
|
|
52
|
+
*/
|
|
53
|
+
export declare function nameAgreesWithReadOnlyHint(name: string, options?: NameOptions): boolean;
|
|
54
|
+
/** The first word of a tool name. See `words`. */
|
|
55
|
+
export declare function firstWord(name: string, options?: NameOptions): string | undefined;
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a tool's own name says it does.
|
|
3
|
+
*
|
|
4
|
+
* This lives in `core` rather than beside the gateway's `classify()` because
|
|
5
|
+
* two things now ask the question and they must not answer it differently.
|
|
6
|
+
* The gateway uses it as the last resort when nothing else classified a tool,
|
|
7
|
+
* and as the thing that *contradicts* a dishonest `readOnlyHint`. The console
|
|
8
|
+
* uses it to warn a reviewer who is about to mark a tool whose name begins
|
|
9
|
+
* with `delete` as a read -- the same dishonest-annotation move, made by a
|
|
10
|
+
* human instead of an upstream. Two copies of these verb lists is how the two
|
|
11
|
+
* come to disagree about what `purge_all` is.
|
|
12
|
+
*
|
|
13
|
+
* It is a heuristic and is only ever used as one: the gateway treats it as the
|
|
14
|
+
* weakest source of a classification, and the console as a warning a person
|
|
15
|
+
* may override.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Leading verbs that indicate what an upstream tool does, used only as a last
|
|
19
|
+
* resort. Anything these do not match stays unclassified, which under the
|
|
20
|
+
* default posture means denied - a new upstream tool is not reachable until
|
|
21
|
+
* someone classifies it.
|
|
22
|
+
*
|
|
23
|
+
* Matched as the first *word* of a name, not as a literal prefix, because MCP
|
|
24
|
+
* servers in the wild name tools every way at once: `delete_note`,
|
|
25
|
+
* `delete-note`, `deleteNote`, `delete.note`. These were `delete_`-style
|
|
26
|
+
* prefixes, which meant a server using any convention but snake_case got no
|
|
27
|
+
* classification from its name at all -- and that is not merely a missed hint.
|
|
28
|
+
* The name is what contradicts a `readOnlyHint`, so a kebab-case `delete-note`
|
|
29
|
+
* claiming to be read-only was believed, which is exactly the thing this
|
|
30
|
+
* function exists to refuse.
|
|
31
|
+
*/
|
|
32
|
+
export const DESTRUCTIVE_VERBS = [
|
|
33
|
+
"delete",
|
|
34
|
+
"remove",
|
|
35
|
+
"destroy",
|
|
36
|
+
"purge",
|
|
37
|
+
"drop",
|
|
38
|
+
"truncate",
|
|
39
|
+
"revoke",
|
|
40
|
+
"wipe",
|
|
41
|
+
"erase",
|
|
42
|
+
"nuke",
|
|
43
|
+
"unlink",
|
|
44
|
+
"rm",
|
|
45
|
+
"del",
|
|
46
|
+
"clear",
|
|
47
|
+
"flush",
|
|
48
|
+
"kill",
|
|
49
|
+
"terminate",
|
|
50
|
+
"shutdown",
|
|
51
|
+
"uninstall",
|
|
52
|
+
"prune"
|
|
53
|
+
];
|
|
54
|
+
const WRITE_VERBS = [
|
|
55
|
+
"create",
|
|
56
|
+
"update",
|
|
57
|
+
"insert",
|
|
58
|
+
"add",
|
|
59
|
+
"set",
|
|
60
|
+
"put",
|
|
61
|
+
"patch",
|
|
62
|
+
"post",
|
|
63
|
+
"write",
|
|
64
|
+
"send",
|
|
65
|
+
"publish",
|
|
66
|
+
"upload",
|
|
67
|
+
"move",
|
|
68
|
+
"rename",
|
|
69
|
+
"assign",
|
|
70
|
+
"approve",
|
|
71
|
+
"cancel",
|
|
72
|
+
"execute",
|
|
73
|
+
"run",
|
|
74
|
+
"invoke",
|
|
75
|
+
"trigger",
|
|
76
|
+
"toggle",
|
|
77
|
+
"enable",
|
|
78
|
+
"disable",
|
|
79
|
+
"start",
|
|
80
|
+
"stop",
|
|
81
|
+
"reset",
|
|
82
|
+
"edit",
|
|
83
|
+
"modify",
|
|
84
|
+
"change",
|
|
85
|
+
"replace",
|
|
86
|
+
"append",
|
|
87
|
+
"overwrite",
|
|
88
|
+
"save",
|
|
89
|
+
"store",
|
|
90
|
+
"commit",
|
|
91
|
+
"push",
|
|
92
|
+
"merge",
|
|
93
|
+
"deploy",
|
|
94
|
+
"install",
|
|
95
|
+
"apply",
|
|
96
|
+
"submit",
|
|
97
|
+
"exec",
|
|
98
|
+
"eval",
|
|
99
|
+
"shell",
|
|
100
|
+
"restart",
|
|
101
|
+
"pay",
|
|
102
|
+
"transfer",
|
|
103
|
+
"charge",
|
|
104
|
+
"refund",
|
|
105
|
+
"buy",
|
|
106
|
+
"sell",
|
|
107
|
+
"order",
|
|
108
|
+
"book",
|
|
109
|
+
"schedule",
|
|
110
|
+
"register",
|
|
111
|
+
"import",
|
|
112
|
+
"sync",
|
|
113
|
+
"grant",
|
|
114
|
+
"invite",
|
|
115
|
+
"share",
|
|
116
|
+
"copy",
|
|
117
|
+
"clone",
|
|
118
|
+
"archive",
|
|
119
|
+
"restore",
|
|
120
|
+
"lock",
|
|
121
|
+
"unlock",
|
|
122
|
+
"notify",
|
|
123
|
+
"email",
|
|
124
|
+
"reply",
|
|
125
|
+
"comment"
|
|
126
|
+
];
|
|
127
|
+
const READ_VERBS = [
|
|
128
|
+
"get",
|
|
129
|
+
"list",
|
|
130
|
+
"read",
|
|
131
|
+
"fetch",
|
|
132
|
+
"search",
|
|
133
|
+
"find",
|
|
134
|
+
"query",
|
|
135
|
+
"describe",
|
|
136
|
+
"show",
|
|
137
|
+
"count",
|
|
138
|
+
"echo",
|
|
139
|
+
"preview"
|
|
140
|
+
];
|
|
141
|
+
/**
|
|
142
|
+
* Words that agree with a `readOnlyHint` without being enough on their own.
|
|
143
|
+
*
|
|
144
|
+
* A hint is believed only when the name says the same thing, and plenty of
|
|
145
|
+
* honest read tools are not named verb-first: `git_status`, `directory_tree`,
|
|
146
|
+
* `check_health`. Requiring one of the dozen verbs above would refuse all of
|
|
147
|
+
* them, and a gateway that refuses most of what it is put in front of gets
|
|
148
|
+
* switched off. So with the hint present this wider vocabulary counts too.
|
|
149
|
+
*
|
|
150
|
+
* It is deliberately kept out of `classifyByName`. There, one word decides the
|
|
151
|
+
* classification with nothing to corroborate it, so the list has to be short
|
|
152
|
+
* and unambiguous. Here two independent signals have to line up.
|
|
153
|
+
*/
|
|
154
|
+
const READ_WORDS_WITH_HINT = [
|
|
155
|
+
"check",
|
|
156
|
+
"view",
|
|
157
|
+
"lookup",
|
|
158
|
+
"retrieve",
|
|
159
|
+
"inspect",
|
|
160
|
+
"status",
|
|
161
|
+
"stat",
|
|
162
|
+
"info",
|
|
163
|
+
"ping",
|
|
164
|
+
"whoami",
|
|
165
|
+
"exists",
|
|
166
|
+
"validate",
|
|
167
|
+
"verify",
|
|
168
|
+
"summarize",
|
|
169
|
+
"analyze",
|
|
170
|
+
"analyse",
|
|
171
|
+
"compare",
|
|
172
|
+
"diff",
|
|
173
|
+
"calculate",
|
|
174
|
+
"compute",
|
|
175
|
+
"convert",
|
|
176
|
+
"translate",
|
|
177
|
+
"parse",
|
|
178
|
+
"explain",
|
|
179
|
+
"help",
|
|
180
|
+
"version",
|
|
181
|
+
"browse",
|
|
182
|
+
"peek",
|
|
183
|
+
"tail",
|
|
184
|
+
"head",
|
|
185
|
+
"cat",
|
|
186
|
+
"ls",
|
|
187
|
+
"tree",
|
|
188
|
+
"grep",
|
|
189
|
+
"select",
|
|
190
|
+
"resolve",
|
|
191
|
+
"estimate",
|
|
192
|
+
"measure",
|
|
193
|
+
"history",
|
|
194
|
+
"log",
|
|
195
|
+
"blame"
|
|
196
|
+
];
|
|
197
|
+
export function classifyByName(name, options = {}) {
|
|
198
|
+
const first = firstWord(name, options);
|
|
199
|
+
if (first === undefined)
|
|
200
|
+
return undefined;
|
|
201
|
+
if (DESTRUCTIVE_VERBS.includes(first))
|
|
202
|
+
return "destructive";
|
|
203
|
+
if (WRITE_VERBS.includes(first))
|
|
204
|
+
return "write";
|
|
205
|
+
if (READ_VERBS.includes(first))
|
|
206
|
+
return "read";
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Whether a tool's name supports its own claim to be read-only.
|
|
211
|
+
*
|
|
212
|
+
* True when some word of the name says "read" and no word says otherwise.
|
|
213
|
+
* Every word is weighed, not only the first: `status_then_delete` has a read
|
|
214
|
+
* word in front and a destructive one behind it, and the one behind it is the
|
|
215
|
+
* one that matters.
|
|
216
|
+
*/
|
|
217
|
+
export function nameAgreesWithReadOnlyHint(name, options = {}) {
|
|
218
|
+
const all = words(name, options);
|
|
219
|
+
if (all.some((w) => DESTRUCTIVE_VERBS.includes(w) || WRITE_VERBS.includes(w)))
|
|
220
|
+
return false;
|
|
221
|
+
return all.some((w) => READ_VERBS.includes(w) || READ_WORDS_WITH_HINT.includes(w));
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* The words of a tool name, under any of the conventions upstreams use.
|
|
225
|
+
*
|
|
226
|
+
* Separators and camelCase boundaries both become word breaks, so
|
|
227
|
+
* `delete_note`, `delete-note`, `deleteNote` and `Delete.Note` all yield
|
|
228
|
+
* `delete`, `note`.
|
|
229
|
+
*
|
|
230
|
+
* A `<server>__` namespace is dropped first. It is the gateway's own prefix,
|
|
231
|
+
* not part of what the upstream called the tool, and reading a verb out of it
|
|
232
|
+
* would let an upstream *named* `delete` turn every one of its reads
|
|
233
|
+
* destructive. Only the first `__` is treated that way: the gateway adds
|
|
234
|
+
* exactly one prefix, so anything after it -- including a second `__` -- is
|
|
235
|
+
* the upstream's own text. Cutting at the *last* one instead let an upstream
|
|
236
|
+
* hide its verb, with `purge__records` reading as `records`.
|
|
237
|
+
*/
|
|
238
|
+
function words(name, options = {}) {
|
|
239
|
+
const namespaced = options.namespaced ?? true;
|
|
240
|
+
const local = namespaced && name.includes("__") ? name.slice(name.indexOf("__") + 2) : name;
|
|
241
|
+
return local
|
|
242
|
+
.replace(/[-._:\s]+/g, " ")
|
|
243
|
+
// lower→upper and acronym→word boundaries, so `deleteNote` and `HTTPGet`
|
|
244
|
+
// both split where a reader would split them.
|
|
245
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
246
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
|
247
|
+
.trim()
|
|
248
|
+
.toLowerCase()
|
|
249
|
+
.split(" ")
|
|
250
|
+
.filter((w) => w.length > 0);
|
|
251
|
+
}
|
|
252
|
+
/** The first word of a tool name. See `words`. */
|
|
253
|
+
export function firstWord(name, options = {}) {
|
|
254
|
+
return words(name, options)[0];
|
|
255
|
+
}
|