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/upstream.js
ADDED
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import { ToolListChangedNotificationSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
4
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
5
|
+
import { credentialFetch } from "./credential.js";
|
|
6
|
+
import { EgressDenied, isSecretRef, resolveSecretRecord } from "hmcp-core";
|
|
7
|
+
import { UpstreamExited, UpstreamSseOnly, scrub, specValues } from "./upstream-errors.js";
|
|
8
|
+
import { VERSION } from "./version.js";
|
|
9
|
+
/** How many of a server's last stderr lines are kept for a problem report. */
|
|
10
|
+
export const STDERR_TAIL_LINES = 20;
|
|
11
|
+
/**
|
|
12
|
+
* The SDK's stdio transport, plus the one fact it does not keep: how the
|
|
13
|
+
* child ended. "Exited with code 1" next to the server's last words is the
|
|
14
|
+
* whole diagnosis for most broken servers, and the SDK drops the code on the
|
|
15
|
+
* floor in its `close` handler. Read off the child right after spawn, which is
|
|
16
|
+
* the only moment the private handle is guaranteed to be there.
|
|
17
|
+
*/
|
|
18
|
+
class WatchedStdioTransport extends StdioClientTransport {
|
|
19
|
+
exitCode = null;
|
|
20
|
+
exitSignal = null;
|
|
21
|
+
exited = false;
|
|
22
|
+
async start() {
|
|
23
|
+
await super.start();
|
|
24
|
+
const child = this._process;
|
|
25
|
+
child?.once("exit", (code, signal) => {
|
|
26
|
+
this.exited = true;
|
|
27
|
+
this.exitCode = code;
|
|
28
|
+
this.exitSignal = signal;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* How many pages of one list to follow before giving up.
|
|
34
|
+
*
|
|
35
|
+
* Every list in the protocol may be paginated and the cursor is opaque, so
|
|
36
|
+
* there is nothing in a cursor to tell a well-behaved server from one that
|
|
37
|
+
* answers every request with another page. The cap bounds the second case:
|
|
38
|
+
* 500 pages is far past any real inventory and still finite.
|
|
39
|
+
*/
|
|
40
|
+
const PAGE_LIMIT = 500;
|
|
41
|
+
/**
|
|
42
|
+
* Follows a paginated list to its end, collecting every page.
|
|
43
|
+
*
|
|
44
|
+
* A server stops by omitting `nextCursor`. A server that repeats a cursor, or
|
|
45
|
+
* that answers an empty page while still offering one, would otherwise loop
|
|
46
|
+
* forever holding up startup, so both end the walk.
|
|
47
|
+
*/
|
|
48
|
+
async function allPages(fetch) {
|
|
49
|
+
const out = [];
|
|
50
|
+
const seen = new Set();
|
|
51
|
+
let cursor;
|
|
52
|
+
for (let page = 0; page < PAGE_LIMIT; page += 1) {
|
|
53
|
+
const { items, nextCursor } = await fetch(cursor);
|
|
54
|
+
out.push(...items);
|
|
55
|
+
if (nextCursor === undefined || nextCursor === cursor || seen.has(nextCursor))
|
|
56
|
+
break;
|
|
57
|
+
seen.add(nextCursor);
|
|
58
|
+
cursor = nextCursor;
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A connection to one upstream MCP server. HTTP upstreams are vetted by the
|
|
64
|
+
* egress guard before a connection is opened, so the allowlist and the SSRF
|
|
65
|
+
* checks apply to servers we did not build just as they do to a REST API.
|
|
66
|
+
*/
|
|
67
|
+
export class UpstreamConnection {
|
|
68
|
+
spec;
|
|
69
|
+
tail;
|
|
70
|
+
transport;
|
|
71
|
+
client;
|
|
72
|
+
tools = [];
|
|
73
|
+
resources = [];
|
|
74
|
+
prompts = [];
|
|
75
|
+
/** Set by `close()`, so an intended close is not reported as a crash. */
|
|
76
|
+
closing = false;
|
|
77
|
+
constructor(spec, client, tail, transport) {
|
|
78
|
+
this.spec = spec;
|
|
79
|
+
this.tail = tail;
|
|
80
|
+
this.transport = transport;
|
|
81
|
+
this.client = client;
|
|
82
|
+
}
|
|
83
|
+
/** The last lines a stdio server wrote to stderr, secrets removed. */
|
|
84
|
+
stderrTail() {
|
|
85
|
+
return [...this.tail];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Calls back once if the server goes away on its own -- a stdio child that
|
|
89
|
+
* exits or crashes while serving. Not called for `close()`.
|
|
90
|
+
*/
|
|
91
|
+
onClosed(listener) {
|
|
92
|
+
const previous = this.client.onclose;
|
|
93
|
+
this.client.onclose = () => {
|
|
94
|
+
previous?.();
|
|
95
|
+
if (this.closing || this.spec.transport !== "stdio")
|
|
96
|
+
return;
|
|
97
|
+
listener(new UpstreamExited(this.spec.name, this.transport?.exitCode ?? null, this.stderrTail(), this.transport?.exitSignal ?? null));
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Reads a stdio upstream's stderr and hands over whole lines.
|
|
102
|
+
*
|
|
103
|
+
* Whole lines rather than chunks, because a chunk boundary falls mid-message
|
|
104
|
+
* and a half-line prefixed with the upstream's name is worse than no line
|
|
105
|
+
* at all. The stream is read to its end either way: this is the only place
|
|
106
|
+
* a stdio server can say why it will not start.
|
|
107
|
+
*/
|
|
108
|
+
static drainStderr(stream, name, sink) {
|
|
109
|
+
let held = "";
|
|
110
|
+
stream.setEncoding("utf8");
|
|
111
|
+
stream.on("data", (chunk) => {
|
|
112
|
+
held += chunk;
|
|
113
|
+
const lines = held.split("\n");
|
|
114
|
+
// The last element is whatever came after the final newline: either the
|
|
115
|
+
// empty string, or the start of a line still being written.
|
|
116
|
+
held = lines.pop() ?? "";
|
|
117
|
+
for (const line of lines) {
|
|
118
|
+
if (line.trim() !== "")
|
|
119
|
+
sink(`[${name}] ${line}`);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
stream.on("end", () => {
|
|
123
|
+
if (held.trim() !== "")
|
|
124
|
+
sink(`[${name}] ${held}`);
|
|
125
|
+
held = "";
|
|
126
|
+
});
|
|
127
|
+
// A read error on a stream that exists only for diagnostics must not take
|
|
128
|
+
// down the connection it belongs to.
|
|
129
|
+
stream.on("error", () => undefined);
|
|
130
|
+
}
|
|
131
|
+
static async connect(spec, egress, options = {}) {
|
|
132
|
+
const client = new Client({ name: "hmcp-gateway", version: VERSION },
|
|
133
|
+
// The gateway does not forward sampling or elicitation on the upstream's
|
|
134
|
+
// behalf: an upstream server must not be able to prompt the human
|
|
135
|
+
// through us, because the human's trust is in the gateway's prompts.
|
|
136
|
+
{ capabilities: {} });
|
|
137
|
+
const tail = [];
|
|
138
|
+
let watched;
|
|
139
|
+
const abort = () => void client.close().catch(() => undefined);
|
|
140
|
+
if (options.signal?.aborted)
|
|
141
|
+
throw options.signal.reason ?? new Error("connect aborted");
|
|
142
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
143
|
+
// What came out of the locked-keys file, so no log line or problem report repeats it.
|
|
144
|
+
const record = spec.transport === "stdio" ? spec.env : spec.headers;
|
|
145
|
+
const secretValues = Object.entries(record)
|
|
146
|
+
.filter(([, v]) => isSecretRef(v))
|
|
147
|
+
.map(([k]) => k);
|
|
148
|
+
const literals = specValues(spec);
|
|
149
|
+
try {
|
|
150
|
+
if (spec.transport === "stdio") {
|
|
151
|
+
// Throws SecretMissing, which the caller reports as "secret-missing".
|
|
152
|
+
const env = resolveSecretRecord(spec.env, options.secrets);
|
|
153
|
+
const resolved = secretValues.map((k) => env[k]).filter((v) => v !== undefined);
|
|
154
|
+
options.onResolved?.(resolved);
|
|
155
|
+
for (const name of spec.pass_env) {
|
|
156
|
+
const value = process.env[name];
|
|
157
|
+
if (value !== undefined)
|
|
158
|
+
env[name] = value;
|
|
159
|
+
}
|
|
160
|
+
// A child process gets only what the config names, so the gateway's own
|
|
161
|
+
// credentials are not handed to every upstream by default.
|
|
162
|
+
watched = new WatchedStdioTransport({
|
|
163
|
+
command: spec.command,
|
|
164
|
+
args: spec.args,
|
|
165
|
+
env,
|
|
166
|
+
...(spec.cwd ? { cwd: spec.cwd } : {}),
|
|
167
|
+
stderr: "pipe"
|
|
168
|
+
});
|
|
169
|
+
/*
|
|
170
|
+
* Before `connect`: the SDK hands out the stderr stream ahead of the
|
|
171
|
+
* spawn precisely so that a server which writes why it cannot start
|
|
172
|
+
* and exits is heard. Those last lines are the tail kept for the
|
|
173
|
+
* problem report, and the only explanation a broken server gives.
|
|
174
|
+
*/
|
|
175
|
+
const sink = options.onStderr ??
|
|
176
|
+
((line) => {
|
|
177
|
+
process.stderr.write(`${line}\n`);
|
|
178
|
+
});
|
|
179
|
+
if (watched.stderr) {
|
|
180
|
+
UpstreamConnection.drainStderr(watched.stderr, spec.name, (line) => {
|
|
181
|
+
const clean = scrub(line, literals, resolved);
|
|
182
|
+
tail.push(clean.slice(`[${spec.name}] `.length));
|
|
183
|
+
if (tail.length > STDERR_TAIL_LINES)
|
|
184
|
+
tail.shift();
|
|
185
|
+
sink(clean);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
await client.connect(watched);
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
/*
|
|
193
|
+
* "Connection closed" says nothing a person can act on. If the
|
|
194
|
+
* child is gone, say that it exited, with its code and its last
|
|
195
|
+
* words -- after a beat, because the exit and the final stderr
|
|
196
|
+
* bytes can land just after the protocol notices.
|
|
197
|
+
*/
|
|
198
|
+
if (/Connection closed/i.test(err?.message ?? "")) {
|
|
199
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
200
|
+
if (watched.exited || !watched.pid) {
|
|
201
|
+
throw Object.assign(new UpstreamExited(spec.name, watched.exitCode, [...tail], watched.exitSignal), { cause: err });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
throw err;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
const check = egress.check(spec.url, "POST");
|
|
209
|
+
if (!check.ok) {
|
|
210
|
+
throw new EgressDenied(`upstream "${spec.name}" at ${spec.url} is not permitted by egress policy: ${check.reason}`, check.code ?? "refused");
|
|
211
|
+
}
|
|
212
|
+
// Resolving here refuses an upstream URL that points into private space
|
|
213
|
+
// before any connection is attempted.
|
|
214
|
+
await egress.resolveVerified(new URL(spec.url).hostname);
|
|
215
|
+
const headers = resolveSecretRecord(spec.headers, options.secrets);
|
|
216
|
+
options.onResolved?.(secretValues.map((k) => headers[k]).filter((v) => v !== undefined));
|
|
217
|
+
try {
|
|
218
|
+
await client.connect(new StreamableHTTPClientTransport(new URL(spec.url), {
|
|
219
|
+
requestInit: { headers },
|
|
220
|
+
/*
|
|
221
|
+
* One client for the process, with the headers decided at send time
|
|
222
|
+
* rather than baked in here. This is what lets a caller's own
|
|
223
|
+
* credential reach the upstream without the gateway ever storing
|
|
224
|
+
* one, and it re-runs the egress check per request instead of only
|
|
225
|
+
* at connect. Outside a tool call -- here, and for tool discovery
|
|
226
|
+
* -- there is no credential in scope, so these requests carry the
|
|
227
|
+
* configured headers and nothing else.
|
|
228
|
+
*/
|
|
229
|
+
fetch: credentialFetch(spec, egress, options.tokenSource)
|
|
230
|
+
}));
|
|
231
|
+
}
|
|
232
|
+
catch (err) {
|
|
233
|
+
const status = err.code;
|
|
234
|
+
if ((status === 404 || status === 405 || status === 400) && (await UpstreamConnection.answersSse(spec.url, egress))) {
|
|
235
|
+
throw new UpstreamSseOnly(spec.url);
|
|
236
|
+
}
|
|
237
|
+
throw err;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
const connection = new UpstreamConnection(spec, client, tail, watched);
|
|
241
|
+
await connection.refreshTools();
|
|
242
|
+
await connection.refreshResources();
|
|
243
|
+
await connection.refreshPrompts();
|
|
244
|
+
return connection;
|
|
245
|
+
}
|
|
246
|
+
catch (err) {
|
|
247
|
+
await client.close().catch(() => undefined);
|
|
248
|
+
throw err;
|
|
249
|
+
}
|
|
250
|
+
finally {
|
|
251
|
+
options.signal?.removeEventListener("abort", abort);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Whether a URL that refused a POST answers a GET with an event stream --
|
|
256
|
+
* the shape of the old HTTP+SSE transport. Asked only after a failure, so a
|
|
257
|
+
* working server never sees the extra request, and bounded to a few
|
|
258
|
+
* seconds because the answer is only worth a better error message.
|
|
259
|
+
*/
|
|
260
|
+
static async answersSse(url, egress) {
|
|
261
|
+
const controller = new AbortController();
|
|
262
|
+
const timer = setTimeout(() => controller.abort(), 3000);
|
|
263
|
+
timer.unref?.();
|
|
264
|
+
try {
|
|
265
|
+
const response = await egress.streamingFetch(url, {
|
|
266
|
+
method: "GET",
|
|
267
|
+
headers: { accept: "text/event-stream" },
|
|
268
|
+
signal: controller.signal
|
|
269
|
+
});
|
|
270
|
+
const type = response.headers.get("content-type") ?? "";
|
|
271
|
+
// Not cancelled: aborting the request below ends the stream without
|
|
272
|
+
// leaving the guard's size-cap pipe rejected with nobody listening.
|
|
273
|
+
return response.ok && type.toLowerCase().startsWith("text/event-stream");
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
finally {
|
|
279
|
+
clearTimeout(timer);
|
|
280
|
+
controller.abort();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/** What this upstream offers beyond tools, as it declared at initialize. */
|
|
284
|
+
get offers() {
|
|
285
|
+
const capabilities = this.client.getServerCapabilities();
|
|
286
|
+
return { resources: capabilities?.resources !== undefined, prompts: capabilities?.prompts !== undefined };
|
|
287
|
+
}
|
|
288
|
+
async refreshTools() {
|
|
289
|
+
const capabilities = this.client.getServerCapabilities();
|
|
290
|
+
if (!capabilities?.tools) {
|
|
291
|
+
this.tools = [];
|
|
292
|
+
return this.tools;
|
|
293
|
+
}
|
|
294
|
+
const tools = await allPages(async (cursor) => {
|
|
295
|
+
const page = await this.client.listTools(cursor === undefined ? undefined : { cursor });
|
|
296
|
+
return { items: page.tools, nextCursor: page.nextCursor };
|
|
297
|
+
});
|
|
298
|
+
this.tools = tools.map((tool) => ({
|
|
299
|
+
name: tool.name,
|
|
300
|
+
description: tool.description ?? "",
|
|
301
|
+
inputSchema: tool.inputSchema,
|
|
302
|
+
outputSchema: tool.outputSchema,
|
|
303
|
+
annotations: tool.annotations
|
|
304
|
+
}));
|
|
305
|
+
return this.tools;
|
|
306
|
+
}
|
|
307
|
+
listTools() {
|
|
308
|
+
return this.tools;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Resources, including templates, which are listed by a separate call and
|
|
312
|
+
* are the same thing to everything downstream: an advertised description
|
|
313
|
+
* and a URI the model will send back.
|
|
314
|
+
*/
|
|
315
|
+
async refreshResources() {
|
|
316
|
+
if (!this.offers.resources) {
|
|
317
|
+
this.resources = [];
|
|
318
|
+
return this.resources;
|
|
319
|
+
}
|
|
320
|
+
const found = [];
|
|
321
|
+
const resources = await allPages(async (cursor) => {
|
|
322
|
+
const page = await this.client.listResources(cursor === undefined ? undefined : { cursor });
|
|
323
|
+
return { items: page.resources, nextCursor: page.nextCursor };
|
|
324
|
+
});
|
|
325
|
+
for (const r of resources) {
|
|
326
|
+
found.push({
|
|
327
|
+
uri: r.uri,
|
|
328
|
+
name: r.name ?? r.uri,
|
|
329
|
+
description: r.description ?? "",
|
|
330
|
+
mimeType: r.mimeType,
|
|
331
|
+
template: false
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
/*
|
|
335
|
+
* Not every server that has resources has templates, and the SDK answers
|
|
336
|
+
* "method not found" rather than an empty list. That is not a failure
|
|
337
|
+
* worth refusing an upstream over.
|
|
338
|
+
*/
|
|
339
|
+
try {
|
|
340
|
+
const templates = await allPages(async (cursor) => {
|
|
341
|
+
const page = await this.client.listResourceTemplates(cursor === undefined ? undefined : { cursor });
|
|
342
|
+
return { items: page.resourceTemplates, nextCursor: page.nextCursor };
|
|
343
|
+
});
|
|
344
|
+
for (const t of templates) {
|
|
345
|
+
found.push({
|
|
346
|
+
uri: t.uriTemplate,
|
|
347
|
+
name: t.name ?? t.uriTemplate,
|
|
348
|
+
description: t.description ?? "",
|
|
349
|
+
mimeType: t.mimeType,
|
|
350
|
+
template: true
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
catch {
|
|
355
|
+
/* no templates on this server */
|
|
356
|
+
}
|
|
357
|
+
this.resources = found;
|
|
358
|
+
return this.resources;
|
|
359
|
+
}
|
|
360
|
+
listResources() {
|
|
361
|
+
return this.resources;
|
|
362
|
+
}
|
|
363
|
+
async refreshPrompts() {
|
|
364
|
+
if (!this.offers.prompts) {
|
|
365
|
+
this.prompts = [];
|
|
366
|
+
return this.prompts;
|
|
367
|
+
}
|
|
368
|
+
const prompts = await allPages(async (cursor) => {
|
|
369
|
+
const page = await this.client.listPrompts(cursor === undefined ? undefined : { cursor });
|
|
370
|
+
return { items: page.prompts, nextCursor: page.nextCursor };
|
|
371
|
+
});
|
|
372
|
+
this.prompts = prompts.map((p) => ({
|
|
373
|
+
name: p.name,
|
|
374
|
+
description: p.description ?? "",
|
|
375
|
+
arguments: (p.arguments ?? []).map((a) => ({
|
|
376
|
+
name: a.name,
|
|
377
|
+
...(a.description === undefined ? {} : { description: a.description }),
|
|
378
|
+
...(a.required === undefined ? {} : { required: a.required })
|
|
379
|
+
}))
|
|
380
|
+
}));
|
|
381
|
+
return this.prompts;
|
|
382
|
+
}
|
|
383
|
+
listPrompts() {
|
|
384
|
+
return this.prompts;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Calls back when this upstream announces that its tool list changed.
|
|
388
|
+
*
|
|
389
|
+
* Registered only when the upstream said it would send them, because the
|
|
390
|
+
* SDK treats a handler for an undeclared notification as a programming
|
|
391
|
+
* error rather than ignoring it.
|
|
392
|
+
*/
|
|
393
|
+
onToolsChanged(listener) {
|
|
394
|
+
if (this.client.getServerCapabilities()?.tools?.listChanged !== true)
|
|
395
|
+
return;
|
|
396
|
+
this.client.setNotificationHandler(ToolListChangedNotificationSchema, async () => {
|
|
397
|
+
listener();
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
async callTool(name, args) {
|
|
401
|
+
return this.client.callTool({ name, arguments: args });
|
|
402
|
+
}
|
|
403
|
+
async readResource(uri) {
|
|
404
|
+
return this.client.readResource({ uri });
|
|
405
|
+
}
|
|
406
|
+
async getPrompt(name, args) {
|
|
407
|
+
return this.client.getPrompt({ name, arguments: args });
|
|
408
|
+
}
|
|
409
|
+
async close() {
|
|
410
|
+
this.closing = true;
|
|
411
|
+
await this.client.close();
|
|
412
|
+
}
|
|
413
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This package's version, read from its own `package.json`.
|
|
3
|
+
*
|
|
4
|
+
* It used to be typed out wherever it was needed, which is five places that
|
|
5
|
+
* each go on saying 0.1.0 after the first release that is not 0.1.0.
|
|
6
|
+
* `package.json` sits one level above both `src/` and `dist/`, and npm always
|
|
7
|
+
* ships it, so the same relative path works from a checkout and from an
|
|
8
|
+
* installed package.
|
|
9
|
+
*/
|
|
10
|
+
export declare const VERSION: string;
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
/**
|
|
3
|
+
* This package's version, read from its own `package.json`.
|
|
4
|
+
*
|
|
5
|
+
* It used to be typed out wherever it was needed, which is five places that
|
|
6
|
+
* each go on saying 0.1.0 after the first release that is not 0.1.0.
|
|
7
|
+
* `package.json` sits one level above both `src/` and `dist/`, and npm always
|
|
8
|
+
* ships it, so the same relative path works from a checkout and from an
|
|
9
|
+
* installed package.
|
|
10
|
+
*/
|
|
11
|
+
export const VERSION = createRequire(import.meta.url)("../package.json").version;
|
package/package.json
CHANGED
|
@@ -1,10 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hmcp-gateway",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "A policy gateway that sits between an agent and the MCP servers it uses
|
|
5
|
-
"license": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A policy gateway that sits between an agent and the MCP servers it uses, enforcing what each tool may do.",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"author": "Aditya Das",
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/aoutix/hardened-mcp.git",
|
|
10
|
+
"directory": "packages/gateway"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/aoutix/hardened-mcp#readme",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"mcp",
|
|
15
|
+
"model-context-protocol",
|
|
16
|
+
"security",
|
|
17
|
+
"policy",
|
|
18
|
+
"gateway",
|
|
19
|
+
"audit"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"bin": {
|
|
23
|
+
"hmcp-gateway": "bin/hmcp-gateway.js"
|
|
24
|
+
},
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"bin",
|
|
32
|
+
"dist",
|
|
33
|
+
"!dist/**/*.map",
|
|
34
|
+
"packs",
|
|
35
|
+
"catalog",
|
|
36
|
+
"!catalog/fixtures",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=22.19.0"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"hmcp-core": "0.1.0",
|
|
48
|
+
"hmcp-scanner": "0.1.0",
|
|
49
|
+
"@modelcontextprotocol/sdk": "^1.32.1",
|
|
50
|
+
"commander": "^15.0.0",
|
|
51
|
+
"yaml": "^2.7.0",
|
|
52
|
+
"zod": "^4.0.0"
|
|
53
|
+
}
|
|
10
54
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Brave's own search MCP server (@brave/brave-search-mcp-server).
|
|
2
|
+
#
|
|
3
|
+
# Tool names from a real run: catalog/fixtures/brave-search.tools.json. Every
|
|
4
|
+
# tool is a search sent to Brave's API and nothing else.
|
|
5
|
+
name: brave-search
|
|
6
|
+
label: Brave Search MCP server
|
|
7
|
+
detect:
|
|
8
|
+
- "@brave/brave-search-mcp-server"
|
|
9
|
+
- mcp/brave-search
|
|
10
|
+
|
|
11
|
+
tools:
|
|
12
|
+
brave_web_search: { effect: read, decision: allow }
|
|
13
|
+
brave_local_search: { effect: read, decision: allow }
|
|
14
|
+
brave_place_search: { effect: read, decision: allow }
|
|
15
|
+
brave_image_search: { effect: read, decision: allow }
|
|
16
|
+
brave_video_search: { effect: read, decision: allow }
|
|
17
|
+
brave_news_search: { effect: read, decision: allow }
|
|
18
|
+
brave_llm_context: { effect: read, decision: allow }
|
|
19
|
+
brave_answers: { effect: read, decision: allow }
|
|
20
|
+
brave_summarizer: { effect: read, decision: allow }
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Context7 (Upstash): up-to-date library documentation.
|
|
2
|
+
#
|
|
3
|
+
# Tool names from a real run against https://mcp.context7.com/mcp:
|
|
4
|
+
# catalog/fixtures/context7.tools.json. Both tools look documentation up.
|
|
5
|
+
name: context7
|
|
6
|
+
label: Context7 MCP server
|
|
7
|
+
detect:
|
|
8
|
+
- "@upstash/context7-mcp"
|
|
9
|
+
- mcp.context7.com
|
|
10
|
+
|
|
11
|
+
tools:
|
|
12
|
+
resolve-library-id: { effect: read, decision: allow }
|
|
13
|
+
query-docs: { effect: read, decision: allow }
|
package/packs/fetch.yaml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# The reference fetch MCP server (mcp-server-fetch).
|
|
2
|
+
#
|
|
3
|
+
# Tool names from a real run: catalog/fixtures/fetch.tools.json. Fetching a
|
|
4
|
+
# page reads it, but the address is chosen by the AI and can carry anything
|
|
5
|
+
# it has seen in the query string, so it waits for a person rather than
|
|
6
|
+
# running freely. A remembered approval covers the sites someone trusts.
|
|
7
|
+
name: fetch
|
|
8
|
+
label: Fetch MCP server
|
|
9
|
+
detect:
|
|
10
|
+
- mcp-server-fetch
|
|
11
|
+
- mcp/fetch
|
|
12
|
+
|
|
13
|
+
tools:
|
|
14
|
+
fetch: { effect: read, decision: approve }
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# The reference filesystem MCP server.
|
|
2
|
+
#
|
|
3
|
+
# Every rule names an exact tool. `adopt` writes only the ones that matched a
|
|
4
|
+
# tool the server actually advertised, and reports the rest, because a pack
|
|
5
|
+
# written against last year's release must not quietly classify nothing.
|
|
6
|
+
#
|
|
7
|
+
# Deliberately not globs. A pattern like `*__{read,list}_*` matches one naming
|
|
8
|
+
# convention, so it misses `readFile` and `read-file`, and where it does match
|
|
9
|
+
# it overrides the gateway's own classification -- including its refusal to
|
|
10
|
+
# believe a server that marks its own delete tool read-only.
|
|
11
|
+
name: filesystem
|
|
12
|
+
label: Filesystem MCP server
|
|
13
|
+
detect:
|
|
14
|
+
- "@modelcontextprotocol/server-filesystem"
|
|
15
|
+
- mcp-server-filesystem
|
|
16
|
+
- server-filesystem
|
|
17
|
+
|
|
18
|
+
# The argument every call here is bounded by. `adopt` substitutes the
|
|
19
|
+
# directory being adopted into, so the written policy carries a real absolute
|
|
20
|
+
# path a reviewer can read.
|
|
21
|
+
path_bound: true
|
|
22
|
+
|
|
23
|
+
tools:
|
|
24
|
+
# Reading runs freely inside the folders it was given, and is blocked
|
|
25
|
+
# outside them by the toolbox itself, with plain words to the AI -- not
|
|
26
|
+
# passed on for the server to refuse, which read as the server failing.
|
|
27
|
+
read_file: { effect: read, decision: allow, on_violation: deny, paths: [path] }
|
|
28
|
+
read_text_file: { effect: read, decision: allow, on_violation: deny, paths: [path] }
|
|
29
|
+
read_media_file: { effect: read, decision: allow, on_violation: deny, paths: [path] }
|
|
30
|
+
read_multiple_files: { effect: read, decision: allow, on_violation: deny, paths: ["paths[]"] }
|
|
31
|
+
list_directory: { effect: read, decision: allow, on_violation: deny, paths: [path] }
|
|
32
|
+
list_directory_with_sizes: { effect: read, decision: allow, on_violation: deny, paths: [path] }
|
|
33
|
+
directory_tree: { effect: read, decision: allow, on_violation: deny, paths: [path] }
|
|
34
|
+
search_files: { effect: read, decision: allow, on_violation: deny, paths: [path] }
|
|
35
|
+
get_file_info: { effect: read, decision: allow, on_violation: deny, paths: [path] }
|
|
36
|
+
list_allowed_directories: { effect: read, decision: allow }
|
|
37
|
+
|
|
38
|
+
# A change asks a person first, and only inside the folder it was given.
|
|
39
|
+
# Outside that folder it is blocked outright. This is what "Protect me now"
|
|
40
|
+
# promises -- changing or deleting a file waits for your OK -- and the
|
|
41
|
+
# gateway's default for a write, so a ready-made rule never quietly loosens
|
|
42
|
+
# it. (An earlier version let writes inside the folder run unasked and set
|
|
43
|
+
# `unknown_args: deny`, which refused every real write because `content`,
|
|
44
|
+
# `edits` and `dryRun` are not paths; a person approving the call sees all
|
|
45
|
+
# of its arguments, so they are left unbounded here.)
|
|
46
|
+
write_file:
|
|
47
|
+
effect: write
|
|
48
|
+
decision: approve
|
|
49
|
+
on_violation: deny
|
|
50
|
+
paths: [path]
|
|
51
|
+
edit_file:
|
|
52
|
+
effect: write
|
|
53
|
+
decision: approve
|
|
54
|
+
on_violation: deny
|
|
55
|
+
paths: [path]
|
|
56
|
+
create_directory:
|
|
57
|
+
effect: write
|
|
58
|
+
decision: approve
|
|
59
|
+
on_violation: deny
|
|
60
|
+
paths: [path]
|
|
61
|
+
append_file:
|
|
62
|
+
effect: write
|
|
63
|
+
decision: approve
|
|
64
|
+
on_violation: deny
|
|
65
|
+
paths: [path]
|
|
66
|
+
copy_file:
|
|
67
|
+
effect: write
|
|
68
|
+
decision: approve
|
|
69
|
+
on_violation: deny
|
|
70
|
+
# Both ends: copying a file in from outside the folder reads it, and
|
|
71
|
+
# copying one out writes outside it.
|
|
72
|
+
paths: [source, destination]
|
|
73
|
+
move_file:
|
|
74
|
+
effect: write
|
|
75
|
+
decision: approve
|
|
76
|
+
on_violation: deny
|
|
77
|
+
# Both ends: moving a file out of the folder is as much an escape as
|
|
78
|
+
# writing one outside it.
|
|
79
|
+
paths: [source, destination]
|
package/packs/git.yaml
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# The reference git MCP server (mcp-server-git).
|
|
2
|
+
#
|
|
3
|
+
# Tool names from a real run: catalog/fixtures/git.tools.json. Looking at the
|
|
4
|
+
# repository runs freely; anything that changes it waits for a person.
|
|
5
|
+
name: git
|
|
6
|
+
label: Git MCP server
|
|
7
|
+
detect:
|
|
8
|
+
# Followed by a space, @ or ==, so "mcp-server-github" is not taken for it.
|
|
9
|
+
- "mcp-server-git "
|
|
10
|
+
- "mcp-server-git@"
|
|
11
|
+
- "mcp-server-git=="
|
|
12
|
+
|
|
13
|
+
tools:
|
|
14
|
+
git_status: { effect: read, decision: allow }
|
|
15
|
+
git_diff: { effect: read, decision: allow }
|
|
16
|
+
git_diff_staged: { effect: read, decision: allow }
|
|
17
|
+
git_diff_unstaged: { effect: read, decision: allow }
|
|
18
|
+
git_log: { effect: read, decision: allow }
|
|
19
|
+
git_show: { effect: read, decision: allow }
|
|
20
|
+
git_branch: { effect: read, decision: allow }
|
|
21
|
+
git_remote: { effect: read, decision: allow }
|
|
22
|
+
|
|
23
|
+
git_add: { effect: write, decision: approve }
|
|
24
|
+
git_commit: { effect: write, decision: approve }
|
|
25
|
+
git_create_branch: { effect: write, decision: approve }
|
|
26
|
+
git_checkout: { effect: write, decision: approve }
|
|
27
|
+
git_merge: { effect: write, decision: approve }
|
|
28
|
+
# Throws away work that was not committed.
|
|
29
|
+
git_reset: { effect: destructive, decision: approve }
|