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
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connect to one server, list what it offers, decide each tool, disconnect.
|
|
3
|
+
* The console's "Test it". CONTRACT (phase 0); implemented by workstream F
|
|
4
|
+
* from adopt-cli.ts's probe()/probeEach().
|
|
5
|
+
*
|
|
6
|
+
* Built on a real `Gateway` rather than a bare MCP client, because the point
|
|
7
|
+
* of testing is to show what the AI app will actually get: the names as the
|
|
8
|
+
* gateway exposes them, descriptions after injection handling, the tools the
|
|
9
|
+
* gateway withholds, and the verdict policy gives each one. A bare client
|
|
10
|
+
* would show the server's own view, which is the one view nobody will use.
|
|
11
|
+
*/
|
|
12
|
+
import { execFileSync } from "node:child_process";
|
|
13
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
14
|
+
import { tmpdir } from "node:os";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
17
|
+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
18
|
+
import { decide, resolveEffect, resolveSecretRecord, RuleSchema } from "hmcp-core";
|
|
19
|
+
import { GatewayConfigSchema } from "./config.js";
|
|
20
|
+
import { Gateway } from "./gateway.js";
|
|
21
|
+
import { applyPack, loadPacks } from "./packs.js";
|
|
22
|
+
import { classifyUpstreamError } from "./upstream-errors.js";
|
|
23
|
+
import { VERSION } from "./version.js";
|
|
24
|
+
/** How long the check call may take: it is a nicety, not the test. */
|
|
25
|
+
const CHECK_TIMEOUT_MS = 8_000;
|
|
26
|
+
const DEFAULT_TIMEOUT_MS = 20_000;
|
|
27
|
+
/** How many stderr lines a failure carries; enough to show the error, not the whole log. */
|
|
28
|
+
const STDERR_TAIL = 20;
|
|
29
|
+
const KEPT = "[kept secret]";
|
|
30
|
+
export async function probeUpstream(spec, options) {
|
|
31
|
+
const started = Date.now();
|
|
32
|
+
const stderr = [];
|
|
33
|
+
/** Every resolved secret value, so none can leave in an error or a stderr line. */
|
|
34
|
+
const values = [];
|
|
35
|
+
const scrub = (text) => values.reduce((out, value) => (value.length >= 4 ? out.split(value).join(KEPT) : out), text);
|
|
36
|
+
const failed = (err) => ({
|
|
37
|
+
ok: false,
|
|
38
|
+
problem: scrubProblem(classifyUpstreamError(err, spec, stderr.slice(-STDERR_TAIL).map(scrub)), scrub),
|
|
39
|
+
tools: [],
|
|
40
|
+
resources: 0,
|
|
41
|
+
prompts: 0,
|
|
42
|
+
findings: [],
|
|
43
|
+
elapsedMs: Date.now() - started
|
|
44
|
+
});
|
|
45
|
+
/*
|
|
46
|
+
* `{ secret }` values are resolved here, in memory, rather than left for the
|
|
47
|
+
* connection: the config handed to the gateway below is never written to
|
|
48
|
+
* disk, so the value exists only in this process for as long as the test
|
|
49
|
+
* runs -- and a missing key is reported as that, before anything starts.
|
|
50
|
+
* The store is still passed on for what only the gateway can resolve: a
|
|
51
|
+
* sign-in (`oauth:`), whose token source belongs to the connection.
|
|
52
|
+
*/
|
|
53
|
+
let resolved;
|
|
54
|
+
try {
|
|
55
|
+
resolved = resolveRefs(spec, options.secrets, values);
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
return failed(err);
|
|
59
|
+
}
|
|
60
|
+
/*
|
|
61
|
+
* The audit log and approvals store go to a scratch folder. A test is not
|
|
62
|
+
* the gateway running: a server someone tried and did not keep should not
|
|
63
|
+
* leave records in the log they will later read as their gateway's history.
|
|
64
|
+
*/
|
|
65
|
+
const scratch = mkdtempSync(join(tmpdir(), "hmcp-probe-"));
|
|
66
|
+
// Who made it, so a console that starts after a crash can tell a leftover from a test still running.
|
|
67
|
+
writeFileSync(join(scratch, "owner"), String(process.pid), { mode: 0o600 });
|
|
68
|
+
const childrenBefore = childPids();
|
|
69
|
+
const policy = {
|
|
70
|
+
...structuredClone(options.policy),
|
|
71
|
+
audit: { ...options.policy.audit, path: join(scratch, "audit.jsonl"), on_failure: "continue" },
|
|
72
|
+
approvals: { ...options.policy.approvals, store_path: join(scratch, "approvals.sqlite") }
|
|
73
|
+
};
|
|
74
|
+
// The check call is the test's own, not the AI's: it runs whatever protection is set to, and is written nowhere that lasts.
|
|
75
|
+
if (options.checkCall)
|
|
76
|
+
policy.enforcement = "observe";
|
|
77
|
+
const config = GatewayConfigSchema.parse({
|
|
78
|
+
version: 1,
|
|
79
|
+
name: "hmcp-test",
|
|
80
|
+
policy: {},
|
|
81
|
+
upstreams: [{ ...resolved, ...(resolved.transport === "stdio" && !resolved.cwd ? { cwd: options.cwd } : {}) }],
|
|
82
|
+
connect_timeout_ms: options.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
83
|
+
// One server, connected once: it either works or the test says why.
|
|
84
|
+
startup: "strict",
|
|
85
|
+
upstream_reload: false,
|
|
86
|
+
policy_reload: false
|
|
87
|
+
});
|
|
88
|
+
const signal = options.signal;
|
|
89
|
+
let gateway;
|
|
90
|
+
let onAbort;
|
|
91
|
+
let work;
|
|
92
|
+
const run = async () => {
|
|
93
|
+
const gatewayOptions = {
|
|
94
|
+
config,
|
|
95
|
+
policy,
|
|
96
|
+
cwd: options.cwd,
|
|
97
|
+
...(options.secrets ? { secrets: options.secrets } : {}),
|
|
98
|
+
onUpstreamStderr: (_upstream, line) => {
|
|
99
|
+
stderr.push(line);
|
|
100
|
+
if (stderr.length > STDERR_TAIL * 2)
|
|
101
|
+
stderr.splice(0, stderr.length - STDERR_TAIL);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
gateway = new Gateway(gatewayOptions);
|
|
105
|
+
/*
|
|
106
|
+
* Recorded while it connects, not only after: a server that hangs while
|
|
107
|
+
* starting is exactly the one a crash leaves behind, and a record written
|
|
108
|
+
* after connecting was never written for it (R2 N1).
|
|
109
|
+
*/
|
|
110
|
+
const recorder = setInterval(() => recordChildren(scratch, childrenBefore), CHILD_RECORD_MS);
|
|
111
|
+
recorder.unref?.();
|
|
112
|
+
try {
|
|
113
|
+
await gateway.connectUpstreams();
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
clearInterval(recorder);
|
|
117
|
+
recordChildren(scratch, childrenBefore);
|
|
118
|
+
}
|
|
119
|
+
// Stopped while connecting: go no further on a gateway being closed.
|
|
120
|
+
if (signal?.aborted)
|
|
121
|
+
throw signal.reason;
|
|
122
|
+
const inventory = gateway.inventory();
|
|
123
|
+
const prefix = config.namespace ? `${spec.name}__` : "";
|
|
124
|
+
const packRules = previewPackRules(options, spec.name, config.namespace, inventory.map((t) => t.localName));
|
|
125
|
+
const preview = packRules.length > 0 ? { ...policy, rules: [...policy.rules, ...packRules] } : policy;
|
|
126
|
+
const packIds = new Set(packRules.map((r) => r.id));
|
|
127
|
+
const tools = inventory.map((entry) => {
|
|
128
|
+
const effect = resolveEffect({ tool: entry.localName, effect: entry.effect ?? undefined, policy: preview });
|
|
129
|
+
const decision = decide({ tool: entry.localName, effect: entry.effect ?? undefined, policy: preview, argsUnknown: true });
|
|
130
|
+
return {
|
|
131
|
+
name: entry.tool.name,
|
|
132
|
+
exposedName: entry.localName,
|
|
133
|
+
...(entry.title ? { title: entry.title } : {}),
|
|
134
|
+
description: entry.description,
|
|
135
|
+
effect: effect ?? "unknown",
|
|
136
|
+
verdict: entry.hidden ? "blocked" : verdictOf(decision.kind),
|
|
137
|
+
...(packIds.has(decision.ruleId) ? { packRule: true } : {}),
|
|
138
|
+
...(entry.hidden
|
|
139
|
+
? { withheld: "Its description carries instructions aimed at the AI, so it is hidden from the app." }
|
|
140
|
+
: {})
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
for (const held of gateway.withheld()) {
|
|
144
|
+
tools.push({
|
|
145
|
+
name: held.localName.startsWith(prefix) ? held.localName.slice(prefix.length) : held.localName,
|
|
146
|
+
exposedName: held.localName,
|
|
147
|
+
description: "",
|
|
148
|
+
effect: "unknown",
|
|
149
|
+
verdict: "blocked",
|
|
150
|
+
withheld: held.reason
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
const scan = gateway.scanUpstreams();
|
|
154
|
+
const findings = scan.findings.map((f) => ({
|
|
155
|
+
severity: f.severity,
|
|
156
|
+
message: `${f.title}. ${f.message}`,
|
|
157
|
+
...(toolOf(f.location.path, inventory.map((t) => t.localName)) ?? {})
|
|
158
|
+
}));
|
|
159
|
+
const { resources, prompts } = await countRelayed(gateway);
|
|
160
|
+
if (options.checkCall && inventory.some((t) => t.tool.name === options.checkCall.tool)) {
|
|
161
|
+
const problem = await runCheckCall(gateway, `${prefix}${options.checkCall.tool}`, options.checkCall.args, scrub);
|
|
162
|
+
if (problem)
|
|
163
|
+
return { ok: false, problem, tools, resources, prompts, findings, elapsedMs: Date.now() - started };
|
|
164
|
+
}
|
|
165
|
+
return { ok: true, tools, resources, prompts, findings, elapsedMs: Date.now() - started };
|
|
166
|
+
};
|
|
167
|
+
try {
|
|
168
|
+
if (signal?.aborted)
|
|
169
|
+
throw signal.reason;
|
|
170
|
+
work = run();
|
|
171
|
+
if (!signal)
|
|
172
|
+
return await work;
|
|
173
|
+
const stopped = new Promise((_resolve, reject) => {
|
|
174
|
+
onAbort = () => reject(signal.reason ?? new Error("the test was stopped"));
|
|
175
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
176
|
+
});
|
|
177
|
+
return await Promise.race([work, stopped]);
|
|
178
|
+
}
|
|
179
|
+
catch (err) {
|
|
180
|
+
return failed(err);
|
|
181
|
+
}
|
|
182
|
+
finally {
|
|
183
|
+
if (onAbort)
|
|
184
|
+
signal?.removeEventListener("abort", onAbort);
|
|
185
|
+
await gateway?.close().catch(() => undefined);
|
|
186
|
+
if (signal?.aborted && work) {
|
|
187
|
+
/*
|
|
188
|
+
* Stopped part way: wait for the rest to finish failing, now that the
|
|
189
|
+
* gateway is closed (a connect still under way ends at connect_timeout_ms
|
|
190
|
+
* and closes what it opened), then close again for anything it opened
|
|
191
|
+
* after the first close -- before the scratch folder goes.
|
|
192
|
+
*/
|
|
193
|
+
await work.catch(() => undefined);
|
|
194
|
+
await gateway?.close().catch(() => undefined);
|
|
195
|
+
}
|
|
196
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/** This process's child processes, by pid; empty where `pgrep` is missing (Windows). */
|
|
200
|
+
/** How often a test's servers are written down while it connects. */
|
|
201
|
+
const CHILD_RECORD_MS = 400;
|
|
202
|
+
function childPids(parent = process.pid) {
|
|
203
|
+
try {
|
|
204
|
+
const out = execFileSync("pgrep", ["-P", String(parent)], { encoding: "utf8", timeout: 2_000, stdio: ["ignore", "pipe", "ignore"] });
|
|
205
|
+
return new Set(out.split(/\s+/).filter(Boolean).map(Number));
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
return new Set();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* The servers this test started, and every process they started in turn,
|
|
213
|
+
* with when each started, written beside the scratch files. A wrapper
|
|
214
|
+
* (`sh -c "node server & wait"`) whose real server is its own child is
|
|
215
|
+
* written down with that child (R2 N1, round 3). If the console is killed
|
|
216
|
+
* mid-test, the next console start stops exactly these -- and only while they
|
|
217
|
+
* are still orphaned (or the child of one being stopped) and are the same
|
|
218
|
+
* processes (a reused pid has a different start time).
|
|
219
|
+
*/
|
|
220
|
+
function recordChildren(scratch, before) {
|
|
221
|
+
try {
|
|
222
|
+
const tree = [];
|
|
223
|
+
const queue = [...childPids()].filter((p) => !before.has(p));
|
|
224
|
+
while (queue.length > 0 && tree.length < 200) {
|
|
225
|
+
const pid = queue.shift();
|
|
226
|
+
if (tree.includes(pid))
|
|
227
|
+
continue;
|
|
228
|
+
tree.push(pid);
|
|
229
|
+
queue.push(...childPids(pid));
|
|
230
|
+
}
|
|
231
|
+
const lines = tree.flatMap((pid) => {
|
|
232
|
+
try {
|
|
233
|
+
const lstart = execFileSync("ps", ["-o", "lstart=", "-p", String(pid)], { encoding: "utf8", timeout: 2_000, stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
234
|
+
return lstart ? [`${pid}\t${lstart}`] : [];
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
return [];
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
if (lines.length > 0)
|
|
241
|
+
writeFileSync(join(scratch, "children"), lines.join("\n") + "\n", { mode: 0o600 });
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
/* best effort: a record that can't be written only means a crash leftover can't be stopped later */
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function verdictOf(kind) {
|
|
248
|
+
return kind === "allow" ? "run" : kind === "approve" ? "ask" : "blocked";
|
|
249
|
+
}
|
|
250
|
+
/** The spec with every `{ secret }` env/header value replaced by its value. */
|
|
251
|
+
function resolveRefs(spec, store, values) {
|
|
252
|
+
const record = spec.transport === "stdio" ? spec.env : spec.headers;
|
|
253
|
+
const hasRef = Object.values(record).some((v) => typeof v !== "string");
|
|
254
|
+
if (!hasRef)
|
|
255
|
+
return spec;
|
|
256
|
+
const plain = resolveSecretRecord(record, store);
|
|
257
|
+
for (const [key, value] of Object.entries(record)) {
|
|
258
|
+
if (typeof value !== "string")
|
|
259
|
+
values.push(plain[key], ...(value.prefix ? [plain[key].slice(value.prefix.length)] : []));
|
|
260
|
+
}
|
|
261
|
+
return spec.transport === "stdio" ? { ...spec, env: plain } : { ...spec, headers: plain };
|
|
262
|
+
}
|
|
263
|
+
function scrubProblem(problem, scrub) {
|
|
264
|
+
return {
|
|
265
|
+
...problem,
|
|
266
|
+
message: scrub(problem.message),
|
|
267
|
+
...(problem.detail
|
|
268
|
+
? {
|
|
269
|
+
detail: {
|
|
270
|
+
...problem.detail,
|
|
271
|
+
...(problem.detail.stderrTail ? { stderrTail: problem.detail.stderrTail.map(scrub) } : {})
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
: {})
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* The rules a pack would add, as policy rules, for the tools actually found.
|
|
279
|
+
* Appended after the policy's own rules, which is where install puts them: a
|
|
280
|
+
* rule someone wrote by hand keeps deciding the tool it names.
|
|
281
|
+
*/
|
|
282
|
+
function previewPackRules(options, upstreamName, namespace, advertised) {
|
|
283
|
+
if (!options.packId)
|
|
284
|
+
return [];
|
|
285
|
+
const pack = loadPacks().find((p) => p.name === options.packId);
|
|
286
|
+
if (!pack)
|
|
287
|
+
return [];
|
|
288
|
+
const within = options.within;
|
|
289
|
+
const applied = applyPack(pack, { upstreamName, namespace, advertised, within });
|
|
290
|
+
return applied.rules.flatMap((rule) => {
|
|
291
|
+
const parsed = RuleSchema.safeParse(rule);
|
|
292
|
+
return parsed.success ? [parsed.data] : [];
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
/** Which tool a scanner finding is about, from its location path. */
|
|
296
|
+
function toolOf(path, names) {
|
|
297
|
+
if (!path)
|
|
298
|
+
return undefined;
|
|
299
|
+
const found = names.find((n) => path === n || path.includes(`.${n}.`) || path.endsWith(`.${n}`) || path.startsWith(`${n}.`));
|
|
300
|
+
return found ? { tool: found } : undefined;
|
|
301
|
+
}
|
|
302
|
+
/** What a check call's failure says about a key. */
|
|
303
|
+
const KEY_REFUSED = /\b(401|403)\b|unauthori[sz]ed|forbidden|invalid[^.\n]{0,40}(key|token|credential)|(key|token)[^.\n]{0,40}(invalid|not valid|expired|revoked)|SUBSCRIPTION_TOKEN_INVALID|authentication failed/i;
|
|
304
|
+
/** A failure that says nothing about the setup: try again later, it is fine. */
|
|
305
|
+
const TRANSIENT = /\b429\b|rate.?limit|too many requests|quota|timed? ?out|temporar/i;
|
|
306
|
+
/**
|
|
307
|
+
* The catalog's check call, over an in-memory session like the AI app's.
|
|
308
|
+
* Null when it worked (or failed only for a reason that is no fault of the
|
|
309
|
+
* setup, such as a rate limit); otherwise the problem, in the same codes as
|
|
310
|
+
* any other failure, so the page offers the same fix.
|
|
311
|
+
*/
|
|
312
|
+
async function runCheckCall(gateway, name, args, scrub) {
|
|
313
|
+
const { server } = gateway.newSessionServer();
|
|
314
|
+
const [clientSide, serverSide] = InMemoryTransport.createLinkedPair();
|
|
315
|
+
const client = new Client({ name: "hmcp-test", version: VERSION }, { capabilities: {} });
|
|
316
|
+
let timer;
|
|
317
|
+
try {
|
|
318
|
+
await server.connect(serverSide);
|
|
319
|
+
await client.connect(clientSide);
|
|
320
|
+
const result = await Promise.race([
|
|
321
|
+
client.callTool({ name, arguments: args }),
|
|
322
|
+
new Promise((done) => {
|
|
323
|
+
timer = setTimeout(() => done("slow"), CHECK_TIMEOUT_MS);
|
|
324
|
+
})
|
|
325
|
+
]);
|
|
326
|
+
if (result === "slow" || !result.isError)
|
|
327
|
+
return null;
|
|
328
|
+
const text = scrub((Array.isArray(result.content) ? result.content : [])
|
|
329
|
+
.map((c) => (c && typeof c === "object" && c.type === "text" ? String(c.text ?? "") : ""))
|
|
330
|
+
.join(" ")
|
|
331
|
+
.slice(0, 600));
|
|
332
|
+
if (TRANSIENT.test(text))
|
|
333
|
+
return null;
|
|
334
|
+
if (KEY_REFUSED.test(text))
|
|
335
|
+
return { code: "auth-rejected", message: `a first test call to ${name} was refused: ${text}` };
|
|
336
|
+
return { code: "unknown", message: `the server started, but a first test call to ${name} failed: ${text}` };
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
return null; // A check that can't even be made says nothing about the setup.
|
|
340
|
+
}
|
|
341
|
+
finally {
|
|
342
|
+
clearTimeout(timer);
|
|
343
|
+
await client.close().catch(() => undefined);
|
|
344
|
+
await server.close().catch(() => undefined);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Resources and prompts as the gateway would advertise them, asked over an
|
|
349
|
+
* in-memory session: the gateway keeps its upstream connections private, and
|
|
350
|
+
* a session sees exactly what the AI app will.
|
|
351
|
+
*/
|
|
352
|
+
async function countRelayed(gateway) {
|
|
353
|
+
const { server } = gateway.newSessionServer();
|
|
354
|
+
const [clientSide, serverSide] = InMemoryTransport.createLinkedPair();
|
|
355
|
+
const client = new Client({ name: "hmcp-test", version: VERSION }, { capabilities: {} });
|
|
356
|
+
try {
|
|
357
|
+
await server.connect(serverSide);
|
|
358
|
+
await client.connect(clientSide);
|
|
359
|
+
const caps = client.getServerCapabilities() ?? {};
|
|
360
|
+
let resources = 0;
|
|
361
|
+
let prompts = 0;
|
|
362
|
+
if (caps.resources) {
|
|
363
|
+
resources += (await client.listResources().catch(() => ({ resources: [] }))).resources.length;
|
|
364
|
+
resources += (await client.listResourceTemplates().catch(() => ({ resourceTemplates: [] }))).resourceTemplates.length;
|
|
365
|
+
}
|
|
366
|
+
if (caps.prompts)
|
|
367
|
+
prompts = (await client.listPrompts().catch(() => ({ prompts: [] }))).prompts.length;
|
|
368
|
+
return { resources, prompts };
|
|
369
|
+
}
|
|
370
|
+
catch {
|
|
371
|
+
return { resources: 0, prompts: 0 };
|
|
372
|
+
}
|
|
373
|
+
finally {
|
|
374
|
+
await client.close().catch(() => undefined);
|
|
375
|
+
await server.close().catch(() => undefined);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mapping an MCP Registry server.json to a draft.
|
|
3
|
+
*
|
|
4
|
+
* The public list says how a server is published (an npm, PyPI or container
|
|
5
|
+
* package, or a hosted address), which settings it reads and which of them
|
|
6
|
+
* are keys. That is enough to build the same draft a catalog entry gives, so
|
|
7
|
+
* the person sees the same form, Test it and locked keys. What it does not
|
|
8
|
+
* say is whether the server is any good: nothing here was checked by anyone,
|
|
9
|
+
* and the draft says so.
|
|
10
|
+
*
|
|
11
|
+
* Only what the gateway can run is mapped: a package started over stdio, or a
|
|
12
|
+
* Streamable HTTP address. An SSE-only server, a package that has to be
|
|
13
|
+
* started separately, or a package type with no runner here (mcpb, nuget,
|
|
14
|
+
* cargo) is returned with `draft: null` and a reason in plain words.
|
|
15
|
+
*/
|
|
16
|
+
import type { RegistryHit } from "./contracts.js";
|
|
17
|
+
/** One `servers[]` item from GET /v0/servers (with its `server` and `_meta`). */
|
|
18
|
+
export type RegistryItem = {
|
|
19
|
+
server: Record<string, unknown>;
|
|
20
|
+
_meta?: Record<string, unknown>;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Why a `docker run …` command line is not contained, in plain words, or
|
|
24
|
+
* null when it is. Only the options before the image are looked at: what
|
|
25
|
+
* comes after the image goes to the server itself, not to Docker.
|
|
26
|
+
*/
|
|
27
|
+
export declare function dockerArgsProblem(args: readonly string[]): string | null;
|
|
28
|
+
/**
|
|
29
|
+
* The options before the image, read the way Docker's own parser reads them
|
|
30
|
+
* (R2 N2): `-v/:/host` is `-v` with the value attached, `-itv /:/host` is
|
|
31
|
+
* `-i`, `-t` and `-v /:/host`, and `--volume=/:/host` is `--volume`. Reading
|
|
32
|
+
* whole tokens only let the first two through. A letter this doesn't know is
|
|
33
|
+
* taken to take a value, so nothing after it is mistaken for the image.
|
|
34
|
+
*/
|
|
35
|
+
export declare function dockerRunOptions(args: readonly string[]): {
|
|
36
|
+
flag: string;
|
|
37
|
+
value: string;
|
|
38
|
+
}[];
|
|
39
|
+
export declare function mapServerJson(item: RegistryItem, catalogNames?: Map<string, string>): RegistryHit;
|