hmcp-web 0.0.1 → 0.2.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 (94) hide show
  1. package/LICENSE +77 -0
  2. package/README.md +49 -2
  3. package/THIRD-PARTY-NOTICES.md +110 -0
  4. package/bin/hmcp-web.js +20 -0
  5. package/dist/api.d.ts +22 -0
  6. package/dist/api.js +1458 -0
  7. package/dist/cli.d.ts +2 -0
  8. package/dist/cli.js +40 -0
  9. package/dist/contracts/onboarding.d.ts +233 -0
  10. package/dist/contracts/onboarding.js +1 -0
  11. package/dist/http.d.ts +72 -0
  12. package/dist/http.js +319 -0
  13. package/dist/index.d.ts +7 -0
  14. package/dist/index.js +7 -0
  15. package/dist/model/app-servers.d.ts +85 -0
  16. package/dist/model/app-servers.js +109 -0
  17. package/dist/model/console-settings.d.ts +37 -0
  18. package/dist/model/console-settings.js +74 -0
  19. package/dist/model/doctor.d.ts +68 -0
  20. package/dist/model/doctor.js +254 -0
  21. package/dist/model/fs-browse.d.ts +9 -0
  22. package/dist/model/fs-browse.js +184 -0
  23. package/dist/model/gateway-config.d.ts +287 -0
  24. package/dist/model/gateway-config.js +760 -0
  25. package/dist/model/import-record.d.ts +80 -0
  26. package/dist/model/import-record.js +179 -0
  27. package/dist/model/metrics.d.ts +65 -0
  28. package/dist/model/metrics.js +213 -0
  29. package/dist/model/oauth-flows.d.ts +60 -0
  30. package/dist/model/oauth-flows.js +79 -0
  31. package/dist/model/pipeline.d.ts +58 -0
  32. package/dist/model/pipeline.js +84 -0
  33. package/dist/model/probe.d.ts +155 -0
  34. package/dist/model/probe.js +385 -0
  35. package/dist/model/process.d.ts +100 -0
  36. package/dist/model/process.js +278 -0
  37. package/dist/model/protection.d.ts +378 -0
  38. package/dist/model/protection.js +470 -0
  39. package/dist/model/registry-cache.d.ts +59 -0
  40. package/dist/model/registry-cache.js +238 -0
  41. package/dist/model/scan.d.ts +48 -0
  42. package/dist/model/scan.js +59 -0
  43. package/dist/model/server.d.ts +127 -0
  44. package/dist/model/server.js +243 -0
  45. package/dist/model/tools.d.ts +61 -0
  46. package/dist/model/tools.js +72 -0
  47. package/dist/routes/catalog.d.ts +8 -0
  48. package/dist/routes/catalog.js +47 -0
  49. package/dist/routes/connect-import.d.ts +9 -0
  50. package/dist/routes/connect-import.js +594 -0
  51. package/dist/routes/connect.d.ts +50 -0
  52. package/dist/routes/connect.js +691 -0
  53. package/dist/routes/github.d.ts +97 -0
  54. package/dist/routes/github.js +587 -0
  55. package/dist/routes/guard.d.ts +3 -0
  56. package/dist/routes/guard.js +166 -0
  57. package/dist/routes/oauth.d.ts +55 -0
  58. package/dist/routes/oauth.js +406 -0
  59. package/dist/routes/policy.d.ts +34 -0
  60. package/dist/routes/policy.js +513 -0
  61. package/dist/routes/runtime.d.ts +12 -0
  62. package/dist/routes/runtime.js +921 -0
  63. package/dist/routes/servers-add.d.ts +32 -0
  64. package/dist/routes/servers-add.js +1091 -0
  65. package/dist/routes/system.d.ts +4 -0
  66. package/dist/routes/system.js +13 -0
  67. package/dist/start.d.ts +56 -0
  68. package/dist/start.js +164 -0
  69. package/dist-ui/assets/Activity-CZ78RyRO.js +1 -0
  70. package/dist-ui/assets/Approvals-DPIlNZsv.js +1 -0
  71. package/dist-ui/assets/AttachApp-DjSeWCbS.js +5 -0
  72. package/dist-ui/assets/Connections-qcA96lXh.js +2 -0
  73. package/dist-ui/assets/Gallery-DB_OO7Vw.js +17 -0
  74. package/dist-ui/assets/PreApprovals-C4cKajcK.js +1 -0
  75. package/dist-ui/assets/Protection-B8h-5YAb.js +1 -0
  76. package/dist-ui/assets/Safety-BQqHyYdo.js +1 -0
  77. package/dist-ui/assets/Settings-Cx50zZSt.js +1 -0
  78. package/dist-ui/assets/StartPage-CdX-v7ej.js +1 -0
  79. package/dist-ui/assets/Suggestions-CRo5UOTT.js +1 -0
  80. package/dist-ui/assets/Tools-xo39lPKB.js +4 -0
  81. package/dist-ui/assets/index-DttDjRpu.js +16 -0
  82. package/dist-ui/assets/index-QeJyD5z2.css +2 -0
  83. package/dist-ui/assets/kit-DRVm92_t.js +4 -0
  84. package/dist-ui/brand/logo-256.png +0 -0
  85. package/dist-ui/brand/logo-512.png +0 -0
  86. package/dist-ui/brand/logo.svg +6 -0
  87. package/dist-ui/brand/mark.svg +5 -0
  88. package/dist-ui/fonts/OFL.txt +99 -0
  89. package/dist-ui/fonts/README.md +21 -0
  90. package/dist-ui/fonts/jetbrainsmono-latin.woff2 +0 -0
  91. package/dist-ui/fonts/manrope-latin.woff2 +0 -0
  92. package/dist-ui/index.html +51 -0
  93. package/package.json +69 -6
  94. package/bin/notice.js +0 -7
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Gateways this console started, and nothing else.
3
+ *
4
+ * Only HTTP mode is offered. A stdio gateway is spawned by the AI app that
5
+ * talks to it -- its stdin and stdout *are* the conversation -- so one started
6
+ * here would have nobody on the other end. The console can see such a gateway
7
+ * (it announces itself in `runtime_state`) but cannot own it, and says so.
8
+ *
9
+ * One process per registered server. A second start for the same id is a
10
+ * conflict rather than a second process, because two gateways with one
11
+ * component write the same `runtime_state` and inventory rows and a reader can
12
+ * no longer tell which one it is looking at.
13
+ */
14
+ export type ProcessState = "stopped" | "starting" | "running" | "exited";
15
+ export interface HttpOptions {
16
+ readonly port: number;
17
+ readonly host: "127.0.0.1" | "::1" | "localhost";
18
+ readonly allowHosts: readonly string[];
19
+ readonly allowOrigins: readonly string[];
20
+ /** Require callers to present a bearer token, generated here. */
21
+ readonly requireToken: boolean;
22
+ }
23
+ export interface ProcessView {
24
+ readonly managed: boolean;
25
+ readonly state: ProcessState;
26
+ readonly pid: number | null;
27
+ readonly startedAt: number | null;
28
+ readonly exitCode: number | null;
29
+ readonly signal: string | null;
30
+ readonly mode: "http";
31
+ readonly http: {
32
+ host: string;
33
+ port: number;
34
+ url: string | null;
35
+ tokenRequired: boolean;
36
+ } | null;
37
+ readonly log: string[];
38
+ }
39
+ /**
40
+ * The gateway's CLI, resolved through the package the console already depends
41
+ * on. The same file `scripts/demo.mjs` runs, found the way an installed console
42
+ * would find it rather than by a path into this repository.
43
+ */
44
+ export declare function gatewayCliPath(): string;
45
+ export declare class ProcessManager {
46
+ /** The command to run; replaced in tests. */
47
+ private readonly command;
48
+ private readonly managed;
49
+ /** Every pid started here, so a row one of them left behind is not taken for someone else's. */
50
+ private readonly spawned;
51
+ private exitHookInstalled;
52
+ constructor(
53
+ /** The command to run; replaced in tests. */
54
+ command?: () => {
55
+ file: string;
56
+ args: string[];
57
+ });
58
+ view(id: string): ProcessView;
59
+ /** The pid of a gateway this console owns and that is still alive. */
60
+ livePid(id: string): number | null;
61
+ /** What the last start was asked for, so a restart can repeat it. */
62
+ optionsFor(id: string): HttpOptions | undefined;
63
+ /**
64
+ * Whether a pid announcing itself is someone else's gateway: not one this
65
+ * console started, and actually alive. A gateway's `runtime_state` row stays
66
+ * fresh for a few seconds after it exits, which without this would make a
67
+ * gateway stopped here look like one an AI app is running.
68
+ */
69
+ isForeign(pid: number): boolean;
70
+ isActive(id: string): boolean;
71
+ /**
72
+ * Starts a gateway and waits until it is listening, or has failed.
73
+ *
74
+ * Returns the token once, when one was generated for this start. A restart
75
+ * reuses the previous token and returns null, so the value is shown to the
76
+ * user exactly once.
77
+ */
78
+ start(id: string, configPath: string, options: HttpOptions, reuseToken?: boolean): Promise<{
79
+ view: ProcessView;
80
+ token: string | null;
81
+ }>;
82
+ /** SIGTERM, then SIGKILL if it has not gone within five seconds. */
83
+ stop(id: string): Promise<ProcessView>;
84
+ /** Forgets a process that is no longer running, e.g. when its server is removed. */
85
+ forget(id: string): void;
86
+ /**
87
+ * Sends every child SIGTERM, synchronously.
88
+ *
89
+ * Synchronous because it runs from an `exit` handler, where nothing
90
+ * asynchronous gets to happen. Each gateway handles SIGTERM by closing its
91
+ * upstreams, so this is enough to leave nothing behind.
92
+ */
93
+ killAll(): void;
94
+ private installExitHook;
95
+ private push;
96
+ }
97
+ /** The console's one manager. Process-wide, like the store cache. */
98
+ export declare const processes: ProcessManager;
99
+ /** For the console's shutdown path. */
100
+ export declare function stopAllManaged(): void;
@@ -0,0 +1,278 @@
1
+ import { spawn } from "node:child_process";
2
+ import { randomBytes } from "node:crypto";
3
+ import { dirname, join } from "node:path";
4
+ import { createRequire } from "node:module";
5
+ const LOG_LINES = 300;
6
+ const STOP_GRACE_MS = 5000;
7
+ /** How long `start` waits for the gateway to say it is listening. */
8
+ const START_WAIT_MS = 15_000;
9
+ /**
10
+ * The gateway's CLI, resolved through the package the console already depends
11
+ * on. The same file `scripts/demo.mjs` runs, found the way an installed console
12
+ * would find it rather than by a path into this repository.
13
+ */
14
+ export function gatewayCliPath() {
15
+ const index = createRequire(import.meta.url).resolve("hmcp-gateway");
16
+ return join(dirname(index), "cli.js");
17
+ }
18
+ export class ProcessManager {
19
+ command;
20
+ managed = new Map();
21
+ /** Every pid started here, so a row one of them left behind is not taken for someone else's. */
22
+ spawned = new Set();
23
+ exitHookInstalled = false;
24
+ constructor(
25
+ /** The command to run; replaced in tests. */
26
+ command = () => ({
27
+ file: process.execPath,
28
+ args: [gatewayCliPath()]
29
+ })) {
30
+ this.command = command;
31
+ }
32
+ view(id) {
33
+ const m = this.managed.get(id);
34
+ if (!m) {
35
+ return {
36
+ managed: false,
37
+ state: "stopped",
38
+ pid: null,
39
+ startedAt: null,
40
+ exitCode: null,
41
+ signal: null,
42
+ mode: "http",
43
+ http: null,
44
+ log: []
45
+ };
46
+ }
47
+ const port = m.boundPort ?? m.options.port;
48
+ const hostForUrl = m.options.host === "::1" ? "[::1]" : m.options.host;
49
+ return {
50
+ managed: true,
51
+ state: m.state,
52
+ pid: m.pid,
53
+ startedAt: m.startedAt,
54
+ exitCode: m.exitCode,
55
+ signal: m.signal,
56
+ mode: "http",
57
+ http: {
58
+ host: m.options.host,
59
+ port,
60
+ url: m.boundPort !== null ? `http://${hostForUrl}:${port}/mcp` : null,
61
+ tokenRequired: m.token !== null
62
+ },
63
+ log: [...m.log]
64
+ };
65
+ }
66
+ /** The pid of a gateway this console owns and that is still alive. */
67
+ livePid(id) {
68
+ const m = this.managed.get(id);
69
+ return m && (m.state === "running" || m.state === "starting") ? m.pid : null;
70
+ }
71
+ /** What the last start was asked for, so a restart can repeat it. */
72
+ optionsFor(id) {
73
+ return this.managed.get(id)?.options;
74
+ }
75
+ /**
76
+ * Whether a pid announcing itself is someone else's gateway: not one this
77
+ * console started, and actually alive. A gateway's `runtime_state` row stays
78
+ * fresh for a few seconds after it exits, which without this would make a
79
+ * gateway stopped here look like one an AI app is running.
80
+ */
81
+ isForeign(pid) {
82
+ if (this.spawned.has(pid))
83
+ return false;
84
+ try {
85
+ process.kill(pid, 0);
86
+ return true;
87
+ }
88
+ catch (err) {
89
+ // EPERM: alive, owned by another user.
90
+ return err.code === "EPERM";
91
+ }
92
+ }
93
+ isActive(id) {
94
+ return this.livePid(id) !== null;
95
+ }
96
+ /**
97
+ * Starts a gateway and waits until it is listening, or has failed.
98
+ *
99
+ * Returns the token once, when one was generated for this start. A restart
100
+ * reuses the previous token and returns null, so the value is shown to the
101
+ * user exactly once.
102
+ */
103
+ async start(id, configPath, options, reuseToken = false) {
104
+ const existing = this.managed.get(id);
105
+ if (existing && (existing.state === "running" || existing.state === "starting")) {
106
+ throw new Error("already running");
107
+ }
108
+ let token = null;
109
+ let fresh = false;
110
+ if (options.requireToken) {
111
+ if (reuseToken && existing?.token)
112
+ token = existing.token;
113
+ else {
114
+ // 32 bytes, well over the gateway's 16-character floor.
115
+ token = randomBytes(32).toString("base64url");
116
+ fresh = true;
117
+ }
118
+ }
119
+ const { file, args } = this.command();
120
+ const argv = [
121
+ ...args,
122
+ "serve",
123
+ "-c",
124
+ configPath,
125
+ "--http",
126
+ String(options.port),
127
+ "--host",
128
+ options.host,
129
+ ...options.allowHosts.flatMap((h) => ["--allow-host", h]),
130
+ ...options.allowOrigins.flatMap((o) => ["--allow-origin", o])
131
+ ];
132
+ /*
133
+ * The token travels in the environment, which is how the gateway reads it
134
+ * (a flag would be visible in the process list). An inherited one is
135
+ * removed when none was asked for, or the gateway would demand a token
136
+ * nobody was shown.
137
+ */
138
+ const env = { ...process.env };
139
+ delete env["HMCP_GATEWAY_TOKEN"];
140
+ if (token)
141
+ env["HMCP_GATEWAY_TOKEN"] = token;
142
+ const child = spawn(file, argv, { cwd: dirname(configPath), env, stdio: ["ignore", "pipe", "pipe"] });
143
+ if (child.pid !== undefined)
144
+ this.spawned.add(child.pid);
145
+ const m = {
146
+ child,
147
+ state: "starting",
148
+ pid: child.pid ?? null,
149
+ startedAt: Date.now(),
150
+ exitCode: null,
151
+ signal: null,
152
+ options,
153
+ configPath,
154
+ boundPort: null,
155
+ log: existing ? existing.log : [],
156
+ token,
157
+ stopping: null
158
+ };
159
+ if (existing)
160
+ this.push(m, `--- started again at ${new Date().toISOString()} ---`);
161
+ this.managed.set(id, m);
162
+ this.installExitHook();
163
+ const ready = new Promise((resolve) => {
164
+ const onLine = (line) => {
165
+ this.push(m, line);
166
+ const listening = /listening on http:\/\/.*:(\d+)\/mcp/.exec(line);
167
+ if (listening && m.state === "starting") {
168
+ m.boundPort = Number(listening[1]);
169
+ m.state = "running";
170
+ resolve();
171
+ }
172
+ };
173
+ lines(child.stdout, onLine);
174
+ lines(child.stderr, onLine);
175
+ child.on("error", (err) => {
176
+ this.push(m, `could not start the gateway: ${err.message}`);
177
+ m.state = "exited";
178
+ resolve();
179
+ });
180
+ child.on("exit", (code, signal) => {
181
+ m.exitCode = code;
182
+ m.signal = signal;
183
+ m.child = null;
184
+ // Stopped on purpose reads as stopped; anything else is an exit to explain.
185
+ m.state = m.stopping ? "stopped" : "exited";
186
+ resolve();
187
+ });
188
+ setTimeout(resolve, START_WAIT_MS).unref();
189
+ });
190
+ await ready;
191
+ return { view: this.view(id), token: fresh ? token : null };
192
+ }
193
+ /** SIGTERM, then SIGKILL if it has not gone within five seconds. */
194
+ async stop(id) {
195
+ const m = this.managed.get(id);
196
+ if (!m || !m.child)
197
+ return this.view(id);
198
+ if (!m.stopping) {
199
+ const child = m.child;
200
+ m.stopping = new Promise((resolve) => {
201
+ const timer = setTimeout(() => {
202
+ this.push(m, "the gateway did not stop within 5 seconds; killing it");
203
+ child.kill("SIGKILL");
204
+ }, STOP_GRACE_MS);
205
+ timer.unref();
206
+ child.once("exit", () => {
207
+ clearTimeout(timer);
208
+ resolve();
209
+ });
210
+ child.kill("SIGTERM");
211
+ });
212
+ }
213
+ await m.stopping;
214
+ m.stopping = null;
215
+ m.state = "stopped";
216
+ return this.view(id);
217
+ }
218
+ /** Forgets a process that is no longer running, e.g. when its server is removed. */
219
+ forget(id) {
220
+ if (!this.isActive(id))
221
+ this.managed.delete(id);
222
+ }
223
+ /**
224
+ * Sends every child SIGTERM, synchronously.
225
+ *
226
+ * Synchronous because it runs from an `exit` handler, where nothing
227
+ * asynchronous gets to happen. Each gateway handles SIGTERM by closing its
228
+ * upstreams, so this is enough to leave nothing behind.
229
+ */
230
+ killAll() {
231
+ for (const m of this.managed.values()) {
232
+ try {
233
+ m.child?.kill("SIGTERM");
234
+ }
235
+ catch {
236
+ /* already gone */
237
+ }
238
+ }
239
+ }
240
+ installExitHook() {
241
+ if (this.exitHookInstalled)
242
+ return;
243
+ this.exitHookInstalled = true;
244
+ process.once("exit", () => this.killAll());
245
+ }
246
+ push(m, line) {
247
+ m.log.push(line);
248
+ if (m.log.length > LOG_LINES)
249
+ m.log.splice(0, m.log.length - LOG_LINES);
250
+ }
251
+ }
252
+ function lines(stream, onLine) {
253
+ let buffered = "";
254
+ stream.setEncoding("utf8");
255
+ stream.on("data", (chunk) => {
256
+ buffered += chunk;
257
+ let index;
258
+ while ((index = buffered.indexOf("\n")) !== -1) {
259
+ onLine(buffered.slice(0, index).replace(/\r$/, ""));
260
+ buffered = buffered.slice(index + 1);
261
+ }
262
+ // A runaway line without a newline is cut rather than held for ever.
263
+ if (buffered.length > 8192) {
264
+ onLine(buffered);
265
+ buffered = "";
266
+ }
267
+ });
268
+ stream.on("end", () => {
269
+ if (buffered)
270
+ onLine(buffered);
271
+ });
272
+ }
273
+ /** The console's one manager. Process-wide, like the store cache. */
274
+ export const processes = new ProcessManager();
275
+ /** For the console's shutdown path. */
276
+ export function stopAllManaged() {
277
+ processes.killAll();
278
+ }