toolfunnel 0.4.0 → 0.6.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.
Files changed (78) hide show
  1. package/README.md +102 -213
  2. package/bin/toolfunnel.js +372 -147
  3. package/docs/KNOWN_BUGS.md +99 -225
  4. package/docs/MANUAL.pdf +0 -0
  5. package/docs/design.md +70 -0
  6. package/docs/packaging.md +115 -115
  7. package/hooks/hooks.manifest.json +27 -27
  8. package/hooks/scripts/example-audit.js +21 -21
  9. package/hooks/scripts/example-deny-dangerous.js +31 -31
  10. package/mcp/expose.example.json +30 -30
  11. package/mcp/servers/mock-modern/server.js +147 -0
  12. package/mcp/servers/mock-upstream/server.js +192 -12
  13. package/package.json +1 -1
  14. package/src/auth/config.js +174 -174
  15. package/src/auth/install.js +181 -181
  16. package/src/auth/resource-server.js +344 -344
  17. package/src/core/config-home.js +134 -134
  18. package/src/core/events.js +105 -105
  19. package/src/core/hook-engine.js +275 -275
  20. package/src/core/hook-loader.js +791 -791
  21. package/src/core/hook-runner.js +346 -346
  22. package/src/core/logger.js +171 -171
  23. package/src/core/matcher.js +51 -51
  24. package/src/core/metrics.js +75 -75
  25. package/src/core/requires.js +138 -138
  26. package/src/core/server-config.js +98 -77
  27. package/src/core/tool-registry.js +180 -180
  28. package/src/extend/add-hook.md +152 -142
  29. package/src/extend/add-mcp.md +122 -121
  30. package/src/extend/configure.md +87 -0
  31. package/src/extend/create-tool.md +168 -152
  32. package/src/extend/howto.js +102 -98
  33. package/src/extend/package.md +187 -187
  34. package/src/extend/wrap.md +99 -0
  35. package/src/mcp/aggregator.js +1246 -807
  36. package/src/mcp/expose-store.js +460 -430
  37. package/src/mcp/gated-run.js +201 -194
  38. package/src/mcp/http-transport.js +1311 -981
  39. package/src/mcp/mcp-client.js +907 -530
  40. package/src/mcp/modern.js +372 -0
  41. package/src/mcp/protocol.js +468 -466
  42. package/src/mcp/server.js +2688 -1322
  43. package/src/packages/packager.js +715 -715
  44. package/src/tools/drift.js +138 -138
  45. package/src/tools/registry.js +513 -511
  46. package/src/tools/tool-state.js +170 -104
  47. package/src/ui/public/app.js +1174 -1055
  48. package/src/ui/public/index.html +286 -249
  49. package/src/ui/public/styles.css +535 -535
  50. package/src/ui/server.js +1348 -1190
  51. package/tools/scripts/base64.js +139 -139
  52. package/tools/scripts/danger.js +61 -61
  53. package/tools/scripts/echo.js +39 -39
  54. package/tools/scripts/hash.js +101 -101
  55. package/tools/scripts/json.js +109 -109
  56. package/tools/scripts/text-stats.js +131 -131
  57. package/tools/scripts/tf-env.js +43 -43
  58. package/tools/scripts/tf-hook-add.js +151 -151
  59. package/tools/scripts/tf-hook-set.js +112 -112
  60. package/tools/scripts/tf-list.js +192 -192
  61. package/tools/scripts/tf-log.js +86 -86
  62. package/tools/scripts/tf-mcp-add.js +117 -117
  63. package/tools/scripts/tf-mcp-set.js +121 -102
  64. package/tools/scripts/tf-pack.js +237 -237
  65. package/tools/scripts/tf-tool-add.js +134 -129
  66. package/tools/scripts/tf-tool-set.js +142 -142
  67. package/tools/scripts/tf-wrap.js +103 -0
  68. package/tools/scripts/uuid.js +102 -102
  69. package/tools/tools.register.json +227 -216
  70. package/docs/admin-ui.md +0 -404
  71. package/docs/configuration.md +0 -402
  72. package/docs/hooks-and-gating.md +0 -428
  73. package/docs/logging.md +0 -223
  74. package/docs/management.md +0 -429
  75. package/docs/oauth.md +0 -103
  76. package/docs/releasing.md +0 -31
  77. package/docs/setup.md +0 -352
  78. package/docs/tools-and-upstreams.md +0 -469
@@ -1,112 +1,112 @@
1
- #!/usr/bin/env node
2
- 'use strict';
3
-
4
- /**
5
- * tf-hook-set.js — management tool: enable, disable, or remove a hook entry.
6
- *
7
- * A first-party "gateway-run" management function: it configures ToolFunnel's
8
- * OWN hook manifest / enabled-state overlay. Discovered via list_tools and
9
- * executed via run_tool through the PreToolUse gate — it is NOT a new
10
- * MCP-protocol command.
11
- *
12
- * Contract with the register (`defaultRunScript`)
13
- * ----------------------------------------------------------
14
- * - Structured args arrive as JSON in env TOOLFUNNEL_TOOL_ARGS ({} if absent).
15
- * - Args: { id, action: "enable" | "disable" | "remove" }.
16
- * - Prints EXACTLY ONE JSON object to stdout and exits 0 — ALWAYS exit 0, even
17
- * on a logical error, which is reported as { ok:false, error }.
18
- *
19
- * Output (stdout), exactly one JSON object:
20
- * enable/disable: { ok:true, id, action, enabled }
21
- * remove: { ok:true, id, action, removed }
22
- * failure: { ok:false, error }
23
- *
24
- * Mapping (verified against src/core/hook-loader.js):
25
- * - enable → loader.setEnabled(id, true) (persists the state overlay + manifest)
26
- * - disable → loader.setEnabled(id, false)
27
- * - remove → loader.removeEntry(id) (returns false if the id was absent;
28
- * that is information, not a fault, so ok stays true)
29
- *
30
- * Safety invariants:
31
- * - Touches only ToolFunnel's own manifest / state (resolved from __dirname).
32
- * - NO network. NEVER throws for ordinary conditions — only JSON on stdout.
33
- */
34
-
35
- const path = require('node:path');
36
- // Shared HOME/engine resolution (see tf-env.js): config beside us, engine from the package.
37
- const { HOME, srcRequire } = require('./tf-env');
38
- const { loadManifest } = srcRequire('core/hook-loader');
39
-
40
- const ROOT = HOME;
41
-
42
- /** The hook manifest, relative to ROOT. */
43
- const MANIFEST = path.join(ROOT, 'hooks', 'hooks.manifest.json');
44
-
45
- /**
46
- * Parse the structured args handed in via env TOOLFUNNEL_TOOL_ARGS. Absent/empty
47
- * is treated as an empty object. Returns the parsed value or a parse-error
48
- * sentinel so the caller surfaces a clean { ok:false, error }.
49
- *
50
- * @returns {{ value: any } | { parseError: string }}
51
- */
52
- function parseStructuredArgs() {
53
- const raw = process.env.TOOLFUNNEL_TOOL_ARGS;
54
- if (raw === undefined || raw === null || raw === '') {
55
- return { value: {} };
56
- }
57
- try {
58
- return { value: JSON.parse(raw) };
59
- } catch (_err) {
60
- return { parseError: `TOOLFUNNEL_TOOL_ARGS was not valid JSON: ${String(raw)}` };
61
- }
62
- }
63
-
64
- /**
65
- * Pure-ish core: validate args and apply the requested action. Never throws for
66
- * ordinary conditions (any loader throw is caught in main).
67
- *
68
- * @param {any} args
69
- * @returns {object} the JSON-serialisable result
70
- */
71
- function run(args) {
72
- if (!args || typeof args !== 'object' || Array.isArray(args)) {
73
- return { ok: false, error: 'args must be an object { id, action }' };
74
- }
75
-
76
- const { id, action } = args;
77
- if (typeof id !== 'string' || id.length === 0) {
78
- return { ok: false, error: 'id must be a non-empty string' };
79
- }
80
- if (action !== 'enable' && action !== 'disable' && action !== 'remove') {
81
- return { ok: false, error: 'action must be one of "enable", "disable", "remove"' };
82
- }
83
-
84
- const loader = loadManifest(MANIFEST);
85
-
86
- if (action === 'remove') {
87
- const removed = loader.removeEntry(id);
88
- return { ok: true, id, action, removed };
89
- }
90
-
91
- const desired = action === 'enable';
92
- loader.setEnabled(id, desired);
93
- return { ok: true, id, action, enabled: desired };
94
- }
95
-
96
- function main() {
97
- const parsed = parseStructuredArgs();
98
- let payload;
99
- if (parsed.parseError) {
100
- payload = { ok: false, error: parsed.parseError };
101
- } else {
102
- try {
103
- payload = run(parsed.value);
104
- } catch (err) {
105
- payload = { ok: false, error: (err && err.message) || String(err) };
106
- }
107
- }
108
- process.stdout.write(JSON.stringify(payload) + '\n');
109
- process.exit(0);
110
- }
111
-
112
- main();
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ /**
5
+ * tf-hook-set.js - management tool: enable, disable, or remove a hook entry.
6
+ *
7
+ * A first-party "gateway-run" management function: it configures ToolFunnel's
8
+ * OWN hook manifest / enabled-state overlay. Discovered via list_tools and
9
+ * executed via run_tool through the PreToolUse gate - it is NOT a new
10
+ * MCP-protocol command.
11
+ *
12
+ * Contract with the register (`defaultRunScript`)
13
+ * ----------------------------------------------------------
14
+ * - Structured args arrive as JSON in env TOOLFUNNEL_TOOL_ARGS ({} if absent).
15
+ * - Args: { id, action: "enable" | "disable" | "remove" }.
16
+ * - Prints EXACTLY ONE JSON object to stdout and exits 0 - ALWAYS exit 0, even
17
+ * on a logical error, which is reported as { ok:false, error }.
18
+ *
19
+ * Output (stdout), exactly one JSON object:
20
+ * enable/disable: { ok:true, id, action, enabled }
21
+ * remove: { ok:true, id, action, removed }
22
+ * failure: { ok:false, error }
23
+ *
24
+ * Mapping (verified against src/core/hook-loader.js):
25
+ * - enable -> loader.setEnabled(id, true) (persists the state overlay + manifest)
26
+ * - disable -> loader.setEnabled(id, false)
27
+ * - remove -> loader.removeEntry(id) (returns false if the id was absent;
28
+ * that is information, not a fault, so ok stays true)
29
+ *
30
+ * Safety invariants:
31
+ * - Touches only ToolFunnel's own manifest / state (resolved from __dirname).
32
+ * - NO network. NEVER throws for ordinary conditions - only JSON on stdout.
33
+ */
34
+
35
+ const path = require('node:path');
36
+ // Shared HOME/engine resolution (see tf-env.js): config beside us, engine from the package.
37
+ const { HOME, srcRequire } = require('./tf-env');
38
+ const { loadManifest } = srcRequire('core/hook-loader');
39
+
40
+ const ROOT = HOME;
41
+
42
+ /** The hook manifest, relative to ROOT. */
43
+ const MANIFEST = path.join(ROOT, 'hooks', 'hooks.manifest.json');
44
+
45
+ /**
46
+ * Parse the structured args handed in via env TOOLFUNNEL_TOOL_ARGS. Absent/empty
47
+ * is treated as an empty object. Returns the parsed value or a parse-error
48
+ * sentinel so the caller surfaces a clean { ok:false, error }.
49
+ *
50
+ * @returns {{ value: any } | { parseError: string }}
51
+ */
52
+ function parseStructuredArgs() {
53
+ const raw = process.env.TOOLFUNNEL_TOOL_ARGS;
54
+ if (raw === undefined || raw === null || raw === '') {
55
+ return { value: {} };
56
+ }
57
+ try {
58
+ return { value: JSON.parse(raw) };
59
+ } catch (_err) {
60
+ return { parseError: `TOOLFUNNEL_TOOL_ARGS was not valid JSON: ${String(raw)}` };
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Pure-ish core: validate args and apply the requested action. Never throws for
66
+ * ordinary conditions (any loader throw is caught in main).
67
+ *
68
+ * @param {any} args
69
+ * @returns {object} the JSON-serialisable result
70
+ */
71
+ function run(args) {
72
+ if (!args || typeof args !== 'object' || Array.isArray(args)) {
73
+ return { ok: false, error: 'args must be an object { id, action }' };
74
+ }
75
+
76
+ const { id, action } = args;
77
+ if (typeof id !== 'string' || id.length === 0) {
78
+ return { ok: false, error: 'id must be a non-empty string' };
79
+ }
80
+ if (action !== 'enable' && action !== 'disable' && action !== 'remove') {
81
+ return { ok: false, error: 'action must be one of "enable", "disable", "remove"' };
82
+ }
83
+
84
+ const loader = loadManifest(MANIFEST);
85
+
86
+ if (action === 'remove') {
87
+ const removed = loader.removeEntry(id);
88
+ return { ok: true, id, action, removed };
89
+ }
90
+
91
+ const desired = action === 'enable';
92
+ loader.setEnabled(id, desired);
93
+ return { ok: true, id, action, enabled: desired };
94
+ }
95
+
96
+ function main() {
97
+ const parsed = parseStructuredArgs();
98
+ let payload;
99
+ if (parsed.parseError) {
100
+ payload = { ok: false, error: parsed.parseError };
101
+ } else {
102
+ try {
103
+ payload = run(parsed.value);
104
+ } catch (err) {
105
+ payload = { ok: false, error: (err && err.message) || String(err) };
106
+ }
107
+ }
108
+ process.stdout.write(JSON.stringify(payload) + '\n');
109
+ process.exit(0);
110
+ }
111
+
112
+ main();
@@ -1,192 +1,192 @@
1
- #!/usr/bin/env node
2
- 'use strict';
3
-
4
- /**
5
- * tf-list.js — a READ-ONLY management inventory tool (category "management").
6
- *
7
- * Purpose
8
- * -------
9
- * One first-party register function that answers "what is configured, and what is
10
- * its live state?" across the three stores the gateway owns:
11
- * - tools → the persisted Tool Register + the ACTIVE/DISABLED + hidden overlay.
12
- * - mcps → the expose store: upstream MCPs and their curated-direct exposes.
13
- * - hooks → the hook manifest + the per-hook enabled-state overlay.
14
- *
15
- * It is discovered via list_tools and executed via run_tool through the PreToolUse
16
- * gate. It does NOT add new MCP-protocol commands; it is a "gateway-run" script
17
- * that reads the gateway's own config files. READ-ONLY: no writes, no network,
18
- * no process mutation.
19
- *
20
- * Contract with the register (registry.js `defaultRunScript`)
21
- * -----------------------------------------------------------
22
- * - Structured args arrive as a JSON string in env TOOLFUNNEL_TOOL_ARGS ({} if
23
- * absent). The only arg is { kind: "tools" | "mcps" | "hooks" }.
24
- * - Prints EXACTLY ONE JSON line to stdout and exits 0 — ALWAYS exit 0. A bad
25
- * kind, a missing/malformed config, or any other logical error is reported as
26
- * { ok:false, error } (still exit 0), never a crash.
27
- *
28
- * Output (stdout), exactly one JSON object:
29
- * success: { ok:true, kind, items:[ ... ] }
30
- * failure: { ok:false, error }
31
- *
32
- * Per-kind item shapes:
33
- * tools → register brief { id, name, summary, category } annotated with the
34
- * overlay-derived { enabled, hidden }.
35
- * mcps → one item per upstream MCP (its full config incl. `enabled`) with its
36
- * curated-direct `exposed` entries nested (each carrying its resolved
37
- * downstream `name` and `enabled` flag). Uses listUpstreams() +
38
- * listExposed() and groups exposes under their owning upstream.
39
- * hooks → one item per manifest hook (full spec) with `enabled` resolved from
40
- * the persisted overlay (readState) when present, else the manifest flag.
41
- *
42
- * Safety invariants (defense-in-depth; mirror the isolation rule):
43
- * - READ-ONLY: only reads config files INSIDE the toolfunnel root, resolved from
44
- * THIS script's own location (__dirname) — never a caller-supplied path.
45
- * - NO network, NO writes, NO process mutation.
46
- * - NEVER throws for ordinary conditions — only well-shaped JSON on stdout.
47
- */
48
-
49
- const path = require('node:path');
50
-
51
- // HOME/engine resolution shared by every tf-* script (see tf-env.js): the config lives beside
52
- // this script (the git-clone root OR a seeded external config home); the engine code is required
53
- // from the package that spawned us when it is not local.
54
- const { HOME, srcRequire } = require('./tf-env');
55
- const { loadRegistry } = srcRequire('tools/registry');
56
- const { loadToolState, isToolEnabled, isToolHidden } = srcRequire('tools/tool-state');
57
- const { loadExposeStore } = srcRequire('mcp/expose-store');
58
- const { loadManifest } = srcRequire('core/hook-loader');
59
-
60
- const ROOT = HOME;
61
-
62
- // Canonical store paths (see the script contract).
63
- const REGISTER_PATH = path.join(ROOT, 'tools', 'tools.register.json');
64
- const STATE_PATH = path.join(ROOT, 'tools', 'tools.state.json');
65
- const EXPOSE_PATH = path.join(ROOT, 'mcp', 'expose.json');
66
- const MANIFEST_PATH = path.join(ROOT, 'hooks', 'hooks.manifest.json');
67
- const SCRIPTS_ROOT = path.join(ROOT, 'tools', 'scripts');
68
-
69
- /**
70
- * Parse the structured args from env. Never throws: a missing/blank/malformed
71
- * TOOLFUNNEL_TOOL_ARGS yields {} so the kind check below produces a clean
72
- * { ok:false, error } rather than a crash.
73
- * @returns {object}
74
- */
75
- function readArgs() {
76
- const raw = process.env.TOOLFUNNEL_TOOL_ARGS;
77
- if (typeof raw !== 'string' || raw.length === 0) return {};
78
- try {
79
- const parsed = JSON.parse(raw);
80
- return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {};
81
- } catch (_err) {
82
- return {};
83
- }
84
- }
85
-
86
- /**
87
- * tools → register briefs annotated with the overlay's enabled + hidden state. HIDDEN tools are a
88
- * manager-view declutter: they are OMITTED by default and only included when args.includeHidden is
89
- * true. (This is the manager surface only — `hidden` never affects the lean list / top-level surface
90
- * the connected AI sees.)
91
- * @param {object} [args] { includeHidden?: boolean }
92
- * @returns {Array<{id,name,summary,category,enabled,hidden}>}
93
- */
94
- function listTools(args) {
95
- const includeHidden = !!(args && args.includeHidden === true);
96
- const registry = loadRegistry(REGISTER_PATH, { scriptsRoot: SCRIPTS_ROOT });
97
- const state = loadToolState(STATE_PATH);
98
- return registry.list()
99
- .map((brief) => ({
100
- ...brief,
101
- enabled: isToolEnabled(state, brief.id),
102
- hidden: isToolHidden(state, brief.id),
103
- }))
104
- .filter((t) => includeHidden || !t.hidden); // declutter: omit hidden unless explicitly asked
105
- }
106
-
107
- /**
108
- * mcps → one item per upstream MCP, with its curated-direct exposes nested.
109
- * Uses listUpstreams() for the connection config (incl. `enabled`) and
110
- * listExposed() for the surfaced tools, grouping each expose under its owning
111
- * upstream and resolving the downstream name via exposedName().
112
- * @returns {Array<object>}
113
- */
114
- function listMcps() {
115
- const store = loadExposeStore(EXPOSE_PATH);
116
- const upstreams = store.listUpstreams();
117
- const exposed = store.listExposed();
118
-
119
- // Group exposes by their upstream id (every expose references an existing
120
- // upstream — addExpose requires it and removeUpstream cascades — but default
121
- // to an empty list so an unmatched entry never throws).
122
- const byUpstream = new Map();
123
- for (const e of exposed) {
124
- const annotated = { ...e, name: store.exposedName(e) };
125
- const bucket = byUpstream.get(e.upstream);
126
- if (bucket) bucket.push(annotated);
127
- else byUpstream.set(e.upstream, [annotated]);
128
- }
129
-
130
- return upstreams.map((u) => ({
131
- ...u,
132
- exposed: byUpstream.get(u.id) || [],
133
- }));
134
- }
135
-
136
- /**
137
- * hooks → one item per manifest hook with `enabled` resolved from the persisted
138
- * overlay (readState) when it carries the hook id, else the manifest's own flag.
139
- * loadManifest already applies the overlay over the in-memory specs, but we read
140
- * the overlay explicitly so the resolved `enabled` is correct regardless of that.
141
- * @returns {Array<object>}
142
- */
143
- function listHooks() {
144
- const loader = loadManifest(MANIFEST_PATH);
145
- const overlay = loader.readState();
146
- return loader.hooks.map((h) => {
147
- const hasOverride = h && typeof h.id === 'string' &&
148
- Object.prototype.hasOwnProperty.call(overlay, h.id);
149
- const enabled = hasOverride ? overlay[h.id] === true : h.enabled === true;
150
- return { ...h, enabled };
151
- });
152
- }
153
-
154
- /**
155
- * Build the inventory for the requested kind. Throws on an unknown kind so main()
156
- * can surface it as { ok:false, error }.
157
- * @param {object} args
158
- * @returns {{ ok:true, kind:string, items:Array<object> }}
159
- */
160
- function build(args) {
161
- const kind = args && args.kind;
162
- let items;
163
- switch (kind) {
164
- case 'tools':
165
- items = listTools(args); // honours args.includeHidden (hidden tools omitted by default)
166
- break;
167
- case 'mcps':
168
- items = listMcps();
169
- break;
170
- case 'hooks':
171
- items = listHooks();
172
- break;
173
- default:
174
- throw new Error(`tf-list: kind must be one of "tools"|"mcps"|"hooks" (got ${JSON.stringify(kind)})`);
175
- }
176
- return { ok: true, kind, items };
177
- }
178
-
179
- function main() {
180
- let payload;
181
- try {
182
- payload = build(readArgs());
183
- } catch (err) {
184
- // Any logical error (bad kind, missing/malformed store) becomes a well-shaped
185
- // failure line. Still exit 0 — the gated runner treats parseable stdout as ok.
186
- payload = { ok: false, error: (err && err.message) || String(err) };
187
- }
188
- process.stdout.write(JSON.stringify(payload) + '\n');
189
- process.exit(0);
190
- }
191
-
192
- main();
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ /**
5
+ * tf-list.js - a READ-ONLY management inventory tool (category "management").
6
+ *
7
+ * Purpose
8
+ * -------
9
+ * One first-party register function that answers "what is configured, and what is
10
+ * its live state?" across the three stores the gateway owns:
11
+ * - tools -> the persisted Tool Register + the ACTIVE/DISABLED + hidden overlay.
12
+ * - mcps -> the expose store: upstream MCPs and their curated-direct exposes.
13
+ * - hooks -> the hook manifest + the per-hook enabled-state overlay.
14
+ *
15
+ * It is discovered via list_tools and executed via run_tool through the PreToolUse
16
+ * gate. It does NOT add new MCP-protocol commands; it is a "gateway-run" script
17
+ * that reads the gateway's own config files. READ-ONLY: no writes, no network,
18
+ * no process mutation.
19
+ *
20
+ * Contract with the register (registry.js `defaultRunScript`)
21
+ * -----------------------------------------------------------
22
+ * - Structured args arrive as a JSON string in env TOOLFUNNEL_TOOL_ARGS ({} if
23
+ * absent). The only arg is { kind: "tools" | "mcps" | "hooks" }.
24
+ * - Prints EXACTLY ONE JSON line to stdout and exits 0 - ALWAYS exit 0. A bad
25
+ * kind, a missing/malformed config, or any other logical error is reported as
26
+ * { ok:false, error } (still exit 0), never a crash.
27
+ *
28
+ * Output (stdout), exactly one JSON object:
29
+ * success: { ok:true, kind, items:[ ... ] }
30
+ * failure: { ok:false, error }
31
+ *
32
+ * Per-kind item shapes:
33
+ * tools -> register brief { id, name, summary, category } annotated with the
34
+ * overlay-derived { enabled, hidden }.
35
+ * mcps -> one item per upstream MCP (its full config incl. `enabled`) with its
36
+ * curated-direct `exposed` entries nested (each carrying its resolved
37
+ * downstream `name` and `enabled` flag). Uses listUpstreams() +
38
+ * listExposed() and groups exposes under their owning upstream.
39
+ * hooks -> one item per manifest hook (full spec) with `enabled` resolved from
40
+ * the persisted overlay (readState) when present, else the manifest flag.
41
+ *
42
+ * Safety invariants (defense-in-depth; mirror the isolation rule):
43
+ * - READ-ONLY: only reads config files INSIDE the toolfunnel root, resolved from
44
+ * THIS script's own location (__dirname) - never a caller-supplied path.
45
+ * - NO network, NO writes, NO process mutation.
46
+ * - NEVER throws for ordinary conditions - only well-shaped JSON on stdout.
47
+ */
48
+
49
+ const path = require('node:path');
50
+
51
+ // HOME/engine resolution shared by every tf-* script (see tf-env.js): the config lives beside
52
+ // this script (the git-clone root OR a seeded external config home); the engine code is required
53
+ // from the package that spawned us when it is not local.
54
+ const { HOME, srcRequire } = require('./tf-env');
55
+ const { loadRegistry } = srcRequire('tools/registry');
56
+ const { loadToolState, isToolEnabled, isToolHidden } = srcRequire('tools/tool-state');
57
+ const { loadExposeStore } = srcRequire('mcp/expose-store');
58
+ const { loadManifest } = srcRequire('core/hook-loader');
59
+
60
+ const ROOT = HOME;
61
+
62
+ // Canonical store paths (see the script contract).
63
+ const REGISTER_PATH = path.join(ROOT, 'tools', 'tools.register.json');
64
+ const STATE_PATH = path.join(ROOT, 'tools', 'tools.state.json');
65
+ const EXPOSE_PATH = path.join(ROOT, 'mcp', 'expose.json');
66
+ const MANIFEST_PATH = path.join(ROOT, 'hooks', 'hooks.manifest.json');
67
+ const SCRIPTS_ROOT = path.join(ROOT, 'tools', 'scripts');
68
+
69
+ /**
70
+ * Parse the structured args from env. Never throws: a missing/blank/malformed
71
+ * TOOLFUNNEL_TOOL_ARGS yields {} so the kind check below produces a clean
72
+ * { ok:false, error } rather than a crash.
73
+ * @returns {object}
74
+ */
75
+ function readArgs() {
76
+ const raw = process.env.TOOLFUNNEL_TOOL_ARGS;
77
+ if (typeof raw !== 'string' || raw.length === 0) return {};
78
+ try {
79
+ const parsed = JSON.parse(raw);
80
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {};
81
+ } catch (_err) {
82
+ return {};
83
+ }
84
+ }
85
+
86
+ /**
87
+ * tools -> register briefs annotated with the overlay's enabled + hidden state. HIDDEN tools are a
88
+ * manager-view declutter: they are OMITTED by default and only included when args.includeHidden is
89
+ * true. (This is the manager surface only - `hidden` never affects the lean list / top-level surface
90
+ * the connected AI sees.)
91
+ * @param {object} [args] { includeHidden?: boolean }
92
+ * @returns {Array<{id,name,summary,category,enabled,hidden}>}
93
+ */
94
+ function listTools(args) {
95
+ const includeHidden = !!(args && args.includeHidden === true);
96
+ const registry = loadRegistry(REGISTER_PATH, { scriptsRoot: SCRIPTS_ROOT });
97
+ const state = loadToolState(STATE_PATH);
98
+ return registry.list()
99
+ .map((brief) => ({
100
+ ...brief,
101
+ enabled: isToolEnabled(state, brief.id),
102
+ hidden: isToolHidden(state, brief.id),
103
+ }))
104
+ .filter((t) => includeHidden || !t.hidden); // declutter: omit hidden unless explicitly asked
105
+ }
106
+
107
+ /**
108
+ * mcps -> one item per upstream MCP, with its curated-direct exposes nested.
109
+ * Uses listUpstreams() for the connection config (incl. `enabled`) and
110
+ * listExposed() for the surfaced tools, grouping each expose under its owning
111
+ * upstream and resolving the downstream name via exposedName().
112
+ * @returns {Array<object>}
113
+ */
114
+ function listMcps() {
115
+ const store = loadExposeStore(EXPOSE_PATH);
116
+ const upstreams = store.listUpstreams();
117
+ const exposed = store.listExposed();
118
+
119
+ // Group exposes by their upstream id (every expose references an existing
120
+ // upstream - addExpose requires it and removeUpstream cascades - but default
121
+ // to an empty list so an unmatched entry never throws).
122
+ const byUpstream = new Map();
123
+ for (const e of exposed) {
124
+ const annotated = { ...e, name: store.exposedName(e) };
125
+ const bucket = byUpstream.get(e.upstream);
126
+ if (bucket) bucket.push(annotated);
127
+ else byUpstream.set(e.upstream, [annotated]);
128
+ }
129
+
130
+ return upstreams.map((u) => ({
131
+ ...u,
132
+ exposed: byUpstream.get(u.id) || [],
133
+ }));
134
+ }
135
+
136
+ /**
137
+ * hooks -> one item per manifest hook with `enabled` resolved from the persisted
138
+ * overlay (readState) when it carries the hook id, else the manifest's own flag.
139
+ * loadManifest already applies the overlay over the in-memory specs, but we read
140
+ * the overlay explicitly so the resolved `enabled` is correct regardless of that.
141
+ * @returns {Array<object>}
142
+ */
143
+ function listHooks() {
144
+ const loader = loadManifest(MANIFEST_PATH);
145
+ const overlay = loader.readState();
146
+ return loader.hooks.map((h) => {
147
+ const hasOverride = h && typeof h.id === 'string' &&
148
+ Object.prototype.hasOwnProperty.call(overlay, h.id);
149
+ const enabled = hasOverride ? overlay[h.id] === true : h.enabled === true;
150
+ return { ...h, enabled };
151
+ });
152
+ }
153
+
154
+ /**
155
+ * Build the inventory for the requested kind. Throws on an unknown kind so main()
156
+ * can surface it as { ok:false, error }.
157
+ * @param {object} args
158
+ * @returns {{ ok:true, kind:string, items:Array<object> }}
159
+ */
160
+ function build(args) {
161
+ const kind = args && args.kind;
162
+ let items;
163
+ switch (kind) {
164
+ case 'tools':
165
+ items = listTools(args); // honours args.includeHidden (hidden tools omitted by default)
166
+ break;
167
+ case 'mcps':
168
+ items = listMcps();
169
+ break;
170
+ case 'hooks':
171
+ items = listHooks();
172
+ break;
173
+ default:
174
+ throw new Error(`tf-list: kind must be one of "tools"|"mcps"|"hooks" (got ${JSON.stringify(kind)})`);
175
+ }
176
+ return { ok: true, kind, items };
177
+ }
178
+
179
+ function main() {
180
+ let payload;
181
+ try {
182
+ payload = build(readArgs());
183
+ } catch (err) {
184
+ // Any logical error (bad kind, missing/malformed store) becomes a well-shaped
185
+ // failure line. Still exit 0 - the gated runner treats parseable stdout as ok.
186
+ payload = { ok: false, error: (err && err.message) || String(err) };
187
+ }
188
+ process.stdout.write(JSON.stringify(payload) + '\n');
189
+ process.exit(0);
190
+ }
191
+
192
+ main();