@stigmer/tool-vocabulary 0.3.2 → 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/custom-tools.d.ts +32 -0
- package/custom-tools.d.ts.map +1 -0
- package/custom-tools.js +305 -0
- package/custom-tools.js.map +1 -0
- package/index.d.ts +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +9 -4
- package/index.js.map +1 -1
- package/package.json +10 -2
- package/secret-args.d.ts +5 -0
- package/secret-args.d.ts.map +1 -0
- package/secret-args.js +33 -0
- package/secret-args.js.map +1 -0
- package/src/custom-tools.ts +314 -0
- package/src/index.ts +9 -4
- package/src/secret-args.ts +34 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** One tool the host app answers itself, as a conversation declares it. */
|
|
2
|
+
export interface CustomToolDeclaration {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
readonly description: string;
|
|
5
|
+
/** A JSON Schema object (`{"type": "object", ...}`). */
|
|
6
|
+
readonly inputSchema: unknown;
|
|
7
|
+
}
|
|
8
|
+
/** The limits a conversation's custom tools are held to (the header). */
|
|
9
|
+
export declare const CUSTOM_TOOL_LIMITS: {
|
|
10
|
+
readonly maxTools: 32;
|
|
11
|
+
readonly maxNameLength: 64;
|
|
12
|
+
readonly maxDescriptionLength: 4096;
|
|
13
|
+
readonly maxSchemaBytes: number;
|
|
14
|
+
};
|
|
15
|
+
/** A custom tool's name: letters, digits, `_` and `-`, 1 to 64 (the header). */
|
|
16
|
+
export declare const CUSTOM_TOOL_NAME_PATTERN: RegExp;
|
|
17
|
+
/** Whether a name is an engine's own tool's, whatever its case (the header). */
|
|
18
|
+
export declare function isReservedCustomToolName(name: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Why a conversation's custom tools cannot be declared, as one sentence
|
|
21
|
+
* naming the tool and the rule; undefined when every one may be.
|
|
22
|
+
*/
|
|
23
|
+
export declare function customToolsProblem(tools: readonly CustomToolDeclaration[]): string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Why a call's input does not match its custom tool's schema, as the
|
|
26
|
+
* sentence the model reads (it names the tool and every mismatch, and says
|
|
27
|
+
* the call did not run); undefined when it matches.
|
|
28
|
+
*/
|
|
29
|
+
export declare function customToolInputProblem(tool: Pick<CustomToolDeclaration, "name" | "inputSchema">, input: unknown): string | undefined;
|
|
30
|
+
/** The longest one input check may run, in milliseconds (the header). Real checks finish in well under one. */
|
|
31
|
+
export declare const CUSTOM_TOOL_CHECK_BUDGET_MS = 250;
|
|
32
|
+
//# sourceMappingURL=custom-tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"custom-tools.d.ts","sourceRoot":"","sources":["src/custom-tools.ts"],"names":[],"mappings":"AA0DA,2EAA2E;AAC3E,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,wDAAwD;IACxD,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;CAC/B;AAED,yEAAyE;AACzE,eAAO,MAAM,kBAAkB;;;;;CAKrB,CAAC;AAEX,gFAAgF;AAChF,eAAO,MAAM,wBAAwB,QAA0B,CAAC;AAsBhE,gFAAgF;AAChF,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE9D;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,SAAS,qBAAqB,EAAE,GAAG,MAAM,GAAG,SAAS,CAqC9F;AA0CD;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,IAAI,CAAC,qBAAqB,EAAE,MAAM,GAAG,aAAa,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAapI;AAED,+GAA+G;AAC/G,eAAO,MAAM,2BAA2B,MAAM,CAAC"}
|
package/custom-tools.js
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// The host app's own tools (a session's `custom_tools`): the one check the
|
|
2
|
+
// control plane runs when a conversation declares them and the runner runs
|
|
3
|
+
// before it offers them and on every call, so "refused when declared" and
|
|
4
|
+
// "refused to the model" cannot drift.
|
|
5
|
+
//
|
|
6
|
+
// What a declaration must be, each rule with its reason:
|
|
7
|
+
// - A name of letters, digits, `_` and `-`, at most 64: the name reaches
|
|
8
|
+
// every model provider as a tool name, and 64 is the budget the runner
|
|
9
|
+
// holds every model-facing name to (`mcp-tool-names.ts` in the runner).
|
|
10
|
+
// Claude Managed Agents allows 128; a longer name is refused with the
|
|
11
|
+
// limit named.
|
|
12
|
+
// - Not a built-in tool's name on either engine, whatever its case, and
|
|
13
|
+
// not starting `mcp__`: on the Stigmer engine a custom tool is bound under
|
|
14
|
+
// its own name beside the engine's tools, and a model offered two tools
|
|
15
|
+
// of one name, or a name that reads as an MCP tool's, cannot tell which
|
|
16
|
+
// it calls. Managed Agents refuses `mcp__` names too.
|
|
17
|
+
// - Unique in the session, with a description the model reads.
|
|
18
|
+
// - An input schema that is a JSON Schema object (`{"type": "object"}`)
|
|
19
|
+
// the validator accepts: every model provider takes a tool's input as an
|
|
20
|
+
// object, and a schema the validator cannot read could never pass a call.
|
|
21
|
+
// - At most 32 tools and 64 KB of schemas in all, so the tool list the
|
|
22
|
+
// model reads stays within a turn's budget.
|
|
23
|
+
// - No top-level input property named like a secret (secret-args.ts:
|
|
24
|
+
// `password`, `token`, `auth`, ...): the runner redacts such an argument
|
|
25
|
+
// on every stored row, so the host would read `[REDACTED]` where the
|
|
26
|
+
// agent wrote a value, in its card, its callback and its own server's
|
|
27
|
+
// read of the log. The rule reads the schema's top-level `properties`
|
|
28
|
+
// only: a field named another way (through `allOf`, `anyOf`, `oneOf`,
|
|
29
|
+
// `if`/`then`, `dependentSchemas`, a top-level `$ref`, or `required`
|
|
30
|
+
// alone) is not refused and is still redacted, as is a property the model
|
|
31
|
+
// adds beyond those declared (where the schema allows extra properties).
|
|
32
|
+
// The rule runs wherever this check runs: where tools are declared
|
|
33
|
+
// (create and update) and each time the runner offers them, so a
|
|
34
|
+
// conversation that declared such a field before the rule is offered no
|
|
35
|
+
// custom tools from its next turn, and its next update is refused until
|
|
36
|
+
// the field is renamed.
|
|
37
|
+
//
|
|
38
|
+
// A call's input is checked against its tool's schema with
|
|
39
|
+
// `@cfworker/json-schema` (draft 2020-12), the validator the Stigmer
|
|
40
|
+
// engine's LangChain stack already uses, so a call the runner admits is one
|
|
41
|
+
// the schema accepts on every engine. The check runs under a time budget
|
|
42
|
+
// (CUSTOM_TOOL_CHECK_BUDGET_MS): a schema's `pattern` is the host's and the
|
|
43
|
+
// input is the model's, so a pattern that backtracks exponentially could
|
|
44
|
+
// otherwise hold the runner's one thread for minutes, its stall watchdog
|
|
45
|
+
// included. It runs through `node:vm` with a timeout, as the runner's hook
|
|
46
|
+
// matchers do (`shared/hooks/matcher.ts` in the runner): V8 checks for the
|
|
47
|
+
// interrupt while it backtracks, and a check stopped there refuses the
|
|
48
|
+
// call.
|
|
49
|
+
//
|
|
50
|
+
// Proven by __tests__/custom-tools.test.ts.
|
|
51
|
+
import { createContext, Script } from "node:vm";
|
|
52
|
+
import { Validator } from "@cfworker/json-schema";
|
|
53
|
+
import { isSecretArgKey } from "./secret-args.js";
|
|
54
|
+
import { CLAUDE_TOOLS, CLAUDE_TOOL_ALIASES, STIGMER_TOOL_COVERS } from "./vocabulary.js";
|
|
55
|
+
/** The limits a conversation's custom tools are held to (the header). */
|
|
56
|
+
export const CUSTOM_TOOL_LIMITS = {
|
|
57
|
+
maxTools: 32,
|
|
58
|
+
maxNameLength: 64,
|
|
59
|
+
maxDescriptionLength: 4096,
|
|
60
|
+
maxSchemaBytes: 64 * 1024,
|
|
61
|
+
};
|
|
62
|
+
/** A custom tool's name: letters, digits, `_` and `-`, 1 to 64 (the header). */
|
|
63
|
+
export const CUSTOM_TOOL_NAME_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
64
|
+
/** The prefix every MCP tool's model-facing name starts with, which a custom tool's may not. */
|
|
65
|
+
const MCP_PREFIX = "mcp__";
|
|
66
|
+
/**
|
|
67
|
+
* The engines' own tool names a custom tool may not take, lower-cased: every
|
|
68
|
+
* Claude Code built-in a Stigmer turn offers or a list may name (the
|
|
69
|
+
* engine's tool search and structured-output tools included), and every
|
|
70
|
+
* tool the Stigmer engine binds.
|
|
71
|
+
*/
|
|
72
|
+
const RESERVED_NAMES = new Set([
|
|
73
|
+
...CLAUDE_TOOLS,
|
|
74
|
+
...CLAUDE_TOOL_ALIASES.keys(),
|
|
75
|
+
"ToolSearch",
|
|
76
|
+
"StructuredOutput",
|
|
77
|
+
...STIGMER_TOOL_COVERS.keys(),
|
|
78
|
+
"stop_background_command",
|
|
79
|
+
].map((name) => name.toLowerCase()));
|
|
80
|
+
/** Whether a name is an engine's own tool's, whatever its case (the header). */
|
|
81
|
+
export function isReservedCustomToolName(name) {
|
|
82
|
+
return RESERVED_NAMES.has(name.toLowerCase()) || name.toLowerCase().startsWith(MCP_PREFIX);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Why a conversation's custom tools cannot be declared, as one sentence
|
|
86
|
+
* naming the tool and the rule; undefined when every one may be.
|
|
87
|
+
*/
|
|
88
|
+
export function customToolsProblem(tools) {
|
|
89
|
+
if (tools.length > CUSTOM_TOOL_LIMITS.maxTools) {
|
|
90
|
+
return `A conversation may declare at most ${CUSTOM_TOOL_LIMITS.maxTools} custom tools; this one declares ${tools.length}.`;
|
|
91
|
+
}
|
|
92
|
+
const seen = new Set();
|
|
93
|
+
let schemaBytes = 0;
|
|
94
|
+
for (const [index, tool] of tools.entries()) {
|
|
95
|
+
const label = tool.name === "" ? `custom tool ${index + 1}` : `custom tool '${tool.name}'`;
|
|
96
|
+
if (tool.name.length > CUSTOM_TOOL_LIMITS.maxNameLength) {
|
|
97
|
+
return `The name of ${label} is ${tool.name.length} characters; a custom tool's name is at most ${CUSTOM_TOOL_LIMITS.maxNameLength}.`;
|
|
98
|
+
}
|
|
99
|
+
if (!CUSTOM_TOOL_NAME_PATTERN.test(tool.name)) {
|
|
100
|
+
return `The name of ${label} may hold only letters, digits, _ and -.`;
|
|
101
|
+
}
|
|
102
|
+
if (isReservedCustomToolName(tool.name)) {
|
|
103
|
+
return tool.name.toLowerCase().startsWith(MCP_PREFIX)
|
|
104
|
+
? `The name of ${label} may not start with mcp__, which names an MCP server's tools.`
|
|
105
|
+
: `The name of ${label} is a built-in tool's; choose another.`;
|
|
106
|
+
}
|
|
107
|
+
if (seen.has(tool.name))
|
|
108
|
+
return `Two custom tools are named '${tool.name}'; each name may appear once.`;
|
|
109
|
+
seen.add(tool.name);
|
|
110
|
+
if (tool.description.trim() === "")
|
|
111
|
+
return `The description of ${label} is empty; the agent reads it to know when to call the tool.`;
|
|
112
|
+
if (tool.description.length > CUSTOM_TOOL_LIMITS.maxDescriptionLength) {
|
|
113
|
+
return `The description of ${label} is ${tool.description.length} characters; at most ${CUSTOM_TOOL_LIMITS.maxDescriptionLength}.`;
|
|
114
|
+
}
|
|
115
|
+
const schema = schemaProblem(tool.inputSchema);
|
|
116
|
+
if (schema !== undefined)
|
|
117
|
+
return `The input_schema of ${label} ${schema}.`;
|
|
118
|
+
const secret = secretProperty(tool.inputSchema);
|
|
119
|
+
if (secret !== undefined) {
|
|
120
|
+
return `The input_schema of ${label} has a property named ${JSON.stringify(secret)}, a name whose value the platform redacts from every tool call, so the app would read [REDACTED] in its place; rename it to say what it holds (as "auth_method" says it for "auth").`;
|
|
121
|
+
}
|
|
122
|
+
schemaBytes += new TextEncoder().encode(JSON.stringify(tool.inputSchema)).length;
|
|
123
|
+
}
|
|
124
|
+
if (schemaBytes > CUSTOM_TOOL_LIMITS.maxSchemaBytes) {
|
|
125
|
+
return `The custom tools' input schemas hold ${schemaBytes} bytes in all; at most ${CUSTOM_TOOL_LIMITS.maxSchemaBytes}.`;
|
|
126
|
+
}
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
/** Why a schema cannot be a custom tool's input schema, as the end of a sentence; undefined when it can. */
|
|
130
|
+
function schemaProblem(schema) {
|
|
131
|
+
if (!isObject(schema))
|
|
132
|
+
return "must be a JSON Schema object";
|
|
133
|
+
if (schema.type !== "object")
|
|
134
|
+
return 'must describe an object ({"type": "object", ...})';
|
|
135
|
+
const ref = unresolvedRef(schema, schema);
|
|
136
|
+
if (ref !== undefined)
|
|
137
|
+
return `cannot be read as a JSON Schema: its $ref ${JSON.stringify(ref)} names nothing in the schema (a custom tool's schema is self-contained)`;
|
|
138
|
+
const pattern = badPattern(schema);
|
|
139
|
+
if (pattern !== undefined)
|
|
140
|
+
return `cannot be read as a JSON Schema: its pattern ${JSON.stringify(pattern)} is not a regular expression`;
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The first top-level property `schema` declares whose name is a secret
|
|
145
|
+
* argument's (the header), as written; undefined when none is.
|
|
146
|
+
*/
|
|
147
|
+
function secretProperty(schema) {
|
|
148
|
+
return isObject(schema.properties) ? Object.keys(schema.properties).find(isSecretArgKey) : undefined;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The first `pattern` (or `patternProperties` key) in `node` the validator
|
|
152
|
+
* could not compile, as it compiles them (`new RegExp(p, "u")`); undefined
|
|
153
|
+
* when every one compiles. A schema is otherwise read only when a call
|
|
154
|
+
* reaches the keyword, so a bad one would refuse every call instead of the
|
|
155
|
+
* declaration.
|
|
156
|
+
*/
|
|
157
|
+
function badPattern(node) {
|
|
158
|
+
if (Array.isArray(node))
|
|
159
|
+
return node.map(badPattern).find((found) => found !== undefined);
|
|
160
|
+
if (!isObject(node))
|
|
161
|
+
return undefined;
|
|
162
|
+
const patterns = [...(typeof node.pattern === "string" ? [node.pattern] : []), ...(isObject(node.patternProperties) ? Object.keys(node.patternProperties) : [])];
|
|
163
|
+
for (const pattern of patterns) {
|
|
164
|
+
try {
|
|
165
|
+
new RegExp(pattern, "u");
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
return pattern;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return Object.values(node).map(badPattern).find((found) => found !== undefined);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Why a call's input does not match its custom tool's schema, as the
|
|
175
|
+
* sentence the model reads (it names the tool and every mismatch, and says
|
|
176
|
+
* the call did not run); undefined when it matches.
|
|
177
|
+
*/
|
|
178
|
+
export function customToolInputProblem(tool, input) {
|
|
179
|
+
let result;
|
|
180
|
+
try {
|
|
181
|
+
result = timedValidate(new Validator(tool.inputSchema, "2020-12", false), input);
|
|
182
|
+
}
|
|
183
|
+
catch (err) {
|
|
184
|
+
if (isScriptTimeout(err)) {
|
|
185
|
+
return `The input could not be checked against ${tool.name}'s input schema in time (a pattern in the schema takes too long on this input); the call did not run.`;
|
|
186
|
+
}
|
|
187
|
+
return `The input could not be checked against ${tool.name}'s input schema (${String(err)}); the call did not run.`;
|
|
188
|
+
}
|
|
189
|
+
if (result.valid)
|
|
190
|
+
return undefined;
|
|
191
|
+
const listed = mismatchesOf(result.errors);
|
|
192
|
+
return `The input does not match ${tool.name}'s input schema (${listed.join("; ")}); the call did not run. Call it again with input that matches.`;
|
|
193
|
+
}
|
|
194
|
+
/** The longest one input check may run, in milliseconds (the header). Real checks finish in well under one. */
|
|
195
|
+
export const CUSTOM_TOOL_CHECK_BUDGET_MS = 250;
|
|
196
|
+
/** The one context and script every timed check runs in, made on the first. */
|
|
197
|
+
let sandbox;
|
|
198
|
+
/** One validation under the budget (the header); throws Node's timeout error when stopped there. */
|
|
199
|
+
function timedValidate(validator, input) {
|
|
200
|
+
sandbox ??= { context: createContext({}), script: new Script("validator.validate(input)") };
|
|
201
|
+
sandbox.context.validator = validator;
|
|
202
|
+
sandbox.context.input = input;
|
|
203
|
+
try {
|
|
204
|
+
return sandbox.script.runInContext(sandbox.context, { timeout: CUSTOM_TOOL_CHECK_BUDGET_MS });
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
delete sandbox.context.validator;
|
|
208
|
+
delete sandbox.context.input;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/** Node's timeout error comes from the context's realm, so it is read by its code, not by `instanceof`. */
|
|
212
|
+
function isScriptTimeout(err) {
|
|
213
|
+
return typeof err === "object" && err !== null && err.code === "ERR_SCRIPT_EXECUTION_TIMEOUT";
|
|
214
|
+
}
|
|
215
|
+
/** The validator's keywords that only report that a nested one failed, which the nested error says better. */
|
|
216
|
+
const WRAPPER_KEYWORDS = new Set(["properties", "items", "prefixItems", "allOf", "$ref", "false"]);
|
|
217
|
+
/** What `additionalProperties` reports for a property its schema does not take. */
|
|
218
|
+
const EXTRA_PROPERTY = /^Property "(.*)" does not match additional properties schema\.$/;
|
|
219
|
+
/**
|
|
220
|
+
* Each mismatch once, where it is, in the model's words: wrapper errors
|
|
221
|
+
* dropped, and a property the schema does not take reported only when
|
|
222
|
+
* nothing more precise is said about it (the validator also reports a
|
|
223
|
+
* declared property that failed its own rule as an extra one).
|
|
224
|
+
*/
|
|
225
|
+
function mismatchesOf(errors) {
|
|
226
|
+
const where = (location) => (location === "#" ? "the input" : location.replace(/^#/, ""));
|
|
227
|
+
const precise = errors.filter((error) => !WRAPPER_KEYWORDS.has(error.keyword) && error.keyword !== "additionalProperties");
|
|
228
|
+
const said = new Set(precise.map((error) => error.instanceLocation));
|
|
229
|
+
const extras = errors.flatMap((error) => {
|
|
230
|
+
const name = error.keyword === "additionalProperties" ? EXTRA_PROPERTY.exec(error.error)?.[1] : undefined;
|
|
231
|
+
if (name === undefined)
|
|
232
|
+
return [];
|
|
233
|
+
const location = `${error.instanceLocation}/${name}`;
|
|
234
|
+
return said.has(location) ? [] : [`${where(location)}: not a property this tool takes`];
|
|
235
|
+
});
|
|
236
|
+
const listed = [...precise.map((error) => `${where(error.instanceLocation)}: ${error.error}`), ...extras];
|
|
237
|
+
return (listed.length > 0 ? listed : errors.map((error) => error.error)).slice(0, 10);
|
|
238
|
+
}
|
|
239
|
+
/** The first `$ref` in `node` that is not a JSON pointer into `root` naming something there; undefined when every one is. */
|
|
240
|
+
function unresolvedRef(node, root) {
|
|
241
|
+
if (Array.isArray(node)) {
|
|
242
|
+
for (const item of node) {
|
|
243
|
+
const found = unresolvedRef(item, root);
|
|
244
|
+
if (found !== undefined)
|
|
245
|
+
return found;
|
|
246
|
+
}
|
|
247
|
+
return undefined;
|
|
248
|
+
}
|
|
249
|
+
if (!isObject(node))
|
|
250
|
+
return undefined;
|
|
251
|
+
const ref = node.$ref;
|
|
252
|
+
if (ref !== undefined && (typeof ref !== "string" || !resolvesIn(root, ref)))
|
|
253
|
+
return String(ref);
|
|
254
|
+
for (const value of Object.values(node)) {
|
|
255
|
+
const found = unresolvedRef(value, root);
|
|
256
|
+
if (found !== undefined)
|
|
257
|
+
return found;
|
|
258
|
+
}
|
|
259
|
+
return undefined;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Whether `ref` is `#` or a JSON pointer (`#/...`) naming a value in `root`,
|
|
263
|
+
* through its own keys alone (an inherited `constructor` names nothing the
|
|
264
|
+
* validator finds), its escapes read as a pointer's.
|
|
265
|
+
*/
|
|
266
|
+
function resolvesIn(root, ref) {
|
|
267
|
+
if (ref === "#")
|
|
268
|
+
return true;
|
|
269
|
+
if (!ref.startsWith("#/"))
|
|
270
|
+
return false;
|
|
271
|
+
let current = root;
|
|
272
|
+
for (const raw of ref.slice(2).split("/")) {
|
|
273
|
+
const key = pointerKey(raw);
|
|
274
|
+
if (key === undefined)
|
|
275
|
+
return false;
|
|
276
|
+
if (Array.isArray(current)) {
|
|
277
|
+
// An array's step is an index it holds, never one of its own other keys (`length`).
|
|
278
|
+
if (!ARRAY_INDEX.test(key) || Number(key) >= current.length)
|
|
279
|
+
return false;
|
|
280
|
+
current = current[Number(key)];
|
|
281
|
+
}
|
|
282
|
+
else if (isObject(current) && Object.hasOwn(current, key)) {
|
|
283
|
+
current = current[key];
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
/** A JSON pointer's array index: a non-negative integer with no leading zero. */
|
|
292
|
+
const ARRAY_INDEX = /^(0|[1-9][0-9]*)$/;
|
|
293
|
+
/** A pointer segment's key, its percent and tilde escapes undone; undefined for a malformed escape. */
|
|
294
|
+
function pointerKey(raw) {
|
|
295
|
+
try {
|
|
296
|
+
return decodeURIComponent(raw).replace(/~1/g, "/").replace(/~0/g, "~");
|
|
297
|
+
}
|
|
298
|
+
catch {
|
|
299
|
+
return undefined;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function isObject(value) {
|
|
303
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
304
|
+
}
|
|
305
|
+
//# sourceMappingURL=custom-tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"custom-tools.js","sourceRoot":"","sources":["src/custom-tools.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,2EAA2E;AAC3E,0EAA0E;AAC1E,uCAAuC;AACvC,EAAE;AACF,yDAAyD;AACzD,0EAA0E;AAC1E,0EAA0E;AAC1E,2EAA2E;AAC3E,yEAAyE;AACzE,kBAAkB;AAClB,yEAAyE;AACzE,8EAA8E;AAC9E,2EAA2E;AAC3E,2EAA2E;AAC3E,yDAAyD;AACzD,gEAAgE;AAChE,yEAAyE;AACzE,4EAA4E;AAC5E,6EAA6E;AAC7E,wEAAwE;AACxE,+CAA+C;AAC/C,sEAAsE;AACtE,4EAA4E;AAC5E,wEAAwE;AACxE,yEAAyE;AACzE,yEAAyE;AACzE,yEAAyE;AACzE,wEAAwE;AACxE,6EAA6E;AAC7E,4EAA4E;AAC5E,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,2EAA2E;AAC3E,2BAA2B;AAC3B,EAAE;AACF,2DAA2D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ary.js";
|
|
2
2
|
export { ACCESS_TOKEN_VAR_SUFFIX, CHANNELS_MCP_SERVER, CONVERSATION_MCP_SERVER, MCP_SERVER_NAME_PATTERN, MEMORY_MCP_SERVER, PLATFORM_MCP_SERVERS, accessTokenVariable, isReservedMcpServerName, } from "./mcp-servers.js";
|
|
3
|
+
export { SECRET_ARG_KEYS, isSecretArgKey } from "./secret-args.js";
|
|
3
4
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AASA,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,UAAU,EACV,aAAa,EACb,YAAY,EACZ,YAAY,EACZ,KAAK,UAAU,EACf,KAAK,UAAU,GAChB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACnB,uBAAuB,EACvB,uBAAuB,EACvB,iBAAiB,EACjB,oBAAoB,EACpB,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC"}
|
package/index.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
// The package entry: re-exports the tool vocabulary (vocabulary.ts)
|
|
2
|
-
// MCP server names (mcp-servers.ts)
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// The package entry: re-exports the tool vocabulary (vocabulary.ts), the
|
|
2
|
+
// MCP server names (mcp-servers.ts) and the secret argument names
|
|
3
|
+
// (secret-args.ts) that the runner, the control plane and the browser kits
|
|
4
|
+
// read, so none reaches into a file path. Nothing else
|
|
5
|
+
// lives here; the tables and their trade-offs are documented and pinned
|
|
6
|
+
// beside them. The custom-tool check is its own entry,
|
|
7
|
+
// `@stigmer/tool-vocabulary/custom-tools`: it runs a JSON Schema validator
|
|
8
|
+
// under `node:vm`, which neither a browser bundle can hold nor a kit needs.
|
|
5
9
|
export { CLAUDE_TOOLS, CLAUDE_TOOL_ALIASES, CLAUDE_TOOL_COVERS, STIGMER_TOOL_COVERS, READ_ONLY_EVAL_TOOLS, SHELL_TOOL, builtinToolOf, claudeNameOf, isClaudeTool, } from "./vocabulary.js";
|
|
6
10
|
export { ACCESS_TOKEN_VAR_SUFFIX, CHANNELS_MCP_SERVER, CONVERSATION_MCP_SERVER, MCP_SERVER_NAME_PATTERN, MEMORY_MCP_SERVER, PLATFORM_MCP_SERVERS, accessTokenVariable, isReservedMcpServerName, } from "./mcp-servers.js";
|
|
11
|
+
export { SECRET_ARG_KEYS, isSecretArgKey } from "./secret-args.js";
|
|
7
12
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,kEAAkE;AAClE,2EAA2E;AAC3E,uDAAuD;AACvD,wEAAwE;AACxE,uDAAuD;AACvD,2EAA2E;AAC3E,4EAA4E;AAE5E,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,UAAU,EACV,aAAa,EACb,YAAY,EACZ,YAAY,GAGb,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACnB,uBAAuB,EACvB,uBAAuB,EACvB,iBAAiB,EACjB,oBAAoB,EACpB,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stigmer/tool-vocabulary",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Claude Code's tool names as Stigmer reads them, shared by the runner and the control plane: the built-in names a tool list may carry, the tables of which Claude tools each engine tool covers (Stigmer, Claude), the reverse reading a grader uses to name a recorded engine call in Claude's words,
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Claude Code's tool names as Stigmer reads them, shared by the runner and the control plane: the built-in names a tool list may carry, the tables of which Claude tools each engine tool covers (Stigmer, Claude), the reverse reading a grader uses to name a recorded engine call in Claude's words, the MCP server names both sides agree on (the platform's own servers, the name grammar, the login variable), the tool-argument names whose values are redacted, and, at ./custom-tools, the check a host app's own tools pass (custom_tools)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -26,6 +26,14 @@
|
|
|
26
26
|
"types": "./index.d.ts",
|
|
27
27
|
"import": "./index.js",
|
|
28
28
|
"default": "./index.js"
|
|
29
|
+
},
|
|
30
|
+
"./custom-tools": {
|
|
31
|
+
"types": "./custom-tools.d.ts",
|
|
32
|
+
"import": "./custom-tools.js",
|
|
33
|
+
"default": "./custom-tools.js"
|
|
29
34
|
}
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@cfworker/json-schema": "4.1.1"
|
|
30
38
|
}
|
|
31
39
|
}
|
package/secret-args.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Argument names whose top-level values are never shown to any reader (the header). */
|
|
2
|
+
export declare const SECRET_ARG_KEYS: ReadonlySet<string>;
|
|
3
|
+
/** Whether a top-level argument name is one whose value is redacted, whatever its case. */
|
|
4
|
+
export declare function isSecretArgKey(key: string): boolean;
|
|
5
|
+
//# sourceMappingURL=secret-args.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secret-args.d.ts","sourceRoot":"","sources":["src/secret-args.ts"],"names":[],"mappings":"AAkBA,wFAAwF;AACxF,eAAO,MAAM,eAAe,EAAE,WAAW,CAAC,MAAM,CAS9C,CAAC;AAEH,2FAA2F;AAC3F,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEnD"}
|
package/secret-args.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The tool-argument names whose values are secrets (stigmer#1119): the runner
|
|
2
|
+
// replaces a top-level argument under one of these names with a marker on
|
|
3
|
+
// every tool row's `args`, in its `args_preview` and in an approval message,
|
|
4
|
+
// so no reader of a conversation ever sees the value. Matched
|
|
5
|
+
// case-insensitively against the top-level key; a nested key, or a name that
|
|
6
|
+
// only contains one of these words (`auth_method`, `token_count`), is kept.
|
|
7
|
+
//
|
|
8
|
+
// One table for both sides that act on it: the runner redacts with it
|
|
9
|
+
// (`shared/args-preview.ts` in the runner), and the custom-tool check
|
|
10
|
+
// (custom-tools.ts) refuses a host tool whose input declares one of these
|
|
11
|
+
// names, since its host would read the marker where the agent wrote a value.
|
|
12
|
+
// `@stigmer/client` keeps its own copy for rows stored before the runner
|
|
13
|
+
// redacted (`isSecretArgKey`), because a client never depends on this
|
|
14
|
+
// package; the cross-language contract all three are held to is
|
|
15
|
+
// `test/fixtures/tool-view/secret-args.json`.
|
|
16
|
+
//
|
|
17
|
+
// Proven by __tests__/secret-args.test.ts.
|
|
18
|
+
/** Argument names whose top-level values are never shown to any reader (the header). */
|
|
19
|
+
export const SECRET_ARG_KEYS = new Set([
|
|
20
|
+
"password",
|
|
21
|
+
"token",
|
|
22
|
+
"secret",
|
|
23
|
+
"api_key",
|
|
24
|
+
"apikey",
|
|
25
|
+
"credentials",
|
|
26
|
+
"auth",
|
|
27
|
+
"authorization",
|
|
28
|
+
]);
|
|
29
|
+
/** Whether a top-level argument name is one whose value is redacted, whatever its case. */
|
|
30
|
+
export function isSecretArgKey(key) {
|
|
31
|
+
return SECRET_ARG_KEYS.has(key.toLowerCase());
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=secret-args.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secret-args.js","sourceRoot":"","sources":["src/secret-args.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,0EAA0E;AAC1E,6EAA6E;AAC7E,8DAA8D;AAC9D,6EAA6E;AAC7E,4EAA4E;AAC5E,EAAE;AACF,sEAAsE;AACtE,sEAAsE;AACtE,0EAA0E;AAC1E,6EAA6E;AAC7E,yEAAyE;AACzE,sEAAsE;AACtE,gEAAgE;AAChE,8CAA8C;AAC9C,EAAE;AACF,2CAA2C;AAE3C,wFAAwF;AACxF,MAAM,CAAC,MAAM,eAAe,GAAwB,IAAI,GAAG,CAAC;IAC1D,UAAU;IACV,OAAO;IACP,QAAQ;IACR,SAAS;IACT,QAAQ;IACR,aAAa;IACb,MAAM;IACN,eAAe;CAChB,CAAC,CAAC;AAEH,2FAA2F;AAC3F,MAAM,UAAU,cAAc,CAAC,GAAW;IACxC,OAAO,eAAe,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// The host app's own tools (a session's `custom_tools`): the one check the
|
|
2
|
+
// control plane runs when a conversation declares them and the runner runs
|
|
3
|
+
// before it offers them and on every call, so "refused when declared" and
|
|
4
|
+
// "refused to the model" cannot drift.
|
|
5
|
+
//
|
|
6
|
+
// What a declaration must be, each rule with its reason:
|
|
7
|
+
// - A name of letters, digits, `_` and `-`, at most 64: the name reaches
|
|
8
|
+
// every model provider as a tool name, and 64 is the budget the runner
|
|
9
|
+
// holds every model-facing name to (`mcp-tool-names.ts` in the runner).
|
|
10
|
+
// Claude Managed Agents allows 128; a longer name is refused with the
|
|
11
|
+
// limit named.
|
|
12
|
+
// - Not a built-in tool's name on either engine, whatever its case, and
|
|
13
|
+
// not starting `mcp__`: on the Stigmer engine a custom tool is bound under
|
|
14
|
+
// its own name beside the engine's tools, and a model offered two tools
|
|
15
|
+
// of one name, or a name that reads as an MCP tool's, cannot tell which
|
|
16
|
+
// it calls. Managed Agents refuses `mcp__` names too.
|
|
17
|
+
// - Unique in the session, with a description the model reads.
|
|
18
|
+
// - An input schema that is a JSON Schema object (`{"type": "object"}`)
|
|
19
|
+
// the validator accepts: every model provider takes a tool's input as an
|
|
20
|
+
// object, and a schema the validator cannot read could never pass a call.
|
|
21
|
+
// - At most 32 tools and 64 KB of schemas in all, so the tool list the
|
|
22
|
+
// model reads stays within a turn's budget.
|
|
23
|
+
// - No top-level input property named like a secret (secret-args.ts:
|
|
24
|
+
// `password`, `token`, `auth`, ...): the runner redacts such an argument
|
|
25
|
+
// on every stored row, so the host would read `[REDACTED]` where the
|
|
26
|
+
// agent wrote a value, in its card, its callback and its own server's
|
|
27
|
+
// read of the log. The rule reads the schema's top-level `properties`
|
|
28
|
+
// only: a field named another way (through `allOf`, `anyOf`, `oneOf`,
|
|
29
|
+
// `if`/`then`, `dependentSchemas`, a top-level `$ref`, or `required`
|
|
30
|
+
// alone) is not refused and is still redacted, as is a property the model
|
|
31
|
+
// adds beyond those declared (where the schema allows extra properties).
|
|
32
|
+
// The rule runs wherever this check runs: where tools are declared
|
|
33
|
+
// (create and update) and each time the runner offers them, so a
|
|
34
|
+
// conversation that declared such a field before the rule is offered no
|
|
35
|
+
// custom tools from its next turn, and its next update is refused until
|
|
36
|
+
// the field is renamed.
|
|
37
|
+
//
|
|
38
|
+
// A call's input is checked against its tool's schema with
|
|
39
|
+
// `@cfworker/json-schema` (draft 2020-12), the validator the Stigmer
|
|
40
|
+
// engine's LangChain stack already uses, so a call the runner admits is one
|
|
41
|
+
// the schema accepts on every engine. The check runs under a time budget
|
|
42
|
+
// (CUSTOM_TOOL_CHECK_BUDGET_MS): a schema's `pattern` is the host's and the
|
|
43
|
+
// input is the model's, so a pattern that backtracks exponentially could
|
|
44
|
+
// otherwise hold the runner's one thread for minutes, its stall watchdog
|
|
45
|
+
// included. It runs through `node:vm` with a timeout, as the runner's hook
|
|
46
|
+
// matchers do (`shared/hooks/matcher.ts` in the runner): V8 checks for the
|
|
47
|
+
// interrupt while it backtracks, and a check stopped there refuses the
|
|
48
|
+
// call.
|
|
49
|
+
//
|
|
50
|
+
// Proven by __tests__/custom-tools.test.ts.
|
|
51
|
+
|
|
52
|
+
import { createContext, Script, type Context } from "node:vm";
|
|
53
|
+
|
|
54
|
+
import { Validator, type OutputUnit, type Schema } from "@cfworker/json-schema";
|
|
55
|
+
|
|
56
|
+
import { isSecretArgKey } from "./secret-args.js";
|
|
57
|
+
import { CLAUDE_TOOLS, CLAUDE_TOOL_ALIASES, STIGMER_TOOL_COVERS } from "./vocabulary.js";
|
|
58
|
+
|
|
59
|
+
/** One tool the host app answers itself, as a conversation declares it. */
|
|
60
|
+
export interface CustomToolDeclaration {
|
|
61
|
+
readonly name: string;
|
|
62
|
+
readonly description: string;
|
|
63
|
+
/** A JSON Schema object (`{"type": "object", ...}`). */
|
|
64
|
+
readonly inputSchema: unknown;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The limits a conversation's custom tools are held to (the header). */
|
|
68
|
+
export const CUSTOM_TOOL_LIMITS = {
|
|
69
|
+
maxTools: 32,
|
|
70
|
+
maxNameLength: 64,
|
|
71
|
+
maxDescriptionLength: 4096,
|
|
72
|
+
maxSchemaBytes: 64 * 1024,
|
|
73
|
+
} as const;
|
|
74
|
+
|
|
75
|
+
/** A custom tool's name: letters, digits, `_` and `-`, 1 to 64 (the header). */
|
|
76
|
+
export const CUSTOM_TOOL_NAME_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
77
|
+
|
|
78
|
+
/** The prefix every MCP tool's model-facing name starts with, which a custom tool's may not. */
|
|
79
|
+
const MCP_PREFIX = "mcp__";
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The engines' own tool names a custom tool may not take, lower-cased: every
|
|
83
|
+
* Claude Code built-in a Stigmer turn offers or a list may name (the
|
|
84
|
+
* engine's tool search and structured-output tools included), and every
|
|
85
|
+
* tool the Stigmer engine binds.
|
|
86
|
+
*/
|
|
87
|
+
const RESERVED_NAMES: ReadonlySet<string> = new Set(
|
|
88
|
+
[
|
|
89
|
+
...CLAUDE_TOOLS,
|
|
90
|
+
...CLAUDE_TOOL_ALIASES.keys(),
|
|
91
|
+
"ToolSearch",
|
|
92
|
+
"StructuredOutput",
|
|
93
|
+
...STIGMER_TOOL_COVERS.keys(),
|
|
94
|
+
"stop_background_command",
|
|
95
|
+
].map((name) => name.toLowerCase()),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
/** Whether a name is an engine's own tool's, whatever its case (the header). */
|
|
99
|
+
export function isReservedCustomToolName(name: string): boolean {
|
|
100
|
+
return RESERVED_NAMES.has(name.toLowerCase()) || name.toLowerCase().startsWith(MCP_PREFIX);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Why a conversation's custom tools cannot be declared, as one sentence
|
|
105
|
+
* naming the tool and the rule; undefined when every one may be.
|
|
106
|
+
*/
|
|
107
|
+
export function customToolsProblem(tools: readonly CustomToolDeclaration[]): string | undefined {
|
|
108
|
+
if (tools.length > CUSTOM_TOOL_LIMITS.maxTools) {
|
|
109
|
+
return `A conversation may declare at most ${CUSTOM_TOOL_LIMITS.maxTools} custom tools; this one declares ${tools.length}.`;
|
|
110
|
+
}
|
|
111
|
+
const seen = new Set<string>();
|
|
112
|
+
let schemaBytes = 0;
|
|
113
|
+
for (const [index, tool] of tools.entries()) {
|
|
114
|
+
const label = tool.name === "" ? `custom tool ${index + 1}` : `custom tool '${tool.name}'`;
|
|
115
|
+
if (tool.name.length > CUSTOM_TOOL_LIMITS.maxNameLength) {
|
|
116
|
+
return `The name of ${label} is ${tool.name.length} characters; a custom tool's name is at most ${CUSTOM_TOOL_LIMITS.maxNameLength}.`;
|
|
117
|
+
}
|
|
118
|
+
if (!CUSTOM_TOOL_NAME_PATTERN.test(tool.name)) {
|
|
119
|
+
return `The name of ${label} may hold only letters, digits, _ and -.`;
|
|
120
|
+
}
|
|
121
|
+
if (isReservedCustomToolName(tool.name)) {
|
|
122
|
+
return tool.name.toLowerCase().startsWith(MCP_PREFIX)
|
|
123
|
+
? `The name of ${label} may not start with mcp__, which names an MCP server's tools.`
|
|
124
|
+
: `The name of ${label} is a built-in tool's; choose another.`;
|
|
125
|
+
}
|
|
126
|
+
if (seen.has(tool.name)) return `Two custom tools are named '${tool.name}'; each name may appear once.`;
|
|
127
|
+
seen.add(tool.name);
|
|
128
|
+
if (tool.description.trim() === "") return `The description of ${label} is empty; the agent reads it to know when to call the tool.`;
|
|
129
|
+
if (tool.description.length > CUSTOM_TOOL_LIMITS.maxDescriptionLength) {
|
|
130
|
+
return `The description of ${label} is ${tool.description.length} characters; at most ${CUSTOM_TOOL_LIMITS.maxDescriptionLength}.`;
|
|
131
|
+
}
|
|
132
|
+
const schema = schemaProblem(tool.inputSchema);
|
|
133
|
+
if (schema !== undefined) return `The input_schema of ${label} ${schema}.`;
|
|
134
|
+
const secret = secretProperty(tool.inputSchema as Record<string, unknown>);
|
|
135
|
+
if (secret !== undefined) {
|
|
136
|
+
return `The input_schema of ${label} has a property named ${JSON.stringify(secret)}, a name whose value the platform redacts from every tool call, so the app would read [REDACTED] in its place; rename it to say what it holds (as "auth_method" says it for "auth").`;
|
|
137
|
+
}
|
|
138
|
+
schemaBytes += new TextEncoder().encode(JSON.stringify(tool.inputSchema)).length;
|
|
139
|
+
}
|
|
140
|
+
if (schemaBytes > CUSTOM_TOOL_LIMITS.maxSchemaBytes) {
|
|
141
|
+
return `The custom tools' input schemas hold ${schemaBytes} bytes in all; at most ${CUSTOM_TOOL_LIMITS.maxSchemaBytes}.`;
|
|
142
|
+
}
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Why a schema cannot be a custom tool's input schema, as the end of a sentence; undefined when it can. */
|
|
147
|
+
function schemaProblem(schema: unknown): string | undefined {
|
|
148
|
+
if (!isObject(schema)) return "must be a JSON Schema object";
|
|
149
|
+
if (schema.type !== "object") return 'must describe an object ({"type": "object", ...})';
|
|
150
|
+
const ref = unresolvedRef(schema, schema);
|
|
151
|
+
if (ref !== undefined) return `cannot be read as a JSON Schema: its $ref ${JSON.stringify(ref)} names nothing in the schema (a custom tool's schema is self-contained)`;
|
|
152
|
+
const pattern = badPattern(schema);
|
|
153
|
+
if (pattern !== undefined) return `cannot be read as a JSON Schema: its pattern ${JSON.stringify(pattern)} is not a regular expression`;
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The first top-level property `schema` declares whose name is a secret
|
|
159
|
+
* argument's (the header), as written; undefined when none is.
|
|
160
|
+
*/
|
|
161
|
+
function secretProperty(schema: Record<string, unknown>): string | undefined {
|
|
162
|
+
return isObject(schema.properties) ? Object.keys(schema.properties).find(isSecretArgKey) : undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The first `pattern` (or `patternProperties` key) in `node` the validator
|
|
167
|
+
* could not compile, as it compiles them (`new RegExp(p, "u")`); undefined
|
|
168
|
+
* when every one compiles. A schema is otherwise read only when a call
|
|
169
|
+
* reaches the keyword, so a bad one would refuse every call instead of the
|
|
170
|
+
* declaration.
|
|
171
|
+
*/
|
|
172
|
+
function badPattern(node: unknown): string | undefined {
|
|
173
|
+
if (Array.isArray(node)) return node.map(badPattern).find((found) => found !== undefined);
|
|
174
|
+
if (!isObject(node)) return undefined;
|
|
175
|
+
const patterns = [...(typeof node.pattern === "string" ? [node.pattern] : []), ...(isObject(node.patternProperties) ? Object.keys(node.patternProperties) : [])];
|
|
176
|
+
for (const pattern of patterns) {
|
|
177
|
+
try {
|
|
178
|
+
new RegExp(pattern, "u");
|
|
179
|
+
} catch {
|
|
180
|
+
return pattern;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return Object.values(node).map(badPattern).find((found) => found !== undefined);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Why a call's input does not match its custom tool's schema, as the
|
|
188
|
+
* sentence the model reads (it names the tool and every mismatch, and says
|
|
189
|
+
* the call did not run); undefined when it matches.
|
|
190
|
+
*/
|
|
191
|
+
export function customToolInputProblem(tool: Pick<CustomToolDeclaration, "name" | "inputSchema">, input: unknown): string | undefined {
|
|
192
|
+
let result: ReturnType<Validator["validate"]>;
|
|
193
|
+
try {
|
|
194
|
+
result = timedValidate(new Validator(tool.inputSchema as Schema, "2020-12", false), input);
|
|
195
|
+
} catch (err) {
|
|
196
|
+
if (isScriptTimeout(err)) {
|
|
197
|
+
return `The input could not be checked against ${tool.name}'s input schema in time (a pattern in the schema takes too long on this input); the call did not run.`;
|
|
198
|
+
}
|
|
199
|
+
return `The input could not be checked against ${tool.name}'s input schema (${String(err)}); the call did not run.`;
|
|
200
|
+
}
|
|
201
|
+
if (result.valid) return undefined;
|
|
202
|
+
const listed = mismatchesOf(result.errors);
|
|
203
|
+
return `The input does not match ${tool.name}'s input schema (${listed.join("; ")}); the call did not run. Call it again with input that matches.`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** The longest one input check may run, in milliseconds (the header). Real checks finish in well under one. */
|
|
207
|
+
export const CUSTOM_TOOL_CHECK_BUDGET_MS = 250;
|
|
208
|
+
|
|
209
|
+
/** The one context and script every timed check runs in, made on the first. */
|
|
210
|
+
let sandbox: { readonly context: Context & { validator?: Validator; input?: unknown }; readonly script: Script } | undefined;
|
|
211
|
+
|
|
212
|
+
/** One validation under the budget (the header); throws Node's timeout error when stopped there. */
|
|
213
|
+
function timedValidate(validator: Validator, input: unknown): ReturnType<Validator["validate"]> {
|
|
214
|
+
sandbox ??= { context: createContext({}), script: new Script("validator.validate(input)") };
|
|
215
|
+
sandbox.context.validator = validator;
|
|
216
|
+
sandbox.context.input = input;
|
|
217
|
+
try {
|
|
218
|
+
return sandbox.script.runInContext(sandbox.context, { timeout: CUSTOM_TOOL_CHECK_BUDGET_MS }) as ReturnType<Validator["validate"]>;
|
|
219
|
+
} finally {
|
|
220
|
+
delete sandbox.context.validator;
|
|
221
|
+
delete sandbox.context.input;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Node's timeout error comes from the context's realm, so it is read by its code, not by `instanceof`. */
|
|
226
|
+
function isScriptTimeout(err: unknown): boolean {
|
|
227
|
+
return typeof err === "object" && err !== null && (err as { code?: unknown }).code === "ERR_SCRIPT_EXECUTION_TIMEOUT";
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** The validator's keywords that only report that a nested one failed, which the nested error says better. */
|
|
231
|
+
const WRAPPER_KEYWORDS: ReadonlySet<string> = new Set(["properties", "items", "prefixItems", "allOf", "$ref", "false"]);
|
|
232
|
+
|
|
233
|
+
/** What `additionalProperties` reports for a property its schema does not take. */
|
|
234
|
+
const EXTRA_PROPERTY = /^Property "(.*)" does not match additional properties schema\.$/;
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Each mismatch once, where it is, in the model's words: wrapper errors
|
|
238
|
+
* dropped, and a property the schema does not take reported only when
|
|
239
|
+
* nothing more precise is said about it (the validator also reports a
|
|
240
|
+
* declared property that failed its own rule as an extra one).
|
|
241
|
+
*/
|
|
242
|
+
function mismatchesOf(errors: readonly OutputUnit[]): string[] {
|
|
243
|
+
const where = (location: string): string => (location === "#" ? "the input" : location.replace(/^#/, ""));
|
|
244
|
+
const precise = errors.filter((error) => !WRAPPER_KEYWORDS.has(error.keyword) && error.keyword !== "additionalProperties");
|
|
245
|
+
const said = new Set(precise.map((error) => error.instanceLocation));
|
|
246
|
+
const extras = errors.flatMap((error) => {
|
|
247
|
+
const name = error.keyword === "additionalProperties" ? EXTRA_PROPERTY.exec(error.error)?.[1] : undefined;
|
|
248
|
+
if (name === undefined) return [];
|
|
249
|
+
const location = `${error.instanceLocation}/${name}`;
|
|
250
|
+
return said.has(location) ? [] : [`${where(location)}: not a property this tool takes`];
|
|
251
|
+
});
|
|
252
|
+
const listed = [...precise.map((error) => `${where(error.instanceLocation)}: ${error.error}`), ...extras];
|
|
253
|
+
return (listed.length > 0 ? listed : errors.map((error) => error.error)).slice(0, 10);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** The first `$ref` in `node` that is not a JSON pointer into `root` naming something there; undefined when every one is. */
|
|
257
|
+
function unresolvedRef(node: unknown, root: Record<string, unknown>): string | undefined {
|
|
258
|
+
if (Array.isArray(node)) {
|
|
259
|
+
for (const item of node) {
|
|
260
|
+
const found = unresolvedRef(item, root);
|
|
261
|
+
if (found !== undefined) return found;
|
|
262
|
+
}
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
if (!isObject(node)) return undefined;
|
|
266
|
+
const ref = node.$ref;
|
|
267
|
+
if (ref !== undefined && (typeof ref !== "string" || !resolvesIn(root, ref))) return String(ref);
|
|
268
|
+
for (const value of Object.values(node)) {
|
|
269
|
+
const found = unresolvedRef(value, root);
|
|
270
|
+
if (found !== undefined) return found;
|
|
271
|
+
}
|
|
272
|
+
return undefined;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Whether `ref` is `#` or a JSON pointer (`#/...`) naming a value in `root`,
|
|
277
|
+
* through its own keys alone (an inherited `constructor` names nothing the
|
|
278
|
+
* validator finds), its escapes read as a pointer's.
|
|
279
|
+
*/
|
|
280
|
+
function resolvesIn(root: Record<string, unknown>, ref: string): boolean {
|
|
281
|
+
if (ref === "#") return true;
|
|
282
|
+
if (!ref.startsWith("#/")) return false;
|
|
283
|
+
let current: unknown = root;
|
|
284
|
+
for (const raw of ref.slice(2).split("/")) {
|
|
285
|
+
const key = pointerKey(raw);
|
|
286
|
+
if (key === undefined) return false;
|
|
287
|
+
if (Array.isArray(current)) {
|
|
288
|
+
// An array's step is an index it holds, never one of its own other keys (`length`).
|
|
289
|
+
if (!ARRAY_INDEX.test(key) || Number(key) >= current.length) return false;
|
|
290
|
+
current = current[Number(key)];
|
|
291
|
+
} else if (isObject(current) && Object.hasOwn(current, key)) {
|
|
292
|
+
current = current[key];
|
|
293
|
+
} else {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** A JSON pointer's array index: a non-negative integer with no leading zero. */
|
|
301
|
+
const ARRAY_INDEX = /^(0|[1-9][0-9]*)$/;
|
|
302
|
+
|
|
303
|
+
/** A pointer segment's key, its percent and tilde escapes undone; undefined for a malformed escape. */
|
|
304
|
+
function pointerKey(raw: string): string | undefined {
|
|
305
|
+
try {
|
|
306
|
+
return decodeURIComponent(raw).replace(/~1/g, "/").replace(/~0/g, "~");
|
|
307
|
+
} catch {
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
313
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
314
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
// The package entry: re-exports the tool vocabulary (vocabulary.ts)
|
|
2
|
-
// MCP server names (mcp-servers.ts)
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// The package entry: re-exports the tool vocabulary (vocabulary.ts), the
|
|
2
|
+
// MCP server names (mcp-servers.ts) and the secret argument names
|
|
3
|
+
// (secret-args.ts) that the runner, the control plane and the browser kits
|
|
4
|
+
// read, so none reaches into a file path. Nothing else
|
|
5
|
+
// lives here; the tables and their trade-offs are documented and pinned
|
|
6
|
+
// beside them. The custom-tool check is its own entry,
|
|
7
|
+
// `@stigmer/tool-vocabulary/custom-tools`: it runs a JSON Schema validator
|
|
8
|
+
// under `node:vm`, which neither a browser bundle can hold nor a kit needs.
|
|
5
9
|
|
|
6
10
|
export {
|
|
7
11
|
CLAUDE_TOOLS,
|
|
@@ -26,3 +30,4 @@ export {
|
|
|
26
30
|
accessTokenVariable,
|
|
27
31
|
isReservedMcpServerName,
|
|
28
32
|
} from "./mcp-servers.js";
|
|
33
|
+
export { SECRET_ARG_KEYS, isSecretArgKey } from "./secret-args.js";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// The tool-argument names whose values are secrets (stigmer#1119): the runner
|
|
2
|
+
// replaces a top-level argument under one of these names with a marker on
|
|
3
|
+
// every tool row's `args`, in its `args_preview` and in an approval message,
|
|
4
|
+
// so no reader of a conversation ever sees the value. Matched
|
|
5
|
+
// case-insensitively against the top-level key; a nested key, or a name that
|
|
6
|
+
// only contains one of these words (`auth_method`, `token_count`), is kept.
|
|
7
|
+
//
|
|
8
|
+
// One table for both sides that act on it: the runner redacts with it
|
|
9
|
+
// (`shared/args-preview.ts` in the runner), and the custom-tool check
|
|
10
|
+
// (custom-tools.ts) refuses a host tool whose input declares one of these
|
|
11
|
+
// names, since its host would read the marker where the agent wrote a value.
|
|
12
|
+
// `@stigmer/client` keeps its own copy for rows stored before the runner
|
|
13
|
+
// redacted (`isSecretArgKey`), because a client never depends on this
|
|
14
|
+
// package; the cross-language contract all three are held to is
|
|
15
|
+
// `test/fixtures/tool-view/secret-args.json`.
|
|
16
|
+
//
|
|
17
|
+
// Proven by __tests__/secret-args.test.ts.
|
|
18
|
+
|
|
19
|
+
/** Argument names whose top-level values are never shown to any reader (the header). */
|
|
20
|
+
export const SECRET_ARG_KEYS: ReadonlySet<string> = new Set([
|
|
21
|
+
"password",
|
|
22
|
+
"token",
|
|
23
|
+
"secret",
|
|
24
|
+
"api_key",
|
|
25
|
+
"apikey",
|
|
26
|
+
"credentials",
|
|
27
|
+
"auth",
|
|
28
|
+
"authorization",
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
/** Whether a top-level argument name is one whose value is redacted, whatever its case. */
|
|
32
|
+
export function isSecretArgKey(key: string): boolean {
|
|
33
|
+
return SECRET_ARG_KEYS.has(key.toLowerCase());
|
|
34
|
+
}
|