constructor-research-mcp 0.4.2 → 0.6.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/INSTALL.md +30 -2
- package/README.md +17 -3
- package/dist/agents.d.ts +72 -0
- package/dist/agents.js +483 -0
- package/dist/api.d.ts +2 -0
- package/dist/api.js +13 -3
- package/dist/deployment.d.ts +16 -0
- package/dist/deployment.js +68 -2
- package/dist/install.d.ts +3 -0
- package/dist/install.js +432 -0
- package/dist/key.d.ts +6 -0
- package/dist/key.js +33 -3
- package/dist/schemas.js +1 -1
- package/dist/server.js +58 -17
- package/dist/set-key.js +5 -17
- package/package.json +3 -1
package/dist/api.js
CHANGED
|
@@ -16,6 +16,8 @@ export class ToolError extends Error {
|
|
|
16
16
|
this.apiCode = apiCode;
|
|
17
17
|
this.details = details;
|
|
18
18
|
}
|
|
19
|
+
/** Constructor's own reason (detail.info.details); never put into messages by default. */
|
|
20
|
+
reason;
|
|
19
21
|
}
|
|
20
22
|
export const segment = (s) => encodeURIComponent(s);
|
|
21
23
|
/** A key as Constructor shows it, with or without the "Bearer " the header adds. */
|
|
@@ -185,15 +187,23 @@ export class PlatformApi {
|
|
|
185
187
|
if (method !== "GET" && response.status >= 500)
|
|
186
188
|
throw new ToolError("uncertain_outcome", `Constructor API returned HTTP ${response.status}. The mutation may have been applied; inspect resources before retrying.`, response.status);
|
|
187
189
|
let apiCode;
|
|
190
|
+
let reason;
|
|
188
191
|
try {
|
|
189
192
|
const d = (await response.json());
|
|
190
|
-
|
|
191
|
-
|
|
193
|
+
// Constructor answers {"detail": {...}}; older specs show {"details": {...}}.
|
|
194
|
+
const body = d?.detail ?? d?.details;
|
|
195
|
+
if (typeof body?.error_code === "number")
|
|
196
|
+
apiCode = body.error_code;
|
|
197
|
+
if (typeof body?.info?.details === "string")
|
|
198
|
+
reason = body.info.details;
|
|
192
199
|
}
|
|
193
200
|
catch {
|
|
194
201
|
/* Error bodies can contain secrets; never echo them. */
|
|
195
202
|
}
|
|
196
|
-
|
|
203
|
+
const err = new ToolError("api_error", `Constructor API returned HTTP ${response.status}${apiCode ? ` (error ${apiCode})` : ""}.`, response.status, apiCode);
|
|
204
|
+
// Kept off the message: only callers that know the reason is safe to show use it.
|
|
205
|
+
err.reason = reason;
|
|
206
|
+
throw err;
|
|
197
207
|
}
|
|
198
208
|
if (response.status === 204)
|
|
199
209
|
return undefined;
|
package/dist/deployment.d.ts
CHANGED
|
@@ -59,6 +59,13 @@ export declare class DeploymentEngine {
|
|
|
59
59
|
deskFor(project: string, create?: string): Promise<string>;
|
|
60
60
|
/** Turn each service's `image` into the project's image_id when the project has it (by reference or name); others are left as `image`. */
|
|
61
61
|
resolveImages(project: string, services: any[]): Promise<any[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Add a public registry image to the project as an external image: no desk, no
|
|
64
|
+
* build. Constructor reads the manifest at once and refuses a missing, private,
|
|
65
|
+
* non-amd64 or malformed image with its reason. Undefined when this Constructor
|
|
66
|
+
* has no such endpoint yet (the caller then builds it from FROM).
|
|
67
|
+
*/
|
|
68
|
+
registerExternal(project: string, service: string, reference: string): Promise<any>;
|
|
62
69
|
listImages(raw: unknown): Promise<{
|
|
63
70
|
results: any[];
|
|
64
71
|
total: number;
|
|
@@ -103,3 +110,12 @@ export declare class DeploymentEngine {
|
|
|
103
110
|
};
|
|
104
111
|
private view;
|
|
105
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Docker Hub references in the short form Constructor accepts: docker.io/library/nginx:1.27
|
|
115
|
+
* is refused (HYP-3189), nginx:1.27 works. Other registries are left as given.
|
|
116
|
+
*/
|
|
117
|
+
export declare function shortReference(reference: string): string;
|
|
118
|
+
/** True when the reference pins a tag or digest; without one it means latest, which moves. */
|
|
119
|
+
export declare function hasTag(reference: string): boolean;
|
|
120
|
+
/** Image name for a reference (Constructor allows 30 characters): repository and tag. */
|
|
121
|
+
export declare function externalName(reference: string): string;
|
package/dist/deployment.js
CHANGED
|
@@ -409,18 +409,47 @@ export class DeploymentEngine {
|
|
|
409
409
|
if (!s.image)
|
|
410
410
|
return s;
|
|
411
411
|
const want = s.image.trim();
|
|
412
|
-
const
|
|
412
|
+
const ref = shortReference(want);
|
|
413
|
+
const same = (im) => typeof im.reference === "string" &&
|
|
414
|
+
shortReference(im.reference) === ref;
|
|
415
|
+
const hits = images.filter((im) => im.reference === want ||
|
|
416
|
+
same(im) ||
|
|
417
|
+
im.name === want ||
|
|
418
|
+
im.id === want);
|
|
413
419
|
const im = hits.length > 1
|
|
414
420
|
? (hits.find((x) => x.reference === want) ??
|
|
421
|
+
hits.find((x) => same(x) && DeploymentEngine.usable(x)) ??
|
|
415
422
|
bad(`Image "${want}" for ${s.name} matches several images: ${hits.map((x) => x.id).join(", ")}. Pass image_id.`))
|
|
416
423
|
: hits[0];
|
|
417
|
-
// Not in the project: the caller
|
|
424
|
+
// Not in the project: the caller registers it (registerExternal).
|
|
418
425
|
if (!im)
|
|
419
426
|
return s;
|
|
420
427
|
const { image: _i, ...rest } = s;
|
|
421
428
|
return { ...rest, image_id: im.id };
|
|
422
429
|
});
|
|
423
430
|
}
|
|
431
|
+
/**
|
|
432
|
+
* Add a public registry image to the project as an external image: no desk, no
|
|
433
|
+
* build. Constructor reads the manifest at once and refuses a missing, private,
|
|
434
|
+
* non-amd64 or malformed image with its reason. Undefined when this Constructor
|
|
435
|
+
* has no such endpoint yet (the caller then builds it from FROM).
|
|
436
|
+
*/
|
|
437
|
+
async registerExternal(project, service, reference) {
|
|
438
|
+
try {
|
|
439
|
+
return await this.api.request("POST", this.root(project) + "/images/external", { name: externalName(reference), reference });
|
|
440
|
+
}
|
|
441
|
+
catch (err) {
|
|
442
|
+
if (!(err instanceof ToolError) || err.code !== "api_error")
|
|
443
|
+
throw err;
|
|
444
|
+
if (err.status === 404 || err.status === 405)
|
|
445
|
+
return undefined;
|
|
446
|
+
if ([400, 421, 422].includes(err.status ?? 0)) {
|
|
447
|
+
const why = cleanReason(err.reason);
|
|
448
|
+
throw new ToolError("image_refused", `Service ${service}: Constructor refused image ${reference}${why ? `: ${why}` : ""}. Nothing was deployed. Only public images with a linux/amd64 variant can be used; check the name and tag (Docker Hub images as name:tag or user/name:tag). Tell the user, or use build with a Dockerfile instead.`, err.status, err.apiCode, { stage: "register", service, image: reference });
|
|
449
|
+
}
|
|
450
|
+
throw err;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
424
453
|
async listImages(raw) {
|
|
425
454
|
const input = listInput.parse(raw);
|
|
426
455
|
await this.project(input.project_id);
|
|
@@ -1303,3 +1332,40 @@ export class DeploymentEngine {
|
|
|
1303
1332
|
return result;
|
|
1304
1333
|
}
|
|
1305
1334
|
}
|
|
1335
|
+
/**
|
|
1336
|
+
* Docker Hub references in the short form Constructor accepts: docker.io/library/nginx:1.27
|
|
1337
|
+
* is refused (HYP-3189), nginx:1.27 works. Other registries are left as given.
|
|
1338
|
+
*/
|
|
1339
|
+
export function shortReference(reference) {
|
|
1340
|
+
const r = reference.trim();
|
|
1341
|
+
const m = /^(?:docker\.io|index\.docker\.io|registry-1\.docker\.io)\/(.+)$/i.exec(r);
|
|
1342
|
+
return m ? m[1].replace(/^library\/(?=[^/]+$)/, "") : r;
|
|
1343
|
+
}
|
|
1344
|
+
/** True when the reference pins a tag or digest; without one it means latest, which moves. */
|
|
1345
|
+
export function hasTag(reference) {
|
|
1346
|
+
return reference.includes("@") || reference.split("/").at(-1).includes(":");
|
|
1347
|
+
}
|
|
1348
|
+
/** Image name for a reference (Constructor allows 30 characters): repository and tag. */
|
|
1349
|
+
export function externalName(reference) {
|
|
1350
|
+
const name = reference
|
|
1351
|
+
.split("/")
|
|
1352
|
+
.at(-1)
|
|
1353
|
+
.split("@")[0]
|
|
1354
|
+
.toLowerCase()
|
|
1355
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
1356
|
+
.slice(0, 30)
|
|
1357
|
+
.replace(/^-+|-+$/g, "");
|
|
1358
|
+
return name || "image";
|
|
1359
|
+
}
|
|
1360
|
+
/** Constructor's refusal reason as one short line, never anything that looks like a key. */
|
|
1361
|
+
function cleanReason(reason) {
|
|
1362
|
+
if (!reason)
|
|
1363
|
+
return undefined;
|
|
1364
|
+
const line = reason
|
|
1365
|
+
.replace(/rk-[A-Za-z0-9_-]+/g, "rk-…")
|
|
1366
|
+
.replace(/\s+/g, " ")
|
|
1367
|
+
.trim()
|
|
1368
|
+
.slice(0, 300)
|
|
1369
|
+
.replace(/[.\s]+$/, "");
|
|
1370
|
+
return line || undefined;
|
|
1371
|
+
}
|
package/dist/install.js
ADDED
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
// `constructor-research-mcp install`: find the coding agents on this machine, let the user pick
|
|
2
|
+
// them and project or global, get and check the access key, save it to the key file, and add the
|
|
3
|
+
// MCP to each agent. The key is never an argument and never part of an agent's settings.
|
|
4
|
+
import * as p from "@clack/prompts";
|
|
5
|
+
import { mkdirSync, mkdtempSync } from "node:fs";
|
|
6
|
+
import { homedir, tmpdir } from "node:os";
|
|
7
|
+
import { join, sep } from "node:path";
|
|
8
|
+
import { styleText } from "node:util";
|
|
9
|
+
import { AGENTS, PACKAGE_SPEC, SERVER, applyEntry, claudeCliWorks, describe, detect, mcpCommand, succeeded, } from "./agents.js";
|
|
10
|
+
import { cleanKey } from "./api.js";
|
|
11
|
+
import { DEFAULT_BASE_URL, configFromEnv, originOf } from "./config.js";
|
|
12
|
+
import { checkKey, readKeyFile, saveKeyFile } from "./key.js";
|
|
13
|
+
const bold = (s) => styleText("bold", s);
|
|
14
|
+
const dim = (s) => styleText("dim", s);
|
|
15
|
+
const accent = (s) => styleText("cyan", s);
|
|
16
|
+
const USAGE = `Constructor Research MCP setup
|
|
17
|
+
|
|
18
|
+
npx -y ${PACKAGE_SPEC} install [options]
|
|
19
|
+
|
|
20
|
+
-y, --yes no questions: detected agents, the level they were found at
|
|
21
|
+
--agents=a,b ${AGENTS.map((a) => a.id).join(", ")}
|
|
22
|
+
--scope=project|global where the MCP entry goes
|
|
23
|
+
--constructor=<link> the Constructor you use (default ${DEFAULT_BASE_URL})
|
|
24
|
+
--key-stdin with -y: read the access key from stdin (or set CONSTRUCTOR_ACCESS_KEY)
|
|
25
|
+
--dry-run change nothing
|
|
26
|
+
--verbose show each agent's file and the exact entry
|
|
27
|
+
--demo try it in a throw-away home folder with fake agents (no key check)
|
|
28
|
+
|
|
29
|
+
The access key is never accepted as an argument.`;
|
|
30
|
+
/** The access key as a short hint: rk-…abcd. */
|
|
31
|
+
export const mask = (key) => `${key.slice(0, 3)}…${key.slice(-4)}`;
|
|
32
|
+
/** Access keys start with rk-; printable ASCII only (cleanKey). */
|
|
33
|
+
const accessKey = (raw) => {
|
|
34
|
+
const key = cleanKey(raw);
|
|
35
|
+
return key?.startsWith("rk-") ? key : undefined;
|
|
36
|
+
};
|
|
37
|
+
/** Accepts acme.constructor.app as well as https://acme.constructor.app. */
|
|
38
|
+
const linkOrigin = (raw) => originOf(/^[a-z][a-z0-9+.-]*:\/\//i.test(raw.trim()) ? raw : `https://${raw.trim()}`);
|
|
39
|
+
async function readStdin() {
|
|
40
|
+
let text = "";
|
|
41
|
+
for await (const chunk of process.stdin)
|
|
42
|
+
text += chunk;
|
|
43
|
+
return text;
|
|
44
|
+
}
|
|
45
|
+
/** Clack draws checkboxes as squares; the agent list shows them as circles (○ / ●). */
|
|
46
|
+
const circles = new Proxy(process.stdout, {
|
|
47
|
+
get(target, key) {
|
|
48
|
+
if (key === "write")
|
|
49
|
+
return (chunk, ...rest) => target.write(typeof chunk === "string"
|
|
50
|
+
? chunk.replaceAll("◻", "○").replaceAll("◼", "●")
|
|
51
|
+
: chunk, ...rest);
|
|
52
|
+
const value = Reflect.get(target, key, target);
|
|
53
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
export async function runInstall(argv) {
|
|
57
|
+
const flag = (n) => argv.includes(n);
|
|
58
|
+
const value = (n) => argv.find((a) => a.startsWith(`${n}=`))?.slice(n.length + 1);
|
|
59
|
+
const known = /^(-y|--yes|--key-stdin|--dry-run|--verbose|--demo|-h|--help|--(agents|harnesses|scope|constructor)=.*)$/;
|
|
60
|
+
const unknown = argv.filter((a) => !known.test(a));
|
|
61
|
+
if (flag("-h") || flag("--help") || unknown.length) {
|
|
62
|
+
if (unknown.length)
|
|
63
|
+
console.error(`Unknown option: ${unknown.join(" ")}\n`);
|
|
64
|
+
(unknown.length ? console.error : console.log)(USAGE);
|
|
65
|
+
return unknown.length ? 2 : 0;
|
|
66
|
+
}
|
|
67
|
+
const yes = flag("-y") || flag("--yes");
|
|
68
|
+
const dry = flag("--dry-run");
|
|
69
|
+
const verbose = flag("--verbose");
|
|
70
|
+
const demo = flag("--demo");
|
|
71
|
+
if (flag("--key-stdin") && process.stdin.isTTY) {
|
|
72
|
+
console.error("--key-stdin reads a piped key, e.g. from a password manager.");
|
|
73
|
+
return 2;
|
|
74
|
+
}
|
|
75
|
+
if (flag("--key-stdin") && !yes) {
|
|
76
|
+
console.error("--key-stdin needs -y: the questions read from the same input.");
|
|
77
|
+
return 2;
|
|
78
|
+
}
|
|
79
|
+
if (!yes && !(process.stdin.isTTY && process.stdout.isTTY)) {
|
|
80
|
+
console.error("Run this in a terminal, or pass -y with --agents, --scope and the key on stdin.");
|
|
81
|
+
return 2;
|
|
82
|
+
}
|
|
83
|
+
// One environment for the settings locations, the key file and the agents' own CLIs.
|
|
84
|
+
const env = { ...process.env };
|
|
85
|
+
let home = homedir();
|
|
86
|
+
let cwd = process.cwd();
|
|
87
|
+
if (demo) {
|
|
88
|
+
home = mkdtempSync(join(tmpdir(), "constructor-research-demo-"));
|
|
89
|
+
cwd = join(home, "my-project");
|
|
90
|
+
for (const d of [
|
|
91
|
+
".claude",
|
|
92
|
+
".codex",
|
|
93
|
+
join(".config", "opencode"),
|
|
94
|
+
".jcode",
|
|
95
|
+
".cline",
|
|
96
|
+
".cursor",
|
|
97
|
+
"my-project",
|
|
98
|
+
])
|
|
99
|
+
mkdirSync(join(home, d), { recursive: true });
|
|
100
|
+
// Nothing outside the demo folder: every location override points into it or is cleared.
|
|
101
|
+
for (const v of [
|
|
102
|
+
"OPENCODE_CONFIG_DIR",
|
|
103
|
+
"CLINE_DIR",
|
|
104
|
+
"CLINE_DATA_DIR",
|
|
105
|
+
"CLINE_MCP_SETTINGS_PATH",
|
|
106
|
+
"APPDATA",
|
|
107
|
+
])
|
|
108
|
+
delete env[v];
|
|
109
|
+
Object.assign(env, {
|
|
110
|
+
HOME: home,
|
|
111
|
+
USERPROFILE: home,
|
|
112
|
+
CLAUDE_CONFIG_DIR: join(home, ".claude"),
|
|
113
|
+
CODEX_HOME: join(home, ".codex"),
|
|
114
|
+
XDG_CONFIG_HOME: join(home, ".config"),
|
|
115
|
+
JCODE_HOME: join(home, ".jcode"),
|
|
116
|
+
CONSTRUCTOR_KEY_FILE: join(home, ".config", "constructor-research", "access-key.json"),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
const config = configFromEnv({ ...env, CONSTRUCTOR_BASE_URL: undefined });
|
|
120
|
+
const show = (path) => path === cwd || path.startsWith(cwd + sep)
|
|
121
|
+
? "." + path.slice(cwd.length)
|
|
122
|
+
: path.startsWith(home + sep)
|
|
123
|
+
? "~" + path.slice(home.length)
|
|
124
|
+
: path;
|
|
125
|
+
const ask = async (prompt) => {
|
|
126
|
+
const answer = await prompt;
|
|
127
|
+
if (p.isCancel(answer)) {
|
|
128
|
+
p.cancel("Cancelled. Nothing more was changed.");
|
|
129
|
+
process.exit(1);
|
|
130
|
+
}
|
|
131
|
+
return answer;
|
|
132
|
+
};
|
|
133
|
+
// ---- 1. which agents ---------------------------------------------------------------------
|
|
134
|
+
console.log("");
|
|
135
|
+
p.intro(`${bold(accent("Constructor Research MCP"))} ${dim("setup")}${demo ? dim(` (demo in ${home})`) : ""}`);
|
|
136
|
+
const found = detect(home, cwd, env);
|
|
137
|
+
if (found.length) {
|
|
138
|
+
const width = Math.max(...found.map((d) => d.agent.label.length));
|
|
139
|
+
p.note(found
|
|
140
|
+
.map((d) => `${bold(d.agent.label.padEnd(width))} ${dim(show(d.path))}`)
|
|
141
|
+
.join("\n"), "Detected coding agents");
|
|
142
|
+
}
|
|
143
|
+
else
|
|
144
|
+
p.log.warn("No coding agents found in this folder or your home folder.");
|
|
145
|
+
const detectedIds = [...new Set(found.map((d) => d.agent.id))];
|
|
146
|
+
let chosen;
|
|
147
|
+
const listed = value("--agents") ?? value("--harnesses");
|
|
148
|
+
if (listed) {
|
|
149
|
+
chosen = [
|
|
150
|
+
...new Set(listed
|
|
151
|
+
.split(",")
|
|
152
|
+
.map((s) => s.trim())
|
|
153
|
+
.filter(Boolean)),
|
|
154
|
+
];
|
|
155
|
+
const bad = chosen.filter((id) => !AGENTS.some((a) => a.id === id));
|
|
156
|
+
if (bad.length) {
|
|
157
|
+
p.log.error(`Unknown agent: ${bad.join(", ")}. Known: ${AGENTS.map((a) => a.id).join(", ")}`);
|
|
158
|
+
return 2;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else if (yes)
|
|
162
|
+
chosen = detectedIds;
|
|
163
|
+
else {
|
|
164
|
+
const mode = detectedIds.length
|
|
165
|
+
? await ask(p.select({
|
|
166
|
+
message: "Install for detected agents only, or add more?",
|
|
167
|
+
options: [
|
|
168
|
+
{
|
|
169
|
+
value: "detected",
|
|
170
|
+
label: "Detected only",
|
|
171
|
+
hint: detectedIds.join(", "),
|
|
172
|
+
},
|
|
173
|
+
{ value: "custom", label: "Customize…" },
|
|
174
|
+
],
|
|
175
|
+
}))
|
|
176
|
+
: "custom";
|
|
177
|
+
chosen =
|
|
178
|
+
mode === "detected"
|
|
179
|
+
? detectedIds
|
|
180
|
+
: await ask(p.autocompleteMultiselect({
|
|
181
|
+
message: "Select coding agents",
|
|
182
|
+
options: AGENTS.map((a) => ({
|
|
183
|
+
value: a.id,
|
|
184
|
+
label: a.label,
|
|
185
|
+
hint: a.hint,
|
|
186
|
+
})),
|
|
187
|
+
initialValues: detectedIds,
|
|
188
|
+
required: true,
|
|
189
|
+
maxItems: AGENTS.length,
|
|
190
|
+
output: circles,
|
|
191
|
+
}));
|
|
192
|
+
}
|
|
193
|
+
if (!chosen.length) {
|
|
194
|
+
p.log.error("No coding agent selected or found. Name them with --agents=…");
|
|
195
|
+
return 1;
|
|
196
|
+
}
|
|
197
|
+
// ---- 2. project or global ------------------------------------------------------------------
|
|
198
|
+
let scope = value("--scope");
|
|
199
|
+
if (scope && scope !== "project" && scope !== "global") {
|
|
200
|
+
p.log.error("--scope is project or global");
|
|
201
|
+
return 2;
|
|
202
|
+
}
|
|
203
|
+
if (!scope) {
|
|
204
|
+
const fallback = found.some((d) => chosen.includes(d.agent.id) && d.scope === "project")
|
|
205
|
+
? "project"
|
|
206
|
+
: "global";
|
|
207
|
+
scope = yes
|
|
208
|
+
? fallback
|
|
209
|
+
: await ask(p.select({
|
|
210
|
+
message: "Install location",
|
|
211
|
+
initialValue: fallback,
|
|
212
|
+
options: [
|
|
213
|
+
{
|
|
214
|
+
value: "project",
|
|
215
|
+
label: "Project",
|
|
216
|
+
hint: show(cwd) === "." ? cwd : show(cwd),
|
|
217
|
+
},
|
|
218
|
+
{ value: "global", label: "Global", hint: "~" },
|
|
219
|
+
],
|
|
220
|
+
}));
|
|
221
|
+
}
|
|
222
|
+
// ---- 3. which Constructor ------------------------------------------------------------------
|
|
223
|
+
let saved;
|
|
224
|
+
try {
|
|
225
|
+
saved = await readKeyFile(config.keyFile);
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
saved = undefined; // A broken key file is replaced by the new key.
|
|
229
|
+
}
|
|
230
|
+
let base;
|
|
231
|
+
try {
|
|
232
|
+
const given = value("--constructor");
|
|
233
|
+
if (given)
|
|
234
|
+
base = linkOrigin(given);
|
|
235
|
+
else if (yes)
|
|
236
|
+
base = saved?.url ?? DEFAULT_BASE_URL;
|
|
237
|
+
else {
|
|
238
|
+
const savedOrg = saved && saved.url !== DEFAULT_BASE_URL ? saved.url : "";
|
|
239
|
+
const where = await ask(p.select({
|
|
240
|
+
message: `Where do you have access to ${bold("Constructor Research")}?`,
|
|
241
|
+
initialValue: savedOrg ? "org" : "public",
|
|
242
|
+
options: [
|
|
243
|
+
{ value: "public", label: "constructor.app" },
|
|
244
|
+
{
|
|
245
|
+
value: "org",
|
|
246
|
+
label: "My organisation's separate instance",
|
|
247
|
+
hint: "e.g. acme.constructor.app",
|
|
248
|
+
},
|
|
249
|
+
],
|
|
250
|
+
}));
|
|
251
|
+
base =
|
|
252
|
+
where === "public"
|
|
253
|
+
? DEFAULT_BASE_URL
|
|
254
|
+
: linkOrigin(await ask(p.text({
|
|
255
|
+
message: "Your organisation's instance address",
|
|
256
|
+
placeholder: "acme.constructor.app",
|
|
257
|
+
initialValue: savedOrg.replace(/^https:\/\//, ""),
|
|
258
|
+
validate: (v) => {
|
|
259
|
+
if (!v?.trim())
|
|
260
|
+
return "Enter the address you open Constructor at";
|
|
261
|
+
try {
|
|
262
|
+
linkOrigin(v);
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
return "Enter an address like acme.constructor.app";
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
})));
|
|
269
|
+
// A typo in the address would send the new key to someone else's server.
|
|
270
|
+
const host = new URL(base).hostname;
|
|
271
|
+
if (host !== "constructor.app" &&
|
|
272
|
+
!host.endsWith(".constructor.app") &&
|
|
273
|
+
!(await ask(p.confirm({
|
|
274
|
+
message: `${bold(host)} is not a constructor.app address. Send your key there to check it?`,
|
|
275
|
+
initialValue: false,
|
|
276
|
+
})))) {
|
|
277
|
+
p.cancel("Nothing was sent. Run this again with the right address.");
|
|
278
|
+
return 1;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
catch (err) {
|
|
283
|
+
p.log.error(err?.message ?? String(err));
|
|
284
|
+
return 2;
|
|
285
|
+
}
|
|
286
|
+
// ---- 4. the access key ---------------------------------------------------------------------
|
|
287
|
+
let keySaved = false;
|
|
288
|
+
let keyProblem = "";
|
|
289
|
+
const check = async (key) => {
|
|
290
|
+
if (demo)
|
|
291
|
+
return "ok";
|
|
292
|
+
const s = p.spinner();
|
|
293
|
+
s.start(`Checking the key with ${base.replace(/^https:\/\//, "")}`);
|
|
294
|
+
const { result } = await checkKey(base, config.apiPath, key);
|
|
295
|
+
if (result === "ok")
|
|
296
|
+
s.stop("Key accepted");
|
|
297
|
+
else if (result === "rejected")
|
|
298
|
+
s.error(`${base} rejected this key`);
|
|
299
|
+
else
|
|
300
|
+
s.error(result === "unreachable"
|
|
301
|
+
? `Could not reach ${base} (VPN?)`
|
|
302
|
+
: `${base} answered without the public API`);
|
|
303
|
+
return result;
|
|
304
|
+
};
|
|
305
|
+
let key;
|
|
306
|
+
if (yes) {
|
|
307
|
+
const raw = flag("--key-stdin")
|
|
308
|
+
? await readStdin()
|
|
309
|
+
: env.CONSTRUCTOR_ACCESS_KEY;
|
|
310
|
+
if (raw !== undefined) {
|
|
311
|
+
key = accessKey(raw);
|
|
312
|
+
if (!key)
|
|
313
|
+
keyProblem = "that is not an access key (it starts with rk-)";
|
|
314
|
+
else if ((await check(key)) !== "ok") {
|
|
315
|
+
keyProblem = "the key was not accepted, so it was not saved";
|
|
316
|
+
key = undefined;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
else if (!(saved && saved.url === base))
|
|
320
|
+
keyProblem = "no key given (CONSTRUCTOR_ACCESS_KEY or --key-stdin)";
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
const keep = saved?.url === base &&
|
|
324
|
+
!(await ask(p.confirm({
|
|
325
|
+
message: `A key for ${base} is already saved (${mask(saved.key)}). Replace it?`,
|
|
326
|
+
initialValue: false,
|
|
327
|
+
})));
|
|
328
|
+
if (!keep) {
|
|
329
|
+
p.note(`1. Open ${accent(`${base}/research/api-keys`)}\n2. Create an access key and copy it (${dim("rk-…")})\n3. Paste it below. It is not shown, and no agent's settings will contain it`, "Get your access key");
|
|
330
|
+
for (let attempt = 0; attempt < 3 && !key; attempt++) {
|
|
331
|
+
const candidate = accessKey(await ask(p.password({
|
|
332
|
+
message: "Paste the access key",
|
|
333
|
+
mask: "•",
|
|
334
|
+
validate: (v) => accessKey(v)
|
|
335
|
+
? undefined
|
|
336
|
+
: "That is not an access key (it starts with rk-)",
|
|
337
|
+
})));
|
|
338
|
+
const result = await check(candidate);
|
|
339
|
+
if (result === "ok")
|
|
340
|
+
key = candidate;
|
|
341
|
+
else if (result !== "rejected") {
|
|
342
|
+
if (await ask(p.confirm({
|
|
343
|
+
message: "Save the key without checking it?",
|
|
344
|
+
initialValue: false,
|
|
345
|
+
})))
|
|
346
|
+
key = candidate;
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (!key)
|
|
351
|
+
keyProblem = "no accepted key, nothing saved";
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (key) {
|
|
355
|
+
if (dry)
|
|
356
|
+
p.log.info(`Dry run: key ${mask(key)} not saved.`);
|
|
357
|
+
else {
|
|
358
|
+
await saveKeyFile(config.keyFile, base, key);
|
|
359
|
+
keySaved = true;
|
|
360
|
+
p.log.success(`Key saved to ${bold(show(config.keyFile))} ${dim("(readable only by you)")}`);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
else if (keyProblem)
|
|
364
|
+
p.log.warn(`Key: ${keyProblem}.`);
|
|
365
|
+
// ---- 5. the MCP entry in each agent ----------------------------------------------------------
|
|
366
|
+
const spec = mcpCommand();
|
|
367
|
+
const targets = chosen.map((id) => {
|
|
368
|
+
const agent = AGENTS.find((a) => a.id === id);
|
|
369
|
+
const level = agent.globalOnly ? "global" : scope;
|
|
370
|
+
return { agent, level };
|
|
371
|
+
});
|
|
372
|
+
const names = (list) => list
|
|
373
|
+
.map((t) => `${t.agent.label}${t.level !== scope ? dim(` (${t.level})`) : ""}`)
|
|
374
|
+
.join(", ");
|
|
375
|
+
if (verbose) {
|
|
376
|
+
const lines = [];
|
|
377
|
+
for (const { agent, level } of targets) {
|
|
378
|
+
lines.push(`${bold(agent.label)} ${dim(`· ${level}`)}`);
|
|
379
|
+
lines.push(` ${dim("file")} ${show(agent.file(level, home, cwd, env))}`);
|
|
380
|
+
describe(agent.kind, spec).forEach((l, i) => lines.push(` ${dim(i ? " " : "add ")} ${l}`));
|
|
381
|
+
lines.push("");
|
|
382
|
+
}
|
|
383
|
+
lines.pop();
|
|
384
|
+
p.note(lines.join("\n"), `MCP "${SERVER}": no key inside, it reads the saved key`);
|
|
385
|
+
}
|
|
386
|
+
const results = [];
|
|
387
|
+
// Without Claude Code's CLI its settings file is edited directly; an open Claude Code would
|
|
388
|
+
// overwrite that edit when it next saves.
|
|
389
|
+
if (!dry && !demo && chosen.includes("claude") && !claudeCliWorks(env))
|
|
390
|
+
p.log.warn(`Quit ${bold("Claude Code")} before you continue: its settings file is edited directly.`);
|
|
391
|
+
if (dry)
|
|
392
|
+
p.log.info(`Dry run: would add the MCP to ${names(targets)}.`);
|
|
393
|
+
else if (yes ||
|
|
394
|
+
(await ask(p.confirm({
|
|
395
|
+
message: `Add the MCP to ${names(targets)}${dim(` · ${scope}`)}?`,
|
|
396
|
+
initialValue: true,
|
|
397
|
+
})))) {
|
|
398
|
+
const s = p.spinner();
|
|
399
|
+
s.start("Adding");
|
|
400
|
+
for (const { agent, level } of targets)
|
|
401
|
+
results.push({
|
|
402
|
+
agent,
|
|
403
|
+
r: applyEntry(agent, level, spec, { cwd, home, env }),
|
|
404
|
+
});
|
|
405
|
+
const good = results.filter(({ r }) => succeeded(r));
|
|
406
|
+
s.stop(good.length
|
|
407
|
+
? `Added to ${good.map(({ agent }) => agent.label).join(", ")}`
|
|
408
|
+
: "Nothing added");
|
|
409
|
+
for (const { agent, r } of results) {
|
|
410
|
+
if (!succeeded(r))
|
|
411
|
+
p.log.warn(`${agent.label}: ${r.status}${r.note ? dim(` · ${r.note}`) : ""}`);
|
|
412
|
+
else if (verbose)
|
|
413
|
+
p.log.info(`${agent.label}: ${r.status}${r.note ? dim(` · ${r.note}`) : ""}`);
|
|
414
|
+
else if (r.note?.includes("jcode no longer imports"))
|
|
415
|
+
p.log.info(`${agent.label}: ${dim(r.note.split("; ").pop())}`);
|
|
416
|
+
}
|
|
417
|
+
if (!verbose && good.length)
|
|
418
|
+
p.log.message(dim("Changed files were backed up first. --verbose shows files and entries."));
|
|
419
|
+
}
|
|
420
|
+
else
|
|
421
|
+
p.log.info("Nothing written to the agents' settings.");
|
|
422
|
+
const okNames = results
|
|
423
|
+
.filter(({ r }) => succeeded(r))
|
|
424
|
+
.map(({ agent }) => agent.label);
|
|
425
|
+
const failed = results.some(({ r }) => !succeeded(r)) || !!keyProblem;
|
|
426
|
+
p.outro(okNames.length
|
|
427
|
+
? `${bold("Done.")} Restart ${okNames.join(", ")} and ask about Constructor.`
|
|
428
|
+
: keySaved
|
|
429
|
+
? `${bold("Key saved.")} Add the MCP to an agent, then restart it.`
|
|
430
|
+
: `${bold("Nothing changed.")} Run this again when ready.`);
|
|
431
|
+
return failed ? 1 : 0;
|
|
432
|
+
}
|
package/dist/key.d.ts
CHANGED
|
@@ -7,3 +7,9 @@ export interface SavedKey {
|
|
|
7
7
|
export declare function readKeyFile(file: string): Promise<SavedKey | undefined>;
|
|
8
8
|
/** Saves the key for one Constructor, mode 600, replacing the file in one step. */
|
|
9
9
|
export declare function saveKeyFile(file: string, url: string, key: string): Promise<void>;
|
|
10
|
+
export type KeyCheck = "ok" | "rejected" | "no_api" | "unreachable";
|
|
11
|
+
/** Ask Constructor whether it accepts the key: 200 accepted, 401 rejected, else no public API. */
|
|
12
|
+
export declare function checkKey(url: string, apiPath: string, key: string, timeoutMs?: number): Promise<{
|
|
13
|
+
result: KeyCheck;
|
|
14
|
+
status?: number;
|
|
15
|
+
}>;
|
package/dist/key.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
2
3
|
import { dirname } from "node:path";
|
|
3
4
|
import { originOf } from "./config.js";
|
|
4
5
|
/** The saved key, or undefined when none was saved. A broken file throws. */
|
|
@@ -27,9 +28,38 @@ export async function readKeyFile(file) {
|
|
|
27
28
|
/** Saves the key for one Constructor, mode 600, replacing the file in one step. */
|
|
28
29
|
export async function saveKeyFile(file, url, key) {
|
|
29
30
|
await mkdir(dirname(file), { recursive: true, mode: 0o700 });
|
|
30
|
-
const tmp = `${file}.${process.pid}.tmp`;
|
|
31
|
+
const tmp = `${file}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
|
|
31
32
|
await writeFile(tmp, JSON.stringify({ url: originOf(url), key }) + "\n", {
|
|
32
33
|
mode: 0o600,
|
|
34
|
+
flag: "wx",
|
|
33
35
|
});
|
|
34
|
-
|
|
36
|
+
try {
|
|
37
|
+
await rename(tmp, file);
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
await rm(tmp, { force: true });
|
|
41
|
+
throw err;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Ask Constructor whether it accepts the key: 200 accepted, 401 rejected, else no public API. */
|
|
45
|
+
export async function checkKey(url, apiPath, key, timeoutMs = 15000) {
|
|
46
|
+
try {
|
|
47
|
+
const response = await fetch(`${url}${apiPath}/auth/session`, {
|
|
48
|
+
headers: {
|
|
49
|
+
"X-Research-AccessKey": `Bearer ${key}`,
|
|
50
|
+
Accept: "application/json",
|
|
51
|
+
},
|
|
52
|
+
redirect: "manual",
|
|
53
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
54
|
+
});
|
|
55
|
+
await response.body?.cancel();
|
|
56
|
+
const status = response.status;
|
|
57
|
+
return {
|
|
58
|
+
result: status === 200 ? "ok" : status === 401 ? "rejected" : "no_api",
|
|
59
|
+
status,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return { result: "unreachable" };
|
|
64
|
+
}
|
|
35
65
|
}
|
package/dist/schemas.js
CHANGED
|
@@ -117,7 +117,7 @@ export const serviceInput = z
|
|
|
117
117
|
.max(256)
|
|
118
118
|
.regex(/^[a-z0-9][a-z0-9._\/:@-]*$/i, "image is a registry reference such as jgraph/drawio:latest or ghcr.io/org/app:1.2.")
|
|
119
119
|
.optional()
|
|
120
|
-
.describe("A registry image reference (
|
|
120
|
+
.describe("A public registry image reference (jgraph/drawio:24.7.17, ghcr.io/org/app:1.2) or the name of an image already in the project; the alternative to build and image_id. If the project has it, it is used as is. Otherwise the MCP adds it to the project as an external image: no desk and no build, it runs unchanged. Constructor checks it at once (public, linux/amd64, tag exists) and a refusal comes back as image_refused with the reason; then tell the user. Prefer a fixed tag over latest. To add files or change it, use build with your own Dockerfile. The service's ports must match the port the image listens on."),
|
|
121
121
|
build: build.optional(),
|
|
122
122
|
hardware: z
|
|
123
123
|
.strictObject({
|