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,64 @@
|
|
|
1
|
+
import type { Effect, Posture } from "hmcp-core";
|
|
2
|
+
import type { AdoptionPlan } from "./adopt-plan.js";
|
|
3
|
+
import type { PackRuleOut } from "./packs.js";
|
|
4
|
+
/**
|
|
5
|
+
* Rendering the two files adoption writes.
|
|
6
|
+
*
|
|
7
|
+
* Pure, and deliberately not a `yaml.stringify` of one big object: the
|
|
8
|
+
* comments are half the product. Someone reading `policy.yaml` in a diff a
|
|
9
|
+
* month from now needs to know why `rules` is empty and why `egress.methods`
|
|
10
|
+
* names POST, and neither is recoverable from the values alone.
|
|
11
|
+
*
|
|
12
|
+
* Each section is stringified on its own and concatenated. Hand-writing the
|
|
13
|
+
* YAML would be shorter and would also mean hand-escaping tool names, commands
|
|
14
|
+
* and URLs that came from somewhere else.
|
|
15
|
+
*/
|
|
16
|
+
export interface ProbedTool {
|
|
17
|
+
readonly localName: string;
|
|
18
|
+
readonly upstream: string;
|
|
19
|
+
readonly effect: Effect | null;
|
|
20
|
+
readonly effectSource: string;
|
|
21
|
+
readonly hidden: boolean;
|
|
22
|
+
readonly injection: {
|
|
23
|
+
readonly patternIds: readonly string[];
|
|
24
|
+
readonly action: string;
|
|
25
|
+
} | null;
|
|
26
|
+
}
|
|
27
|
+
export interface EmitOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Ready-made rules for a server `adopt` recognised, already checked against
|
|
30
|
+
* what that server advertised a moment ago.
|
|
31
|
+
*/
|
|
32
|
+
readonly packs?: readonly {
|
|
33
|
+
readonly packName: string;
|
|
34
|
+
readonly packLabel: string;
|
|
35
|
+
readonly upstreamName: string;
|
|
36
|
+
readonly rules: readonly PackRuleOut[];
|
|
37
|
+
readonly unmatched: readonly string[];
|
|
38
|
+
readonly skippedBounds: readonly string[];
|
|
39
|
+
}[];
|
|
40
|
+
readonly name: string;
|
|
41
|
+
readonly posture: Posture;
|
|
42
|
+
/** What the gateway's entry is called in the client, which decides how long a tool name may be. */
|
|
43
|
+
readonly entryName?: string | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Whether the generated policy is applied or only recorded. `observe` unless
|
|
46
|
+
* told otherwise: a gateway that starts by refusing things is one whose
|
|
47
|
+
* trial ends at the first refusal.
|
|
48
|
+
*/
|
|
49
|
+
readonly enforcement?: "observe" | "enforce" | undefined;
|
|
50
|
+
/** What a tool nothing classified gets. `approve` unless told otherwise. */
|
|
51
|
+
readonly onUnclassified?: "deny" | "approve" | undefined;
|
|
52
|
+
/** Tools the probe could not classify, which policy will hold or refuse. */
|
|
53
|
+
readonly unclassified?: readonly ProbedTool[] | undefined;
|
|
54
|
+
/** How many tools the gateway would expose, for the tool_budget note. */
|
|
55
|
+
readonly toolCount?: number | undefined;
|
|
56
|
+
}
|
|
57
|
+
export declare function emitGatewayYaml(plan: AdoptionPlan, options: EmitOptions): string;
|
|
58
|
+
export declare function emitPolicyYaml(plan: AdoptionPlan, options: EmitOptions): string;
|
|
59
|
+
/** The entry that replaces the adopted servers in the client's own config. */
|
|
60
|
+
export declare function emitClientEntry(plan: AdoptionPlan, options: {
|
|
61
|
+
readonly configPath: string;
|
|
62
|
+
readonly command: string;
|
|
63
|
+
readonly args: readonly string[];
|
|
64
|
+
}): Record<string, unknown>;
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { stringify } from "yaml";
|
|
2
|
+
import { clientToolName, toolNameBudget } from "./toolname.js";
|
|
3
|
+
function yamlBlock(value) {
|
|
4
|
+
return stringify(value, { lineWidth: 0 });
|
|
5
|
+
}
|
|
6
|
+
/** Drops the keys `UpstreamSchema`'s defaults filled in with nothing. */
|
|
7
|
+
function pruneUpstream(upstream) {
|
|
8
|
+
const out = {};
|
|
9
|
+
for (const [key, value] of Object.entries(upstream)) {
|
|
10
|
+
if (Array.isArray(value) && value.length === 0)
|
|
11
|
+
continue;
|
|
12
|
+
if (value !== null && typeof value === "object" && Object.keys(value).length === 0)
|
|
13
|
+
continue;
|
|
14
|
+
out[key] = value;
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
export function emitGatewayYaml(plan, options) {
|
|
19
|
+
const out = [];
|
|
20
|
+
out.push("# Generated by `hmcp-gateway adopt`. Safe to edit, and safe to commit: no");
|
|
21
|
+
out.push("# secret is written here. A server's env values stay in the client config that");
|
|
22
|
+
out.push("# already held them and are forwarded by name through pass_env; a header that");
|
|
23
|
+
out.push("# carries a key, and in a project a key in a server's env, is kept in the locked");
|
|
24
|
+
out.push("# keys (secrets.json) and named here as {secret: ...}.");
|
|
25
|
+
if (plan.secretsOnDisk) {
|
|
26
|
+
out.push("#");
|
|
27
|
+
out.push("# EXCEPT: --copy-env was used, so a literal credential IS present below.");
|
|
28
|
+
out.push("# Do not commit this file.");
|
|
29
|
+
}
|
|
30
|
+
if (plan.skipped.length > 0) {
|
|
31
|
+
out.push("#");
|
|
32
|
+
out.push("# Servers that were NOT adopted, and are still configured directly in the");
|
|
33
|
+
out.push("# client (recorded here so the reason outlives the terminal):");
|
|
34
|
+
for (const skip of plan.skipped) {
|
|
35
|
+
out.push(`# ${skip.name}: ${skip.reason}`);
|
|
36
|
+
out.push(`# -> ${skip.remedy}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
out.push("");
|
|
40
|
+
out.push(yamlBlock({ version: 1, name: options.name }).trimEnd());
|
|
41
|
+
out.push("");
|
|
42
|
+
out.push("# Relative to this file's own directory.");
|
|
43
|
+
out.push(yamlBlock({ policy: "./policy.yaml" }).trimEnd());
|
|
44
|
+
out.push("");
|
|
45
|
+
out.push("# Each stdio upstream inherits nothing from this process except the names it");
|
|
46
|
+
out.push("# lists in pass_env, so one upstream's credential is not handed to the rest.");
|
|
47
|
+
out.push(yamlBlock({
|
|
48
|
+
upstreams: plan.upstreams.map((u) => pruneUpstream(u.upstream))
|
|
49
|
+
}).trimEnd());
|
|
50
|
+
out.push("");
|
|
51
|
+
out.push("# What to do with an upstream tool whose description carries injected");
|
|
52
|
+
out.push("# instructions. `strip` replaces the text and keeps the tool usable; `deny`");
|
|
53
|
+
out.push("# hides it; `annotate` labels the text as untrusted and leaves it.");
|
|
54
|
+
out.push(yamlBlock({ on_injection: "strip" }).trimEnd());
|
|
55
|
+
out.push("");
|
|
56
|
+
out.push("# Prefix every tool with `<server>__`, so two upstreams cannot collide and the");
|
|
57
|
+
out.push("# audit log always says which server a call went to.");
|
|
58
|
+
out.push(yamlBlock({ namespace: true }).trimEnd());
|
|
59
|
+
if (options.entryName !== undefined) {
|
|
60
|
+
out.push("");
|
|
61
|
+
out.push("# The longest tool name this gateway will serve; a longer one is withheld.");
|
|
62
|
+
out.push("#");
|
|
63
|
+
out.push("# A model API rejects a request carrying any tool name over 64 characters,");
|
|
64
|
+
out.push("# which fails every tool rather than the one. The client sends");
|
|
65
|
+
out.push(`# \`${clientToolName(options.entryName, "<server>__<tool>")}\`, so this is 64 less its prefix.`);
|
|
66
|
+
out.push("# Wrapping a server makes its names longer than they were when it was used");
|
|
67
|
+
out.push("# directly. To make room, give the upstream above a shorter name.");
|
|
68
|
+
out.push(yamlBlock({ max_tool_name_length: toolNameBudget(options.entryName) }).trimEnd());
|
|
69
|
+
}
|
|
70
|
+
return `${out.join("\n")}\n`;
|
|
71
|
+
}
|
|
72
|
+
export function emitPolicyYaml(plan, options) {
|
|
73
|
+
const out = [];
|
|
74
|
+
const http = plan.upstreams.filter((u) => u.upstream.transport === "http");
|
|
75
|
+
out.push("# Generated by `hmcp-gateway adopt`. This file is the whole of what the");
|
|
76
|
+
out.push("# gateway will and will not let through, so it is the one worth reviewing.");
|
|
77
|
+
out.push("");
|
|
78
|
+
out.push(yamlBlock({ version: 1, name: `${options.name}-adopted` }).trimEnd());
|
|
79
|
+
out.push("");
|
|
80
|
+
const enforcement = options.enforcement ?? "observe";
|
|
81
|
+
const onUnclassified = options.onUnclassified ?? "approve";
|
|
82
|
+
if (enforcement === "observe") {
|
|
83
|
+
out.push("# OBSERVE MODE: this policy is NOT enforced.");
|
|
84
|
+
out.push("#");
|
|
85
|
+
out.push("# Every call runs, including ones the rules below would refuse or hold, and");
|
|
86
|
+
out.push("# each is recorded with what this policy would have decided. That is the way");
|
|
87
|
+
out.push("# to put the gateway in front of real work without breaking it: use your agent");
|
|
88
|
+
out.push("# as usual, then run `hmcp-gateway suggest` to see what enforcing would change.");
|
|
89
|
+
out.push("#");
|
|
90
|
+
out.push("# It protects nothing, with one exception: a tool you switch off");
|
|
91
|
+
out.push("# (`hmcp-gateway exposure off <tool>`) is refused in either mode.");
|
|
92
|
+
out.push("#");
|
|
93
|
+
out.push("# To start enforcing, change the line below to `enforcement: enforce`.");
|
|
94
|
+
out.push("# A running gateway picks that up without a restart.");
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
out.push("# This policy is enforced. `enforcement: observe` would instead let every call");
|
|
98
|
+
out.push("# run and only record what the policy would have decided.");
|
|
99
|
+
}
|
|
100
|
+
out.push(yamlBlock({ enforcement }).trimEnd());
|
|
101
|
+
out.push("");
|
|
102
|
+
out.push("# `mode` decides every call no rule matches. Under approve-writes a read goes");
|
|
103
|
+
out.push("# straight through and anything that mutates is held for a human. Under");
|
|
104
|
+
out.push("# read-only a write is refused outright; under locked, so is a read.");
|
|
105
|
+
out.push("#");
|
|
106
|
+
if (onUnclassified === "approve") {
|
|
107
|
+
out.push("# `on_unclassified: approve` holds a tool nothing could classify for a human,");
|
|
108
|
+
out.push("# every call, until someone says what it does: approving one can classify it");
|
|
109
|
+
out.push("# (`hmcp approve <id> --classify read|write|destructive`), and so can");
|
|
110
|
+
out.push("# `hmcp-gateway rule <tool>`. A tool an upstream adds tomorrow is therefore a");
|
|
111
|
+
out.push("# question rather than something reachable. `deny` refuses it outright.");
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
out.push("# `on_unclassified: deny` is what makes a tool an upstream adds tomorrow");
|
|
115
|
+
out.push("# unreachable today. See the stubs at the bottom of this file for how to");
|
|
116
|
+
out.push("# classify one deliberately. `approve` would hold it for a human instead.");
|
|
117
|
+
}
|
|
118
|
+
out.push(yamlBlock({ defaults: { mode: options.posture, on_unclassified: onUnclassified } }).trimEnd());
|
|
119
|
+
out.push("");
|
|
120
|
+
const packs = options.packs ?? [];
|
|
121
|
+
if (packs.length > 0) {
|
|
122
|
+
/* eslint-disable-next-line no-irregular-whitespace */
|
|
123
|
+
/*
|
|
124
|
+
* Rules this command wrote, from a pack for a server it recognised. Every
|
|
125
|
+
* one names a tool that was actually advertised a moment ago, checked
|
|
126
|
+
* against the probe rather than taken on faith -- and names it exactly,
|
|
127
|
+
* for the reason the note below gives about globs.
|
|
128
|
+
*/
|
|
129
|
+
out.push("# Written by `adopt` from the ready-made classification for the servers it");
|
|
130
|
+
out.push("# recognised. Each rule names one exact tool, and each tool was advertised by");
|
|
131
|
+
out.push("# the server when this file was written -- nothing here was taken on faith:");
|
|
132
|
+
for (const pack of packs) {
|
|
133
|
+
out.push(`# ${pack.packLabel} as "${pack.upstreamName}": ${pack.rules.length} rule(s)`);
|
|
134
|
+
if (pack.skippedBounds.length > 0) {
|
|
135
|
+
out.push(`# no path bound written for: ${pack.skippedBounds.join(", ")}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
out.push("#");
|
|
139
|
+
out.push("# A tool a pack does not name is not thereby allowed: it falls to the posture");
|
|
140
|
+
out.push("# and to `on_unclassified` above. These rules are yours now, and a later");
|
|
141
|
+
out.push("# `adopt` leaves this file alone.");
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
out.push("# Deliberately empty.");
|
|
145
|
+
out.push("#");
|
|
146
|
+
out.push("# A rule is only needed to say something the posture above does not already");
|
|
147
|
+
out.push("# say. The gateway classifies each tool from its MCP annotations and its name,");
|
|
148
|
+
out.push("# handling snake_case, kebab-case and camelCase alike, and `decide()` falls");
|
|
149
|
+
out.push("# back to the posture for anything no rule matches.");
|
|
150
|
+
}
|
|
151
|
+
out.push("#");
|
|
152
|
+
out.push("# Beware of reaching for a glob like `*__{get,list,read}_*` here: it matches");
|
|
153
|
+
out.push("# only one naming convention, so it silently misses `getIssue` and");
|
|
154
|
+
out.push("# `list-issues`, and where it does match it overrides both the posture and the");
|
|
155
|
+
out.push("# gateway's own classification -- including its refusal to believe a server");
|
|
156
|
+
out.push("# that marks its own delete tool read-only.");
|
|
157
|
+
out.push(yamlBlock({ rules: packs.flatMap((p) => p.rules) }).trimEnd());
|
|
158
|
+
out.push("");
|
|
159
|
+
if (http.length > 0) {
|
|
160
|
+
out.push("# Applies to HTTP upstreams only. A stdio upstream is a child process, and");
|
|
161
|
+
out.push("# what it may reach is governed by gateway.yaml instead.");
|
|
162
|
+
out.push("#");
|
|
163
|
+
out.push("# `methods` must include POST: opening an MCP session over Streamable HTTP");
|
|
164
|
+
out.push("# is a POST, and it is checked against this list before the connection is");
|
|
165
|
+
out.push("# made. The schema default is [GET, HEAD], which refuses every upstream.");
|
|
166
|
+
const egress = {
|
|
167
|
+
allow: plan.egressAllow,
|
|
168
|
+
methods: ["GET", "POST"]
|
|
169
|
+
};
|
|
170
|
+
if (plan.egressNeedsLocalHttp) {
|
|
171
|
+
out.push("#");
|
|
172
|
+
out.push("# WARNING: an adopted upstream listens on loopback or a private address, so");
|
|
173
|
+
out.push("# the SSRF protections below are off -- for EVERY HTTP upstream of this");
|
|
174
|
+
out.push("# gateway, not just that one. The upstreams that forced it:");
|
|
175
|
+
for (const u of http) {
|
|
176
|
+
if (u.warnings.some((w) => w.includes("SSRF"))) {
|
|
177
|
+
out.push(`# ${u.upstream.name} (${u.originalName})`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
out.push("# Removing that upstream and setting these back to true is the safer end");
|
|
181
|
+
out.push("# state if it was adopted by accident.");
|
|
182
|
+
egress["block_private_ips"] = false;
|
|
183
|
+
egress["allow_ip_literals"] = true;
|
|
184
|
+
egress["allow_http"] = true;
|
|
185
|
+
}
|
|
186
|
+
out.push(yamlBlock({ egress }).trimEnd());
|
|
187
|
+
out.push("");
|
|
188
|
+
}
|
|
189
|
+
out.push("# `both` tries the client first and parks the request if it cannot ask.");
|
|
190
|
+
out.push("#");
|
|
191
|
+
out.push("# Worth leaving alone: the gateway offers an in-client prompt only when the");
|
|
192
|
+
out.push("# client advertises the elicitation capability. With `elicit` and a client");
|
|
193
|
+
out.push("# that does not, a held write waits with no way to approve it. With `both`,");
|
|
194
|
+
out.push("# the same write is waiting for `hmcp pending` and `hmcp approve <id>`, or for");
|
|
195
|
+
out.push("# the console.");
|
|
196
|
+
out.push(yamlBlock({ approvals: { mode: "both", ttl_seconds: 300, single_use: true, wait_seconds: 25 } }).trimEnd());
|
|
197
|
+
out.push("");
|
|
198
|
+
out.push("# Every decision, allowed or refused, with each record chained to the last so");
|
|
199
|
+
out.push("# an edit to the middle of the log does not verify. `hmcp audit verify`.");
|
|
200
|
+
out.push(yamlBlock({ audit: { enabled: true, path: "~/.hmcp/audit.jsonl", hash_chain: true } }).trimEnd());
|
|
201
|
+
if (options.toolCount !== undefined) {
|
|
202
|
+
out.push("");
|
|
203
|
+
out.push(`# This gateway currently exposes ${options.toolCount} tool(s). The default ceiling is 40;`);
|
|
204
|
+
out.push("# above it the scanner reports that the tool set is hard to review, which is");
|
|
205
|
+
out.push("# a real cost of wrapping several servers at once. Raise it deliberately, or");
|
|
206
|
+
out.push("# switch off what the agent does not need (`hmcp-gateway exposure off`).");
|
|
207
|
+
out.push(`# tool_budget: ${Math.max(40, options.toolCount)}`);
|
|
208
|
+
}
|
|
209
|
+
const unclassified = options.unclassified ?? [];
|
|
210
|
+
if (unclassified.length > 0) {
|
|
211
|
+
out.push("");
|
|
212
|
+
out.push(`# ${unclassified.length} tool(s) could not be classified, and are ` +
|
|
213
|
+
`${onUnclassified === "deny" ? "refused" : "held for a human"} by`);
|
|
214
|
+
out.push(`# \`on_unclassified: ${onUnclassified}\` above. Uncomment a stub to classify one.`);
|
|
215
|
+
out.push("#");
|
|
216
|
+
out.push("# `effect:` is the part that matters and the part that is easy to leave out.");
|
|
217
|
+
out.push("# An unclassified tool is refused BEFORE rules are consulted, so a rule");
|
|
218
|
+
out.push("# carrying only `decision: allow` changes nothing at all -- it is the");
|
|
219
|
+
out.push("# `effect:` that gives the tool a classification for the decision to act on.");
|
|
220
|
+
out.push("#");
|
|
221
|
+
out.push("# Add these to `rules` above, not here.");
|
|
222
|
+
out.push("#");
|
|
223
|
+
out.push("# Or let something else write them, which also places each rule where it");
|
|
224
|
+
out.push("# actually takes effect and will not let you forget the `effect:`:");
|
|
225
|
+
out.push("# hmcp-gateway rule --list");
|
|
226
|
+
out.push("# hmcp-gateway rule <tool>");
|
|
227
|
+
out.push("# The console's Functions page asks the same two questions.");
|
|
228
|
+
for (const tool of unclassified) {
|
|
229
|
+
out.push("#");
|
|
230
|
+
out.push(`# - id: classify-${tool.localName}`);
|
|
231
|
+
out.push(`# match: ${JSON.stringify(tool.localName)}`);
|
|
232
|
+
out.push("# effect: read # or write, or destructive");
|
|
233
|
+
out.push("# decision: allow # or approve, or deny");
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return `${out.join("\n")}\n`;
|
|
237
|
+
}
|
|
238
|
+
/** The entry that replaces the adopted servers in the client's own config. */
|
|
239
|
+
export function emitClientEntry(plan, options) {
|
|
240
|
+
const entry = {
|
|
241
|
+
command: options.command,
|
|
242
|
+
args: [...options.args, "serve", "-c", options.configPath]
|
|
243
|
+
};
|
|
244
|
+
if (Object.keys(plan.gatewayEnv).length > 0) {
|
|
245
|
+
entry["env"] = { ...plan.gatewayEnv };
|
|
246
|
+
}
|
|
247
|
+
return entry;
|
|
248
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { type Upstream } from "./config.js";
|
|
2
|
+
import { type ClaudeServer } from "./client-config.js";
|
|
3
|
+
/**
|
|
4
|
+
* Turning the MCP servers a client already has into gateway upstreams.
|
|
5
|
+
*
|
|
6
|
+
* Pure: no filesystem, no network, no child processes. Everything that can be
|
|
7
|
+
* decided from the config alone is decided here, so the hard part of adoption
|
|
8
|
+
* is testable without starting anything.
|
|
9
|
+
*
|
|
10
|
+
* The rule throughout is that a server is either adopted or reported with a
|
|
11
|
+
* reason and a remedy. Nothing is silently dropped -- a server that quietly
|
|
12
|
+
* stops being reachable after adoption is the worst outcome this command has,
|
|
13
|
+
* because it looks exactly like the gateway working.
|
|
14
|
+
*/
|
|
15
|
+
export interface PlannedUpstream {
|
|
16
|
+
readonly upstream: Upstream;
|
|
17
|
+
/** What the client called it, which is not always a legal upstream name. */
|
|
18
|
+
readonly originalName: string;
|
|
19
|
+
readonly warnings: readonly string[];
|
|
20
|
+
/**
|
|
21
|
+
* Headers whose literal value is a key: written to the locked keys and
|
|
22
|
+
* referenced from gateway.yaml as `{secret: ...}` when the plan is applied,
|
|
23
|
+
* never written into gateway.yaml itself.
|
|
24
|
+
*/
|
|
25
|
+
readonly lockHeaders?: readonly string[];
|
|
26
|
+
}
|
|
27
|
+
export interface SkippedServer {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly reason: string;
|
|
30
|
+
readonly remedy: string;
|
|
31
|
+
}
|
|
32
|
+
export interface AdoptionPlan {
|
|
33
|
+
readonly upstreams: readonly PlannedUpstream[];
|
|
34
|
+
readonly skipped: readonly SkippedServer[];
|
|
35
|
+
/**
|
|
36
|
+
* Environment the gateway's own client entry must carry, so each upstream
|
|
37
|
+
* can name it in `pass_env`.
|
|
38
|
+
*/
|
|
39
|
+
readonly gatewayEnv: Readonly<Record<string, string>>;
|
|
40
|
+
/** Hostnames to seed `egress.allow` with. */
|
|
41
|
+
readonly egressAllow: readonly string[];
|
|
42
|
+
/** An adopted HTTP upstream is only reachable with the SSRF guard relaxed. */
|
|
43
|
+
readonly egressNeedsLocalHttp: boolean;
|
|
44
|
+
/** `--copy-env` put a literal value into the generated config. */
|
|
45
|
+
readonly secretsOnDisk: boolean;
|
|
46
|
+
/** Original name to upstream name, for every server whose name changed. */
|
|
47
|
+
readonly nameMap: ReadonlyMap<string, string>;
|
|
48
|
+
/** Malformed client entries, passed through so one report covers everything. */
|
|
49
|
+
readonly malformed: readonly {
|
|
50
|
+
readonly name: string;
|
|
51
|
+
readonly message: string;
|
|
52
|
+
}[];
|
|
53
|
+
}
|
|
54
|
+
export interface PlanOptions {
|
|
55
|
+
/** The name the gateway's own entry will have, so it is never adopted. */
|
|
56
|
+
readonly gatewayEntryName: string;
|
|
57
|
+
/** The config path the gateway will be pointed at, for the recursion check. */
|
|
58
|
+
readonly gatewayConfigPath?: string | undefined;
|
|
59
|
+
/** Resolve `${VAR}` and write literal secrets into the generated config. */
|
|
60
|
+
readonly copyEnv?: boolean | undefined;
|
|
61
|
+
/** Adopt HTTP upstreams on loopback or private addresses. */
|
|
62
|
+
readonly allowLocalHttp?: boolean | undefined;
|
|
63
|
+
/** Pin seeded egress entries to the port the upstream uses. */
|
|
64
|
+
readonly strictEgress?: boolean | undefined;
|
|
65
|
+
/** `--name <original>=<upstream>` overrides. */
|
|
66
|
+
readonly names?: ReadonlyMap<string, string> | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Upstream names already in use, when adopting more servers into a gateway
|
|
69
|
+
* that exists. A new upstream must not take a name whose tools already have
|
|
70
|
+
* exposure rows and approvals stored under it.
|
|
71
|
+
*/
|
|
72
|
+
readonly reservedNames?: readonly string[] | undefined;
|
|
73
|
+
/** For `${VAR}` resolution under `--copy-env`. */
|
|
74
|
+
readonly env?: Readonly<Record<string, string | undefined>> | undefined;
|
|
75
|
+
readonly malformed?: readonly {
|
|
76
|
+
readonly name: string;
|
|
77
|
+
readonly message: string;
|
|
78
|
+
}[] | undefined;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* An upstream name legal under `UpstreamSchema`: lowercase letters, digits and
|
|
82
|
+
* underscores.
|
|
83
|
+
*
|
|
84
|
+
* This is not cosmetic. The result becomes the `<server>__` prefix on every
|
|
85
|
+
* tool name, and that name is a storage key -- the exposure switch, every
|
|
86
|
+
* approval row and each binding hash are stored under it. So it has to be
|
|
87
|
+
* stable across runs, which is why collisions resolve by sorted original name
|
|
88
|
+
* rather than by whatever order the client's JSON happened to be in.
|
|
89
|
+
*/
|
|
90
|
+
export declare function normalizeUpstreamName(name: string): string;
|
|
91
|
+
/**
|
|
92
|
+
* Whether this host is one the egress guard refuses without the SSRF
|
|
93
|
+
* protections turned off.
|
|
94
|
+
*
|
|
95
|
+
* `localhost` counts even though it is not a literal: `resolveVerified`
|
|
96
|
+
* resolves it and then refuses the loopback address it gets back, so the
|
|
97
|
+
* hostname form is no more adoptable than `127.0.0.1`.
|
|
98
|
+
*/
|
|
99
|
+
export declare function isLocalTarget(hostname: string): boolean;
|
|
100
|
+
export declare function planAdoption(servers: ReadonlyMap<string, ClaudeServer>, options: PlanOptions): AdoptionPlan;
|
|
101
|
+
/**
|
|
102
|
+
* Expands `${VAR}` and `${VAR:-default}`, the two forms the client supports.
|
|
103
|
+
*
|
|
104
|
+
* Returns undefined when a variable with no default is unset, so the caller
|
|
105
|
+
* can skip the server and say which one rather than writing an empty
|
|
106
|
+
* credential into a config.
|
|
107
|
+
*/
|
|
108
|
+
export declare function expandInterpolation(value: string, env: Readonly<Record<string, string | undefined>>): string | undefined;
|