hmcp-gateway 0.0.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +77 -0
- package/README.md +135 -9
- package/bin/hmcp-gateway.js +20 -0
- package/catalog/atlassian.yaml +41 -0
- package/catalog/brave-search.yaml +57 -0
- package/catalog/context7.yaml +42 -0
- package/catalog/fetch.yaml +37 -0
- package/catalog/figma.yaml +48 -0
- package/catalog/filesystem.yaml +48 -0
- package/catalog/git.yaml +48 -0
- package/catalog/github.yaml +103 -0
- package/catalog/huggingface.yaml +59 -0
- package/catalog/linear.yaml +47 -0
- package/catalog/memory.yaml +47 -0
- package/catalog/notion.yaml +41 -0
- package/catalog/playwright.yaml +38 -0
- package/catalog/postgres.yaml +66 -0
- package/catalog/sentry.yaml +39 -0
- package/catalog/sequential-thinking.yaml +29 -0
- package/catalog/stripe.yaml +41 -0
- package/catalog/supabase.yaml +45 -0
- package/catalog/time.yaml +49 -0
- package/dist/adopt-cli.d.ts +355 -0
- package/dist/adopt-cli.js +1885 -0
- package/dist/adopt-emit.d.ts +64 -0
- package/dist/adopt-emit.js +248 -0
- package/dist/adopt-plan.d.ts +108 -0
- package/dist/adopt-plan.js +501 -0
- package/dist/catalog.d.ts +140 -0
- package/dist/catalog.js +250 -0
- package/dist/classify.d.ts +23 -0
- package/dist/classify.js +47 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +656 -0
- package/dist/client-allow.d.ts +42 -0
- package/dist/client-allow.js +95 -0
- package/dist/client-config.d.ts +263 -0
- package/dist/client-config.js +995 -0
- package/dist/config-source.d.ts +106 -0
- package/dist/config-source.js +156 -0
- package/dist/config.d.ts +159 -0
- package/dist/config.js +330 -0
- package/dist/connect-client.d.ts +51 -0
- package/dist/connect-client.js +299 -0
- package/dist/contracts.d.ts +251 -0
- package/dist/contracts.js +9 -0
- package/dist/credential.d.ts +63 -0
- package/dist/credential.js +136 -0
- package/dist/desktop-extensions.d.ts +53 -0
- package/dist/desktop-extensions.js +126 -0
- package/dist/draft.d.ts +71 -0
- package/dist/draft.js +596 -0
- package/dist/exposure-cli.d.ts +28 -0
- package/dist/exposure-cli.js +119 -0
- package/dist/gateway.d.ts +766 -0
- package/dist/gateway.js +2122 -0
- package/dist/github-doors.d.ts +162 -0
- package/dist/github-doors.js +780 -0
- package/dist/guard-install.d.ts +109 -0
- package/dist/guard-install.js +396 -0
- package/dist/guard.d.ts +265 -0
- package/dist/guard.js +975 -0
- package/dist/import-client.d.ts +184 -0
- package/dist/import-client.js +578 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +31 -0
- package/dist/notify.d.ts +44 -0
- package/dist/notify.js +93 -0
- package/dist/oauth.d.ts +139 -0
- package/dist/oauth.js +641 -0
- package/dist/observe.d.ts +10 -0
- package/dist/observe.js +26 -0
- package/dist/open.d.ts +24 -0
- package/dist/open.js +31 -0
- package/dist/packs.d.ts +119 -0
- package/dist/packs.js +131 -0
- package/dist/pin-cli.d.ts +28 -0
- package/dist/pin-cli.js +103 -0
- package/dist/probe-upstream.d.ts +31 -0
- package/dist/probe-upstream.js +377 -0
- package/dist/registry-map.d.ts +39 -0
- package/dist/registry-map.js +390 -0
- package/dist/rule-cli.d.ts +51 -0
- package/dist/rule-cli.js +412 -0
- package/dist/serve-http.d.ts +67 -0
- package/dist/serve-http.js +478 -0
- package/dist/snippet.d.ts +20 -0
- package/dist/snippet.js +826 -0
- package/dist/status-cli.d.ts +61 -0
- package/dist/status-cli.js +195 -0
- package/dist/suggest-cli.d.ts +43 -0
- package/dist/suggest-cli.js +279 -0
- package/dist/toml-edit.d.ts +43 -0
- package/dist/toml-edit.js +363 -0
- package/dist/toolname.d.ts +47 -0
- package/dist/toolname.js +149 -0
- package/dist/upstream-errors.d.ts +54 -0
- package/dist/upstream-errors.js +330 -0
- package/dist/upstream.d.ts +128 -0
- package/dist/upstream.js +413 -0
- package/dist/version.d.ts +10 -0
- package/dist/version.js +11 -0
- package/package.json +51 -6
- package/packs/brave-search.yaml +20 -0
- package/packs/context7.yaml +13 -0
- package/packs/fetch.yaml +14 -0
- package/packs/filesystem.yaml +79 -0
- package/packs/git.yaml +29 -0
- package/packs/github.yaml +47 -0
- package/packs/huggingface.yaml +16 -0
- package/packs/memory.yaml +23 -0
- package/packs/playwright.yaml +41 -0
- package/packs/sequential-thinking.yaml +13 -0
- package/packs/time.yaml +14 -0
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
import { isAbsolute } from "node:path";
|
|
2
|
+
import { classifyAddress, isIpLiteral } from "hmcp-core";
|
|
3
|
+
import { UpstreamSchema } from "./config.js";
|
|
4
|
+
import { isRemote } from "./client-config.js";
|
|
5
|
+
/** Headers that carry no credential, left as they are. Everything else literal is treated as a key. */
|
|
6
|
+
const HARMLESS_HEADERS = new Set(["accept", "accept-language", "content-type", "user-agent", "mcp-protocol-version"]);
|
|
7
|
+
const UPSTREAM_NAME = /^[a-z0-9_]+$/;
|
|
8
|
+
const INTERPOLATION = /\$\{[^}]*\}/;
|
|
9
|
+
/**
|
|
10
|
+
* An upstream name legal under `UpstreamSchema`: lowercase letters, digits and
|
|
11
|
+
* underscores.
|
|
12
|
+
*
|
|
13
|
+
* This is not cosmetic. The result becomes the `<server>__` prefix on every
|
|
14
|
+
* tool name, and that name is a storage key -- the exposure switch, every
|
|
15
|
+
* approval row and each binding hash are stored under it. So it has to be
|
|
16
|
+
* stable across runs, which is why collisions resolve by sorted original name
|
|
17
|
+
* rather than by whatever order the client's JSON happened to be in.
|
|
18
|
+
*/
|
|
19
|
+
export function normalizeUpstreamName(name) {
|
|
20
|
+
return name
|
|
21
|
+
.normalize("NFC")
|
|
22
|
+
.toLowerCase()
|
|
23
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
24
|
+
.replace(/^_+|_+$/g, "");
|
|
25
|
+
}
|
|
26
|
+
function assignNames(originals, overrides, reserved) {
|
|
27
|
+
const map = new Map();
|
|
28
|
+
const problems = new Map();
|
|
29
|
+
const taken = new Set(reserved);
|
|
30
|
+
let anonymous = 0;
|
|
31
|
+
// Sorted, so two runs over the same set produce the same prefixes.
|
|
32
|
+
for (const original of [...originals].sort()) {
|
|
33
|
+
const override = overrides.get(original);
|
|
34
|
+
if (override !== undefined) {
|
|
35
|
+
if (!UPSTREAM_NAME.test(override)) {
|
|
36
|
+
problems.set(original, `--name ${original}=${override} is not a legal upstream name (${UPSTREAM_NAME})`);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (taken.has(override)) {
|
|
40
|
+
problems.set(original, `--name ${original}=${override} collides with another upstream`);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
taken.add(override);
|
|
44
|
+
map.set(original, override);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
let base = normalizeUpstreamName(original);
|
|
48
|
+
if (base === "")
|
|
49
|
+
base = `server_${++anonymous}`;
|
|
50
|
+
let candidate = base;
|
|
51
|
+
for (let n = 2; taken.has(candidate); n++)
|
|
52
|
+
candidate = `${base}_${n}`;
|
|
53
|
+
taken.add(candidate);
|
|
54
|
+
map.set(original, candidate);
|
|
55
|
+
}
|
|
56
|
+
return { map, problems };
|
|
57
|
+
}
|
|
58
|
+
/** Hostname of a URL, or undefined when it will not parse. */
|
|
59
|
+
function hostOf(url) {
|
|
60
|
+
try {
|
|
61
|
+
const parsed = new URL(url);
|
|
62
|
+
return { hostname: parsed.hostname, host: parsed.host, protocol: parsed.protocol };
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Whether this host is one the egress guard refuses without the SSRF
|
|
70
|
+
* protections turned off.
|
|
71
|
+
*
|
|
72
|
+
* `localhost` counts even though it is not a literal: `resolveVerified`
|
|
73
|
+
* resolves it and then refuses the loopback address it gets back, so the
|
|
74
|
+
* hostname form is no more adoptable than `127.0.0.1`.
|
|
75
|
+
*/
|
|
76
|
+
export function isLocalTarget(hostname) {
|
|
77
|
+
const bare = hostname.replace(/^\[|\]$/g, "");
|
|
78
|
+
if (bare === "localhost" || bare.endsWith(".localhost"))
|
|
79
|
+
return true;
|
|
80
|
+
if (!isIpLiteral(bare))
|
|
81
|
+
return false;
|
|
82
|
+
return classifyAddress(bare).blocked;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Whether this entry is the gateway itself.
|
|
86
|
+
*
|
|
87
|
+
* Three independent checks because any one of them can be defeated by a user
|
|
88
|
+
* who renamed something: the entry's name, the command it runs, and the config
|
|
89
|
+
* it points at. Adopting the gateway would make it an upstream of itself.
|
|
90
|
+
*/
|
|
91
|
+
function isGatewayItself(name, server, options) {
|
|
92
|
+
if (name === options.gatewayEntryName)
|
|
93
|
+
return true;
|
|
94
|
+
if (isRemote(server))
|
|
95
|
+
return false;
|
|
96
|
+
const words = [server.command, ...server.args];
|
|
97
|
+
if (words.some((w) => w.includes("hmcp-gateway")))
|
|
98
|
+
return true;
|
|
99
|
+
if (options.gatewayConfigPath !== undefined && words.includes(options.gatewayConfigPath))
|
|
100
|
+
return true;
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
/** Tool names from an app's list, for a reason a person reads: names only, and not too many. */
|
|
104
|
+
function toolNames(list) {
|
|
105
|
+
const names = list.filter((t) => typeof t === "string");
|
|
106
|
+
const shown = names.slice(0, 5).join(", ");
|
|
107
|
+
return names.length > 5 ? `${shown} and ${names.length - 5} more` : shown;
|
|
108
|
+
}
|
|
109
|
+
const LEAVE_IT = "the toolbox would expose all of its tools, so it was left where it is";
|
|
110
|
+
const ADD_IT_THERE = "add it from the console and switch the other tools off there, or leave it configured directly in the app";
|
|
111
|
+
/**
|
|
112
|
+
* A limit the app itself puts on one server, which adopting would lose.
|
|
113
|
+
*
|
|
114
|
+
* Behind the gateway the app sees one entry, the gateway's, so a limit
|
|
115
|
+
* written on the original entry no longer applies: every tool the server has
|
|
116
|
+
* would be exposed, under a starting policy that only watches. So such a
|
|
117
|
+
* server is not adopted, and says why. Field names and tool names are shown;
|
|
118
|
+
* no other value from the entry is.
|
|
119
|
+
*
|
|
120
|
+
* - `tools` (GitHub Copilot CLI): which tools the app may use; `["*"]` is all.
|
|
121
|
+
* - `disabled` (Roo Code, Windsurf, Devin, others): switched off in the app.
|
|
122
|
+
* - `disabledTools` (Windsurf, Devin), `excludeTools` / `includeTools`
|
|
123
|
+
* (Gemini and Antigravity CLI): tools left out or picked.
|
|
124
|
+
* - `trust` (Gemini and Antigravity CLI): runs its tools without asking.
|
|
125
|
+
* - Codex: `enabled = false`, `enabled_tools`, `disabled_tools`, a `tools`
|
|
126
|
+
* table of per-tool settings, `default_tools_approval_mode`; and sign-in
|
|
127
|
+
* settings it cannot carry over (`bearer_token_env_var`,
|
|
128
|
+
* `env_http_headers`, `auth`, `env_vars`). These field names are from Codex's docs as
|
|
129
|
+
* known when this was written, not from the research notes.
|
|
130
|
+
* - `cwd` given relative to the app: the toolbox starts it from elsewhere.
|
|
131
|
+
*/
|
|
132
|
+
function appLimit(server) {
|
|
133
|
+
const entry = server;
|
|
134
|
+
const tools = entry["tools"];
|
|
135
|
+
if (Array.isArray(tools) && !(tools.length === 1 && tools[0] === "*")) {
|
|
136
|
+
const which = tools.length === 0 ? "none of them" : `only ${toolNames(tools)}`;
|
|
137
|
+
return {
|
|
138
|
+
reason: `it limits which tools the app may use (${which}); the toolbox would expose all of them, so it was left where it is`,
|
|
139
|
+
remedy: ADD_IT_THERE
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (tools !== null && typeof tools === "object" && !Array.isArray(tools)) {
|
|
143
|
+
return {
|
|
144
|
+
reason: `it has settings for single tools in the app (tools); ${LEAVE_IT}`,
|
|
145
|
+
remedy: ADD_IT_THERE
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
for (const [field, off] of [["disabled", true], ["enabled", false]]) {
|
|
149
|
+
if (entry[field] === off) {
|
|
150
|
+
return {
|
|
151
|
+
reason: `it is switched off in the app (${field}); behind the toolbox it would be on again, so it was left where it is`,
|
|
152
|
+
remedy: "switch it on in the app first if you want it behind the toolbox, then run this again"
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// Codex: `enabled_tools` picks tools (an empty list picks none); `disabled_tools` leaves some out.
|
|
157
|
+
const enabledTools = entry["enabled_tools"];
|
|
158
|
+
if (Array.isArray(enabledTools)) {
|
|
159
|
+
const which = enabledTools.length === 0 ? "none of them" : toolNames(enabledTools);
|
|
160
|
+
return { reason: `it limits which tools the app may use (enabled_tools: ${which}); ${LEAVE_IT}`, remedy: ADD_IT_THERE };
|
|
161
|
+
}
|
|
162
|
+
for (const field of ["disabledTools", "excludeTools", "includeTools", "disabled_tools"]) {
|
|
163
|
+
const list = entry[field];
|
|
164
|
+
if (Array.isArray(list) && list.length > 0) {
|
|
165
|
+
return { reason: `it limits which tools the app may use (${field}: ${toolNames(list)}); ${LEAVE_IT}`, remedy: ADD_IT_THERE };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (entry["trust"] === true) {
|
|
169
|
+
return {
|
|
170
|
+
reason: "the app runs its tools without asking first (trust); the toolbox cannot carry that setting over, so it was left where it is",
|
|
171
|
+
remedy: "remove the trust setting in the app if you want it behind the toolbox, then run this again"
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if (entry["default_tools_approval_mode"] !== undefined) {
|
|
175
|
+
return {
|
|
176
|
+
reason: "it sets how the app asks before its tools run (default_tools_approval_mode); the toolbox cannot carry that " +
|
|
177
|
+
"setting over, so it was left where it is",
|
|
178
|
+
remedy: ADD_IT_THERE
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/*
|
|
182
|
+
* Codex's ways of signing in to a remote server that the toolbox cannot
|
|
183
|
+
* carry over: the server would be adopted without its key and quietly fail.
|
|
184
|
+
*/
|
|
185
|
+
for (const field of ["bearer_token_env_var", "env_http_headers", "auth"]) {
|
|
186
|
+
if (entry[field] !== undefined) {
|
|
187
|
+
return {
|
|
188
|
+
reason: `it signs in in a way the toolbox can't carry over yet (${field}), so it was left where it is`,
|
|
189
|
+
remedy: "add it from the console and save its key there, or leave it configured directly in the app"
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Codex: names of its own environment variables passed on to the server, which the toolbox's process never has.
|
|
194
|
+
const envVars = entry["env_vars"];
|
|
195
|
+
if (Array.isArray(envVars) && envVars.length > 0) {
|
|
196
|
+
return {
|
|
197
|
+
reason: "it passes the server keys from Codex's own environment (env_vars), which the toolbox can't see, so it was left where it is",
|
|
198
|
+
remedy: "add it from the console and save its keys there, or leave it configured directly in the app"
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
const cwd = entry["cwd"];
|
|
202
|
+
if (cwd !== undefined && !(typeof cwd === "string" && isAbsolute(cwd))) {
|
|
203
|
+
return {
|
|
204
|
+
reason: "it starts in a folder given relative to the app (cwd), and the toolbox starts it from elsewhere, so it was left where it is",
|
|
205
|
+
remedy: "give it the full path of its folder in the app, then run this again"
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
export function planAdoption(servers, options) {
|
|
211
|
+
const upstreams = [];
|
|
212
|
+
const skipped = [];
|
|
213
|
+
const gatewayEnv = {};
|
|
214
|
+
/** Which server claimed each env name, so a collision can name both. */
|
|
215
|
+
const envOwner = new Map();
|
|
216
|
+
const egressAllow = new Set();
|
|
217
|
+
const nameMap = new Map();
|
|
218
|
+
let egressNeedsLocalHttp = false;
|
|
219
|
+
let secretsOnDisk = false;
|
|
220
|
+
const { map: names, problems: nameProblems } = assignNames([...servers.keys()], options.names ?? new Map(), options.reservedNames ?? []);
|
|
221
|
+
for (const [original, problem] of nameProblems) {
|
|
222
|
+
skipped.push({
|
|
223
|
+
name: original,
|
|
224
|
+
reason: problem,
|
|
225
|
+
remedy: `pass a different --name ${original}=<name> using lowercase letters, digits and underscores`
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
for (const original of [...servers.keys()].sort()) {
|
|
229
|
+
const server = servers.get(original);
|
|
230
|
+
const name = names.get(original);
|
|
231
|
+
if (name === undefined)
|
|
232
|
+
continue; // already reported as a name problem
|
|
233
|
+
if (isGatewayItself(original, server, options)) {
|
|
234
|
+
skipped.push({
|
|
235
|
+
name: original,
|
|
236
|
+
reason: "this entry is the gateway itself",
|
|
237
|
+
remedy: "nothing to do; the gateway cannot be an upstream of itself"
|
|
238
|
+
});
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
const limit = appLimit(server);
|
|
242
|
+
if (limit) {
|
|
243
|
+
skipped.push({ name: original, ...limit });
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (isRemote(server)) {
|
|
247
|
+
const planned = planRemote(original, name, server, options, egressAllow);
|
|
248
|
+
if ("skip" in planned) {
|
|
249
|
+
skipped.push(planned.skip);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (planned.needsLocalHttp)
|
|
253
|
+
egressNeedsLocalHttp = true;
|
|
254
|
+
if (planned.secretOnDisk)
|
|
255
|
+
secretsOnDisk = true;
|
|
256
|
+
upstreams.push(planned.planned);
|
|
257
|
+
if (name !== original)
|
|
258
|
+
nameMap.set(original, name);
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
const planned = planStdio(original, name, server, options, gatewayEnv, envOwner);
|
|
262
|
+
if ("skip" in planned) {
|
|
263
|
+
skipped.push(planned.skip);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if (planned.secretOnDisk)
|
|
267
|
+
secretsOnDisk = true;
|
|
268
|
+
upstreams.push(planned.planned);
|
|
269
|
+
if (name !== original)
|
|
270
|
+
nameMap.set(original, name);
|
|
271
|
+
}
|
|
272
|
+
/*
|
|
273
|
+
* The translator and the schema must not be able to drift. Validating here
|
|
274
|
+
* means a tightened `UpstreamSchema` fails in this package's tests rather
|
|
275
|
+
* than in a user's terminal as a zod dump halfway through adoption.
|
|
276
|
+
*/
|
|
277
|
+
for (const { upstream, originalName } of upstreams) {
|
|
278
|
+
const result = UpstreamSchema.safeParse(upstream);
|
|
279
|
+
if (!result.success) {
|
|
280
|
+
throw new Error(`internal: the upstream planned for "${originalName}" does not satisfy UpstreamSchema:\n` +
|
|
281
|
+
result.error.issues.map((i) => ` ${i.path.join(".") || "(root)"}: ${i.message}`).join("\n"));
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return {
|
|
285
|
+
upstreams,
|
|
286
|
+
skipped,
|
|
287
|
+
gatewayEnv,
|
|
288
|
+
egressAllow: [...egressAllow].sort(),
|
|
289
|
+
egressNeedsLocalHttp,
|
|
290
|
+
secretsOnDisk,
|
|
291
|
+
nameMap,
|
|
292
|
+
malformed: options.malformed ?? []
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
function planRemote(original, name, server, options, egressAllow) {
|
|
296
|
+
if (server.type === "sse") {
|
|
297
|
+
return {
|
|
298
|
+
skip: {
|
|
299
|
+
name: original,
|
|
300
|
+
reason: "it speaks SSE, and the gateway's upstream transports are stdio and Streamable HTTP only",
|
|
301
|
+
remedy: "ask the vendor for the Streamable HTTP endpoint, or leave this server configured directly and " +
|
|
302
|
+
"outside the gateway"
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
const parsed = hostOf(server.url);
|
|
307
|
+
if (!parsed) {
|
|
308
|
+
return {
|
|
309
|
+
skip: {
|
|
310
|
+
name: original,
|
|
311
|
+
reason: `its url ${JSON.stringify(server.url)} will not parse`,
|
|
312
|
+
remedy: "fix the url in the client config and run adopt again"
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
const warnings = [];
|
|
317
|
+
let secretOnDisk = false;
|
|
318
|
+
const headers = {};
|
|
319
|
+
const lockHeaders = [];
|
|
320
|
+
for (const [header, value] of Object.entries(server.headers)) {
|
|
321
|
+
if (!INTERPOLATION.test(value)) {
|
|
322
|
+
headers[header] = value;
|
|
323
|
+
if (!HARMLESS_HEADERS.has(header.toLowerCase())) {
|
|
324
|
+
lockHeaders.push(header);
|
|
325
|
+
warnings.push(`the ${header} header's value goes into your locked keys, and gateway.yaml only names it`);
|
|
326
|
+
}
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
/*
|
|
330
|
+
* `gateway.yaml` rejects `${...}` in a header rather than expanding it, so
|
|
331
|
+
* a config that looked like it was interpolating a secret would ship the
|
|
332
|
+
* word. Refusing here, with the name of the header, is the same refusal
|
|
333
|
+
* the schema would make later -- but as a line in a report instead of a
|
|
334
|
+
* zod dump mid-adoption.
|
|
335
|
+
*
|
|
336
|
+
* Note this is not a case for credential_passthrough, whatever the
|
|
337
|
+
* schema's own message suggests: that forwards the *caller's* token, and a
|
|
338
|
+
* client speaking stdio to the gateway presents no token at all.
|
|
339
|
+
*/
|
|
340
|
+
if (!options.copyEnv) {
|
|
341
|
+
return {
|
|
342
|
+
skip: {
|
|
343
|
+
name: original,
|
|
344
|
+
reason: `its ${header} header interpolates ${value.match(INTERPOLATION)[0]}, which gateway.yaml refuses because the value would be sent literally`,
|
|
345
|
+
remedy: "re-run with --copy-env to resolve it from this shell now, or add the upstream by hand with the " +
|
|
346
|
+
"literal value in place"
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
const resolved = expandInterpolation(value, options.env ?? process.env);
|
|
351
|
+
if (resolved === undefined) {
|
|
352
|
+
return {
|
|
353
|
+
skip: {
|
|
354
|
+
name: original,
|
|
355
|
+
reason: `its ${header} header interpolates a variable that is not set in this shell`,
|
|
356
|
+
remedy: `export it and run adopt again, or add the upstream by hand with the literal value`
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
headers[header] = resolved;
|
|
361
|
+
secretOnDisk = true;
|
|
362
|
+
warnings.push(`the ${header} header was resolved to a literal value in the generated config`);
|
|
363
|
+
}
|
|
364
|
+
if (isLocalTarget(parsed.hostname) && !options.allowLocalHttp) {
|
|
365
|
+
return {
|
|
366
|
+
skip: {
|
|
367
|
+
name: original,
|
|
368
|
+
reason: `it listens on ${parsed.host}, and the egress guard refuses loopback and private addresses ` +
|
|
369
|
+
`(egress.block_private_ips, allow_ip_literals and allow_http all stand in the way)`,
|
|
370
|
+
remedy: "re-run with --allow-local-http, which turns those protections off for every HTTP upstream this " +
|
|
371
|
+
"gateway has -- not just this one"
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
const local = isLocalTarget(parsed.hostname);
|
|
376
|
+
if (local) {
|
|
377
|
+
warnings.push("adopting this upstream relaxes the SSRF protections for every HTTP upstream in this toolbox");
|
|
378
|
+
}
|
|
379
|
+
if (parsed.protocol === "http:") {
|
|
380
|
+
warnings.push("it is reached over plaintext HTTP, which the generated policy has to permit");
|
|
381
|
+
}
|
|
382
|
+
egressAllow.add(options.strictEgress ? parsed.host : parsed.hostname);
|
|
383
|
+
return {
|
|
384
|
+
planned: {
|
|
385
|
+
upstream: UpstreamSchema.parse({
|
|
386
|
+
name,
|
|
387
|
+
transport: "http",
|
|
388
|
+
url: server.url,
|
|
389
|
+
headers
|
|
390
|
+
}),
|
|
391
|
+
originalName: original,
|
|
392
|
+
warnings,
|
|
393
|
+
...(lockHeaders.length > 0 ? { lockHeaders } : {})
|
|
394
|
+
},
|
|
395
|
+
needsLocalHttp: local,
|
|
396
|
+
secretOnDisk
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
function planStdio(original, name, server, options, gatewayEnv, envOwner) {
|
|
400
|
+
const warnings = [];
|
|
401
|
+
const passEnv = [];
|
|
402
|
+
const ownEnv = {};
|
|
403
|
+
let secretOnDisk = false;
|
|
404
|
+
for (const key of Object.keys(server.env).sort()) {
|
|
405
|
+
const value = server.env[key];
|
|
406
|
+
if (options.copyEnv) {
|
|
407
|
+
/*
|
|
408
|
+
* `--copy-env` is the only place two upstreams can hold different values
|
|
409
|
+
* for the same variable name, because each gets its own `env` block.
|
|
410
|
+
*/
|
|
411
|
+
const resolved = INTERPOLATION.test(value) ? expandInterpolation(value, options.env ?? process.env) : value;
|
|
412
|
+
if (resolved === undefined) {
|
|
413
|
+
return {
|
|
414
|
+
skip: {
|
|
415
|
+
name: original,
|
|
416
|
+
reason: `its ${key} is set from a variable that is not set in this shell`,
|
|
417
|
+
remedy: "export it and run adopt again"
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
ownEnv[key] = resolved;
|
|
422
|
+
secretOnDisk = true;
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
/*
|
|
426
|
+
* The value moves to the gateway's own client entry and the upstream names
|
|
427
|
+
* it in `pass_env`. That keeps the secret in the file it was already in,
|
|
428
|
+
* at the permissions it already had, and keeps `gateway.yaml` free of
|
|
429
|
+
* secrets and safe to commit. It also works whether the original value was
|
|
430
|
+
* a literal or a `${VAR}` the client expands, because the value is carried
|
|
431
|
+
* across verbatim for the client to handle as it already did.
|
|
432
|
+
*
|
|
433
|
+
* Copying it into `gateway.yaml` instead would put a secret in a new file,
|
|
434
|
+
* and naming it in `pass_env` without moving it would read the *gateway's*
|
|
435
|
+
* environment, where a value that only ever lived in the client config is
|
|
436
|
+
* simply absent -- the server would then fail to authenticate with nothing
|
|
437
|
+
* to point at.
|
|
438
|
+
*/
|
|
439
|
+
const owner = envOwner.get(key);
|
|
440
|
+
if (owner !== undefined && gatewayEnv[key] !== value) {
|
|
441
|
+
/*
|
|
442
|
+
* `pass_env` is a list of names, so it cannot rename: one gateway entry
|
|
443
|
+
* can carry exactly one value for `API_KEY`. Both servers are skipped
|
|
444
|
+
* rather than one quietly getting the other's credential.
|
|
445
|
+
*/
|
|
446
|
+
return {
|
|
447
|
+
skip: {
|
|
448
|
+
name: original,
|
|
449
|
+
reason: `it sets ${key} to a different value than "${owner}" does, and the gateway forwards an ` +
|
|
450
|
+
`environment variable under its own name, so one entry cannot carry both`,
|
|
451
|
+
remedy: `re-run with --copy-env, which gives each upstream its own env block in gateway.yaml`
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
if (owner === undefined) {
|
|
456
|
+
gatewayEnv[key] = value;
|
|
457
|
+
envOwner.set(key, original);
|
|
458
|
+
}
|
|
459
|
+
passEnv.push(key);
|
|
460
|
+
}
|
|
461
|
+
if (passEnv.length > 0) {
|
|
462
|
+
warnings.push(`${passEnv.join(", ")} moved to the toolbox's own entry in your app and passed on by name`);
|
|
463
|
+
}
|
|
464
|
+
return {
|
|
465
|
+
planned: {
|
|
466
|
+
upstream: UpstreamSchema.parse({
|
|
467
|
+
name,
|
|
468
|
+
transport: "stdio",
|
|
469
|
+
command: server.command,
|
|
470
|
+
args: server.args,
|
|
471
|
+
env: ownEnv,
|
|
472
|
+
pass_env: passEnv,
|
|
473
|
+
// The folder it starts in (Codex, Antigravity), when the app gave one; only a full path reaches here.
|
|
474
|
+
...(typeof server["cwd"] === "string" ? { cwd: server["cwd"] } : {})
|
|
475
|
+
}),
|
|
476
|
+
originalName: original,
|
|
477
|
+
warnings
|
|
478
|
+
},
|
|
479
|
+
secretOnDisk
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Expands `${VAR}` and `${VAR:-default}`, the two forms the client supports.
|
|
484
|
+
*
|
|
485
|
+
* Returns undefined when a variable with no default is unset, so the caller
|
|
486
|
+
* can skip the server and say which one rather than writing an empty
|
|
487
|
+
* credential into a config.
|
|
488
|
+
*/
|
|
489
|
+
export function expandInterpolation(value, env) {
|
|
490
|
+
let missing = false;
|
|
491
|
+
const out = value.replace(/\$\{([^}:]+)(?::-([^}]*))?\}/g, (_match, name, fallback) => {
|
|
492
|
+
const resolved = env[name];
|
|
493
|
+
if (resolved !== undefined && resolved !== "")
|
|
494
|
+
return resolved;
|
|
495
|
+
if (fallback !== undefined)
|
|
496
|
+
return fallback;
|
|
497
|
+
missing = true;
|
|
498
|
+
return "";
|
|
499
|
+
});
|
|
500
|
+
return missing ? undefined : out;
|
|
501
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { CatalogEntry } from "./contracts.js";
|
|
3
|
+
declare const LaunchSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
4
|
+
transport: z.ZodLiteral<"stdio">;
|
|
5
|
+
command: z.ZodString;
|
|
6
|
+
args: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
7
|
+
env: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
|
|
8
|
+
field: z.ZodString;
|
|
9
|
+
prefix: z.ZodOptional<z.ZodString>;
|
|
10
|
+
}, z.core.$strict>]>>>;
|
|
11
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
12
|
+
transport: z.ZodLiteral<"http">;
|
|
13
|
+
url: z.ZodString;
|
|
14
|
+
headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
|
|
15
|
+
field: z.ZodString;
|
|
16
|
+
prefix: z.ZodOptional<z.ZodString>;
|
|
17
|
+
}, z.core.$strict>]>>>;
|
|
18
|
+
oauth: z.ZodOptional<z.ZodBoolean>;
|
|
19
|
+
}, z.core.$strict>], "transport">;
|
|
20
|
+
export declare const CatalogEntrySchema: z.ZodObject<{
|
|
21
|
+
id: z.ZodString;
|
|
22
|
+
name: z.ZodString;
|
|
23
|
+
category: z.ZodEnum<{
|
|
24
|
+
files: "files";
|
|
25
|
+
developer: "developer";
|
|
26
|
+
productivity: "productivity";
|
|
27
|
+
data: "data";
|
|
28
|
+
web: "web";
|
|
29
|
+
knowledge: "knowledge";
|
|
30
|
+
design: "design";
|
|
31
|
+
}>;
|
|
32
|
+
summary: z.ZodString;
|
|
33
|
+
description: z.ZodOptional<z.ZodString>;
|
|
34
|
+
publisher: z.ZodObject<{
|
|
35
|
+
name: z.ZodString;
|
|
36
|
+
official: z.ZodBoolean;
|
|
37
|
+
}, z.core.$strict>;
|
|
38
|
+
homepage: z.ZodString;
|
|
39
|
+
registry_name: z.ZodOptional<z.ZodString>;
|
|
40
|
+
requires: z.ZodOptional<z.ZodArray<z.ZodEnum<{
|
|
41
|
+
node: "node";
|
|
42
|
+
uv: "uv";
|
|
43
|
+
docker: "docker";
|
|
44
|
+
python: "python";
|
|
45
|
+
git: "git";
|
|
46
|
+
}>>>;
|
|
47
|
+
variants: z.ZodArray<z.ZodObject<{
|
|
48
|
+
id: z.ZodString;
|
|
49
|
+
label: z.ZodString;
|
|
50
|
+
summary: z.ZodOptional<z.ZodString>;
|
|
51
|
+
launch: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
52
|
+
transport: z.ZodLiteral<"stdio">;
|
|
53
|
+
command: z.ZodString;
|
|
54
|
+
args: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
55
|
+
env: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
|
|
56
|
+
field: z.ZodString;
|
|
57
|
+
prefix: z.ZodOptional<z.ZodString>;
|
|
58
|
+
}, z.core.$strict>]>>>;
|
|
59
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
60
|
+
transport: z.ZodLiteral<"http">;
|
|
61
|
+
url: z.ZodString;
|
|
62
|
+
headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
|
|
63
|
+
field: z.ZodString;
|
|
64
|
+
prefix: z.ZodOptional<z.ZodString>;
|
|
65
|
+
}, z.core.$strict>]>>>;
|
|
66
|
+
oauth: z.ZodOptional<z.ZodBoolean>;
|
|
67
|
+
}, z.core.$strict>], "transport">;
|
|
68
|
+
fields: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
69
|
+
requires: z.ZodOptional<z.ZodArray<z.ZodEnum<{
|
|
70
|
+
node: "node";
|
|
71
|
+
uv: "uv";
|
|
72
|
+
docker: "docker";
|
|
73
|
+
python: "python";
|
|
74
|
+
git: "git";
|
|
75
|
+
}>>>;
|
|
76
|
+
}, z.core.$strict>>;
|
|
77
|
+
fields: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
78
|
+
id: z.ZodString;
|
|
79
|
+
kind: z.ZodEnum<{
|
|
80
|
+
secret: "secret";
|
|
81
|
+
url: "url";
|
|
82
|
+
folder: "folder";
|
|
83
|
+
folders: "folders";
|
|
84
|
+
text: "text";
|
|
85
|
+
choice: "choice";
|
|
86
|
+
toggle: "toggle";
|
|
87
|
+
}>;
|
|
88
|
+
label: z.ZodString;
|
|
89
|
+
help: z.ZodOptional<z.ZodString>;
|
|
90
|
+
placeholder: z.ZodOptional<z.ZodString>;
|
|
91
|
+
required: z.ZodOptional<z.ZodBoolean>;
|
|
92
|
+
default: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodBoolean, z.ZodArray<z.ZodString>]>>;
|
|
93
|
+
choices: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
94
|
+
value: z.ZodString;
|
|
95
|
+
label: z.ZodString;
|
|
96
|
+
}, z.core.$strict>>>;
|
|
97
|
+
validate: z.ZodOptional<z.ZodObject<{
|
|
98
|
+
pattern: z.ZodString;
|
|
99
|
+
message: z.ZodString;
|
|
100
|
+
}, z.core.$strict>>;
|
|
101
|
+
technical: z.ZodOptional<z.ZodString>;
|
|
102
|
+
}, z.core.$strict>>>;
|
|
103
|
+
guide: z.ZodArray<z.ZodObject<{
|
|
104
|
+
title: z.ZodString;
|
|
105
|
+
body: z.ZodString;
|
|
106
|
+
link: z.ZodOptional<z.ZodObject<{
|
|
107
|
+
label: z.ZodString;
|
|
108
|
+
url: z.ZodString;
|
|
109
|
+
}, z.core.$strict>>;
|
|
110
|
+
image: z.ZodOptional<z.ZodString>;
|
|
111
|
+
}, z.core.$strict>>;
|
|
112
|
+
pack: z.ZodOptional<z.ZodString>;
|
|
113
|
+
pack_within_field: z.ZodOptional<z.ZodString>;
|
|
114
|
+
try_prompt: z.ZodOptional<z.ZodString>;
|
|
115
|
+
keywords: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
116
|
+
check: z.ZodOptional<z.ZodObject<{
|
|
117
|
+
tool: z.ZodString;
|
|
118
|
+
args: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
119
|
+
}, z.core.$strict>>;
|
|
120
|
+
}, z.core.$strict>;
|
|
121
|
+
/** Every `{{fields.x}}`, `{{...fields.x}}` and `{ field: x }` in a launch template. */
|
|
122
|
+
export declare function fieldsUsedBy(launch: z.infer<typeof LaunchSchema> | CatalogEntry["variants"][number]["launch"]): {
|
|
123
|
+
id: string;
|
|
124
|
+
how: "inline" | "spread" | "value";
|
|
125
|
+
}[];
|
|
126
|
+
/** The catalog folder shipped with the package; the same from src/ and dist/. */
|
|
127
|
+
export declare function catalogDir(): string;
|
|
128
|
+
/** Every entry, validated, sorted by name. Throws naming the file on a bad entry. */
|
|
129
|
+
export declare function loadCatalog(dir?: string): CatalogEntry[];
|
|
130
|
+
/** A catalog entry's check call (see the schema above). */
|
|
131
|
+
export interface CatalogCheck {
|
|
132
|
+
tool: string;
|
|
133
|
+
args: Record<string, unknown>;
|
|
134
|
+
}
|
|
135
|
+
/** The check call our own catalog sets for an entry, or undefined. Never taken from a request. */
|
|
136
|
+
export declare function catalogCheck(id: string, dir?: string): CatalogCheck | undefined;
|
|
137
|
+
export declare function catalogEntry(id: string, dir?: string): CatalogEntry | undefined;
|
|
138
|
+
/** registry_name -> catalog id, so a public-list result can open the curated entry instead. */
|
|
139
|
+
export declare function catalogRegistryNames(dir?: string): Map<string, string>;
|
|
140
|
+
export {};
|