hmcp-gateway 0.1.0 → 0.2.1

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.
@@ -0,0 +1,184 @@
1
+ import { type SecretStore } from "hmcp-core";
2
+ import { ClientConfigError, type Scope, type ScopeTarget } from "./client-config.js";
3
+ import { type AdoptionPlan } from "./adopt-plan.js";
4
+ /**
5
+ * Bringing the servers an app already runs into a toolbox that was connected
6
+ * to it from the console -- and taking them out of the app's own settings, so
7
+ * they run only through the toolbox.
8
+ *
9
+ * Why not `adopt`: adopt writes gateway.yaml and policy.yaml from scratch, and
10
+ * a toolbox connected from the console is one the person built there, with
11
+ * their own servers and rules in it. So this reuses adopt's reading and
12
+ * planning (the same translation from an app's entry to an upstream, the same
13
+ * name rules, the same idea of which values are keys) and leaves writing the
14
+ * toolbox to the console, which already knows how to add a server to an
15
+ * existing gateway.yaml without disturbing anything else in it.
16
+ *
17
+ * The undo rides on the record `connectClient` already writes. That record
18
+ * has a `replaced` map -- empty for a console connection -- which is exactly
19
+ * what "Undo connection" (and `adopt --revert`) put back. Each server taken
20
+ * out of the app goes into it first, so undoing the whole connection gives
21
+ * the app back every server it had, and the file comes back byte for byte.
22
+ */
23
+ export declare class ImportClientError extends ClientConfigError {
24
+ }
25
+ export type ImportSource = "config" | "extension";
26
+ /** One server an app has, and whether (and as what) it can come into the toolbox. */
27
+ export interface ImportCandidate {
28
+ /** The entry's name in the app's settings, or the extension's id. */
29
+ readonly key: string;
30
+ /** What a person knows it by: the entry name, or the extension's display name. */
31
+ readonly name: string;
32
+ readonly source: ImportSource;
33
+ readonly transport: "stdio" | "http" | "sse";
34
+ readonly importable: boolean;
35
+ readonly reason?: string;
36
+ /** The name it has, or would have, in the toolbox. */
37
+ readonly upstreamName?: string;
38
+ /** An upstream already in the toolbox that runs the same program or reaches the same address. */
39
+ readonly inToolbox?: string;
40
+ /** Setting names (env or header) whose values would go into the locked keys. Names only. */
41
+ readonly keySettings: readonly string[];
42
+ /** Its address needs the private-address protection switched off. */
43
+ readonly needsLocalHttp: boolean;
44
+ /** One upstream's plan, with values. Never for a browser. */
45
+ readonly plan?: AdoptionPlan;
46
+ /** The app's entry exactly as it is, so moving it out can check it did not change meanwhile. */
47
+ readonly entry?: unknown;
48
+ /** Extensions only: switched on in Claude. */
49
+ readonly enabled?: boolean;
50
+ }
51
+ export interface ImportCandidatesOptions {
52
+ readonly scope: Scope;
53
+ /** The project folder for a project scope (and Claude Code's `local`). */
54
+ readonly cwd: string;
55
+ readonly home: string;
56
+ readonly platform?: string;
57
+ /**
58
+ * The toolbox the app is connected to. Without one the list still says
59
+ * what the app has and what could come in, but not under which name or
60
+ * whether the toolbox has it already.
61
+ */
62
+ readonly gatewayConfigPath?: string;
63
+ /** The toolbox's own entry in the app, never offered. Default "hmcp". */
64
+ readonly entryName?: string;
65
+ /**
66
+ * The locked keys, read (never written) to tell whether the toolbox's copy
67
+ * of a server uses the same key as the app's. Without it, a server whose
68
+ * toolbox copy names a locked key is never taken as the same one.
69
+ */
70
+ readonly secrets?: SecretStore;
71
+ }
72
+ export interface ImportCandidatesResult {
73
+ readonly target: ScopeTarget;
74
+ readonly candidates: readonly ImportCandidate[];
75
+ /** Entries that could not be read at all, with why. */
76
+ readonly malformed: readonly {
77
+ readonly name: string;
78
+ readonly message: string;
79
+ }[];
80
+ }
81
+ /**
82
+ * Whether this very place in the app's settings runs this toolbox: an entry
83
+ * under the toolbox's name that points at its gateway.yaml.
84
+ *
85
+ * The connection's undo record alone is not enough. Claude Code keeps one
86
+ * record for `local` across every project (it sits beside `~/.claude.json`,
87
+ * and says nothing about which project), and a person can delete the entry
88
+ * by hand; either way the record says "connected" for servers the toolbox
89
+ * never runs. So the app's own file is asked.
90
+ */
91
+ export declare function runsToolbox(options: ClientScopeOptions, entryName: string, gatewayConfigPath: string): boolean;
92
+ /**
93
+ * Every part of a command line (or an address) that is a key, for refusing
94
+ * the server and for taking those parts out of anything shown about it: the
95
+ * shapes `keyParts` finds, the value given to a key-named flag
96
+ * (`--github-token <key>`, `--stripe-api-key=<key>`), and a `--header` / `-H`
97
+ * value naming a credential header.
98
+ */
99
+ export declare function keyWords(words: readonly string[]): string[];
100
+ /**
101
+ * Settings that are plainly not secrets, and so are written into the
102
+ * server's own settings as they are. Everything else an imported server
103
+ * sets goes into the locked keys: a password is often called something no
104
+ * pattern recognises (`DB_PASS`, `CONN`), and a value that looks harmless
105
+ * (`redis://:pw@cache`) can still be one. A folder setting stays plain only
106
+ * when its value is a plain path.
107
+ */
108
+ export declare function plainSetting(name: string, value: string): boolean;
109
+ /**
110
+ * Every server the app has (and, for Claude Desktop, every extension), each
111
+ * with whether it can come into the toolbox and the name it would have there.
112
+ *
113
+ * Reads only. Names are worked out over the whole list in a fixed order --
114
+ * the app's entries by name, then extensions by id -- so the name shown in a
115
+ * list is the name a later import of any part of that list uses.
116
+ */
117
+ export declare function importCandidates(options: ImportCandidatesOptions): ImportCandidatesResult;
118
+ export interface ImportLock {
119
+ /** The locked key's name. */
120
+ readonly name: string;
121
+ readonly value: string;
122
+ readonly label: string;
123
+ }
124
+ /**
125
+ * Where one server's values go once it runs from the toolbox rather than from
126
+ * the app.
127
+ *
128
+ * Adopt moves a stdio server's settings onto the toolbox's own entry in the
129
+ * app and passes them on by name. That entry here is the console's, shared by
130
+ * every server in the toolbox, so each value goes with its server instead:
131
+ * every setting becomes a locked key that gateway.yaml only names, except the
132
+ * few that are plainly not secrets (`plainSetting`: a log level, a folder),
133
+ * which are written into that server's own settings as they were. A web
134
+ * server's credential headers are locked as adopt locks them.
135
+ *
136
+ * Pure: it reads `store` to reuse a key already holding the same value, and
137
+ * writes nothing. `taken` (name to value) is shared by every server in one
138
+ * import, so two never pick one name for different values; a name two of
139
+ * them pick for the same value is listed by both, and created once.
140
+ */
141
+ export declare function planImportSecrets(plan: AdoptionPlan, store: SecretStore, taken?: Map<string, string>): {
142
+ plan: AdoptionPlan;
143
+ locks: ImportLock[];
144
+ };
145
+ export interface ClientScopeOptions {
146
+ readonly scope: Scope;
147
+ readonly cwd: string;
148
+ readonly home: string;
149
+ readonly platform?: string;
150
+ }
151
+ /**
152
+ * Takes these entries out of the app's settings, keeping each in the
153
+ * connection's undo record first.
154
+ *
155
+ * `expected` is each entry as it was when it was checked: one that changed
156
+ * since (the person edited it, or the app rewrote it) is not taken out on the
157
+ * strength of a check of something else. The record is written before the
158
+ * app's file, so there is no moment when a server is in neither; if the app's
159
+ * file can't be written, the record goes back as it was.
160
+ */
161
+ export declare function moveOutOfClient(options: ClientScopeOptions, expected: Readonly<Record<string, unknown>>): {
162
+ configPath: string;
163
+ moved: string[];
164
+ };
165
+ /**
166
+ * Puts entries taken out by `moveOutOfClient` back into the app's settings,
167
+ * from the undo record, and takes them out of the record.
168
+ *
169
+ * A name in use in the app again is refused, as Undo connection refuses it:
170
+ * whatever is there now was put there on purpose -- unless it is the very
171
+ * entry being put back, which needs nothing. Names the record no longer holds
172
+ * (the whole connection was undone meanwhile) are reported, not guessed.
173
+ *
174
+ * `where` is the place in the app's settings the servers were taken from, as
175
+ * the import recorded it: Claude Code's `local` record is shared by every
176
+ * project, and a server is never put back into a different one.
177
+ */
178
+ export declare function putBackInClient(options: ClientScopeOptions, names: readonly string[], from?: {
179
+ readonly where?: string;
180
+ }): {
181
+ configPath: string;
182
+ restored: string[];
183
+ missing: string[];
184
+ };