hmcp-gateway 0.0.2 → 0.1.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 +41 -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 +351 -0
- package/dist/adopt-cli.js +1857 -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 +386 -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 +535 -0
- package/dist/client-allow.d.ts +42 -0
- package/dist/client-allow.js +95 -0
- package/dist/client-config.d.ts +200 -0
- package/dist/client-config.js +784 -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 +49 -0
- package/dist/connect-client.js +249 -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/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 +756 -0
- package/dist/gateway.js +2106 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +26 -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/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 +50 -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
package/dist/snippet.js
ADDED
|
@@ -0,0 +1,826 @@
|
|
|
1
|
+
import { isIpLiteral } from "hmcp-core";
|
|
2
|
+
import { addressHoldsKey, addressKind, localAddressWarning, safeName } from "./draft.js";
|
|
3
|
+
import { dockerArgsProblem, dockerRunOptions } from "./registry-map.js";
|
|
4
|
+
import { loadCatalog } from "./catalog.js";
|
|
5
|
+
/** Setting names that hold a key. */
|
|
6
|
+
const SECRET_NAME = /(TOKEN|KEY|SECRET|PASSWORD|PASSWD|CREDENTIAL|(^|_)PAT($|_))/i;
|
|
7
|
+
/** Values that are a real key whatever they are called. */
|
|
8
|
+
const SECRET_VALUE = [
|
|
9
|
+
/^gh[pousr]_[A-Za-z0-9]{20,}$/,
|
|
10
|
+
/^github_pat_[A-Za-z0-9_]{20,}$/,
|
|
11
|
+
/^sk-[A-Za-z0-9_-]{16,}$/,
|
|
12
|
+
/^sk_(live|test)_[A-Za-z0-9]{16,}$/,
|
|
13
|
+
/^rk_(live|test)_[A-Za-z0-9]{16,}$/,
|
|
14
|
+
/^xox[abposr]-[A-Za-z0-9-]{10,}$/,
|
|
15
|
+
/^hf_[A-Za-z0-9]{20,}$/,
|
|
16
|
+
/^glpat-[A-Za-z0-9_-]{16,}$/,
|
|
17
|
+
/^AKIA[0-9A-Z]{16}$/,
|
|
18
|
+
/^ntn_[A-Za-z0-9]{20,}$/,
|
|
19
|
+
/^lin_api_[A-Za-z0-9]{20,}$/,
|
|
20
|
+
/^sntrys_[A-Za-z0-9_]{20,}$/,
|
|
21
|
+
/^AIza[0-9A-Za-z_-]{30,}$/
|
|
22
|
+
];
|
|
23
|
+
/** A connection string with a password in it: postgresql://user:pw@host/db. */
|
|
24
|
+
const URL_WITH_PASSWORD = /^[a-z][a-z0-9+.-]*:\/\/[^/\s:@]+:[^/\s@]+@/i;
|
|
25
|
+
const looksLikeKey = (v) => SECRET_VALUE.some((re) => re.test(v.trim()));
|
|
26
|
+
/** Text that stands for something the person has to fill in. */
|
|
27
|
+
function isPlaceholder(v) {
|
|
28
|
+
const t = v.trim();
|
|
29
|
+
return (t === "" ||
|
|
30
|
+
/^<[^>]+>$/.test(t) ||
|
|
31
|
+
/<[A-Za-z_ -]+>/.test(t) ||
|
|
32
|
+
/^\$\{[^}]+\}$/.test(t) ||
|
|
33
|
+
/^\$[A-Z_][A-Z0-9_]*$/.test(t) ||
|
|
34
|
+
/\{\{[^}]+\}\}/.test(t) ||
|
|
35
|
+
/\bYOUR[_ -]?[A-Z0-9_ -]*/i.test(t) ||
|
|
36
|
+
/\*{3,}/.test(t) ||
|
|
37
|
+
/@example\.(com|org|net)\b/i.test(t) ||
|
|
38
|
+
/(^|[/\\])path[/\\]to([/\\]|$)/i.test(t) ||
|
|
39
|
+
/^(x{3,}|\.{3})$/i.test(t) ||
|
|
40
|
+
/_here$/i.test(t) ||
|
|
41
|
+
/^(token|api[_-]?key|secret|password)$/i.test(t) ||
|
|
42
|
+
/:\/\/(user(name)?|you):(pass(word)?|secret)@/i.test(t));
|
|
43
|
+
}
|
|
44
|
+
/** Why an address can't be kept as written: a key in it would be saved as plain text. Null when it's fine. */
|
|
45
|
+
export function addressKeyProblem(url) {
|
|
46
|
+
const named = addressHoldsKey(url);
|
|
47
|
+
if (named)
|
|
48
|
+
return named;
|
|
49
|
+
try {
|
|
50
|
+
for (const [, v] of new URL(url).searchParams)
|
|
51
|
+
if (looksLikeKey(v))
|
|
52
|
+
return "query";
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
/* not an address */
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
/** A folder in an example ("/path/to/dir", "/Users/username/Desktop"). */
|
|
60
|
+
function isPlaceholderPath(v) {
|
|
61
|
+
return (/^(\/|~\/|[A-Za-z]:\\)/.test(v) &&
|
|
62
|
+
/(\/path\/to\b|\/Users\/(username|user|you|yourname|<[^>]+>)\b|\/home\/(username|user|you)\b|\\Users\\(username|user|you)\b|\/other\/allowed|allowed\/dir|\/to\/)/i.test(v));
|
|
63
|
+
}
|
|
64
|
+
/* ------------------------------------------------------------------ entry point */
|
|
65
|
+
export function parseSnippet(text) {
|
|
66
|
+
const warnings = [];
|
|
67
|
+
const cleaned = stripFences(text).trim();
|
|
68
|
+
if (!cleaned)
|
|
69
|
+
return { servers: [], warnings: ["Nothing was pasted."], recognisedAs: null };
|
|
70
|
+
const json = tryJson(cleaned);
|
|
71
|
+
if (json !== undefined)
|
|
72
|
+
return fromJson(json, warnings);
|
|
73
|
+
const lines = joinContinuations(cleaned)
|
|
74
|
+
.split("\n")
|
|
75
|
+
.map((l) => l.trim().replace(/^\$\s+/, "").replace(/^>\s+/, ""))
|
|
76
|
+
.filter((l) => l && !l.startsWith("#"));
|
|
77
|
+
const servers = [];
|
|
78
|
+
let recognisedAs = null;
|
|
79
|
+
for (const line of lines) {
|
|
80
|
+
if (/^claude\s+mcp\s+add(-json)?\b/.test(line)) {
|
|
81
|
+
const parsed = fromClaudeAdd(line, warnings);
|
|
82
|
+
if (parsed)
|
|
83
|
+
servers.push(parsed);
|
|
84
|
+
recognisedAs ??= "a Claude Code command";
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (/^https?:\/\/\S+$/.test(line)) {
|
|
88
|
+
const parsed = build({ url: line }, warnings, {});
|
|
89
|
+
if (parsed)
|
|
90
|
+
servers.push(parsed);
|
|
91
|
+
recognisedAs ??= "a server address";
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const words = tokenize(line);
|
|
95
|
+
const env = {};
|
|
96
|
+
while (words.length > 0 && /^[A-Za-z_][A-Za-z0-9_]*=/.test(words[0])) {
|
|
97
|
+
const w = words.shift();
|
|
98
|
+
const i = w.indexOf("=");
|
|
99
|
+
env[w.slice(0, i)] = w.slice(i + 1);
|
|
100
|
+
}
|
|
101
|
+
if (words.length > 0 && RUNNERS.has(baseCommand(words[0]))) {
|
|
102
|
+
const parsed = build({ command: words[0], args: words.slice(1), env }, warnings, {});
|
|
103
|
+
if (parsed)
|
|
104
|
+
servers.push(parsed);
|
|
105
|
+
recognisedAs ??= "a one-line command";
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (servers.length === 0 && warnings.length === 0) {
|
|
109
|
+
warnings.push("This didn't look like setup instructions. Copy the part of the server's instructions that shows how to add it: usually a block of text containing \"mcpServers\", a line starting with npx, uvx or docker, or a web address.");
|
|
110
|
+
}
|
|
111
|
+
return { servers, warnings, recognisedAs };
|
|
112
|
+
}
|
|
113
|
+
/* ------------------------------------------------------------------ JSON */
|
|
114
|
+
function fromJson(json, warnings) {
|
|
115
|
+
const obj = asRecord(json);
|
|
116
|
+
if (!obj)
|
|
117
|
+
return { servers: [], warnings: ["This JSON is not an object with servers in it."], recognisedAs: "JSON" };
|
|
118
|
+
let table;
|
|
119
|
+
let inputs = [];
|
|
120
|
+
let recognisedAs;
|
|
121
|
+
const mcp = asRecord(obj["mcp"]);
|
|
122
|
+
if (asRecord(obj["mcpServers"])) {
|
|
123
|
+
table = asRecord(obj["mcpServers"]);
|
|
124
|
+
recognisedAs = "settings for Claude Desktop or a similar app";
|
|
125
|
+
}
|
|
126
|
+
else if (mcp && asRecord(mcp["servers"])) {
|
|
127
|
+
table = asRecord(mcp["servers"]);
|
|
128
|
+
inputs = readInputs(mcp["inputs"]);
|
|
129
|
+
recognisedAs = "VS Code settings";
|
|
130
|
+
}
|
|
131
|
+
else if (asRecord(obj["servers"])) {
|
|
132
|
+
table = asRecord(obj["servers"]);
|
|
133
|
+
inputs = readInputs(obj["inputs"]);
|
|
134
|
+
recognisedAs = "VS Code settings";
|
|
135
|
+
}
|
|
136
|
+
else if (isEntry(obj)) {
|
|
137
|
+
table = { "": obj };
|
|
138
|
+
recognisedAs = "a single server entry";
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
// `"name": { "command": … }` pasted with its braces, or several of them.
|
|
142
|
+
const named = Object.entries(obj).filter(([, v]) => isEntry(asRecord(v)));
|
|
143
|
+
if (named.length === 0) {
|
|
144
|
+
return {
|
|
145
|
+
servers: [],
|
|
146
|
+
warnings: ["This JSON has no servers in it: look for a block with \"command\" or \"url\"."],
|
|
147
|
+
recognisedAs: "JSON"
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
table = Object.fromEntries(named);
|
|
151
|
+
recognisedAs = named.length === 1 ? "a single server entry" : "server entries";
|
|
152
|
+
}
|
|
153
|
+
const byId = new Map(inputs.map((i) => [i.id, i]));
|
|
154
|
+
const servers = [];
|
|
155
|
+
for (const [name, raw] of Object.entries(table ?? {})) {
|
|
156
|
+
const e = asRecord(raw);
|
|
157
|
+
if (!e || !isEntry(e)) {
|
|
158
|
+
warnings.push(`“${name}” was skipped: it has neither a command nor an address.`);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (e["disabled"] === true)
|
|
162
|
+
warnings.push(`“${name}” is switched off in what you pasted; it was read anyway.`);
|
|
163
|
+
const entry = {
|
|
164
|
+
...(name ? { name } : {}),
|
|
165
|
+
...(typeof e["command"] === "string" ? { command: e["command"] } : {}),
|
|
166
|
+
...(Array.isArray(e["args"]) ? { args: e["args"].map(String) } : {}),
|
|
167
|
+
...(asRecord(e["env"]) ? { env: stringRecord(asRecord(e["env"])) } : {}),
|
|
168
|
+
...(typeof (e["url"] ?? e["serverUrl"] ?? e["httpUrl"]) === "string" ? { url: String(e["url"] ?? e["serverUrl"] ?? e["httpUrl"]) } : {}),
|
|
169
|
+
...(typeof e["type"] === "string" ? { type: e["type"] } : {}),
|
|
170
|
+
...(asRecord(e["headers"]) ? { headers: stringRecord(asRecord(e["headers"])) } : {})
|
|
171
|
+
};
|
|
172
|
+
const parsed = build(entry, warnings, Object.fromEntries(byId));
|
|
173
|
+
if (parsed)
|
|
174
|
+
servers.push(parsed);
|
|
175
|
+
}
|
|
176
|
+
return { servers, warnings, recognisedAs };
|
|
177
|
+
}
|
|
178
|
+
function isEntry(e) {
|
|
179
|
+
return !!e && (typeof e["command"] === "string" || typeof e["url"] === "string" || typeof e["serverUrl"] === "string" || typeof e["httpUrl"] === "string");
|
|
180
|
+
}
|
|
181
|
+
function readInputs(raw) {
|
|
182
|
+
if (!Array.isArray(raw))
|
|
183
|
+
return [];
|
|
184
|
+
return raw
|
|
185
|
+
.map(asRecord)
|
|
186
|
+
.filter((i) => !!i && typeof i["id"] === "string")
|
|
187
|
+
.map((i) => ({
|
|
188
|
+
id: String(i["id"]),
|
|
189
|
+
...(typeof i["description"] === "string" ? { description: i["description"] } : {}),
|
|
190
|
+
...(i["password"] === true ? { password: true } : {})
|
|
191
|
+
}));
|
|
192
|
+
}
|
|
193
|
+
/** JSON with comments and trailing commas, or a `"name": {…}` fragment without its outer braces. */
|
|
194
|
+
function tryJson(text) {
|
|
195
|
+
if (!/^[[{"]/.test(text))
|
|
196
|
+
return undefined;
|
|
197
|
+
const clean = stripTrailingCommas(stripComments(text));
|
|
198
|
+
for (const candidate of [clean, `{${clean}}`]) {
|
|
199
|
+
try {
|
|
200
|
+
return JSON.parse(candidate);
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
/* try the next */
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
function stripComments(s) {
|
|
209
|
+
let out = "";
|
|
210
|
+
let inString = false;
|
|
211
|
+
for (let i = 0; i < s.length; i++) {
|
|
212
|
+
const c = s[i];
|
|
213
|
+
if (inString) {
|
|
214
|
+
out += c;
|
|
215
|
+
if (c === "\\")
|
|
216
|
+
out += s[++i] ?? "";
|
|
217
|
+
else if (c === '"')
|
|
218
|
+
inString = false;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (c === '"') {
|
|
222
|
+
inString = true;
|
|
223
|
+
out += c;
|
|
224
|
+
}
|
|
225
|
+
else if (c === "/" && s[i + 1] === "/") {
|
|
226
|
+
while (i < s.length && s[i] !== "\n")
|
|
227
|
+
i++;
|
|
228
|
+
out += "\n";
|
|
229
|
+
}
|
|
230
|
+
else if (c === "/" && s[i + 1] === "*") {
|
|
231
|
+
i += 2;
|
|
232
|
+
while (i < s.length && !(s[i] === "*" && s[i + 1] === "/"))
|
|
233
|
+
i++;
|
|
234
|
+
i++;
|
|
235
|
+
}
|
|
236
|
+
else
|
|
237
|
+
out += c;
|
|
238
|
+
}
|
|
239
|
+
return out;
|
|
240
|
+
}
|
|
241
|
+
function stripTrailingCommas(s) {
|
|
242
|
+
let out = "";
|
|
243
|
+
let inString = false;
|
|
244
|
+
for (let i = 0; i < s.length; i++) {
|
|
245
|
+
const c = s[i];
|
|
246
|
+
if (inString) {
|
|
247
|
+
out += c;
|
|
248
|
+
if (c === "\\")
|
|
249
|
+
out += s[++i] ?? "";
|
|
250
|
+
else if (c === '"')
|
|
251
|
+
inString = false;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (c === '"')
|
|
255
|
+
inString = true;
|
|
256
|
+
if (c === ",") {
|
|
257
|
+
let j = i + 1;
|
|
258
|
+
while (j < s.length && /\s/.test(s[j]))
|
|
259
|
+
j++;
|
|
260
|
+
if (s[j] === "}" || s[j] === "]")
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
out += c;
|
|
264
|
+
}
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
267
|
+
/* ------------------------------------------------------------------ command lines */
|
|
268
|
+
const RUNNERS = new Map([
|
|
269
|
+
["npx", "node"],
|
|
270
|
+
["node", "node"],
|
|
271
|
+
["bunx", null],
|
|
272
|
+
["uvx", "uv"],
|
|
273
|
+
["uv", "uv"],
|
|
274
|
+
["docker", "docker"],
|
|
275
|
+
["python", "python"],
|
|
276
|
+
["python3", "python"],
|
|
277
|
+
["pipx", "python"],
|
|
278
|
+
["deno", null]
|
|
279
|
+
]);
|
|
280
|
+
const baseCommand = (cmd) => (cmd.split(/[\\/]/).pop() ?? cmd).replace(/\.(exe|cmd)$/i, "").toLowerCase();
|
|
281
|
+
function fromClaudeAdd(line, warnings) {
|
|
282
|
+
const words = tokenize(line).slice(3); // claude mcp add
|
|
283
|
+
if (/^claude\s+mcp\s+add-json\b/.test(line)) {
|
|
284
|
+
const name = words[0];
|
|
285
|
+
const json = tryJson(words[1] ?? "");
|
|
286
|
+
const parsed = fromJson({ [name ?? "server"]: json }, warnings);
|
|
287
|
+
return parsed.servers[0] ?? null;
|
|
288
|
+
}
|
|
289
|
+
let transport = "stdio";
|
|
290
|
+
const env = {};
|
|
291
|
+
const headers = {};
|
|
292
|
+
const rest = [];
|
|
293
|
+
let command = null;
|
|
294
|
+
for (let i = 0; i < words.length; i++) {
|
|
295
|
+
const w = words[i];
|
|
296
|
+
if (command) {
|
|
297
|
+
command.push(w);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (w === "--") {
|
|
301
|
+
command = [];
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
const [flag, inline] = w.startsWith("--") && w.includes("=") ? [w.slice(0, w.indexOf("=")), w.slice(w.indexOf("=") + 1)] : [w, undefined];
|
|
305
|
+
const next = () => inline ?? words[++i] ?? "";
|
|
306
|
+
if (flag === "--transport" || flag === "-t")
|
|
307
|
+
transport = next();
|
|
308
|
+
else if (flag === "-e" || flag === "--env") {
|
|
309
|
+
const kv = next();
|
|
310
|
+
const eq = kv.indexOf("=");
|
|
311
|
+
if (eq > 0)
|
|
312
|
+
env[kv.slice(0, eq)] = kv.slice(eq + 1);
|
|
313
|
+
}
|
|
314
|
+
else if (flag === "-H" || flag === "--header") {
|
|
315
|
+
const kv = next();
|
|
316
|
+
const colon = kv.indexOf(":");
|
|
317
|
+
if (colon > 0)
|
|
318
|
+
headers[kv.slice(0, colon).trim()] = kv.slice(colon + 1).trim();
|
|
319
|
+
}
|
|
320
|
+
else if (flag === "-s" || flag === "--scope" || flag === "--client-id" || flag === "--callback-port")
|
|
321
|
+
next();
|
|
322
|
+
else if (flag === "--client-secret") {
|
|
323
|
+
/* takes no value on the command line */
|
|
324
|
+
}
|
|
325
|
+
else if (rest.length >= 1 && transport === "stdio" && !w.startsWith("-")) {
|
|
326
|
+
// `claude mcp add name npx pkg` with no `--`: everything after the name is the command.
|
|
327
|
+
command = [w];
|
|
328
|
+
}
|
|
329
|
+
else
|
|
330
|
+
rest.push(w);
|
|
331
|
+
}
|
|
332
|
+
const name = rest[0];
|
|
333
|
+
if (transport === "sse") {
|
|
334
|
+
warnings.push(`“${name ?? "This server"}” uses an older kind of connection (SSE) that hardened-mcp does not support.`);
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
if (transport === "http" || transport === "streamable-http") {
|
|
338
|
+
const url = rest[1];
|
|
339
|
+
if (!url) {
|
|
340
|
+
warnings.push("The claude mcp add command has no address after the name.");
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
return build({ ...(name ? { name } : {}), url, headers }, warnings, {});
|
|
344
|
+
}
|
|
345
|
+
if (!command || command.length === 0) {
|
|
346
|
+
warnings.push("The claude mcp add command has no program to run after the name.");
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
return build({ ...(name ? { name } : {}), command: command[0], args: command.slice(1), env }, warnings, {});
|
|
350
|
+
}
|
|
351
|
+
/** Shell-style words: quotes, backslash escapes, `\` line continuations. */
|
|
352
|
+
export function tokenize(line) {
|
|
353
|
+
const out = [];
|
|
354
|
+
let cur = "";
|
|
355
|
+
let has = false;
|
|
356
|
+
let quote = null;
|
|
357
|
+
for (let i = 0; i < line.length; i++) {
|
|
358
|
+
const c = line[i];
|
|
359
|
+
if (quote === "'") {
|
|
360
|
+
if (c === "'")
|
|
361
|
+
quote = null;
|
|
362
|
+
else
|
|
363
|
+
cur += c;
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (quote === '"') {
|
|
367
|
+
if (c === '"')
|
|
368
|
+
quote = null;
|
|
369
|
+
else if (c === "\\" && /["\\$`]/.test(line[i + 1] ?? ""))
|
|
370
|
+
cur += line[++i];
|
|
371
|
+
else
|
|
372
|
+
cur += c;
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (c === "'" || c === '"') {
|
|
376
|
+
quote = c;
|
|
377
|
+
has = true;
|
|
378
|
+
}
|
|
379
|
+
else if (c === "\\" && i + 1 < line.length) {
|
|
380
|
+
const n = line[++i];
|
|
381
|
+
if (n !== "\n") {
|
|
382
|
+
cur += n;
|
|
383
|
+
has = true;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
else if (/\s/.test(c)) {
|
|
387
|
+
if (has || cur)
|
|
388
|
+
out.push(cur);
|
|
389
|
+
cur = "";
|
|
390
|
+
has = false;
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
cur += c;
|
|
394
|
+
has = true;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
if (has || cur)
|
|
398
|
+
out.push(cur);
|
|
399
|
+
return out;
|
|
400
|
+
}
|
|
401
|
+
function joinContinuations(text) {
|
|
402
|
+
return text.replace(/\\\r?\n\s*/g, " ").replace(/\^\r?\n\s*/g, " ");
|
|
403
|
+
}
|
|
404
|
+
function stripFences(text) {
|
|
405
|
+
const fence = /```[a-z]*\s*\n([\s\S]*?)```/gi;
|
|
406
|
+
const blocks = [...text.matchAll(fence)].map((m) => m[1]);
|
|
407
|
+
return blocks.length > 0 ? blocks.join("\n") : text;
|
|
408
|
+
}
|
|
409
|
+
/* ------------------------------------------------------------------ building a draft */
|
|
410
|
+
class Fields {
|
|
411
|
+
list = [];
|
|
412
|
+
values = {};
|
|
413
|
+
ids = new Set();
|
|
414
|
+
add(raw, field, value) {
|
|
415
|
+
let id = raw.toLowerCase().replace(/[^a-z0-9_]+/g, "_").replace(/^_+|_+$/g, "");
|
|
416
|
+
if (!/^[a-z]/.test(id))
|
|
417
|
+
id = `f_${id}`;
|
|
418
|
+
let unique = id;
|
|
419
|
+
for (let i = 2; this.ids.has(unique); i++)
|
|
420
|
+
unique = `${id}_${i}`;
|
|
421
|
+
this.ids.add(unique);
|
|
422
|
+
this.list.push({ id: unique, ...field });
|
|
423
|
+
if (value !== undefined && value !== "" && !(Array.isArray(value) && value.length === 0))
|
|
424
|
+
this.values[unique] = value;
|
|
425
|
+
return unique;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/** A setting (env or header) as a template value, lifting keys out. */
|
|
429
|
+
function settingValue(key, raw, fields, inputs, where, warnings) {
|
|
430
|
+
let value = raw;
|
|
431
|
+
let prefix;
|
|
432
|
+
const bearer = /^(bearer|token|basic)\s+(.+)$/i.exec(value);
|
|
433
|
+
// "Sentry-Bearer ${SENTRY_ACCESS_TOKEN}": any short word in front of a placeholder is the prefix.
|
|
434
|
+
const before = /^(\S[^\s]*(?:\s\S+)*?\s+)(\S+)$/.exec(value);
|
|
435
|
+
if (where === "header" && bearer) {
|
|
436
|
+
prefix = `${bearer[1][0].toUpperCase()}${bearer[1].slice(1).toLowerCase()} `;
|
|
437
|
+
value = bearer[2];
|
|
438
|
+
}
|
|
439
|
+
else if (where === "header" && before && isPlaceholder(before[2]) && !isPlaceholder(before[1].trim()) && before[1].length <= 32) {
|
|
440
|
+
prefix = before[1].replace(/\s+$/, " ");
|
|
441
|
+
value = before[2];
|
|
442
|
+
}
|
|
443
|
+
const inputRef = /^\$\{input:([^}]+)\}$/.exec(value.trim());
|
|
444
|
+
const input = inputRef ? inputs[inputRef[1]] : undefined;
|
|
445
|
+
// `${SENTRY_ACCESS_TOKEN}` / `${env:X}` / `$X`: ask for X by its own name.
|
|
446
|
+
const varRef = inputRef ? undefined : /^\$\{(?:env:)?([A-Za-z_][A-Za-z0-9_]*)\}$|^\$([A-Za-z_][A-Za-z0-9_]*)$/.exec(value.trim());
|
|
447
|
+
const varName = varRef ? (varRef[1] ?? varRef[2]) : undefined;
|
|
448
|
+
const nameSaysKey = SECRET_NAME.test(key) || (where === "header" && /^(authorization|x-api-key|api-key)$/i.test(key));
|
|
449
|
+
// A value that is itself a set of headers or a sign-in line ("Bearer ntn_…") holds a key.
|
|
450
|
+
const valueCarriesKey = /\b(bearer|authorization)\b/i.test(value) || (!!varName && SECRET_NAME.test(varName));
|
|
451
|
+
const valueIsKey = looksLikeKey(value) || URL_WITH_PASSWORD.test(value);
|
|
452
|
+
const placeholder = isPlaceholder(value) || !!inputRef;
|
|
453
|
+
const secret = nameSaysKey || valueIsKey || valueCarriesKey || input?.password === true || (!!prefix && placeholder);
|
|
454
|
+
if (secret) {
|
|
455
|
+
const label = input?.description ?? humanise(varName && placeholder ? varName : key);
|
|
456
|
+
const help = input?.description ?? `Kept in your locked keys. Its technical name is ${key}.`;
|
|
457
|
+
const id = fields.add(key, { kind: "secret", label, required: true, technical: key, help }, placeholder ? undefined : value);
|
|
458
|
+
return prefix ? { field: id, prefix } : { field: id };
|
|
459
|
+
}
|
|
460
|
+
if (placeholder) {
|
|
461
|
+
const example = !inputRef && !varName && value.trim() !== "" ? value.trim() : undefined;
|
|
462
|
+
const id = fields.add(key, {
|
|
463
|
+
kind: "text",
|
|
464
|
+
label: input?.description ?? humanise(key),
|
|
465
|
+
required: true,
|
|
466
|
+
technical: key,
|
|
467
|
+
...(example ? { placeholder: example } : {})
|
|
468
|
+
});
|
|
469
|
+
return prefix ? `${prefix}{{fields.${id}}}` : `{{fields.${id}}}`;
|
|
470
|
+
}
|
|
471
|
+
if (/\$\{[^}]+\}/.test(raw))
|
|
472
|
+
warnings.push(`The ${where === "env" ? "setting" : "header"} ${key} refers to another value with \${…}; it was kept as written.`);
|
|
473
|
+
return raw;
|
|
474
|
+
}
|
|
475
|
+
function build(entry, warnings, inputs) {
|
|
476
|
+
const fields = new Fields();
|
|
477
|
+
const serverName = entry.name ?? guessName(entry);
|
|
478
|
+
const display = catalogDisplay(entry) ?? (entry.name ? humanise(entry.name) : (displayForAddress(entry.url) ?? localDisplay(entry) ?? humanise(serverName)));
|
|
479
|
+
let launch;
|
|
480
|
+
const requires = [];
|
|
481
|
+
const draftWarnings = [];
|
|
482
|
+
const type = (entry.type ?? "").toLowerCase();
|
|
483
|
+
if (entry.url !== undefined || type === "http" || type === "sse" || type === "streamable-http" || type === "streamablehttp") {
|
|
484
|
+
if (type === "sse" || (!type && /\/sse\/?$/.test(entry.url ?? ""))) {
|
|
485
|
+
warnings.push(`“${display}” uses an older kind of connection (SSE) that hardened-mcp does not support. Look in its README for an address ending in /mcp.`);
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
const url = entry.url ?? "";
|
|
489
|
+
let parsedUrl;
|
|
490
|
+
try {
|
|
491
|
+
parsedUrl = new URL(url);
|
|
492
|
+
}
|
|
493
|
+
catch {
|
|
494
|
+
warnings.push(`“${display}” has an address that is not complete: ${url}`);
|
|
495
|
+
return null;
|
|
496
|
+
}
|
|
497
|
+
const keyIn = addressKeyProblem(url);
|
|
498
|
+
if (keyIn === "userinfo") {
|
|
499
|
+
warnings.push(`“${display}” has a name and password inside its address, which cannot be kept locked. Remove them and use a key instead.`);
|
|
500
|
+
return null;
|
|
501
|
+
}
|
|
502
|
+
if (keyIn === "query" || keyIn === "path") {
|
|
503
|
+
warnings.push(`“${display}” has a key inside its address${keyIn === "query" ? " (after the “?”)" : ""}, where it would be saved as plain text. ` +
|
|
504
|
+
"hardened-mcp only keeps keys in the locked keys file, so this server was not added. " +
|
|
505
|
+
"Check its instructions for a way to send the key as a header instead.");
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
const headers = {};
|
|
509
|
+
for (const [k, v] of Object.entries(entry.headers ?? {}))
|
|
510
|
+
headers[k] = settingValue(k, v, fields, inputs, "header", warnings);
|
|
511
|
+
launch = { transport: "http", url, ...(Object.keys(headers).length ? { headers } : {}) };
|
|
512
|
+
const kind = addressKind(parsedUrl.hostname);
|
|
513
|
+
if (kind === "link-local") {
|
|
514
|
+
warnings.push(`“${display}”: ${localAddressWarning(kind)}`);
|
|
515
|
+
return null;
|
|
516
|
+
}
|
|
517
|
+
const local = localAddressWarning(kind);
|
|
518
|
+
if (local)
|
|
519
|
+
draftWarnings.push(local);
|
|
520
|
+
else if (parsedUrl.protocol === "http:")
|
|
521
|
+
draftWarnings.push("This address is not encrypted (http, not https).");
|
|
522
|
+
}
|
|
523
|
+
else {
|
|
524
|
+
const command = entry.command ?? "";
|
|
525
|
+
const runner = RUNNERS.get(baseCommand(command));
|
|
526
|
+
if (runner)
|
|
527
|
+
requires.push(runner);
|
|
528
|
+
const docker = baseCommand(command) === "docker";
|
|
529
|
+
/*
|
|
530
|
+
* `docker run -e NAME=value`: the value would sit in the arguments as
|
|
531
|
+
* plain text. Docker reads `-e NAME` from its own environment, so the
|
|
532
|
+
* value becomes a setting (a locked key when it is one) and only the
|
|
533
|
+
* name stays on the command line.
|
|
534
|
+
*/
|
|
535
|
+
const rawArgs = [];
|
|
536
|
+
const fromDocker = {};
|
|
537
|
+
for (let i = 0; i < (entry.args ?? []).length; i++) {
|
|
538
|
+
const a = entry.args[i];
|
|
539
|
+
/*
|
|
540
|
+
* `-e NAME=value`, `--env=NAME=value`, and `-eNAME=value` with the value
|
|
541
|
+
* attached (R2 N5), and the same after Docker's combined one-letter
|
|
542
|
+
* switches: `-ie NAME=value`, `-itdeNAME=value` (R2 round 3).
|
|
543
|
+
*/
|
|
544
|
+
const glued = /^(?:--env=|-([itdP]*)e=?)([A-Za-z_][A-Za-z0-9_]*=.*)$/.exec(a);
|
|
545
|
+
const alone = /^(?:--env|-([itdP]*)e)$/.exec(a);
|
|
546
|
+
const pair = docker ? (glued ? glued[2] : alone ? entry.args[i + 1] : undefined) : undefined;
|
|
547
|
+
const eq = pair ? pair.indexOf("=") : -1;
|
|
548
|
+
if (pair !== undefined && eq > 0 && /^[A-Za-z_][A-Za-z0-9_]*$/.test(pair.slice(0, eq))) {
|
|
549
|
+
const k = pair.slice(0, eq);
|
|
550
|
+
const switches = (glued ?? alone)?.[1];
|
|
551
|
+
fromDocker[k] = pair.slice(eq + 1);
|
|
552
|
+
if (switches)
|
|
553
|
+
rawArgs.push(`-${switches}`);
|
|
554
|
+
rawArgs.push("-e", k);
|
|
555
|
+
if (!glued)
|
|
556
|
+
i++;
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
rawArgs.push(a);
|
|
560
|
+
}
|
|
561
|
+
const env = {};
|
|
562
|
+
for (const [k, v] of Object.entries({ ...fromDocker, ...(entry.env ?? {}) }))
|
|
563
|
+
env[k] = settingValue(k, v, fields, inputs, "env", warnings);
|
|
564
|
+
const args = [];
|
|
565
|
+
let folders = null;
|
|
566
|
+
for (let i = 0; i < rawArgs.length; i++) {
|
|
567
|
+
const a = rawArgs[i];
|
|
568
|
+
// Example folders become one folder picker, wherever they sit together.
|
|
569
|
+
if (isPlaceholderPath(a) && !a.includes("=")) {
|
|
570
|
+
if (!folders) {
|
|
571
|
+
folders = [];
|
|
572
|
+
args.push("{{...fields.folders}}");
|
|
573
|
+
}
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
const inline = /^(--?[A-Za-z0-9-]+=)(.*)$/.exec(a);
|
|
577
|
+
const valuePart = inline ? inline[2] : a;
|
|
578
|
+
// `--api-key X` or `--api-key=X`: a key in the arguments, real or a placeholder.
|
|
579
|
+
const flag = inline ? inline[1].slice(0, -1) : (rawArgs[i - 1] ?? "");
|
|
580
|
+
const keyFlag = (/^--?[a-z-]*(token|api-key|apikey|secret|password)$/i.test(flag) && (inline !== null || !a.startsWith("-"))) ||
|
|
581
|
+
// `--header=Authorization: Bearer …` or `--header "X-Api-Key: …"`: a key in a header given on the command line.
|
|
582
|
+
(/^--?(H|header|headers)$/.test(flag) &&
|
|
583
|
+
(inline !== null || !a.startsWith("-")) &&
|
|
584
|
+
/^\s*([A-Za-z0-9-]*(authorization|token|key|secret|cookie|auth)[A-Za-z0-9-]*)\s*:\s*\S/i.test(valuePart));
|
|
585
|
+
if (looksLikeKey(valuePart) || URL_WITH_PASSWORD.test(valuePart) || keyFlag || jsonHoldsKey(valuePart) || looksLikeKey(/^[A-Za-z_][A-Za-z0-9_]*=(.+)$/.exec(valuePart)?.[1] ?? "")) {
|
|
586
|
+
warnings.push(`“${display}” takes a key on its command line, where it would be saved as plain text. ` +
|
|
587
|
+
"hardened-mcp only keeps keys in the locked keys file, so this server was not added. " +
|
|
588
|
+
"Check its README for a way to give the key as a setting (an environment variable).");
|
|
589
|
+
return null;
|
|
590
|
+
}
|
|
591
|
+
// `--repository path/to/git/repo`: an example folder after a flag is one folder to choose.
|
|
592
|
+
if (/(^|[/\\])path[/\\]to([/\\]|$)/i.test(valuePart) && (inline || /^--?[A-Za-z]/.test(rawArgs[i - 1] ?? ""))) {
|
|
593
|
+
const flagName = (inline ? inline[1].slice(0, -1) : rawArgs[i - 1]).replace(/^-+/, "");
|
|
594
|
+
const id = fields.add(flagName || "folder", {
|
|
595
|
+
kind: "folder",
|
|
596
|
+
label: humanise(flagName || "Folder"),
|
|
597
|
+
help: `The instructions showed an example here (${valuePart}). Choose your own.`,
|
|
598
|
+
required: true
|
|
599
|
+
});
|
|
600
|
+
args.push(inline ? `${inline[1]}{{fields.${id}}}` : `{{fields.${id}}}`);
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
const inputRef = /\$\{input:([^}]+)\}/.exec(valuePart);
|
|
604
|
+
if (inputRef || /^<[^>]+>$/.test(valuePart) || /^\$\{[^}]+\}$/.test(valuePart)) {
|
|
605
|
+
const input = inputRef ? inputs[inputRef[1]] : undefined;
|
|
606
|
+
if (input?.password) {
|
|
607
|
+
warnings.push(`“${display}” takes a key on its command line, where it would be saved as plain text, so it was not added.`);
|
|
608
|
+
return null;
|
|
609
|
+
}
|
|
610
|
+
const raw = inputRef?.[1] ?? valuePart.replace(/[<>${}]/g, "");
|
|
611
|
+
const id = fields.add(raw, { kind: "text", label: input?.description ?? humanise(raw), required: true });
|
|
612
|
+
args.push(inline ? `${inline[1]}{{fields.${id}}}` : valuePart.replace(/\$\{input:[^}]+\}|^<[^>]+>$|^\$\{[^}]+\}$/, `{{fields.${id}}}`));
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
args.push(a);
|
|
616
|
+
}
|
|
617
|
+
if (folders) {
|
|
618
|
+
fields.add("folders", {
|
|
619
|
+
kind: "folders",
|
|
620
|
+
label: "Folders the server may use",
|
|
621
|
+
help: "The README showed example folders here. Pick your own.",
|
|
622
|
+
required: true
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
if (docker) {
|
|
626
|
+
// Read as Docker reads them: `-v/:/host` and `-itv /:/host` are mounts too (R2 N2).
|
|
627
|
+
const mounts = dockerRunOptions(args).flatMap(({ flag, value }) => {
|
|
628
|
+
if (flag === "-v" || flag === "--volume")
|
|
629
|
+
return [value];
|
|
630
|
+
if (flag === "--mount")
|
|
631
|
+
return [/(?:^|,)(?:source|src)=([^,]+)/.exec(value)?.[1] ?? "/"];
|
|
632
|
+
return [];
|
|
633
|
+
});
|
|
634
|
+
// A named volume ("claude-memory:/app/dist") is Docker's own storage, not a folder of yours.
|
|
635
|
+
if (mounts.some((m) => /^([/~.]|[A-Za-z]:[\\/]|\$)/.test(m))) {
|
|
636
|
+
draftWarnings.push("This runs in Docker with folders of this computer mounted into it. Check the folder paths in the command before testing.");
|
|
637
|
+
}
|
|
638
|
+
const risky = dockerArgsProblem(args);
|
|
639
|
+
if (risky)
|
|
640
|
+
draftWarnings.push(risky);
|
|
641
|
+
}
|
|
642
|
+
launch = { transport: "stdio", command, ...(args.length ? { args } : {}), ...(Object.keys(env).length ? { env } : {}) };
|
|
643
|
+
if (baseCommand(command) === "cmd")
|
|
644
|
+
draftWarnings.push("This was written for Windows (cmd /c).");
|
|
645
|
+
}
|
|
646
|
+
const guide = [
|
|
647
|
+
{
|
|
648
|
+
title: "Check what was found",
|
|
649
|
+
body: "These settings were read from what you pasted. Anything that looked like a key is now in a locked field " +
|
|
650
|
+
"and will be kept in the locked keys file, never in a settings file."
|
|
651
|
+
},
|
|
652
|
+
// Only when there is something to fill in: "Fill in any empty boxes" above "Nothing to fill in" contradicted itself (UT6).
|
|
653
|
+
...(fields.list.length > 0
|
|
654
|
+
? [{ title: "Fill in what is missing", body: "Fill in any empty boxes, then click **Test it**. Nothing is saved until you press Save and turn on." }]
|
|
655
|
+
: [{ title: "Test it", body: "Click **Test it** to see what it can do. Nothing is saved until you press Save and turn on." }])
|
|
656
|
+
];
|
|
657
|
+
const draft = {
|
|
658
|
+
source: { kind: "paste" },
|
|
659
|
+
name: safeName(serverName),
|
|
660
|
+
displayName: display,
|
|
661
|
+
launch,
|
|
662
|
+
fields: fields.list,
|
|
663
|
+
guide,
|
|
664
|
+
requires,
|
|
665
|
+
warnings: draftWarnings
|
|
666
|
+
};
|
|
667
|
+
return { draft, values: fields.values };
|
|
668
|
+
}
|
|
669
|
+
/** A name from what runs: "@modelcontextprotocol/server-filesystem" -> "filesystem". */
|
|
670
|
+
function guessName(entry) {
|
|
671
|
+
if (entry.url) {
|
|
672
|
+
try {
|
|
673
|
+
const hostname = new URL(entry.url).hostname.replace(/^\[|\]$/g, "");
|
|
674
|
+
// "127" is no name: a server on an address gets one that says where it is.
|
|
675
|
+
if (addressKind(hostname) === "loopback" || isIpLiteral(hostname)) {
|
|
676
|
+
return addressKind(hostname) === "loopback" ? "local_server" : safeName(`server_${hostname}`);
|
|
677
|
+
}
|
|
678
|
+
const host = hostname.split(".").filter((p) => !["mcp", "www", "api", "com", "dev", "app", "io", "co", "ai", "org", "net"].includes(p));
|
|
679
|
+
return host[0] ?? "server";
|
|
680
|
+
}
|
|
681
|
+
catch {
|
|
682
|
+
return "server";
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
const words = [entry.command ?? "", ...(entry.args ?? [])];
|
|
686
|
+
// `node -e <code>` / `python -c <code>`: the code is not a name.
|
|
687
|
+
const skip = new Set(words.flatMap((w, i) => (/^(-e|--eval|-c|-p|--print)$/.test(w) ? [i + 1] : [])));
|
|
688
|
+
const pkg = words.find((w, i) => i > 0 && !skip.has(i) && !w.startsWith("-") && !["run", "x", "dlx", "exec", "tool"].includes(w) && /[a-z]/i.test(w) && !w.includes("=") && !w.startsWith("/"));
|
|
689
|
+
const spec = (pkg ?? entry.command ?? "server").replace(/(.)[@:]v?[\d.]+.*$/, "$1").replace(/==.*$/, "");
|
|
690
|
+
let base = spec
|
|
691
|
+
.split("/")
|
|
692
|
+
.pop()
|
|
693
|
+
.replace(/^(mcp-server-|server-)/, "")
|
|
694
|
+
.replace(/(-mcp-server|-mcp)$/, "");
|
|
695
|
+
// "@stripe/mcp": the scope is the name.
|
|
696
|
+
if (/^(mcp|server|mcp-server)?$/i.test(base) && spec.startsWith("@") && spec.includes("/"))
|
|
697
|
+
base = spec.slice(1, spec.indexOf("/"));
|
|
698
|
+
return base || "server";
|
|
699
|
+
}
|
|
700
|
+
/** Programs that run a script rather than being the server themselves. */
|
|
701
|
+
const INTERPRETERS = new Set(["node", "nodejs", "python", "python3", "bun", "deno", "sh", "bash", "zsh", "ruby", "php", "java"]);
|
|
702
|
+
/**
|
|
703
|
+
* A server started by a script on this computer: its script's name ("Weather
|
|
704
|
+
* server" for `node ./weather-server.js`), or "Server on this computer" when
|
|
705
|
+
* it is code typed on the command line (`node -e …`), never "Node" (UT6).
|
|
706
|
+
*/
|
|
707
|
+
function localDisplay(entry) {
|
|
708
|
+
const command = baseCommand(entry.command ?? "");
|
|
709
|
+
if (!INTERPRETERS.has(command))
|
|
710
|
+
return undefined;
|
|
711
|
+
const script = (entry.args ?? []).find((a) => /\.(c?m?js|ts|py|rb|php|sh|jar)$/i.test(a));
|
|
712
|
+
if (script) {
|
|
713
|
+
const name = script.split(/[\\/]/).pop().replace(/\.[^.]+$/, "");
|
|
714
|
+
if (!/^(index|main|server|app|cli)$/i.test(name))
|
|
715
|
+
return humanise(name);
|
|
716
|
+
}
|
|
717
|
+
return "Server on this computer";
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* A pasted server that is one of the catalog's, by the package it runs: the
|
|
721
|
+
* name the catalog gives it ("Files on this computer", not "Filesystem", UT6),
|
|
722
|
+
* unless the person named it something of their own.
|
|
723
|
+
*/
|
|
724
|
+
function catalogDisplay(entry) {
|
|
725
|
+
if (entry.url !== undefined)
|
|
726
|
+
return undefined;
|
|
727
|
+
const words = [entry.command ?? "", ...(entry.args ?? [])];
|
|
728
|
+
const pkgOf = (w) => w.replace(/(.)@v?[\d.]+.*$/, "$1").replace(/==.*$/, "");
|
|
729
|
+
try {
|
|
730
|
+
for (const c of loadCatalog()) {
|
|
731
|
+
for (const v of c.variants) {
|
|
732
|
+
const launch = v.launch;
|
|
733
|
+
if (launch.transport !== "stdio")
|
|
734
|
+
continue;
|
|
735
|
+
const pkg = (launch.args ?? []).find((a) => !a.startsWith("-") && !a.includes("{{") && /[a-z]/i.test(a) && /[@/]|^mcp-|-mcp|server/.test(a));
|
|
736
|
+
if (!pkg || !words.some((w) => pkgOf(w) === pkgOf(pkg)))
|
|
737
|
+
continue;
|
|
738
|
+
if (entry.name && ![c.id, guessName(entry)].includes(entry.name.toLowerCase()))
|
|
739
|
+
return undefined;
|
|
740
|
+
return c.name;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
catch {
|
|
745
|
+
/* without the catalog, the name from what runs */
|
|
746
|
+
}
|
|
747
|
+
return undefined;
|
|
748
|
+
}
|
|
749
|
+
/** A pasted address's display name: where it is, as written ("context7.com", "Server on this computer, port 3845"). */
|
|
750
|
+
function displayForAddress(url) {
|
|
751
|
+
if (!url)
|
|
752
|
+
return undefined;
|
|
753
|
+
try {
|
|
754
|
+
const u = new URL(url);
|
|
755
|
+
const hostname = u.hostname.replace(/^\[|\]$/g, "");
|
|
756
|
+
if (addressKind(hostname) === "loopback") {
|
|
757
|
+
return u.port ? `Server on this computer (port ${u.port})` : "Server on this computer";
|
|
758
|
+
}
|
|
759
|
+
if (isIpLiteral(hostname))
|
|
760
|
+
return `Server at ${u.host}`;
|
|
761
|
+
return hostname.replace(/^(www|mcp|api)\./, "");
|
|
762
|
+
}
|
|
763
|
+
catch {
|
|
764
|
+
return undefined;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
/** Words as people write them: the casing a README uses for well-known names. */
|
|
768
|
+
const PROPER = {
|
|
769
|
+
api: "API",
|
|
770
|
+
apis: "APIs",
|
|
771
|
+
url: "URL",
|
|
772
|
+
id: "ID",
|
|
773
|
+
mcp: "MCP",
|
|
774
|
+
pat: "PAT",
|
|
775
|
+
ssh: "SSH",
|
|
776
|
+
sql: "SQL",
|
|
777
|
+
http: "HTTP",
|
|
778
|
+
https: "HTTPS",
|
|
779
|
+
json: "JSON",
|
|
780
|
+
aws: "AWS",
|
|
781
|
+
oauth: "OAuth",
|
|
782
|
+
github: "GitHub",
|
|
783
|
+
gitlab: "GitLab",
|
|
784
|
+
openai: "OpenAI",
|
|
785
|
+
postgresql: "PostgreSQL",
|
|
786
|
+
mongodb: "MongoDB",
|
|
787
|
+
youtube: "YouTube",
|
|
788
|
+
hubspot: "HubSpot",
|
|
789
|
+
clickup: "ClickUp"
|
|
790
|
+
};
|
|
791
|
+
function humanise(name) {
|
|
792
|
+
const t = name
|
|
793
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
794
|
+
.replace(/[_-]+/g, " ")
|
|
795
|
+
.trim()
|
|
796
|
+
.toLowerCase();
|
|
797
|
+
if (!t)
|
|
798
|
+
return name;
|
|
799
|
+
const words = t.split(/\s+/).map((w) => PROPER[w] ?? w);
|
|
800
|
+
const first = words[0];
|
|
801
|
+
words[0] = PROPER[first.toLowerCase()] ? first : first[0].toUpperCase() + first.slice(1);
|
|
802
|
+
return words.join(" ");
|
|
803
|
+
}
|
|
804
|
+
function asRecord(v) {
|
|
805
|
+
return typeof v === "object" && v !== null && !Array.isArray(v) ? v : undefined;
|
|
806
|
+
}
|
|
807
|
+
function stringRecord(r) {
|
|
808
|
+
const out = {};
|
|
809
|
+
for (const [k, v] of Object.entries(r))
|
|
810
|
+
if (typeof v === "string" || typeof v === "number" || typeof v === "boolean")
|
|
811
|
+
out[k] = String(v);
|
|
812
|
+
return out;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* A key inside a JSON argument: `--config={"apiKey":"sk-…"}` (R2 round 3).
|
|
816
|
+
* Only for a value that is JSON-shaped, so an ordinary path or word is never
|
|
817
|
+
* split up and read as a key.
|
|
818
|
+
*/
|
|
819
|
+
function jsonHoldsKey(value) {
|
|
820
|
+
const v = value.trim();
|
|
821
|
+
if (!/^[[{]/.test(v))
|
|
822
|
+
return false;
|
|
823
|
+
if (/"[^"]*(api[_-]?key|token|secret|password|passwd|credential|auth)[^"]*"\s*:\s*"[^"\s]{4,}"/i.test(v))
|
|
824
|
+
return true;
|
|
825
|
+
return (v.match(/"([^"]*)"/g) ?? []).some((quoted) => looksLikeKey(quoted.slice(1, -1)));
|
|
826
|
+
}
|