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.
Files changed (102) hide show
  1. package/LICENSE +77 -0
  2. package/README.md +41 -9
  3. package/bin/hmcp-gateway.js +20 -0
  4. package/catalog/atlassian.yaml +41 -0
  5. package/catalog/brave-search.yaml +57 -0
  6. package/catalog/context7.yaml +42 -0
  7. package/catalog/fetch.yaml +37 -0
  8. package/catalog/figma.yaml +48 -0
  9. package/catalog/filesystem.yaml +48 -0
  10. package/catalog/git.yaml +48 -0
  11. package/catalog/github.yaml +103 -0
  12. package/catalog/huggingface.yaml +59 -0
  13. package/catalog/linear.yaml +47 -0
  14. package/catalog/memory.yaml +47 -0
  15. package/catalog/notion.yaml +41 -0
  16. package/catalog/playwright.yaml +38 -0
  17. package/catalog/postgres.yaml +66 -0
  18. package/catalog/sentry.yaml +39 -0
  19. package/catalog/sequential-thinking.yaml +29 -0
  20. package/catalog/stripe.yaml +41 -0
  21. package/catalog/supabase.yaml +45 -0
  22. package/catalog/time.yaml +49 -0
  23. package/dist/adopt-cli.d.ts +351 -0
  24. package/dist/adopt-cli.js +1857 -0
  25. package/dist/adopt-emit.d.ts +64 -0
  26. package/dist/adopt-emit.js +248 -0
  27. package/dist/adopt-plan.d.ts +108 -0
  28. package/dist/adopt-plan.js +386 -0
  29. package/dist/catalog.d.ts +140 -0
  30. package/dist/catalog.js +250 -0
  31. package/dist/classify.d.ts +23 -0
  32. package/dist/classify.js +47 -0
  33. package/dist/cli.d.ts +2 -0
  34. package/dist/cli.js +535 -0
  35. package/dist/client-allow.d.ts +42 -0
  36. package/dist/client-allow.js +95 -0
  37. package/dist/client-config.d.ts +200 -0
  38. package/dist/client-config.js +784 -0
  39. package/dist/config-source.d.ts +106 -0
  40. package/dist/config-source.js +156 -0
  41. package/dist/config.d.ts +159 -0
  42. package/dist/config.js +330 -0
  43. package/dist/connect-client.d.ts +49 -0
  44. package/dist/connect-client.js +249 -0
  45. package/dist/contracts.d.ts +251 -0
  46. package/dist/contracts.js +9 -0
  47. package/dist/credential.d.ts +63 -0
  48. package/dist/credential.js +136 -0
  49. package/dist/draft.d.ts +71 -0
  50. package/dist/draft.js +596 -0
  51. package/dist/exposure-cli.d.ts +28 -0
  52. package/dist/exposure-cli.js +119 -0
  53. package/dist/gateway.d.ts +756 -0
  54. package/dist/gateway.js +2106 -0
  55. package/dist/index.d.ts +26 -0
  56. package/dist/index.js +26 -0
  57. package/dist/notify.d.ts +44 -0
  58. package/dist/notify.js +93 -0
  59. package/dist/oauth.d.ts +139 -0
  60. package/dist/oauth.js +641 -0
  61. package/dist/observe.d.ts +10 -0
  62. package/dist/observe.js +26 -0
  63. package/dist/open.d.ts +24 -0
  64. package/dist/open.js +31 -0
  65. package/dist/packs.d.ts +119 -0
  66. package/dist/packs.js +131 -0
  67. package/dist/pin-cli.d.ts +28 -0
  68. package/dist/pin-cli.js +103 -0
  69. package/dist/probe-upstream.d.ts +31 -0
  70. package/dist/probe-upstream.js +377 -0
  71. package/dist/registry-map.d.ts +39 -0
  72. package/dist/registry-map.js +390 -0
  73. package/dist/rule-cli.d.ts +51 -0
  74. package/dist/rule-cli.js +412 -0
  75. package/dist/serve-http.d.ts +67 -0
  76. package/dist/serve-http.js +478 -0
  77. package/dist/snippet.d.ts +20 -0
  78. package/dist/snippet.js +826 -0
  79. package/dist/status-cli.d.ts +61 -0
  80. package/dist/status-cli.js +195 -0
  81. package/dist/suggest-cli.d.ts +43 -0
  82. package/dist/suggest-cli.js +279 -0
  83. package/dist/toolname.d.ts +47 -0
  84. package/dist/toolname.js +149 -0
  85. package/dist/upstream-errors.d.ts +54 -0
  86. package/dist/upstream-errors.js +330 -0
  87. package/dist/upstream.d.ts +128 -0
  88. package/dist/upstream.js +413 -0
  89. package/dist/version.d.ts +10 -0
  90. package/dist/version.js +11 -0
  91. package/package.json +50 -6
  92. package/packs/brave-search.yaml +20 -0
  93. package/packs/context7.yaml +13 -0
  94. package/packs/fetch.yaml +14 -0
  95. package/packs/filesystem.yaml +79 -0
  96. package/packs/git.yaml +29 -0
  97. package/packs/github.yaml +47 -0
  98. package/packs/huggingface.yaml +16 -0
  99. package/packs/memory.yaml +23 -0
  100. package/packs/playwright.yaml +41 -0
  101. package/packs/sequential-thinking.yaml +13 -0
  102. package/packs/time.yaml +14 -0
@@ -0,0 +1,250 @@
1
+ /**
2
+ * The curated catalog: packages/gateway/catalog/*.yaml, one file per server.
3
+ *
4
+ * Each entry is what a person with no technical background sees when they
5
+ * pick a server: what it lets the AI do, a numbered guide with links to the
6
+ * exact pages, and the fields to fill in. The launch template underneath is
7
+ * only ever turned into an upstream by `materializeDraft`, so the rules about
8
+ * keys (a secret field never inlined into a string) are checked here, at load
9
+ * time, rather than trusted to every author.
10
+ */
11
+ import { readFileSync, readdirSync } from "node:fs";
12
+ import { dirname, join, resolve } from "node:path";
13
+ import { fileURLToPath } from "node:url";
14
+ import { parse as parseYaml } from "yaml";
15
+ import { z } from "zod";
16
+ const ID = /^[a-z0-9][a-z0-9-]*$/;
17
+ const httpsUrl = z.string().url().refine((u) => u.startsWith("https://"), { message: "links must be https" });
18
+ const FieldSchema = z
19
+ .object({
20
+ id: z.string().regex(/^[a-z][a-z0-9_]*$/),
21
+ kind: z.enum(["secret", "folder", "folders", "url", "text", "choice", "toggle"]),
22
+ label: z.string().min(1),
23
+ help: z.string().optional(),
24
+ placeholder: z.string().optional(),
25
+ required: z.boolean().optional(),
26
+ default: z.union([z.string(), z.boolean(), z.array(z.string())]).optional(),
27
+ choices: z.array(z.object({ value: z.string(), label: z.string().min(1) }).strict()).optional(),
28
+ validate: z
29
+ .object({ pattern: z.string().min(1), message: z.string().min(1) })
30
+ .strict()
31
+ .refine((v) => {
32
+ try {
33
+ new RegExp(v.pattern);
34
+ return true;
35
+ }
36
+ catch {
37
+ return false;
38
+ }
39
+ }, { message: "validate.pattern is not a valid regular expression" })
40
+ .optional(),
41
+ technical: z.string().optional()
42
+ })
43
+ .strict()
44
+ .superRefine((f, ctx) => {
45
+ if (f.kind === "choice" && (!f.choices || f.choices.length === 0)) {
46
+ ctx.addIssue({ code: "custom", message: `field "${f.id}" is a choice with no choices` });
47
+ }
48
+ // A default key would ship a secret in the package.
49
+ if (f.kind === "secret" && f.default !== undefined) {
50
+ ctx.addIssue({ code: "custom", message: `secret field "${f.id}" cannot have a default` });
51
+ }
52
+ });
53
+ const TemplateValueSchema = z.union([
54
+ z.string(),
55
+ z.object({ field: z.string().min(1), prefix: z.string().max(32).optional() }).strict()
56
+ ]);
57
+ const LaunchSchema = z.discriminatedUnion("transport", [
58
+ z
59
+ .object({
60
+ transport: z.literal("stdio"),
61
+ command: z.string().min(1),
62
+ args: z.array(z.string()).optional(),
63
+ env: z.record(z.string(), TemplateValueSchema).optional()
64
+ })
65
+ .strict(),
66
+ z
67
+ .object({
68
+ transport: z.literal("http"),
69
+ url: z.string().min(1),
70
+ headers: z.record(z.string(), TemplateValueSchema).optional(),
71
+ oauth: z.boolean().optional()
72
+ })
73
+ .strict()
74
+ ]);
75
+ const RuntimeSchema = z.enum(["node", "uv", "docker", "python", "git"]);
76
+ const VariantSchema = z
77
+ .object({
78
+ id: z.string().regex(ID),
79
+ label: z.string().min(1),
80
+ summary: z.string().optional(),
81
+ launch: LaunchSchema,
82
+ fields: z.array(z.string()).optional(),
83
+ requires: z.array(RuntimeSchema).optional()
84
+ })
85
+ .strict();
86
+ const GuideStepSchema = z
87
+ .object({
88
+ title: z.string().min(1),
89
+ body: z.string().min(1),
90
+ link: z.object({ label: z.string().min(1), url: httpsUrl }).strict().optional(),
91
+ image: z.string().optional()
92
+ })
93
+ .strict();
94
+ export const CatalogEntrySchema = z
95
+ .object({
96
+ id: z.string().regex(ID),
97
+ name: z.string().min(1),
98
+ category: z.enum(["files", "developer", "productivity", "data", "web", "knowledge", "design"]),
99
+ summary: z.string().min(1),
100
+ description: z.string().optional(),
101
+ publisher: z.object({ name: z.string().min(1), official: z.boolean() }).strict(),
102
+ homepage: httpsUrl,
103
+ registry_name: z.string().optional(),
104
+ requires: z.array(RuntimeSchema).optional(),
105
+ variants: z.array(VariantSchema).min(1),
106
+ fields: z.array(FieldSchema).default([]),
107
+ guide: z.array(GuideStepSchema).min(2),
108
+ pack: z.string().optional(),
109
+ pack_within_field: z.string().optional(),
110
+ try_prompt: z.string().optional(),
111
+ keywords: z.array(z.string()).optional(),
112
+ /*
113
+ * A harmless call Test it makes after listing the tools, so a wrong key
114
+ * or an unusable place shows up now rather than the first time the AI
115
+ * uses it. Read-only and cheap; a server that needs none has none.
116
+ */
117
+ check: z
118
+ .object({ tool: z.string().min(1), args: z.record(z.string(), z.unknown()).default({}) })
119
+ .strict()
120
+ .optional()
121
+ })
122
+ .strict()
123
+ .superRefine((e, ctx) => {
124
+ const fields = new Map(e.fields.map((f) => [f.id, f]));
125
+ if (fields.size !== e.fields.length)
126
+ ctx.addIssue({ code: "custom", message: "field ids must be unique" });
127
+ const variantIds = new Set(e.variants.map((v) => v.id));
128
+ if (variantIds.size !== e.variants.length)
129
+ ctx.addIssue({ code: "custom", message: "variant ids must be unique" });
130
+ for (const v of e.variants) {
131
+ const declared = new Set(v.fields ?? []);
132
+ for (const id of declared) {
133
+ if (!fields.has(id))
134
+ ctx.addIssue({ code: "custom", message: `variant "${v.id}" lists unknown field "${id}"` });
135
+ }
136
+ for (const used of fieldsUsedBy(v.launch)) {
137
+ const f = fields.get(used.id);
138
+ if (!f) {
139
+ ctx.addIssue({ code: "custom", message: `variant "${v.id}" uses unknown field "${used.id}"` });
140
+ continue;
141
+ }
142
+ if (!declared.has(used.id)) {
143
+ ctx.addIssue({ code: "custom", message: `variant "${v.id}" uses field "${used.id}" without listing it` });
144
+ }
145
+ /*
146
+ * A key inlined into a string would land in gateway.yaml as text. It
147
+ * may only appear as a `{ field }` value, which becomes `{ secret }`.
148
+ */
149
+ if (f.kind === "secret" && used.how === "inline") {
150
+ ctx.addIssue({ code: "custom", message: `variant "${v.id}" inlines secret field "${used.id}" into a string` });
151
+ }
152
+ if (used.how === "spread" && f.kind !== "folders") {
153
+ ctx.addIssue({ code: "custom", message: `variant "${v.id}" spreads "${used.id}", which is not a folders field` });
154
+ }
155
+ }
156
+ if (v.launch.transport === "http" && v.launch.oauth) {
157
+ const auth = Object.keys(v.launch.headers ?? {}).some((h) => h.toLowerCase() === "authorization");
158
+ if (auth)
159
+ ctx.addIssue({ code: "custom", message: `variant "${v.id}" signs in and also sets an authorization header` });
160
+ }
161
+ }
162
+ if (e.pack_within_field) {
163
+ const f = fields.get(e.pack_within_field);
164
+ if (!f || (f.kind !== "folders" && f.kind !== "folder")) {
165
+ ctx.addIssue({ code: "custom", message: "pack_within_field must name a folder or folders field" });
166
+ }
167
+ }
168
+ });
169
+ /** Every `{{fields.x}}`, `{{...fields.x}}` and `{ field: x }` in a launch template. */
170
+ export function fieldsUsedBy(launch) {
171
+ const out = [];
172
+ const scan = (s, inArgs) => {
173
+ const spread = /^\{\{\.\.\.fields\.([a-z][a-z0-9_]*)\}\}$/.exec(s);
174
+ if (spread && inArgs) {
175
+ out.push({ id: spread[1], how: "spread" });
176
+ return;
177
+ }
178
+ for (const m of s.matchAll(/\{\{\s*(\.\.\.)?fields\.([a-z][a-z0-9_]*)\s*\}\}/g)) {
179
+ out.push({ id: m[2], how: m[1] ? "spread" : "inline" });
180
+ }
181
+ };
182
+ const value = (v) => {
183
+ if (typeof v === "string")
184
+ scan(v, false);
185
+ else
186
+ out.push({ id: v.field, how: "value" });
187
+ };
188
+ if (launch.transport === "stdio") {
189
+ scan(launch.command, false);
190
+ for (const a of launch.args ?? [])
191
+ scan(a, true);
192
+ for (const v of Object.values(launch.env ?? {}))
193
+ value(v);
194
+ }
195
+ else {
196
+ scan(launch.url, false);
197
+ for (const v of Object.values(launch.headers ?? {}))
198
+ value(v);
199
+ }
200
+ return out;
201
+ }
202
+ /** The catalog folder shipped with the package; the same from src/ and dist/. */
203
+ export function catalogDir() {
204
+ return resolve(dirname(fileURLToPath(import.meta.url)), "..", "catalog");
205
+ }
206
+ const cache = new Map();
207
+ /** Every entry, validated, sorted by name. Throws naming the file on a bad entry. */
208
+ export function loadCatalog(dir = catalogDir()) {
209
+ const hit = cache.get(dir);
210
+ if (hit)
211
+ return hit;
212
+ const entries = [];
213
+ const seen = new Map();
214
+ for (const file of readdirSync(dir).sort()) {
215
+ if (!file.endsWith(".yaml"))
216
+ continue;
217
+ const path = join(dir, file);
218
+ const parsed = CatalogEntrySchema.safeParse(parseYaml(readFileSync(path, "utf8")));
219
+ if (!parsed.success) {
220
+ const why = parsed.error.issues.map((i) => `${i.path.join(".") || "(root)"}: ${i.message}`).join("; ");
221
+ throw new Error(`catalog entry ${file} is invalid: ${why}`);
222
+ }
223
+ const entry = parsed.data;
224
+ if (`${entry.id}.yaml` !== file)
225
+ throw new Error(`catalog entry ${file} has id "${entry.id}"; name the file ${entry.id}.yaml`);
226
+ const twin = seen.get(entry.id);
227
+ if (twin)
228
+ throw new Error(`catalog entries ${twin} and ${file} share the id "${entry.id}"`);
229
+ seen.set(entry.id, file);
230
+ entries.push(entry);
231
+ }
232
+ entries.sort((a, b) => a.name.localeCompare(b.name));
233
+ cache.set(dir, entries);
234
+ return entries;
235
+ }
236
+ /** The check call our own catalog sets for an entry, or undefined. Never taken from a request. */
237
+ export function catalogCheck(id, dir = catalogDir()) {
238
+ return catalogEntry(id, dir)?.check;
239
+ }
240
+ export function catalogEntry(id, dir = catalogDir()) {
241
+ return loadCatalog(dir).find((e) => e.id === id);
242
+ }
243
+ /** registry_name -> catalog id, so a public-list result can open the curated entry instead. */
244
+ export function catalogRegistryNames(dir = catalogDir()) {
245
+ const map = new Map();
246
+ for (const e of loadCatalog(dir))
247
+ if (e.registry_name)
248
+ map.set(e.registry_name, e.id);
249
+ return map;
250
+ }
@@ -0,0 +1,23 @@
1
+ import type { Effect, Policy } from "hmcp-core";
2
+ import type { UpstreamTool } from "./upstream.js";
3
+ export interface Classification {
4
+ readonly effect: Effect | undefined;
5
+ /** How the effect was decided, for the audit log and for `hmcp gateway tools`. */
6
+ readonly source: "policy" | "annotation" | "name" | "unclassified";
7
+ }
8
+ /**
9
+ * Decides what an upstream tool does.
10
+ *
11
+ * Policy rules win, because they are the only signal a human wrote. The MCP
12
+ * annotation hints come next, but a `readOnlyHint` is a claim by the server
13
+ * being gated, so on its own it is worth nothing. It is believed only when the
14
+ * tool's name says the same thing. A name that says something else wins, and
15
+ * a name that says nothing recognisable leaves the tool unclassified -- which
16
+ * the posture refuses or holds -- rather than letting the server's word for
17
+ * it stand in for a check.
18
+ *
19
+ * It used to be the other way round: the hint was believed unless the name
20
+ * began with a verb on a fixed list, so `erase_all_records`, `exec_shell` and
21
+ * `transfer_funds` were all reads the moment their server said so.
22
+ */
23
+ export declare function classify(tool: UpstreamTool, localName: string, policy: Policy): Classification;
@@ -0,0 +1,47 @@
1
+ import { classifyByName, globMatch, nameAgreesWithReadOnlyHint } from "hmcp-core";
2
+ /**
3
+ * Decides what an upstream tool does.
4
+ *
5
+ * Policy rules win, because they are the only signal a human wrote. The MCP
6
+ * annotation hints come next, but a `readOnlyHint` is a claim by the server
7
+ * being gated, so on its own it is worth nothing. It is believed only when the
8
+ * tool's name says the same thing. A name that says something else wins, and
9
+ * a name that says nothing recognisable leaves the tool unclassified -- which
10
+ * the posture refuses or holds -- rather than letting the server's word for
11
+ * it stand in for a check.
12
+ *
13
+ * It used to be the other way round: the hint was believed unless the name
14
+ * began with a verb on a fixed list, so `erase_all_records`, `exec_shell` and
15
+ * `transfer_funds` were all reads the moment their server said so.
16
+ */
17
+ export function classify(tool, localName, policy) {
18
+ const rule = policy.rules.find((r) => globMatch(r.match, localName));
19
+ if (rule?.effect)
20
+ return { effect: rule.effect, source: "policy" };
21
+ // The upstream's own name, exactly as advertised. `localName` only adds the
22
+ // gateway's prefix, and reading the name with that prefix stripped would
23
+ // mean guessing where the prefix ends.
24
+ const own = { namespaced: false };
25
+ const nameEffect = classifyByName(tool.name, own);
26
+ const annotations = tool.annotations;
27
+ if (annotations?.destructiveHint === true)
28
+ return { effect: "destructive", source: "annotation" };
29
+ if (annotations?.readOnlyHint === true) {
30
+ // Two signals that have to agree: the server's claim, and its own name.
31
+ if (nameAgreesWithReadOnlyHint(tool.name, own)) {
32
+ return { effect: "read", source: "annotation" };
33
+ }
34
+ // The name says it mutates: believe the name.
35
+ if (nameEffect && nameEffect !== "read")
36
+ return { effect: nameEffect, source: "name" };
37
+ // The name says nothing we can read, or contradicts itself. Not a read.
38
+ return { effect: undefined, source: "unclassified" };
39
+ }
40
+ if (annotations?.readOnlyHint === false) {
41
+ // The server says it mutates. Believe that, and take the worse of the two.
42
+ return { effect: nameEffect === "destructive" ? "destructive" : "write", source: "annotation" };
43
+ }
44
+ if (nameEffect)
45
+ return { effect: nameEffect, source: "name" };
46
+ return { effect: undefined, source: "unclassified" };
47
+ }
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};