hmcp-gateway 0.1.0 → 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/README.md +94 -0
- package/dist/adopt-cli.d.ts +5 -1
- package/dist/adopt-cli.js +45 -17
- package/dist/adopt-plan.js +116 -1
- package/dist/cli.js +124 -3
- package/dist/client-config.d.ts +65 -2
- package/dist/client-config.js +235 -24
- package/dist/connect-client.d.ts +3 -1
- package/dist/connect-client.js +55 -5
- package/dist/desktop-extensions.d.ts +53 -0
- package/dist/desktop-extensions.js +126 -0
- package/dist/gateway.d.ts +10 -0
- package/dist/gateway.js +21 -5
- 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 +5 -0
- package/dist/index.js +5 -0
- package/dist/toml-edit.d.ts +43 -0
- package/dist/toml-edit.js +363 -0
- package/package.json +4 -3
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import { parse } from "smol-toml";
|
|
3
|
+
/**
|
|
4
|
+
* Codex's `config.toml` (shared with the ChatGPT desktop app): reading it,
|
|
5
|
+
* and changing only the MCP server tables in it.
|
|
6
|
+
*
|
|
7
|
+
* The TOML library is used to read, never to write. Writing a whole TOML
|
|
8
|
+
* document back from its parsed value loses every comment and all of the
|
|
9
|
+
* person's layout, in a file they edit by hand. So every change here is a
|
|
10
|
+
* cut or an append on the file's own text: a server's table (and its
|
|
11
|
+
* `[mcp_servers.<name>.*]` sub-tables) removed, or a new table added at the
|
|
12
|
+
* end. After each change the result is read again and must equal the
|
|
13
|
+
* original with exactly that change, or nothing is written.
|
|
14
|
+
*
|
|
15
|
+
* A file whose servers are written in a form these cuts cannot handle safely
|
|
16
|
+
* -- an inline table, dotted keys, an array of tables, a server split across
|
|
17
|
+
* the file -- can be read and listed, but is refused for editing.
|
|
18
|
+
*/
|
|
19
|
+
export class TomlEditError extends Error {
|
|
20
|
+
}
|
|
21
|
+
/** The file as a plain JSON value: no null prototypes, dates as their TOML text. */
|
|
22
|
+
export function parseToml(text) {
|
|
23
|
+
return JSON.parse(JSON.stringify(parse(text)));
|
|
24
|
+
}
|
|
25
|
+
const SERVERS = "mcp_servers";
|
|
26
|
+
const BOM = "";
|
|
27
|
+
function lineEnd(text, i) {
|
|
28
|
+
const nl = text.indexOf("\n", i);
|
|
29
|
+
return nl === -1 ? text.length : nl + 1;
|
|
30
|
+
}
|
|
31
|
+
function skipBlanks(text, i) {
|
|
32
|
+
while (i < text.length && (text[i] === " " || text[i] === "\t"))
|
|
33
|
+
i++;
|
|
34
|
+
return i;
|
|
35
|
+
}
|
|
36
|
+
/** Where a string starting at `i` ends, or -1 when no string starts there. */
|
|
37
|
+
function stringEnd(text, i) {
|
|
38
|
+
if (text.startsWith('"""', i) || text.startsWith("'''", i)) {
|
|
39
|
+
const quote = text[i];
|
|
40
|
+
const close = quote.repeat(3);
|
|
41
|
+
let j = i + 3;
|
|
42
|
+
while (j < text.length) {
|
|
43
|
+
if (quote === '"' && text[j] === "\\") {
|
|
44
|
+
j += 2;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (text.startsWith(close, j)) {
|
|
48
|
+
j += 3;
|
|
49
|
+
// Up to two quotes may sit right before the closing three.
|
|
50
|
+
for (let k = 0; k < 2 && text[j] === quote; k++)
|
|
51
|
+
j++;
|
|
52
|
+
return j;
|
|
53
|
+
}
|
|
54
|
+
j++;
|
|
55
|
+
}
|
|
56
|
+
throw new TomlEditError("a multi-line string is not closed");
|
|
57
|
+
}
|
|
58
|
+
if (text[i] === '"') {
|
|
59
|
+
let j = i + 1;
|
|
60
|
+
while (j < text.length && text[j] !== '"')
|
|
61
|
+
j += text[j] === "\\" ? 2 : 1;
|
|
62
|
+
return j + 1;
|
|
63
|
+
}
|
|
64
|
+
if (text[i] === "'") {
|
|
65
|
+
const close = text.indexOf("'", i + 1);
|
|
66
|
+
if (close === -1)
|
|
67
|
+
throw new TomlEditError("a string is not closed");
|
|
68
|
+
return close + 1;
|
|
69
|
+
}
|
|
70
|
+
return -1;
|
|
71
|
+
}
|
|
72
|
+
function unescapeBasic(body) {
|
|
73
|
+
return body.replace(/\\(u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|.)/g, (_, e) => {
|
|
74
|
+
if (e[0] === "u" || e[0] === "U")
|
|
75
|
+
return String.fromCodePoint(parseInt(e.slice(1), 16));
|
|
76
|
+
const map = { b: "\b", t: "\t", n: "\n", f: "\f", r: "\r", e: "\u001b", '"': '"', "\\": "\\" };
|
|
77
|
+
return map[e] ?? e;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/** A dotted key starting at `i`: its segments, and where it ends. */
|
|
81
|
+
function readKey(text, i) {
|
|
82
|
+
const path = [];
|
|
83
|
+
for (;;) {
|
|
84
|
+
i = skipBlanks(text, i);
|
|
85
|
+
const c = text[i];
|
|
86
|
+
if (c === '"' || c === "'") {
|
|
87
|
+
const end = stringEnd(text, i);
|
|
88
|
+
const body = text.slice(i + 1, end - 1);
|
|
89
|
+
path.push(c === '"' ? unescapeBasic(body) : body);
|
|
90
|
+
i = end;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
const m = /^[A-Za-z0-9_-]+/.exec(text.slice(i, i + 256));
|
|
94
|
+
if (!m)
|
|
95
|
+
throw new TomlEditError("a key could not be read");
|
|
96
|
+
path.push(m[0]);
|
|
97
|
+
i += m[0].length;
|
|
98
|
+
}
|
|
99
|
+
i = skipBlanks(text, i);
|
|
100
|
+
if (text[i] !== ".")
|
|
101
|
+
return { path, next: i };
|
|
102
|
+
i++;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** Where the value starting at `i` ends: strings, arrays and inline tables over any number of lines. */
|
|
106
|
+
function valueEnd(text, i) {
|
|
107
|
+
i = skipBlanks(text, i);
|
|
108
|
+
const asString = stringEnd(text, i);
|
|
109
|
+
if (asString !== -1)
|
|
110
|
+
return asString;
|
|
111
|
+
if (text[i] === "[" || text[i] === "{") {
|
|
112
|
+
let depth = 0;
|
|
113
|
+
while (i < text.length) {
|
|
114
|
+
const s = stringEnd(text, i);
|
|
115
|
+
if (s !== -1) {
|
|
116
|
+
i = s;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const c = text[i];
|
|
120
|
+
if (c === "#") {
|
|
121
|
+
i = lineEnd(text, i);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (c === "[" || c === "{")
|
|
125
|
+
depth++;
|
|
126
|
+
else if (c === "]" || c === "}") {
|
|
127
|
+
depth--;
|
|
128
|
+
if (depth === 0)
|
|
129
|
+
return i + 1;
|
|
130
|
+
}
|
|
131
|
+
i++;
|
|
132
|
+
}
|
|
133
|
+
throw new TomlEditError("an array or inline table is not closed");
|
|
134
|
+
}
|
|
135
|
+
while (i < text.length && !/[\s#]/.test(text[i]))
|
|
136
|
+
i++;
|
|
137
|
+
return i;
|
|
138
|
+
}
|
|
139
|
+
/** Every table header and every key line, with where each one sits in the text. */
|
|
140
|
+
function scan(text) {
|
|
141
|
+
const headers = [];
|
|
142
|
+
const keys = [];
|
|
143
|
+
let table = null;
|
|
144
|
+
// A byte-order mark some editors put first is not part of the first line.
|
|
145
|
+
let i = text.startsWith(BOM) ? 1 : 0;
|
|
146
|
+
while (i < text.length) {
|
|
147
|
+
const start = i;
|
|
148
|
+
i = skipBlanks(text, i);
|
|
149
|
+
const c = text[i];
|
|
150
|
+
if (c === undefined)
|
|
151
|
+
break;
|
|
152
|
+
if (c === "\n" || c === "\r" || c === "#") {
|
|
153
|
+
i = lineEnd(text, i);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (c === "[") {
|
|
157
|
+
const array = text[i + 1] === "[";
|
|
158
|
+
const key = readKey(text, i + (array ? 2 : 1));
|
|
159
|
+
i = skipBlanks(text, key.next);
|
|
160
|
+
if (!text.startsWith(array ? "]]" : "]", i))
|
|
161
|
+
throw new TomlEditError("a table header could not be read");
|
|
162
|
+
i = lineEnd(text, i);
|
|
163
|
+
headers.push({ start, end: i, path: key.path, array });
|
|
164
|
+
table = key.path;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const key = readKey(text, i);
|
|
168
|
+
i = skipBlanks(text, key.next);
|
|
169
|
+
if (text[i] !== "=")
|
|
170
|
+
throw new TomlEditError("a line could not be read");
|
|
171
|
+
i = lineEnd(text, valueEnd(text, i + 1));
|
|
172
|
+
keys.push({ start, end: i, path: key.path, table });
|
|
173
|
+
}
|
|
174
|
+
return { headers, keys };
|
|
175
|
+
}
|
|
176
|
+
const isServerHeader = (h, name) => h.path[0] === SERVERS && h.path.length >= 2 && (name === undefined || h.path[1] === name);
|
|
177
|
+
/**
|
|
178
|
+
* Why this file's servers cannot be changed by cutting and appending text, or
|
|
179
|
+
* null when they can. Names a form, never a value.
|
|
180
|
+
*/
|
|
181
|
+
export function tomlEditProblem(text) {
|
|
182
|
+
let layout;
|
|
183
|
+
try {
|
|
184
|
+
parse(text);
|
|
185
|
+
layout = scan(text);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return "the file could not be read closely enough to change it safely";
|
|
189
|
+
}
|
|
190
|
+
if (layout.headers.some((h) => h.array && h.path[0] === SERVERS)) {
|
|
191
|
+
return "its MCP servers are written as a list of tables ([[mcp_servers]])";
|
|
192
|
+
}
|
|
193
|
+
if (layout.keys.some((k) => k.table === null && k.path[0] === SERVERS)) {
|
|
194
|
+
return "its MCP servers are written on one line (mcp_servers = { ... } or mcp_servers.<name>.<setting> = ...)";
|
|
195
|
+
}
|
|
196
|
+
if (layout.keys.some((k) => k.table !== null && k.table.length === 1 && k.table[0] === SERVERS)) {
|
|
197
|
+
return "its MCP servers are written as keys inside [mcp_servers] rather than as [mcp_servers.<name>] tables";
|
|
198
|
+
}
|
|
199
|
+
const names = new Set(layout.headers.filter((h) => isServerHeader(h)).map((h) => h.path[1]));
|
|
200
|
+
for (const name of names) {
|
|
201
|
+
const at = layout.headers.flatMap((h, i) => (isServerHeader(h, name) ? [i] : []));
|
|
202
|
+
if (at[at.length - 1] - at[0] + 1 !== at.length) {
|
|
203
|
+
return `the settings of the server "${name}" are split across the file`;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
/** The text range of one server's tables, from its first header to the end of its last setting. */
|
|
209
|
+
function serverRange(text, layout, name) {
|
|
210
|
+
const at = layout.headers.flatMap((h, i) => (isServerHeader(h, name) ? [i] : []));
|
|
211
|
+
if (at.length === 0)
|
|
212
|
+
throw new TomlEditError(`the server "${name}" has no [mcp_servers.${name}] table to remove`);
|
|
213
|
+
const first = layout.headers[at[0]];
|
|
214
|
+
const next = layout.headers[at[at.length - 1] + 1];
|
|
215
|
+
const limit = next ? next.start : text.length;
|
|
216
|
+
// Comments and blank lines after its last setting are left: they may be about what follows.
|
|
217
|
+
let end = first.end;
|
|
218
|
+
for (const item of [...layout.headers, ...layout.keys]) {
|
|
219
|
+
if (item.start >= first.start && item.start < limit)
|
|
220
|
+
end = Math.max(end, item.end);
|
|
221
|
+
}
|
|
222
|
+
return [first.start, end];
|
|
223
|
+
}
|
|
224
|
+
/* ------------------------------------------------------------ the writer */
|
|
225
|
+
/** A TOML basic string: quotes, backslashes and control characters escaped. */
|
|
226
|
+
export function tomlString(value) {
|
|
227
|
+
let out = '"';
|
|
228
|
+
for (const ch of value) {
|
|
229
|
+
const code = ch.codePointAt(0);
|
|
230
|
+
if (ch === '"')
|
|
231
|
+
out += '\\"';
|
|
232
|
+
else if (ch === "\\")
|
|
233
|
+
out += "\\\\";
|
|
234
|
+
else if (ch === "\n")
|
|
235
|
+
out += "\\n";
|
|
236
|
+
else if (ch === "\r")
|
|
237
|
+
out += "\\r";
|
|
238
|
+
else if (ch === "\t")
|
|
239
|
+
out += "\\t";
|
|
240
|
+
else if (ch === "\b")
|
|
241
|
+
out += "\\b";
|
|
242
|
+
else if (ch === "\f")
|
|
243
|
+
out += "\\f";
|
|
244
|
+
else if (code < 0x20 || code === 0x7f)
|
|
245
|
+
out += `\\u${code.toString(16).padStart(4, "0")}`;
|
|
246
|
+
else
|
|
247
|
+
out += ch;
|
|
248
|
+
}
|
|
249
|
+
return `${out}"`;
|
|
250
|
+
}
|
|
251
|
+
/** A key, bare when TOML allows it and quoted otherwise. */
|
|
252
|
+
export function tomlKey(key) {
|
|
253
|
+
return /^[A-Za-z0-9_-]+$/.test(key) ? key : tomlString(key);
|
|
254
|
+
}
|
|
255
|
+
const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
256
|
+
function inlineValue(value) {
|
|
257
|
+
if (typeof value === "string")
|
|
258
|
+
return tomlString(value);
|
|
259
|
+
if (typeof value === "boolean")
|
|
260
|
+
return String(value);
|
|
261
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
262
|
+
return String(value);
|
|
263
|
+
if (Array.isArray(value))
|
|
264
|
+
return `[${value.map(inlineValue).join(", ")}]`;
|
|
265
|
+
if (isRecord(value)) {
|
|
266
|
+
const body = Object.entries(value).map(([k, v]) => `${tomlKey(k)} = ${inlineValue(v)}`);
|
|
267
|
+
return body.length === 0 ? "{}" : `{ ${body.join(", ")} }`;
|
|
268
|
+
}
|
|
269
|
+
throw new TomlEditError("a setting has a value that cannot be written as TOML");
|
|
270
|
+
}
|
|
271
|
+
/** `[a.b]` with its plain settings, then each table-valued setting as a `[a.b.<key>]` of its own. */
|
|
272
|
+
export function tomlTable(path, value, eol = "\n") {
|
|
273
|
+
const lines = [`[${path.map(tomlKey).join(".")}]`];
|
|
274
|
+
const subs = [];
|
|
275
|
+
for (const [key, v] of Object.entries(value)) {
|
|
276
|
+
if (isRecord(v))
|
|
277
|
+
subs.push([key, v]);
|
|
278
|
+
else
|
|
279
|
+
lines.push(`${tomlKey(key)} = ${inlineValue(v)}`);
|
|
280
|
+
}
|
|
281
|
+
let out = lines.join(eol) + eol;
|
|
282
|
+
for (const [key, v] of subs)
|
|
283
|
+
out += eol + tomlTable([...path, key], v, eol);
|
|
284
|
+
return out;
|
|
285
|
+
}
|
|
286
|
+
/* -------------------------------------------------------------- the edit */
|
|
287
|
+
/** Whether two read TOML documents say the same, a missing `mcp_servers` and an empty one being the same. */
|
|
288
|
+
export function sameToml(a, b) {
|
|
289
|
+
if (!isRecord(a) || !isRecord(b))
|
|
290
|
+
return false;
|
|
291
|
+
return isDeepStrictEqual(withoutEmptyServers(a), withoutEmptyServers(b));
|
|
292
|
+
}
|
|
293
|
+
function withoutEmptyServers(doc) {
|
|
294
|
+
const servers = doc[SERVERS];
|
|
295
|
+
if (!isRecord(servers) || Object.keys(servers).length > 0)
|
|
296
|
+
return doc;
|
|
297
|
+
const rest = { ...doc };
|
|
298
|
+
delete rest[SERVERS];
|
|
299
|
+
return rest;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* The file's text with its servers changed from `current` to `next`: each
|
|
303
|
+
* server that went or changed has its tables cut out, each one that is new
|
|
304
|
+
* or changed is appended at the end. Everything else is the file's own bytes.
|
|
305
|
+
*
|
|
306
|
+
* Refuses, with nothing written by the caller, when the servers are in a form
|
|
307
|
+
* this cannot cut safely or when the result does not read back as exactly
|
|
308
|
+
* the intended change.
|
|
309
|
+
*/
|
|
310
|
+
export function editTomlServers(text, next) {
|
|
311
|
+
const problem = tomlEditProblem(text);
|
|
312
|
+
if (problem)
|
|
313
|
+
throw new TomlEditError(problem);
|
|
314
|
+
const original = parseToml(text);
|
|
315
|
+
const current = isRecord(original[SERVERS]) ? original[SERVERS] : {};
|
|
316
|
+
const changed = (name) => !isDeepStrictEqual(current[name], JSON.parse(JSON.stringify(next[name] ?? null)));
|
|
317
|
+
const remove = Object.keys(current).filter((name) => !(name in next) || changed(name));
|
|
318
|
+
const add = Object.keys(next).filter((name) => !(name in current) || changed(name));
|
|
319
|
+
const layout = scan(text);
|
|
320
|
+
const ranges = remove.map((name) => serverRange(text, layout, name)).sort((a, b) => b[0] - a[0]);
|
|
321
|
+
// A byte-order mark stays first in the file, whatever is cut or added.
|
|
322
|
+
const bom = text.startsWith(BOM) ? BOM : "";
|
|
323
|
+
let out = text;
|
|
324
|
+
for (const [start, end] of ranges) {
|
|
325
|
+
const before = out.slice(0, start);
|
|
326
|
+
let after = out.slice(end);
|
|
327
|
+
// One blank line is enough where two tables used to meet around this one.
|
|
328
|
+
if (before === bom)
|
|
329
|
+
after = after.replace(/^(\r?\n)+/, "");
|
|
330
|
+
else if (/\n\r?\n$/.test(before))
|
|
331
|
+
after = after.replace(/^\r?\n/, "");
|
|
332
|
+
// The blank line an append put before a table that was last in the file goes with it.
|
|
333
|
+
out = after === "" ? before.replace(/(\n)\r?\n$/, "$1") : before + after;
|
|
334
|
+
}
|
|
335
|
+
const eol = text.includes("\r\n") ? "\r\n" : "\n";
|
|
336
|
+
for (const name of add) {
|
|
337
|
+
const value = next[name];
|
|
338
|
+
if (!isRecord(value))
|
|
339
|
+
throw new TomlEditError(`the server "${name}" is not a table of settings`);
|
|
340
|
+
if (out.slice(bom.length).trim() !== "") {
|
|
341
|
+
if (!out.endsWith("\n"))
|
|
342
|
+
out += eol;
|
|
343
|
+
if (!out.endsWith(`${eol}${eol}`))
|
|
344
|
+
out += eol;
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
out = bom;
|
|
348
|
+
}
|
|
349
|
+
out += tomlTable([SERVERS, name], value, eol);
|
|
350
|
+
}
|
|
351
|
+
let reread;
|
|
352
|
+
try {
|
|
353
|
+
reread = parseToml(out);
|
|
354
|
+
}
|
|
355
|
+
catch {
|
|
356
|
+
throw new TomlEditError("the changed file would not read back, so nothing was changed");
|
|
357
|
+
}
|
|
358
|
+
const expected = { ...original, [SERVERS]: JSON.parse(JSON.stringify(next)) };
|
|
359
|
+
if (!isDeepStrictEqual(withoutEmptyServers(reread), withoutEmptyServers(expected))) {
|
|
360
|
+
throw new TomlEditError("the changed file would not read back as exactly the intended change, so nothing was changed");
|
|
361
|
+
}
|
|
362
|
+
return out;
|
|
363
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hmcp-gateway",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A policy gateway that sits between an agent and the MCP servers it uses, enforcing what each tool may do.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"author": "Aditya Das",
|
|
@@ -44,10 +44,11 @@
|
|
|
44
44
|
"node": ">=22.19.0"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"hmcp-core": "0.
|
|
48
|
-
"hmcp-scanner": "0.
|
|
47
|
+
"hmcp-core": "0.2.0",
|
|
48
|
+
"hmcp-scanner": "0.2.0",
|
|
49
49
|
"@modelcontextprotocol/sdk": "^1.32.1",
|
|
50
50
|
"commander": "^15.0.0",
|
|
51
|
+
"smol-toml": "^1.9.1",
|
|
51
52
|
"yaml": "^2.7.0",
|
|
52
53
|
"zod": "^4.0.0"
|
|
53
54
|
}
|