toolcraft 0.0.2 → 0.0.3
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 +458 -58
- package/dist/cli.compile-check.js +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +768 -40
- package/dist/human-in-loop/approval-tasks.d.ts +31 -0
- package/dist/human-in-loop/approval-tasks.js +201 -0
- package/dist/human-in-loop/approvals-commands.d.ts +11 -0
- package/dist/human-in-loop/approvals-commands.js +191 -0
- package/dist/human-in-loop/config.d.ts +11 -0
- package/dist/human-in-loop/config.js +21 -0
- package/dist/human-in-loop/default-provider.d.ts +2 -0
- package/dist/human-in-loop/default-provider.js +26 -0
- package/dist/human-in-loop/gate.d.ts +4 -0
- package/dist/human-in-loop/gate.js +57 -0
- package/dist/human-in-loop/index.d.ts +7 -0
- package/dist/human-in-loop/index.js +4 -0
- package/dist/human-in-loop/runner.d.ts +3 -0
- package/dist/human-in-loop/runner.js +196 -0
- package/dist/human-in-loop/spawn.d.ts +3 -0
- package/dist/human-in-loop/spawn.js +16 -0
- package/dist/human-in-loop/state-machine.d.ts +4 -0
- package/dist/human-in-loop/state-machine.js +10 -0
- package/dist/human-in-loop/types.d.ts +41 -0
- package/dist/human-in-loop/types.js +13 -0
- package/dist/index.compile-check.js +24 -0
- package/dist/index.d.ts +32 -13
- package/dist/index.js +82 -17
- package/dist/json-schema-converter.d.ts +21 -0
- package/dist/json-schema-converter.js +432 -0
- package/dist/mcp-proxy.d.ts +8 -0
- package/dist/mcp-proxy.js +383 -0
- package/dist/mcp.compile-check.js +1 -0
- package/dist/mcp.d.ts +2 -0
- package/dist/mcp.js +103 -11
- package/dist/sdk.compile-check.js +77 -0
- package/dist/sdk.d.ts +14 -5
- package/dist/sdk.js +57 -6
- package/dist/user-error.d.ts +3 -0
- package/dist/user-error.js +6 -0
- package/node_modules/@poe-code/agent-human-in-loop/README.md +42 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/index.d.ts +5 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/index.js +3 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/providers/mock.d.ts +2 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/providers/mock.js +11 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/providers/osascript-script.d.ts +4 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/providers/osascript-script.js +40 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/providers/osascript.d.ts +6 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/providers/osascript.js +33 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/request-approval.d.ts +4 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/request-approval.js +4 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/types.d.ts +14 -0
- package/node_modules/@poe-code/agent-human-in-loop/dist/types.js +1 -0
- package/node_modules/@poe-code/agent-human-in-loop/package.json +25 -0
- package/node_modules/@poe-code/agent-mcp-config/dist/apply.d.ts +6 -0
- package/node_modules/@poe-code/agent-mcp-config/dist/apply.js +175 -0
- package/node_modules/@poe-code/agent-mcp-config/dist/configs.d.ts +22 -0
- package/node_modules/@poe-code/agent-mcp-config/dist/configs.js +74 -0
- package/node_modules/@poe-code/agent-mcp-config/dist/index.d.ts +3 -0
- package/node_modules/@poe-code/agent-mcp-config/dist/index.js +2 -0
- package/node_modules/@poe-code/agent-mcp-config/dist/shapes.d.ts +31 -0
- package/node_modules/@poe-code/agent-mcp-config/dist/shapes.js +87 -0
- package/node_modules/@poe-code/agent-mcp-config/dist/types.d.ts +25 -0
- package/node_modules/@poe-code/agent-mcp-config/dist/types.js +1 -0
- package/node_modules/@poe-code/agent-mcp-config/package.json +25 -0
- package/node_modules/@poe-code/task-list/README.md +114 -0
- package/node_modules/@poe-code/task-list/dist/backends/markdown-dir.d.ts +2 -0
- package/node_modules/@poe-code/task-list/dist/backends/markdown-dir.js +466 -0
- package/node_modules/@poe-code/task-list/dist/backends/utils.d.ts +8 -0
- package/node_modules/@poe-code/task-list/dist/backends/utils.js +58 -0
- package/node_modules/@poe-code/task-list/dist/backends/yaml-file.d.ts +2 -0
- package/node_modules/@poe-code/task-list/dist/backends/yaml-file.js +444 -0
- package/node_modules/@poe-code/task-list/dist/index.d.ts +4 -0
- package/node_modules/@poe-code/task-list/dist/index.js +4 -0
- package/node_modules/@poe-code/task-list/dist/open.d.ts +3 -0
- package/node_modules/@poe-code/task-list/dist/open.js +34 -0
- package/node_modules/@poe-code/task-list/dist/schema/store.schema.json +32 -0
- package/node_modules/@poe-code/task-list/dist/schema/task.schema.json +33 -0
- package/node_modules/@poe-code/task-list/dist/state-machine.d.ts +16 -0
- package/node_modules/@poe-code/task-list/dist/state-machine.js +67 -0
- package/node_modules/@poe-code/task-list/dist/state.d.ts +29 -0
- package/node_modules/@poe-code/task-list/dist/state.js +61 -0
- package/node_modules/@poe-code/task-list/dist/types.d.ts +116 -0
- package/node_modules/@poe-code/task-list/dist/types.js +37 -0
- package/node_modules/@poe-code/task-list/package.json +26 -0
- package/package.json +22 -7
package/README.md
CHANGED
|
@@ -1,91 +1,491 @@
|
|
|
1
1
|
# toolcraft
|
|
2
2
|
|
|
3
|
-
tools for agents and humans
|
|
3
|
+
Create tools for both agents and humans.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Define a command once. Get a typed CLI, an MCP server, and a typed SDK from the same source. Built on `toolcraft-schema`.
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
You have a folder of one-off scripts and a couple of MCP servers. Each one re-derives its own argument parsing, env handling, and help text. Running them from a chatbot needs another adapter. Calling them from another script means subprocessing.
|
|
10
|
+
|
|
11
|
+
`toolcraft` is the consolidation step. You write each operation as one `defineCommand`, group them, and pick which surfaces to expose:
|
|
12
|
+
|
|
13
|
+
- `runCLI` — argv parsing, `--help`, kebab/snake flags, exit codes.
|
|
14
|
+
- `createMCPServer` / `runMCP` — JSON-RPC over stdio with auto-generated tool schemas.
|
|
15
|
+
- `createSDK` — typed in-process function calls.
|
|
16
|
+
|
|
17
|
+
Same handler runs everywhere. Schema, secrets, preconditions, and human-in-loop gating are declared once.
|
|
18
|
+
|
|
19
|
+
Building from an OpenAPI spec? Use [`toolcraft-openapi`](../toolcraft-openapi/) to generate toolcraft commands from the API contract.
|
|
20
|
+
|
|
21
|
+
## What the owner decides
|
|
22
|
+
|
|
23
|
+
Before writing toolcraft code, make a small tool map. For each script or MCP tool, write down:
|
|
24
|
+
|
|
25
|
+
- **Name** — the generic command path, like `issues.list` or `messages.send`.
|
|
26
|
+
- **Inputs** — params, defaults, and which ones should be positional CLI args.
|
|
27
|
+
- **Output** — the structured value the handler returns.
|
|
28
|
+
- **Secrets** — env vars or credentials the command needs.
|
|
29
|
+
- **Side effects** — files, APIs, databases, money, or user-visible changes.
|
|
30
|
+
- **Surfaces** — where it should appear: CLI, MCP, SDK, or all three.
|
|
31
|
+
- **Safety** — whether it needs approval, auth, or another precondition.
|
|
32
|
+
|
|
33
|
+
Keep the first migration boring:
|
|
34
|
+
|
|
35
|
+
1. Wrap existing scripts as thin `defineCommand` handlers.
|
|
36
|
+
2. Proxy existing MCP servers with `defineGroup({ mcp })` when you do not want to rewrite them yet.
|
|
37
|
+
3. Group commands by domain and put shared secrets or approvals on the group.
|
|
38
|
+
4. Add MCP scope only to tools that are safe and useful for agents.
|
|
39
|
+
5. Document exposed env vars and config options in the package README.
|
|
40
|
+
|
|
41
|
+
Once the tool map exists, the rest is mechanical: add commands to `root`, expose the same tree through CLI, MCP, and SDK, and remove old entrypoints when they are no longer needed.
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
npm install toolcraft toolcraft-schema
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Requires Node 20+.
|
|
50
|
+
|
|
51
|
+
## Hello world
|
|
8
52
|
|
|
9
53
|
```ts
|
|
10
|
-
|
|
11
|
-
import { S } from "toolcraft
|
|
54
|
+
// src/commands/greet.ts
|
|
55
|
+
import { defineCommand, S } from "toolcraft";
|
|
12
56
|
|
|
13
|
-
const
|
|
14
|
-
name: "
|
|
57
|
+
export const greet = defineCommand({
|
|
58
|
+
name: "greet",
|
|
59
|
+
description: "Say hello",
|
|
15
60
|
params: S.Object({
|
|
16
|
-
|
|
61
|
+
name: S.String({ description: "Who to greet" }),
|
|
62
|
+
loud: S.Optional(S.Boolean({ default: false })),
|
|
17
63
|
}),
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
},
|
|
64
|
+
handler: async ({ params }) => {
|
|
65
|
+
const message = `Hello, ${params.name}`;
|
|
66
|
+
return { message: params.loud ? message.toUpperCase() : message };
|
|
22
67
|
},
|
|
23
|
-
handler: async ({ params, secrets }) => ({
|
|
24
|
-
service: params.service,
|
|
25
|
-
authenticated: Boolean(secrets.apiKey),
|
|
26
|
-
}),
|
|
27
68
|
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
// src/root.ts
|
|
73
|
+
import { defineGroup } from "toolcraft";
|
|
74
|
+
import { greet } from "./commands/greet.js";
|
|
28
75
|
|
|
29
76
|
export const root = defineGroup({
|
|
30
|
-
name: "
|
|
31
|
-
children: [
|
|
77
|
+
name: "mytool",
|
|
78
|
+
children: [greet],
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
// src/bin.ts
|
|
84
|
+
#!/usr/bin/env node
|
|
85
|
+
import { runCLI } from "toolcraft/cli";
|
|
86
|
+
import { root } from "./root.js";
|
|
87
|
+
|
|
88
|
+
await runCLI(root, { version: "0.1.0" });
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
```sh
|
|
92
|
+
mytool greet --name world
|
|
93
|
+
mytool greet --name world --loud
|
|
94
|
+
mytool greet --help
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Project layout
|
|
98
|
+
|
|
99
|
+
A typical toolcraft project:
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
package.json
|
|
103
|
+
src/
|
|
104
|
+
bin.ts # one entrypoint, dispatches by argv (see below)
|
|
105
|
+
root.ts # defineGroup({ children: [...] })
|
|
106
|
+
commands/
|
|
107
|
+
greet.ts # one defineCommand per file
|
|
108
|
+
deploy.ts
|
|
109
|
+
...
|
|
110
|
+
groups/
|
|
111
|
+
issues/
|
|
112
|
+
index.ts # defineGroup, exports a sub-tree
|
|
113
|
+
list.ts
|
|
114
|
+
create.ts
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`package.json`:
|
|
118
|
+
|
|
119
|
+
```json
|
|
120
|
+
{
|
|
121
|
+
"name": "mytool",
|
|
122
|
+
"type": "module",
|
|
123
|
+
"bin": { "mytool": "./dist/bin.js" },
|
|
124
|
+
"scripts": { "build": "tsc" },
|
|
125
|
+
"dependencies": {
|
|
126
|
+
"toolcraft": "^0.0.1",
|
|
127
|
+
"toolcraft-schema": "^0.0.1"
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
`tsconfig.json` needs `"module": "NodeNext"` (or `"ESNext"`) and `"moduleResolution": "NodeNext"`.
|
|
133
|
+
|
|
134
|
+
## One binary, three runtimes
|
|
135
|
+
|
|
136
|
+
Most consumers ship one bin and dispatch on the first argv:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
// src/bin.ts
|
|
140
|
+
#!/usr/bin/env node
|
|
141
|
+
import { runCLI } from "toolcraft/cli";
|
|
142
|
+
import { runMCP } from "toolcraft/mcp";
|
|
143
|
+
import { root } from "./root.js";
|
|
144
|
+
|
|
145
|
+
const mode = process.argv[2];
|
|
146
|
+
|
|
147
|
+
if (mode === "mcp") {
|
|
148
|
+
await runMCP(root, { name: "mytool", version: "0.1.0" });
|
|
149
|
+
} else {
|
|
150
|
+
await runCLI(root, { version: "0.1.0" });
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
```sh
|
|
155
|
+
mytool greet --name world # CLI
|
|
156
|
+
mytool mcp # MCP stdio server (Claude Desktop, etc.)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
The SDK is a separate import for in-process callers — your library code, tests, other packages:
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
import { createSDK } from "toolcraft/sdk";
|
|
163
|
+
import { root } from "mytool/root";
|
|
164
|
+
|
|
165
|
+
const sdk = createSDK(root);
|
|
166
|
+
const { message } = await sdk.greet({ name: "world", loud: true });
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
The same `root` flows into all three. No duplication.
|
|
170
|
+
|
|
171
|
+
## Mental model
|
|
172
|
+
|
|
173
|
+
**Command**: one operation. Has a name, a `params` schema, optional `secrets`, a `handler`. The handler receives `{ params, secrets, fetch, fs, env, progress, ...services }` and returns a value.
|
|
174
|
+
|
|
175
|
+
**Group**: a folder. Has a name and `children`. Inheritable fields (`secrets`, `requires`, `scope`, `humanInLoop`) cascade to descendants. A group can also proxy an upstream MCP server (see below).
|
|
176
|
+
|
|
177
|
+
**Scope**: which runtimes a node is exposed on. Per-command default is `["cli", "sdk"]`. Set `scope: ["cli", "mcp", "sdk"]` to also surface as an MCP tool. Inherited from parent group when not set on the child.
|
|
178
|
+
|
|
179
|
+
**Tree**: the `root` group is a `defineGroup` whose children are commands and sub-groups. Any depth. CLI flags, MCP tool names, and SDK methods are derived from the path.
|
|
180
|
+
|
|
181
|
+
## Secrets
|
|
182
|
+
|
|
183
|
+
Declare env-backed secrets on a command or group. Toolcraft reads `process.env` at command-run time and passes the values to the handler:
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
const deploy = defineCommand({
|
|
187
|
+
name: "deploy",
|
|
188
|
+
params: S.Object({ service: S.String() }),
|
|
189
|
+
secrets: {
|
|
190
|
+
apiKey: { env: "DEPLOY_API_KEY", description: "Required for /deploy endpoint" },
|
|
191
|
+
debugToken: { env: "DEPLOY_DEBUG", optional: true },
|
|
192
|
+
},
|
|
193
|
+
handler: async ({ params, secrets }) => {
|
|
194
|
+
// secrets.apiKey: string
|
|
195
|
+
// secrets.debugToken: string | undefined
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Required secrets that aren't set produce a `UserError` with the env var name and description before the handler runs. Declaring `secrets` on a group cascades them down — a group-level `apiKey` is visible inside every descendant handler with full type inference.
|
|
201
|
+
|
|
202
|
+
## Preconditions (`requires`)
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
defineCommand({
|
|
206
|
+
// ...
|
|
207
|
+
requires: {
|
|
208
|
+
auth: true, // fails if POE_API_KEY (or runner-specified env) is missing
|
|
209
|
+
apiVersion: ">=1.2.0", // fails if runner reports older apiVersion
|
|
210
|
+
check: async (ctx) => ({ // arbitrary async gate
|
|
211
|
+
ok: ctx.fs.exists(".lock") === false,
|
|
212
|
+
message: ".lock present, refusing to run",
|
|
213
|
+
}),
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Runners pass `{ apiVersion }` to `runCLI` / `runMCP` / `createSDK` to populate the `apiVersion` check. Group-level `requires.check` runs before the child's; both must pass.
|
|
219
|
+
|
|
220
|
+
## Services (dependency injection)
|
|
221
|
+
|
|
222
|
+
Inject shared services (DB clients, loggers, fetch wrappers) once at the runtime boundary, get them in every handler:
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
type Services = { db: DbClient; logger: Logger };
|
|
226
|
+
|
|
227
|
+
const root = defineGroup<Services>({ ... });
|
|
228
|
+
|
|
229
|
+
await runCLI(root, {
|
|
230
|
+
services: { db, logger },
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Inside a handler:
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
defineCommand<Services>({
|
|
238
|
+
// ...
|
|
239
|
+
handler: async ({ params, db, logger }) => {
|
|
240
|
+
logger.info("running");
|
|
241
|
+
return db.query(params.id);
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Services are merged into the handler context alongside the built-ins (`fetch`, `fs`, `env`, `progress`).
|
|
247
|
+
|
|
248
|
+
## Output rendering
|
|
249
|
+
|
|
250
|
+
Handlers return raw values. Add per-format renderers when you want richer CLI output:
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
defineCommand({
|
|
254
|
+
// ...
|
|
255
|
+
handler: async () => ({ rows: [{ id: 1 }, { id: 2 }] }),
|
|
256
|
+
render: {
|
|
257
|
+
rich: (result, { renderTable }) =>
|
|
258
|
+
console.log(renderTable({ rows: result.rows, columns: ["id"] })),
|
|
259
|
+
markdown: (result) => `Found ${result.rows.length} rows`,
|
|
260
|
+
json: (result) => result,
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
CLI picks `rich` by default, `--json` switches to `json`. SDK and MCP always return the raw handler value.
|
|
266
|
+
|
|
267
|
+
## MCP proxy: adopt an existing MCP server
|
|
268
|
+
|
|
269
|
+
If you already run an upstream MCP (e.g. `github-mcp-server`) and you want a subset under your tree:
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
defineGroup({
|
|
273
|
+
name: "github",
|
|
274
|
+
mcp: {
|
|
275
|
+
transport: "stdio",
|
|
276
|
+
command: "github-mcp-server",
|
|
277
|
+
},
|
|
278
|
+
tools: ["create_issue", "list_issues"],
|
|
279
|
+
rename: {
|
|
280
|
+
create_issue: "issues.create",
|
|
281
|
+
},
|
|
282
|
+
children: [],
|
|
283
|
+
});
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
- `tools` filters by upstream tool name.
|
|
287
|
+
- `rename` remaps to dotted toolcraft paths; missing intermediate groups are created.
|
|
288
|
+
- Discovery is cached at `<projectRoot>/.toolcraft/mcp/<group>.json` (project root = nearest ancestor with `package.json`).
|
|
289
|
+
- `TOOLCRAFT_MCP_REFRESH=1` refreshes all proxies; `TOOLCRAFT_MCP_REFRESH=github,linear` refreshes specific ones.
|
|
290
|
+
- Discovery output goes to stderr only.
|
|
291
|
+
|
|
292
|
+
## Human-in-loop approvals
|
|
293
|
+
|
|
294
|
+
Gate destructive commands on a human approval. Configure on the command (or inherit from a group):
|
|
295
|
+
|
|
296
|
+
```ts
|
|
297
|
+
defineGroup({
|
|
298
|
+
name: "deploy",
|
|
299
|
+
humanInLoop: {
|
|
300
|
+
mode: "async",
|
|
301
|
+
message: ({ commandPath, params }) => `Run ${commandPath} for ${params.target}?`,
|
|
302
|
+
},
|
|
303
|
+
children: [
|
|
304
|
+
defineCommand({
|
|
305
|
+
name: "prod",
|
|
306
|
+
params: S.Object({ target: S.String() }),
|
|
307
|
+
handler: async ({ params }) => ({ target: params.target }),
|
|
308
|
+
}),
|
|
309
|
+
defineCommand({
|
|
310
|
+
name: "preview",
|
|
311
|
+
params: S.Object({ target: S.String() }),
|
|
312
|
+
humanInLoop: null, // opt out
|
|
313
|
+
handler: async ({ params }) => ({ target: params.target }),
|
|
314
|
+
}),
|
|
315
|
+
],
|
|
32
316
|
});
|
|
33
317
|
```
|
|
34
318
|
|
|
35
|
-
|
|
319
|
+
Modes:
|
|
320
|
+
|
|
321
|
+
- `sync` — handler waits for approval before running.
|
|
322
|
+
- `async` — toolcraft enqueues the command, returns a pending marker, and runs it in a fresh process when an operator approves via the reserved `approvals` group.
|
|
323
|
+
|
|
324
|
+
Wire the same `humanInLoop` options into every entrypoint:
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
const humanInLoop = {
|
|
328
|
+
provider: slackApprovalProvider({ channel: "#deploys", client }),
|
|
329
|
+
taskList: { dir: ".toolcraft/approvals.yaml", format: "yaml-file" as const },
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
await runCLI(root, { humanInLoop });
|
|
333
|
+
createMCPServer(root, { name: "mytool", version: "0.1.0", humanInLoop });
|
|
334
|
+
const sdk = createSDK(root, { humanInLoop });
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
If `provider` is omitted, toolcraft picks a default lazily on first use: `osascriptProvider` on macOS; otherwise a stub that throws `UserError("no human-in-loop provider configured for this platform")`.
|
|
338
|
+
|
|
339
|
+
A built-in `approvals` group is auto-merged into every root:
|
|
340
|
+
|
|
341
|
+
- `approvals list` — list pending tasks (CLI, MCP, SDK).
|
|
342
|
+
- `approvals show --approval-id <id>` — show one task.
|
|
343
|
+
- `approvals run --approval-id <id>` — execute one queued task. CLI-only; used by the detached runner.
|
|
36
344
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
- `
|
|
40
|
-
|
|
41
|
-
-
|
|
345
|
+
The name `approvals` is reserved. Defining your own `approvals` group fails at startup.
|
|
346
|
+
|
|
347
|
+
The async runner re-execs your binary (`process.execPath` + `process.argv[1]` by default; override via `humanInLoop.binPath`). Re-exec calls the same toolcraft entrypoint with the same `humanInLoop` options — do not branch on `argv` before calling `runCLI`/`runMCP`/`createSDK`.
|
|
348
|
+
|
|
349
|
+
Async results must be JSON-serializable; non-serializable returns mark the approval as failed instead of being persisted.
|
|
350
|
+
|
|
351
|
+
A minimal Slack-style provider:
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
import type { ApprovalRequest, ApprovalResult, HumanInLoopProvider } from "@poe-code/agent-human-in-loop";
|
|
355
|
+
|
|
356
|
+
export function slackApprovalProvider(opts: {
|
|
357
|
+
channel: string;
|
|
358
|
+
client: {
|
|
359
|
+
postApprovalMessage(channel: string, message: string): Promise<string>;
|
|
360
|
+
waitForButtonClick(ts: string): Promise<{ action: "approve" | "decline"; userId: string }>;
|
|
361
|
+
openModal(userId: string, prompt: string): Promise<string | undefined>;
|
|
362
|
+
};
|
|
363
|
+
}): HumanInLoopProvider {
|
|
364
|
+
return {
|
|
365
|
+
id: "slack-approval",
|
|
366
|
+
async requestApproval(request: ApprovalRequest): Promise<ApprovalResult> {
|
|
367
|
+
const ts = await opts.client.postApprovalMessage(opts.channel, request.message);
|
|
368
|
+
const click = await opts.client.waitForButtonClick(ts);
|
|
369
|
+
|
|
370
|
+
if (click.action === "approve") {
|
|
371
|
+
return { outcome: "approved" };
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (request.declineInputPrompt) {
|
|
375
|
+
const reason = await opts.client.openModal(click.userId, request.declineInputPrompt);
|
|
376
|
+
return reason ? { outcome: "declined", reason } : { outcome: "declined" };
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return { outcome: "declined" };
|
|
380
|
+
},
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
## Errors
|
|
386
|
+
|
|
387
|
+
Throw `UserError` for expected, user-facing failures. The CLI prints the message without a stack trace and sets exit code 1; MCP and SDK surface the message as the error body. Any other thrown error is treated as unexpected and shows a stack with `--verbose`.
|
|
388
|
+
|
|
389
|
+
## Migrating from a folder of scripts
|
|
390
|
+
|
|
391
|
+
Pattern for adopting toolcraft incrementally:
|
|
392
|
+
|
|
393
|
+
1. Pick one script. Wrap its logic in `defineCommand`. Keep the existing imports, `fetch` calls, file I/O — they all work inside a handler.
|
|
394
|
+
2. Move env-var reads to `secrets`. Replace `process.env.X` access in the script body with `secrets.x` from the handler context.
|
|
395
|
+
3. Add the command to a `defineGroup`. Repeat. The tree grows file by file.
|
|
396
|
+
4. When you're ready, point your `bin` at `runCLI` and delete the per-script entry points. The script files become handler implementations imported by `defineCommand`s.
|
|
397
|
+
5. To expose to an MCP client, set `scope: ["cli", "mcp", "sdk"]` on the command and add the `runMCP` branch to the bin. No code changes needed inside handlers.
|
|
398
|
+
6. To expose to other JS code, `import { createSDK }` from your package and call methods directly.
|
|
399
|
+
|
|
400
|
+
If you have an existing MCP server you want to keep running, use the MCP proxy: a `defineGroup` with an `mcp` field pulls its tools into your tree without rewriting them.
|
|
42
401
|
|
|
43
402
|
## Environment variables
|
|
44
403
|
|
|
45
|
-
-
|
|
46
|
-
-
|
|
404
|
+
- `TOOLCRAFT_MCP_REFRESH` — MCP proxy cache refresh (`unset` = use cache, `1`/`true` = refresh all, comma-separated names = refresh those).
|
|
405
|
+
- Per-command `secrets` declarations name additional env vars. They are read at command run time and passed to the handler.
|
|
47
406
|
|
|
48
|
-
##
|
|
407
|
+
## API reference
|
|
49
408
|
|
|
50
409
|
### `defineCommand(config)`
|
|
51
410
|
|
|
52
|
-
- `name: string
|
|
53
|
-
- `description?: string
|
|
54
|
-
- `aliases?: string[]
|
|
55
|
-
- `positional?: string[]
|
|
56
|
-
- `params: S.Object(...)
|
|
57
|
-
- `secrets?: Record<string, { env: string; description?: string; optional?: boolean }
|
|
58
|
-
- `scope?: Array<"cli" | "mcp" | "sdk"
|
|
59
|
-
- `confirm?: boolean
|
|
60
|
-
- `
|
|
61
|
-
- `
|
|
62
|
-
- `
|
|
411
|
+
- `name: string`
|
|
412
|
+
- `description?: string`
|
|
413
|
+
- `aliases?: string[]`
|
|
414
|
+
- `positional?: string[]` — parameter names mapped from CLI argv order.
|
|
415
|
+
- `params: S.Object(...)` — input schema from `toolcraft-schema`.
|
|
416
|
+
- `secrets?: Record<string, { env: string; description?: string; optional?: boolean }>`
|
|
417
|
+
- `scope?: Array<"cli" | "mcp" | "sdk">` — defaults to `["cli", "sdk"]`.
|
|
418
|
+
- `confirm?: boolean` — deprecated CLI-only TTY confirmation; use `humanInLoop` instead. Cannot be combined with `humanInLoop`.
|
|
419
|
+
- `humanInLoop?: { mode: "sync" | "async"; message: ({ params, commandPath }) => string; declineInputPrompt?: string } | null`
|
|
420
|
+
- `requires?: { auth?: boolean; apiVersion?: string; check?: (ctx) => Promise<{ ok: boolean; message?: string }> }`
|
|
421
|
+
- `handler: (ctx) => Promise<unknown>`
|
|
422
|
+
- `render?: { rich?, markdown?, json? }` — per-format output renderers.
|
|
63
423
|
|
|
64
424
|
### `defineGroup(config)`
|
|
65
425
|
|
|
66
|
-
- `name: string
|
|
67
|
-
- `description?: string
|
|
68
|
-
- `aliases?: string[]
|
|
69
|
-
- `
|
|
70
|
-
- `
|
|
71
|
-
- `
|
|
72
|
-
- `
|
|
73
|
-
- `
|
|
426
|
+
- `name: string`
|
|
427
|
+
- `description?: string`
|
|
428
|
+
- `aliases?: string[]`
|
|
429
|
+
- `mcp?: McpServerConfig` — proxy an upstream MCP server; uses the standard `@poe-code/agent-mcp-config` shape.
|
|
430
|
+
- `tools?: string[]` — proxy allowlist by upstream tool name.
|
|
431
|
+
- `rename?: Record<string, string>` — proxy upstream → dotted toolcraft path.
|
|
432
|
+
- `scope?` / `humanInLoop?` / `secrets?` / `requires?` — inherited by descendants that don't override. Set `humanInLoop: null` on a child to opt out.
|
|
433
|
+
- `children: Array<Command | Group>`
|
|
434
|
+
- `default?: Command` — invoked when no child token matches.
|
|
435
|
+
|
|
436
|
+
### `runCLI(root, options)`
|
|
437
|
+
|
|
438
|
+
- `casing?: "kebab" | "snake"` — generated CLI flag style.
|
|
439
|
+
- `services?: TServices` — merged into every handler context.
|
|
440
|
+
- `version?: string` — surfaced via `--version`.
|
|
441
|
+
- `apiVersion?: string` — for `requires.apiVersion`.
|
|
442
|
+
- `humanInLoop?: HumanInLoopRuntimeOptions`
|
|
443
|
+
|
|
444
|
+
### `createSDK(root, options)`
|
|
445
|
+
|
|
446
|
+
- `casing?: "camel"` — generated SDK member style.
|
|
447
|
+
- `services?` / `humanInLoop?` / `apiVersion?`
|
|
74
448
|
|
|
75
449
|
### `createMCPServer(root, options)` / `runMCP(root, options)`
|
|
76
450
|
|
|
77
|
-
- `name: string
|
|
78
|
-
- `version: string
|
|
79
|
-
- `services
|
|
80
|
-
- `tools?: string[]
|
|
81
|
-
- `casing?: "snake" | "camel"
|
|
451
|
+
- `name: string`
|
|
452
|
+
- `version: string`
|
|
453
|
+
- `services?` / `humanInLoop?` / `apiVersion?`
|
|
454
|
+
- `tools?: string[]` — allowlist of MCP tool names or group prefixes. Tool names are `__`-joined snake_case path segments (`root__bot__create`); a prefix like `root__bot` includes every descendant tool.
|
|
455
|
+
- `casing?: "snake" | "camel"` — affects MCP **input-schema property names** only. Tool names always stay `__`-joined snake_case.
|
|
456
|
+
|
|
457
|
+
### `HumanInLoopRuntimeOptions`
|
|
458
|
+
|
|
459
|
+
```ts
|
|
460
|
+
type HumanInLoopRuntimeOptions = {
|
|
461
|
+
provider?: HumanInLoopProvider;
|
|
462
|
+
taskList?: TaskList | { dir: string; format: "markdown-dir" | "yaml-file" };
|
|
463
|
+
listName?: string; // defaults to "approvals"
|
|
464
|
+
binPath?: { execPath: string; entryArgs: readonly string[] };
|
|
465
|
+
};
|
|
466
|
+
```
|
|
82
467
|
|
|
83
468
|
### Handler context
|
|
84
469
|
|
|
85
|
-
- `params
|
|
86
|
-
- `secrets
|
|
87
|
-
- `fetch
|
|
88
|
-
- `fs
|
|
89
|
-
- `env
|
|
90
|
-
- `progress(message: string): void
|
|
91
|
-
-
|
|
470
|
+
- `params` — inferred from the command `params` schema.
|
|
471
|
+
- `secrets` — inferred from the command `secrets` declaration.
|
|
472
|
+
- `fetch: typeof globalThis.fetch`
|
|
473
|
+
- `fs: { readFile, writeFile, exists }`
|
|
474
|
+
- `env: { get(key: string): string | undefined }`
|
|
475
|
+
- `progress(message: string): void`
|
|
476
|
+
- All `services` keys merged in.
|
|
477
|
+
|
|
478
|
+
### Exports
|
|
479
|
+
|
|
480
|
+
- `defineCommand`, `defineGroup`
|
|
481
|
+
- `S`, `toJsonSchema`, type helpers — re-exported from `toolcraft-schema`
|
|
482
|
+
- `UserError`, `ApprovalDeclinedError`
|
|
483
|
+
- Type exports: `Command`, `Group`, `Scope`, `HandlerContext`, `HumanInLoopConfig`, `HumanInLoopPending`, `HumanInLoopRuntimeOptions`, schema types from `toolcraft-schema`.
|
|
484
|
+
|
|
485
|
+
Subpath imports:
|
|
486
|
+
|
|
487
|
+
- `toolcraft/cli` — `runCLI`
|
|
488
|
+
- `toolcraft/sdk` — `createSDK`
|
|
489
|
+
- `toolcraft/mcp` — `runMCP`, `createMCPServer`
|
|
490
|
+
- `toolcraft/human-in-loop` — provider helpers
|
|
491
|
+
- `toolcraft/mcp-proxy` — proxy internals
|
package/dist/cli.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { Group } from "./index.js";
|
|
2
|
+
import type { HumanInLoopRuntimeOptions } from "./human-in-loop/index.js";
|
|
2
3
|
type Casing = "kebab" | "snake";
|
|
3
4
|
export interface RunCLIOptions<TServices extends object = Record<string, unknown>> {
|
|
4
5
|
apiVersion?: string;
|
|
5
6
|
casing?: Casing;
|
|
7
|
+
humanInLoop?: HumanInLoopRuntimeOptions;
|
|
6
8
|
rootDisplayName?: string;
|
|
7
9
|
rootUsageName?: string;
|
|
8
10
|
services?: TServices;
|