toolfunnel 0.6.0 → 0.7.1

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 (72) hide show
  1. package/README.md +228 -231
  2. package/SECURITY.md +84 -0
  3. package/bin/toolfunnel.js +451 -372
  4. package/docs/KNOWN_BUGS.md +80 -99
  5. package/docs/MANUAL.pdf +0 -0
  6. package/docs/design.md +84 -70
  7. package/docs/packaging.md +115 -115
  8. package/hooks/hooks.manifest.json +27 -27
  9. package/hooks/scripts/example-audit.js +21 -21
  10. package/hooks/scripts/example-deny-dangerous.js +39 -31
  11. package/mcp/expose.example.json +30 -30
  12. package/mcp/servers/mock-modern/server.js +147 -147
  13. package/mcp/servers/mock-upstream/server.js +380 -350
  14. package/package.json +3 -1
  15. package/src/auth/config.js +174 -174
  16. package/src/auth/install.js +181 -181
  17. package/src/auth/resource-server.js +353 -344
  18. package/src/core/child-reaper.js +61 -0
  19. package/src/core/config-home.js +134 -134
  20. package/src/core/events.js +105 -105
  21. package/src/core/hook-engine.js +329 -275
  22. package/src/core/hook-loader.js +824 -791
  23. package/src/core/hook-runner.js +566 -346
  24. package/src/core/logger.js +297 -171
  25. package/src/core/matcher.js +100 -51
  26. package/src/core/metrics.js +75 -75
  27. package/src/core/rename-migration.js +163 -0
  28. package/src/core/requires.js +138 -138
  29. package/src/core/server-config.js +98 -98
  30. package/src/extend/add-hook.md +152 -152
  31. package/src/extend/add-mcp.md +119 -122
  32. package/src/extend/configure.md +88 -87
  33. package/src/extend/create-tool.md +168 -168
  34. package/src/extend/howto.js +102 -102
  35. package/src/extend/package.md +190 -187
  36. package/src/extend/wrap.md +99 -99
  37. package/src/mcp/aggregator.js +1719 -1246
  38. package/src/mcp/expose-store.js +577 -460
  39. package/src/mcp/gated-run.js +210 -201
  40. package/src/mcp/http-transport.js +1563 -1311
  41. package/src/mcp/mcp-client.js +1070 -907
  42. package/src/mcp/modern.js +406 -372
  43. package/src/mcp/protocol.js +468 -468
  44. package/src/mcp/server.js +3112 -2688
  45. package/src/tools/registry.js +661 -513
  46. package/src/tools/tool-state.js +170 -170
  47. package/src/ui/public/app.js +1187 -1174
  48. package/src/ui/public/index.html +286 -286
  49. package/src/ui/public/styles.css +535 -535
  50. package/src/ui/server.js +1616 -1348
  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 +120 -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 -121
  64. package/tools/scripts/tf-pack.js +237 -237
  65. package/tools/scripts/tf-tool-add.js +134 -134
  66. package/tools/scripts/tf-tool-set.js +142 -142
  67. package/tools/scripts/tf-wrap.js +103 -103
  68. package/tools/scripts/uuid.js +102 -102
  69. package/tools/tools.register.json +227 -227
  70. package/src/core/tool-registry.js +0 -180
  71. package/src/packages/packager.js +0 -715
  72. package/src/tools/drift.js +0 -138
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "toolfunnel",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "A zero-dependency MCP gateway: host your own tools, forward and curate tools from other MCP servers, expose them leanly to cut agent token cost, and gate every call through your own policy hooks before it runs.",
5
5
  "keywords": [
6
6
  "mcp",
@@ -32,6 +32,7 @@
32
32
  "http": "node bin/toolfunnel.js --http",
33
33
  "install-oauth": "node bin/toolfunnel.js install-oauth",
34
34
  "test": "node test/run-all.js",
35
+ "prepublishOnly": "node test/run-all.js",
35
36
  "release": "node scripts/release.js",
36
37
  "test:integration": "node test/integration-real-mcp.test.js && node test/integration-http-client.test.js"
37
38
  },
@@ -56,6 +57,7 @@
56
57
  "hooks/scripts/",
57
58
  "docs/",
58
59
  "README.md",
60
+ "SECURITY.md",
59
61
  "LICENSE"
60
62
  ],
61
63
  "devDependencies": {
@@ -1,174 +1,174 @@
1
- 'use strict';
2
-
3
- /**
4
- * auth/config.js - the toggle + settings for OPTIONAL OAuth 2.1 resource-server validation.
5
- *
6
- * DEFAULT OFF (the gateway is loopback-only and unauthenticated until you turn this on). Nothing
7
- * is read or required unless auth has been explicitly enabled via setConfig(). A MISSING config
8
- * file means disabled - the safe default - so a fresh checkout authenticates nothing and pulls in
9
- * no dependency.
10
- *
11
- * Design rules (mirrors src/core/logger.js - the proven config pattern):
12
- * - Zero RUNTIME dependencies. Node built-ins only (fs, path). The jose library is pulled in ONLY
13
- * by the validator (src/auth/resource-server.js), ONLY when auth is enabled, and is installed
14
- * on demand (CLI `install-oauth` / the UI button). This module never touches jose.
15
- * - Config is read FRESH on every call (no caching), so a toggle/edit takes effect for the very
16
- * next request without a reconnect or restart - exactly like the logger + tool-state overlays.
17
- * - Nothing here throws. A bad/unreadable/malformed config degrades to the safe default (disabled)
18
- * so a misconfiguration can never crash the transport.
19
- *
20
- * Config file: <root>/auth/auth.config.json (NOT created until setConfig() writes it)
21
- * {
22
- * "enabled": false,
23
- * "issuer": "https://auth.example.com", // the OAuth 2.1 / OIDC authorization server
24
- * "audience": "https://gateway.example.com", // THIS gateway's resource URI - RFC 8707 audience
25
- * // binding; the confused-deputy defence
26
- * "jwksUri": "https://auth.example.com/.well-known/jwks.json", // explicit JWKS; else derived
27
- * // from the issuer via OIDC discovery
28
- * "algorithms": ["RS256", "ES256"], // the PINNED signature-algorithm allowlist
29
- * "requiredScopes": [], // optional - every listed scope must be present
30
- * "clockToleranceSec": 30 // exp/nbf skew tolerance (seconds)
31
- * }
32
- *
33
- * CommonJS only.
34
- */
35
-
36
- const fs = require('node:fs');
37
- const path = require('node:path');
38
-
39
- /** The CONFIG HOME (TOOLFUNNEL_HOME / --config-dir; defaults to the package root - see
40
- * src/core/config-home.js). Auth config is user-state, so it lives with the home. */
41
- const { resolveConfigHome } = require('../core/config-home');
42
- const ROOT = resolveConfigHome();
43
-
44
- /** The toggle/config file. NOT created until setConfig() writes it. */
45
- const CONFIG_PATH = path.join(ROOT, 'auth', 'auth.config.json');
46
-
47
- /** Safe defaults - used whenever the config file is absent or unreadable. */
48
- const DEFAULTS = Object.freeze({
49
- enabled: false,
50
- issuer: '',
51
- audience: '',
52
- jwksUri: '',
53
- algorithms: ['RS256', 'ES256'],
54
- requiredScopes: [],
55
- clockToleranceSec: 30,
56
- });
57
-
58
- /** A string field, defaulting to '' when not a non-empty string. */
59
- function str(v, dflt) {
60
- return typeof v === 'string' && v.length > 0 ? v : dflt;
61
- }
62
-
63
- /** A string[] field - keep only non-empty strings; fall back to `dflt` when not a usable array. */
64
- function strArray(v, dflt) {
65
- if (!Array.isArray(v)) return dflt.slice();
66
- const out = v.filter((x) => typeof x === 'string' && x.length > 0);
67
- return out.length > 0 ? out : dflt.slice();
68
- }
69
-
70
- /**
71
- * getConfig - the resolved auth config. Reads the file fresh; a missing/unreadable/malformed file
72
- * resolves to the safe defaults (disabled). NEVER throws.
73
- *
74
- * @returns {{ enabled:boolean, issuer:string, audience:string, jwksUri:string,
75
- * algorithms:string[], requiredScopes:string[], clockToleranceSec:number }}
76
- */
77
- function getConfig() {
78
- try {
79
- const raw = fs.readFileSync(CONFIG_PATH, 'utf8');
80
- const parsed = JSON.parse(raw);
81
- if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
82
- return cloneDefaults();
83
- }
84
- const clock = Number(parsed.clockToleranceSec);
85
- return {
86
- enabled: parsed.enabled === true,
87
- issuer: str(parsed.issuer, DEFAULTS.issuer),
88
- audience: str(parsed.audience, DEFAULTS.audience),
89
- jwksUri: str(parsed.jwksUri, DEFAULTS.jwksUri),
90
- algorithms: strArray(parsed.algorithms, DEFAULTS.algorithms),
91
- requiredScopes: strArray(parsed.requiredScopes, DEFAULTS.requiredScopes),
92
- // Clamp to a sane, non-negative tolerance; a >5min skew signals a clock problem, not validity.
93
- clockToleranceSec:
94
- Number.isFinite(clock) && clock >= 0 ? Math.min(Math.floor(clock), 300) : DEFAULTS.clockToleranceSec,
95
- };
96
- } catch (_err) {
97
- // Missing file = disabled (the safe default); also covers unreadable/malformed.
98
- return cloneDefaults();
99
- }
100
- }
101
-
102
- /** A fresh, mutation-safe copy of the defaults (arrays cloned so a caller can't poison the frozen one). */
103
- function cloneDefaults() {
104
- return {
105
- enabled: DEFAULTS.enabled,
106
- issuer: DEFAULTS.issuer,
107
- audience: DEFAULTS.audience,
108
- jwksUri: DEFAULTS.jwksUri,
109
- algorithms: DEFAULTS.algorithms.slice(),
110
- requiredScopes: DEFAULTS.requiredScopes.slice(),
111
- clockToleranceSec: DEFAULTS.clockToleranceSec,
112
- };
113
- }
114
-
115
- /**
116
- * configError - a human-readable reason the CURRENT config could not gate requests, or null if it
117
- * is coherent enough to enforce. Used by the transport + UI to fail CLOSED with a clear message
118
- * rather than silently mis-validating. Only meaningful when `enabled` is true.
119
- *
120
- * Enforcement requires: an issuer, an audience (the RFC 8707 confused-deputy defence - a gateway
121
- * that does not bind tokens to its own resource URI is exploitable), at least one pinned algorithm,
122
- * and a key source (explicit jwksUri OR an issuer to derive one from via OIDC discovery).
123
- *
124
- * @param {object} [cfg] defaults to getConfig()
125
- * @returns {string|null}
126
- */
127
- function configError(cfg) {
128
- const c = cfg || getConfig();
129
- if (!c.enabled) return null; // disabled -> nothing to enforce, no error
130
- if (!c.issuer) return 'auth enabled but "issuer" is not set';
131
- if (!c.audience) return 'auth enabled but "audience" (this gateway\'s resource URI) is not set - required to bind tokens (RFC 8707)';
132
- if (!Array.isArray(c.algorithms) || c.algorithms.length === 0) return 'auth enabled but no signature "algorithms" are pinned';
133
- // (A key source is always available: issuer is mandatory above, and jwksUri is derived from it
134
- // via OIDC discovery when not given - so no separate jwksUri-or-issuer check is needed here.)
135
- return null;
136
- }
137
-
138
- /**
139
- * setConfig - atomically merge a patch into auth/auth.config.json (temp + rename). Merges with the
140
- * current resolved config so a partial patch (e.g. { enabled: true }) preserves the other fields.
141
- * Creates the auth/ dir and the file if absent - this is the ONLY function that creates it.
142
- *
143
- * @param {object} patch any subset of the config fields
144
- * @returns {object} the merged, written config
145
- */
146
- function setConfig(patch) {
147
- const current = getConfig();
148
- const p = patch && typeof patch === 'object' && !Array.isArray(patch) ? patch : {};
149
-
150
- const next = {
151
- enabled: typeof p.enabled === 'boolean' ? p.enabled : current.enabled,
152
- issuer: typeof p.issuer === 'string' ? p.issuer : current.issuer,
153
- audience: typeof p.audience === 'string' ? p.audience : current.audience,
154
- jwksUri: typeof p.jwksUri === 'string' ? p.jwksUri : current.jwksUri,
155
- algorithms: Array.isArray(p.algorithms) ? strArray(p.algorithms, current.algorithms) : current.algorithms,
156
- requiredScopes: Array.isArray(p.requiredScopes)
157
- ? p.requiredScopes.filter((x) => typeof x === 'string' && x.length > 0)
158
- : current.requiredScopes,
159
- clockToleranceSec:
160
- Number.isFinite(Number(p.clockToleranceSec)) && Number(p.clockToleranceSec) >= 0
161
- ? Math.min(Math.floor(Number(p.clockToleranceSec)), 300)
162
- : current.clockToleranceSec,
163
- };
164
-
165
- fs.mkdirSync(path.dirname(CONFIG_PATH), { recursive: true });
166
- // Atomic write: unique temp file in the same dir, then rename over the target.
167
- const tmp = CONFIG_PATH + '.' + process.pid + '.' + Date.now() + '.tmp';
168
- fs.writeFileSync(tmp, JSON.stringify(next, null, 2) + '\n');
169
- fs.renameSync(tmp, CONFIG_PATH);
170
-
171
- return next;
172
- }
173
-
174
- module.exports = { getConfig, setConfig, configError, CONFIG_PATH, DEFAULTS };
1
+ 'use strict';
2
+
3
+ /**
4
+ * auth/config.js - the toggle + settings for OPTIONAL OAuth 2.1 resource-server validation.
5
+ *
6
+ * DEFAULT OFF (the gateway is loopback-only and unauthenticated until you turn this on). Nothing
7
+ * is read or required unless auth has been explicitly enabled via setConfig(). A MISSING config
8
+ * file means disabled - the safe default - so a fresh checkout authenticates nothing and pulls in
9
+ * no dependency.
10
+ *
11
+ * Design rules (mirrors src/core/logger.js - the proven config pattern):
12
+ * - Zero RUNTIME dependencies. Node built-ins only (fs, path). The jose library is pulled in ONLY
13
+ * by the validator (src/auth/resource-server.js), ONLY when auth is enabled, and is installed
14
+ * on demand (CLI `install-oauth` / the UI button). This module never touches jose.
15
+ * - Config is read FRESH on every call (no caching), so a toggle/edit takes effect for the very
16
+ * next request without a reconnect or restart - exactly like the logger + tool-state overlays.
17
+ * - Nothing here throws. A bad/unreadable/malformed config degrades to the safe default (disabled)
18
+ * so a misconfiguration can never crash the transport.
19
+ *
20
+ * Config file: <root>/auth/auth.config.json (NOT created until setConfig() writes it)
21
+ * {
22
+ * "enabled": false,
23
+ * "issuer": "https://auth.example.com", // the OAuth 2.1 / OIDC authorization server
24
+ * "audience": "https://gateway.example.com", // THIS gateway's resource URI - RFC 8707 audience
25
+ * // binding; the confused-deputy defence
26
+ * "jwksUri": "https://auth.example.com/.well-known/jwks.json", // explicit JWKS; else derived
27
+ * // from the issuer via OIDC discovery
28
+ * "algorithms": ["RS256", "ES256"], // the PINNED signature-algorithm allowlist
29
+ * "requiredScopes": [], // optional - every listed scope must be present
30
+ * "clockToleranceSec": 30 // exp/nbf skew tolerance (seconds)
31
+ * }
32
+ *
33
+ * CommonJS only.
34
+ */
35
+
36
+ const fs = require('node:fs');
37
+ const path = require('node:path');
38
+
39
+ /** The CONFIG HOME (TOOLFUNNEL_HOME / --config-dir; defaults to the package root - see
40
+ * src/core/config-home.js). Auth config is user-state, so it lives with the home. */
41
+ const { resolveConfigHome } = require('../core/config-home');
42
+ const ROOT = resolveConfigHome();
43
+
44
+ /** The toggle/config file. NOT created until setConfig() writes it. */
45
+ const CONFIG_PATH = path.join(ROOT, 'auth', 'auth.config.json');
46
+
47
+ /** Safe defaults - used whenever the config file is absent or unreadable. */
48
+ const DEFAULTS = Object.freeze({
49
+ enabled: false,
50
+ issuer: '',
51
+ audience: '',
52
+ jwksUri: '',
53
+ algorithms: ['RS256', 'ES256'],
54
+ requiredScopes: [],
55
+ clockToleranceSec: 30,
56
+ });
57
+
58
+ /** A string field, defaulting to '' when not a non-empty string. */
59
+ function str(v, dflt) {
60
+ return typeof v === 'string' && v.length > 0 ? v : dflt;
61
+ }
62
+
63
+ /** A string[] field - keep only non-empty strings; fall back to `dflt` when not a usable array. */
64
+ function strArray(v, dflt) {
65
+ if (!Array.isArray(v)) return dflt.slice();
66
+ const out = v.filter((x) => typeof x === 'string' && x.length > 0);
67
+ return out.length > 0 ? out : dflt.slice();
68
+ }
69
+
70
+ /**
71
+ * getConfig - the resolved auth config. Reads the file fresh; a missing/unreadable/malformed file
72
+ * resolves to the safe defaults (disabled). NEVER throws.
73
+ *
74
+ * @returns {{ enabled:boolean, issuer:string, audience:string, jwksUri:string,
75
+ * algorithms:string[], requiredScopes:string[], clockToleranceSec:number }}
76
+ */
77
+ function getConfig() {
78
+ try {
79
+ const raw = fs.readFileSync(CONFIG_PATH, 'utf8');
80
+ const parsed = JSON.parse(raw);
81
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
82
+ return cloneDefaults();
83
+ }
84
+ const clock = Number(parsed.clockToleranceSec);
85
+ return {
86
+ enabled: parsed.enabled === true,
87
+ issuer: str(parsed.issuer, DEFAULTS.issuer),
88
+ audience: str(parsed.audience, DEFAULTS.audience),
89
+ jwksUri: str(parsed.jwksUri, DEFAULTS.jwksUri),
90
+ algorithms: strArray(parsed.algorithms, DEFAULTS.algorithms),
91
+ requiredScopes: strArray(parsed.requiredScopes, DEFAULTS.requiredScopes),
92
+ // Clamp to a sane, non-negative tolerance; a >5min skew signals a clock problem, not validity.
93
+ clockToleranceSec:
94
+ Number.isFinite(clock) && clock >= 0 ? Math.min(Math.floor(clock), 300) : DEFAULTS.clockToleranceSec,
95
+ };
96
+ } catch (_err) {
97
+ // Missing file = disabled (the safe default); also covers unreadable/malformed.
98
+ return cloneDefaults();
99
+ }
100
+ }
101
+
102
+ /** A fresh, mutation-safe copy of the defaults (arrays cloned so a caller can't corrupt the frozen one). */
103
+ function cloneDefaults() {
104
+ return {
105
+ enabled: DEFAULTS.enabled,
106
+ issuer: DEFAULTS.issuer,
107
+ audience: DEFAULTS.audience,
108
+ jwksUri: DEFAULTS.jwksUri,
109
+ algorithms: DEFAULTS.algorithms.slice(),
110
+ requiredScopes: DEFAULTS.requiredScopes.slice(),
111
+ clockToleranceSec: DEFAULTS.clockToleranceSec,
112
+ };
113
+ }
114
+
115
+ /**
116
+ * configError - a human-readable reason the CURRENT config could not gate requests, or null if it
117
+ * is coherent enough to enforce. Used by the transport + UI to fail CLOSED with a clear message
118
+ * rather than silently mis-validating. Only meaningful when `enabled` is true.
119
+ *
120
+ * Enforcement requires: an issuer, an audience (the RFC 8707 confused-deputy defence - a gateway
121
+ * that does not bind tokens to its own resource URI is unsafe), at least one pinned algorithm,
122
+ * and a key source (explicit jwksUri OR an issuer to derive one from via OIDC discovery).
123
+ *
124
+ * @param {object} [cfg] defaults to getConfig()
125
+ * @returns {string|null}
126
+ */
127
+ function configError(cfg) {
128
+ const c = cfg || getConfig();
129
+ if (!c.enabled) return null; // disabled -> nothing to enforce, no error
130
+ if (!c.issuer) return 'auth enabled but "issuer" is not set';
131
+ if (!c.audience) return 'auth enabled but "audience" (this gateway\'s resource URI) is not set - required to bind tokens (RFC 8707)';
132
+ if (!Array.isArray(c.algorithms) || c.algorithms.length === 0) return 'auth enabled but no signature "algorithms" are pinned';
133
+ // (A key source is always available: issuer is mandatory above, and jwksUri is derived from it
134
+ // via OIDC discovery when not given - so no separate jwksUri-or-issuer check is needed here.)
135
+ return null;
136
+ }
137
+
138
+ /**
139
+ * setConfig - atomically merge a patch into auth/auth.config.json (temp + rename). Merges with the
140
+ * current resolved config so a partial patch (e.g. { enabled: true }) preserves the other fields.
141
+ * Creates the auth/ dir and the file if absent - this is the ONLY function that creates it.
142
+ *
143
+ * @param {object} patch any subset of the config fields
144
+ * @returns {object} the merged, written config
145
+ */
146
+ function setConfig(patch) {
147
+ const current = getConfig();
148
+ const p = patch && typeof patch === 'object' && !Array.isArray(patch) ? patch : {};
149
+
150
+ const next = {
151
+ enabled: typeof p.enabled === 'boolean' ? p.enabled : current.enabled,
152
+ issuer: typeof p.issuer === 'string' ? p.issuer : current.issuer,
153
+ audience: typeof p.audience === 'string' ? p.audience : current.audience,
154
+ jwksUri: typeof p.jwksUri === 'string' ? p.jwksUri : current.jwksUri,
155
+ algorithms: Array.isArray(p.algorithms) ? strArray(p.algorithms, current.algorithms) : current.algorithms,
156
+ requiredScopes: Array.isArray(p.requiredScopes)
157
+ ? p.requiredScopes.filter((x) => typeof x === 'string' && x.length > 0)
158
+ : current.requiredScopes,
159
+ clockToleranceSec:
160
+ Number.isFinite(Number(p.clockToleranceSec)) && Number(p.clockToleranceSec) >= 0
161
+ ? Math.min(Math.floor(Number(p.clockToleranceSec)), 300)
162
+ : current.clockToleranceSec,
163
+ };
164
+
165
+ fs.mkdirSync(path.dirname(CONFIG_PATH), { recursive: true });
166
+ // Atomic write: unique temp file in the same dir, then rename over the target.
167
+ const tmp = CONFIG_PATH + '.' + process.pid + '.' + Date.now() + '.tmp';
168
+ fs.writeFileSync(tmp, JSON.stringify(next, null, 2) + '\n');
169
+ fs.renameSync(tmp, CONFIG_PATH);
170
+
171
+ return next;
172
+ }
173
+
174
+ module.exports = { getConfig, setConfig, configError, CONFIG_PATH, DEFAULTS };