@blackbelt-technology/pi-dashboard-mcp-client-plugin 0.10.0 → 0.11.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/README.md +1 -1
- package/package.json +7 -4
- package/src/client/FolderMcpPage.tsx +27 -3
- package/src/client/FolderMcpSection.tsx +3 -3
- package/src/client/ProjectSettings.tsx +2 -2
- package/src/client/ServerEditor.tsx +2 -2
- package/src/client/ServerList.tsx +2 -2
- package/src/client/api.ts +1 -1
- package/src/client/folder-view.ts +2 -2
- package/src/client/hooks.ts +2 -1
- package/src/client/schema.ts +1 -1
- package/src/core/index.ts +8 -78
- package/src/server/index.ts +4 -4
- package/src/server/pi-runner.ts +1 -1
- package/src/server/routes.ts +4 -4
- package/schema/mcp-config.schema.json +0 -64
- package/src/core/config-io.ts +0 -70
- package/src/core/config-writer.ts +0 -634
- package/src/core/effective-view.ts +0 -251
- package/src/core/layers.ts +0 -61
- package/src/core/live-state.ts +0 -134
- package/src/core/path-utils.ts +0 -41
- package/src/core/pi-rules.ts +0 -297
- package/src/core/schema-validation.ts +0 -57
- package/src/core/service.ts +0 -141
- package/src/core/types.ts +0 -278
|
@@ -1,634 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* mcp-client-plugin · CORE config writer over pi's built-in MCP layers.
|
|
3
|
-
*
|
|
4
|
-
* Every write targets exactly one layer (`global` → the Pi-global `mcp.json`,
|
|
5
|
-
* `project` → `<cwd>/.pi/mcp.json`), replaces or deletes exactly one
|
|
6
|
-
* `mcpServers.<name>` entry, preserves every other entry and top-level key,
|
|
7
|
-
* writes atomically, and refuses to write over a file pi cannot parse
|
|
8
|
-
* (strict JSON). Before any byte is written the resulting entry is checked
|
|
9
|
-
* against the mirrored pi rules (`pi-rules.ts`), the dashboard's transport
|
|
10
|
-
* rule, project-scope `auth`, and `-`/`_` namespace collisions, so the
|
|
11
|
-
* writer never persists an entry pi would reject or drop.
|
|
12
|
-
*
|
|
13
|
-
* Refusals are the closed {@link ConfigRefusalCode} set, shared with
|
|
14
|
-
* `checkConfigFiles` (same `planEnsure` function), so check mode can never
|
|
15
|
-
* report a healthy file that write mode would refuse.
|
|
16
|
-
*
|
|
17
|
-
* See change: migrate-mcp-to-pi-builtin (D3); earlier: extract-mcp-client-plugin.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import { isAllowedCwd, tokenAllows } from "@blackbelt-technology/pi-dashboard-shared/cwd-guard.js";
|
|
21
|
-
import { type LayerPaths, type LayerRead, nullProto, readLayer } from "./layers.js";
|
|
22
|
-
import { deepEqual, isPlainObject } from "./path-utils.js";
|
|
23
|
-
import {
|
|
24
|
-
ADAPTER_ONLY_KEYS,
|
|
25
|
-
classifyProjectEntry,
|
|
26
|
-
displayExposure,
|
|
27
|
-
isFullEntry,
|
|
28
|
-
isProjectOverride,
|
|
29
|
-
isValidServerName,
|
|
30
|
-
keptGlobalOf,
|
|
31
|
-
mcpNamespace,
|
|
32
|
-
OVERRIDE_KEYS,
|
|
33
|
-
transportOf,
|
|
34
|
-
validatePiEntry,
|
|
35
|
-
} from "./pi-rules.js";
|
|
36
|
-
import { errorFields, validateServerEntry } from "./schema-validation.js";
|
|
37
|
-
import type {
|
|
38
|
-
ConfigIO,
|
|
39
|
-
ConfigRefusal,
|
|
40
|
-
ConfigWriteResult,
|
|
41
|
-
ConvertLegacyResult,
|
|
42
|
-
ParseStatus,
|
|
43
|
-
ProjectOverridePatch,
|
|
44
|
-
ProjectOverrideResult,
|
|
45
|
-
RemoveResult,
|
|
46
|
-
Scope,
|
|
47
|
-
ServerEntry,
|
|
48
|
-
SetEnabledResult,
|
|
49
|
-
} from "./types.js";
|
|
50
|
-
|
|
51
|
-
/** Thrown when a project scope names a cwd outside the known-folder set. */
|
|
52
|
-
export class NotAllowedCwdError extends Error {
|
|
53
|
-
public readonly cwd: string;
|
|
54
|
-
|
|
55
|
-
constructor(cwd: string) {
|
|
56
|
-
super(`cwd not allowed: ${cwd}`);
|
|
57
|
-
this.cwd = cwd;
|
|
58
|
-
this.name = "NotAllowedCwdError";
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** Secret-bearing fields never copied silently from the global entry into a folder copy. */
|
|
63
|
-
export const FOLDER_COPY_OMITTED = ["headers", "env", "oauth.clientSecret", "auth"] as const;
|
|
64
|
-
|
|
65
|
-
export interface ConfigWriterDeps {
|
|
66
|
-
configIO: ConfigIO;
|
|
67
|
-
paths: LayerPaths;
|
|
68
|
-
knownCwds: () => string[];
|
|
69
|
-
/** pi's project-trust rule. Absent → every project is untrusted. */
|
|
70
|
-
isProjectTrusted?: (cwd: string) => boolean;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export interface ConfigWriter {
|
|
74
|
-
targetPath(scope: Scope): string;
|
|
75
|
-
readServerEntry(name: string, scope: Scope): ServerEntry | undefined;
|
|
76
|
-
ensureServerEntry(name: string, fields: Partial<ServerEntry>, scope: Scope): ConfigWriteResult;
|
|
77
|
-
saveServer(
|
|
78
|
-
name: string,
|
|
79
|
-
entry: ServerEntry,
|
|
80
|
-
scope: Scope,
|
|
81
|
-
opts?: { previousName?: string; create?: boolean },
|
|
82
|
-
): ConfigWriteResult;
|
|
83
|
-
removeServer(name: string, scope: Scope): RemoveResult;
|
|
84
|
-
setEnabled(name: string, enabled: boolean, scope: Scope): SetEnabledResult;
|
|
85
|
-
setProjectOverride(name: string, patch: ProjectOverridePatch, scope: Scope): ProjectOverrideResult;
|
|
86
|
-
convertLegacyCopy(name: string, scope: Scope): ConvertLegacyResult;
|
|
87
|
-
convertAdapterLeftovers(name: string, scope: Scope): ConfigWriteResult;
|
|
88
|
-
/** Dry run of `ensureServerEntry`: the refusal it would return, or null. */
|
|
89
|
-
previewEnsure(name: string, fields: Partial<ServerEntry>, scope: Scope): ConfigRefusal | null;
|
|
90
|
-
readParseStatus(path: string): ParseStatus;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
type Prepared =
|
|
94
|
-
| { ok: true; path: string; layer: Extract<LayerRead, { ok: true }> }
|
|
95
|
-
| { ok: false; refusal: ConfigRefusal };
|
|
96
|
-
|
|
97
|
-
/** A copy of a global entry for a folder layer, without secret values or `auth`. */
|
|
98
|
-
export function folderCopyOf(entry: Record<string, unknown>): { copy: Record<string, unknown>; omitted: string[] } {
|
|
99
|
-
const copy = nullProto({ ...entry });
|
|
100
|
-
const omitted: string[] = [];
|
|
101
|
-
for (const k of ["headers", "env", "auth"] as const) {
|
|
102
|
-
if (copy[k] !== undefined) {
|
|
103
|
-
delete copy[k];
|
|
104
|
-
omitted.push(k);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (isPlainObject(copy.oauth) && copy.oauth.clientSecret !== undefined) {
|
|
108
|
-
const { clientSecret: _drop, ...rest } = copy.oauth;
|
|
109
|
-
copy.oauth = rest;
|
|
110
|
-
omitted.push("oauth.clientSecret");
|
|
111
|
-
}
|
|
112
|
-
return { copy, omitted };
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const OVERRIDE_ROUTE_HINT = "use the project-override operation to change a global server for one folder";
|
|
116
|
-
|
|
117
|
-
function omitKeys(entry: Record<string, unknown>, keys: readonly string[]): Record<string, unknown> {
|
|
118
|
-
return Object.fromEntries(Object.entries(entry).filter(([k]) => !keys.includes(k)));
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* A whole-entry folder copy of `globalRaw` made by the old disable path: both
|
|
123
|
-
* are full entries, the folder entry carries none of the secret-bearing fields
|
|
124
|
-
* (a re-entered secret vetoes), and apart from the override keys it equals the
|
|
125
|
-
* global entry without those fields. See change: mcp-client-project-overrides (D7).
|
|
126
|
-
*/
|
|
127
|
-
export function isLegacyFolderCopy(folderRaw: unknown, globalRaw: unknown): boolean {
|
|
128
|
-
if (!isFullEntry(folderRaw) || !isFullEntry(globalRaw)) return false;
|
|
129
|
-
if (folderRaw.headers !== undefined || folderRaw.env !== undefined || folderRaw.auth !== undefined) return false;
|
|
130
|
-
if (isPlainObject(folderRaw.oauth) && folderRaw.oauth.clientSecret !== undefined) return false;
|
|
131
|
-
return deepEqual(omitKeys(folderRaw, OVERRIDE_KEYS), omitKeys(folderCopyOf(globalRaw).copy, OVERRIDE_KEYS));
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** `existing` override with `patch` applied: absent key = unchanged, `null` = delete the key. */
|
|
135
|
-
function patchedOverride(existing: Record<string, unknown> | undefined, patch: ProjectOverridePatch): Record<string, unknown> {
|
|
136
|
-
const next = nullProto({ ...(existing ?? {}) });
|
|
137
|
-
for (const k of OVERRIDE_KEYS) {
|
|
138
|
-
const v = (patch as Record<string, unknown>)[k];
|
|
139
|
-
if (v === undefined) continue;
|
|
140
|
-
if (v === null) delete next[k];
|
|
141
|
-
else next[k] = v;
|
|
142
|
-
}
|
|
143
|
-
return next;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** A legacy copy rewritten to only the override keys whose value differs from the global entry. */
|
|
147
|
-
function convertedOverride(folder: Record<string, unknown>, base: Record<string, unknown>): Record<string, unknown> {
|
|
148
|
-
const next: Record<string, unknown> = nullProto({});
|
|
149
|
-
// A non-boolean `enabled` is carried forward so the merged validation refuses it, never silently dropped.
|
|
150
|
-
if (folder.enabled !== undefined && folder.enabled !== (base.enabled !== false)) next.enabled = folder.enabled;
|
|
151
|
-
if (folder.exposure !== undefined && displayExposure(folder.exposure) !== displayExposure(base.exposure)) {
|
|
152
|
-
next.exposure = folder.exposure;
|
|
153
|
-
}
|
|
154
|
-
if (folder.toolExposure !== undefined && !deepEqual(folder.toolExposure, base.toolExposure)) {
|
|
155
|
-
next.toolExposure = folder.toolExposure;
|
|
156
|
-
}
|
|
157
|
-
return next;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/** Shape-check a patch BEFORE any IO: only override keys, correctly typed (`null` = delete). */
|
|
161
|
-
function patchShapeRefusal(name: string, patch: unknown): ConfigRefusal | null {
|
|
162
|
-
if (!isPlainObject(patch)) {
|
|
163
|
-
return { code: "invalid-entry", message: `server "${name}": override patch must be an object` };
|
|
164
|
-
}
|
|
165
|
-
const present = Object.fromEntries(Object.entries(patch).filter(([, v]) => v !== null && v !== undefined));
|
|
166
|
-
const shape = validateServerEntry(present, { kind: "override" });
|
|
167
|
-
if (shape.ok) return null;
|
|
168
|
-
const f = errorFields(shape.errors);
|
|
169
|
-
return { code: "invalid-entry", message: `server "${name}": invalid project override${f.length ? ` (${f.join(", ")})` : ""}` };
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function wholeEntryRefusal(name: string, path: string): { ok: false; refusal: ConfigRefusal } {
|
|
173
|
-
return {
|
|
174
|
-
ok: false,
|
|
175
|
-
refusal: {
|
|
176
|
-
code: "invalid-entry",
|
|
177
|
-
message: `server "${name}" is a whole-entry folder entry, not an override (${OVERRIDE_ROUTE_HINT})`,
|
|
178
|
-
path,
|
|
179
|
-
},
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/** The refusal when `next` merged onto its kept global base is not a valid pi override. */
|
|
184
|
-
function mergedOverrideRefusal(
|
|
185
|
-
name: string,
|
|
186
|
-
next: Record<string, unknown>,
|
|
187
|
-
kept: ReadonlyMap<string, Record<string, unknown>>,
|
|
188
|
-
path: string,
|
|
189
|
-
): ConfigRefusal | null {
|
|
190
|
-
const cls = classifyProjectEntry(name, next, kept);
|
|
191
|
-
if (cls.kind === "override") return null;
|
|
192
|
-
return { code: "invalid-entry", message: cls.kind === "replace" ? OVERRIDE_ROUTE_HINT : cls.message, path };
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
export function createConfigWriter(deps: ConfigWriterDeps): ConfigWriter {
|
|
196
|
-
const { configIO, paths, knownCwds } = deps;
|
|
197
|
-
const trusted = (cwd: string): boolean => deps.isProjectTrusted?.(cwd) === true;
|
|
198
|
-
|
|
199
|
-
function targetPath(scope: Scope): string {
|
|
200
|
-
if (scope.kind === "global") return paths.globalPath();
|
|
201
|
-
// A live request-scoped token means the route already admitted this exact cwd
|
|
202
|
-
// (async, once): accept it without a second, synchronous git probe. Any other
|
|
203
|
-
// scope (non-route callers, a released token) is decided by the sync guard.
|
|
204
|
-
if (tokenAllows(scope.admission, scope.cwd)) return paths.projectPath(scope.cwd);
|
|
205
|
-
if (!isAllowedCwd(scope.cwd, knownCwds)) throw new NotAllowedCwdError(scope.cwd);
|
|
206
|
-
return paths.projectPath(scope.cwd);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function nameRefusal(name: string): ConfigRefusal | null {
|
|
210
|
-
if (isValidServerName(name)) return null;
|
|
211
|
-
return {
|
|
212
|
-
code: "invalid-name",
|
|
213
|
-
message: `invalid server name ${JSON.stringify(name)} (use letters, digits, "_" and "-")`,
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function prepare(name: string, scope: Scope): Prepared {
|
|
218
|
-
const bad = nameRefusal(name);
|
|
219
|
-
if (bad) return { ok: false, refusal: bad };
|
|
220
|
-
const path = targetPath(scope);
|
|
221
|
-
const layer = readLayer(configIO, path);
|
|
222
|
-
if (!layer.ok) {
|
|
223
|
-
return {
|
|
224
|
-
ok: false,
|
|
225
|
-
refusal: { code: "unparseable", message: `${layer.message} (pi skips this file; fix it by hand)`, path },
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
return { ok: true, path, layer };
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/** Every entry a write could collide with, and the file it lives in. */
|
|
232
|
-
function collisionCandidates(scope: Scope): Array<{ name: string; path: string }> {
|
|
233
|
-
const out: Array<{ name: string; path: string }> = [];
|
|
234
|
-
const add = (path: string, skipOverrides: boolean): void => {
|
|
235
|
-
const l = readLayer(configIO, path);
|
|
236
|
-
if (!l.ok) return;
|
|
237
|
-
for (const [n, raw] of Object.entries(l.servers)) {
|
|
238
|
-
// pi never clash-checks a project override (it reuses the global entry's name).
|
|
239
|
-
if (skipOverrides && isProjectOverride(raw)) continue;
|
|
240
|
-
out.push({ name: n, path });
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
add(paths.globalPath(), false);
|
|
244
|
-
if (scope.kind === "project") {
|
|
245
|
-
add(paths.projectPath(scope.cwd), true);
|
|
246
|
-
} else {
|
|
247
|
-
// pi loads the global layer first, then the trusted project layer, and
|
|
248
|
-
// DROPS the project entry on a collision — so a global write must not
|
|
249
|
-
// collide with any known trusted folder's entry.
|
|
250
|
-
for (const cwd of knownCwds()) if (trusted(cwd)) add(paths.projectPath(cwd), true);
|
|
251
|
-
}
|
|
252
|
-
return out;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Validation shared by every write that sets an entry's content. `replacing`
|
|
257
|
-
* names the entry a rename removes; it is excluded from the collision check
|
|
258
|
-
* only in the TARGET layer — a same-named entry in another layer still
|
|
259
|
-
* collides (pi would drop it).
|
|
260
|
-
*/
|
|
261
|
-
function validateEntry(
|
|
262
|
-
name: string,
|
|
263
|
-
entry: Record<string, unknown>,
|
|
264
|
-
scope: Scope,
|
|
265
|
-
replacing?: string,
|
|
266
|
-
): ConfigRefusal | null {
|
|
267
|
-
if (typeof entry.command === "string" && typeof entry.url === "string") {
|
|
268
|
-
return {
|
|
269
|
-
code: "transport-conflict",
|
|
270
|
-
message: `server "${name}" carries both command and url; keep exactly one transport`,
|
|
271
|
-
fields: ["command", "url"],
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
const piError = validatePiEntry(name, entry);
|
|
275
|
-
if (piError) return { code: "invalid-entry", message: piError };
|
|
276
|
-
if (scope.kind === "project" && entry.auth !== undefined && transportOf(entry) === "http") {
|
|
277
|
-
return {
|
|
278
|
-
code: "invalid-entry",
|
|
279
|
-
message: `server "${name}": auth is only allowed in the global mcp.json (provider auth is global-only)`,
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
const ns = mcpNamespace(name);
|
|
283
|
-
const target = targetPath(scope);
|
|
284
|
-
const clash = collisionCandidates(scope).find(
|
|
285
|
-
(c) =>
|
|
286
|
-
c.name !== name && !(c.name === replacing && c.path === target) && mcpNamespace(c.name) === ns,
|
|
287
|
-
);
|
|
288
|
-
if (clash) {
|
|
289
|
-
return {
|
|
290
|
-
code: "name-collision",
|
|
291
|
-
message: `server "${name}" conflicts with "${clash.name}" in ${clash.path} (pi maps "-" and "_" to one namespace)`,
|
|
292
|
-
conflict: { name: clash.name, path: clash.path },
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
return null;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
function write(path: string, config: Record<string, unknown>, servers: Record<string, unknown>): ConfigWriteResult {
|
|
299
|
-
const next = nullProto({ ...config });
|
|
300
|
-
next.mcpServers = servers;
|
|
301
|
-
if (JSON.stringify(next) === JSON.stringify(config)) return { ok: true };
|
|
302
|
-
try {
|
|
303
|
-
configIO.writeFileAtomic(path, `${JSON.stringify(next, null, 2)}\n`);
|
|
304
|
-
return { ok: true };
|
|
305
|
-
} catch (e) {
|
|
306
|
-
const err = e as NodeJS.ErrnoException;
|
|
307
|
-
return {
|
|
308
|
-
ok: false,
|
|
309
|
-
refusal: {
|
|
310
|
-
code: "write-failed",
|
|
311
|
-
message: `failed to write ${path}: ${err.message}`,
|
|
312
|
-
path,
|
|
313
|
-
...(err.code ? { ioCode: err.code } : {}),
|
|
314
|
-
},
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
function existingObject(
|
|
320
|
-
p: Extract<Prepared, { ok: true }>,
|
|
321
|
-
name: string,
|
|
322
|
-
): { ok: true; entry: Record<string, unknown> | undefined } | { ok: false; refusal: ConfigRefusal } {
|
|
323
|
-
const raw = p.layer.servers[name];
|
|
324
|
-
if (raw === undefined) return { ok: true, entry: undefined };
|
|
325
|
-
if (!isPlainObject(raw)) {
|
|
326
|
-
return {
|
|
327
|
-
ok: false,
|
|
328
|
-
refusal: { code: "entry-not-object", message: `${p.path}: server "${name}" is not an object`, path: p.path },
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
return { ok: true, entry: nullProto({ ...raw }) };
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
function planEnsure(
|
|
335
|
-
name: string,
|
|
336
|
-
fields: Partial<ServerEntry>,
|
|
337
|
-
scope: Scope,
|
|
338
|
-
): { ok: true; p: Extract<Prepared, { ok: true }>; next: Record<string, unknown> } | { ok: false; refusal: ConfigRefusal } {
|
|
339
|
-
const p = prepare(name, scope);
|
|
340
|
-
if (!p.ok) return p;
|
|
341
|
-
const existing = existingObject(p, name);
|
|
342
|
-
if (!existing.ok) return existing;
|
|
343
|
-
const next = nullProto({ ...(existing.entry ?? {}) });
|
|
344
|
-
for (const [k, v] of Object.entries(fields)) {
|
|
345
|
-
if (v === undefined) delete next[k];
|
|
346
|
-
else next[k] = v;
|
|
347
|
-
}
|
|
348
|
-
const refusal = validateEntry(name, next, scope);
|
|
349
|
-
if (refusal) return { ok: false, refusal };
|
|
350
|
-
return { ok: true, p, next };
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
function ensureServerEntry(name: string, fields: Partial<ServerEntry>, scope: Scope): ConfigWriteResult {
|
|
354
|
-
const plan = planEnsure(name, fields, scope);
|
|
355
|
-
if (!plan.ok) return plan;
|
|
356
|
-
const servers = nullProto({ ...plan.p.layer.servers });
|
|
357
|
-
servers[name] = plan.next;
|
|
358
|
-
return write(plan.p.path, plan.p.layer.config, servers);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
function previewEnsure(name: string, fields: Partial<ServerEntry>, scope: Scope): ConfigRefusal | null {
|
|
362
|
-
const plan = planEnsure(name, fields, scope);
|
|
363
|
-
return plan.ok ? null : plan.refusal;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function saveServer(
|
|
367
|
-
name: string,
|
|
368
|
-
entry: ServerEntry,
|
|
369
|
-
scope: Scope,
|
|
370
|
-
opts?: { previousName?: string; create?: boolean },
|
|
371
|
-
): ConfigWriteResult {
|
|
372
|
-
const p = prepare(name, scope);
|
|
373
|
-
if (!p.ok) return p;
|
|
374
|
-
if (!isPlainObject(entry)) {
|
|
375
|
-
return { ok: false, refusal: { code: "invalid-entry", message: `server "${name}" must be an object` } };
|
|
376
|
-
}
|
|
377
|
-
const prev = opts?.previousName;
|
|
378
|
-
const renaming = prev !== undefined && prev !== name;
|
|
379
|
-
if (renaming) {
|
|
380
|
-
const bad = nameRefusal(prev);
|
|
381
|
-
if (bad) return { ok: false, refusal: bad };
|
|
382
|
-
}
|
|
383
|
-
if (scope.kind === "project") {
|
|
384
|
-
// Overrides have exactly one write path (`setProjectOverride`).
|
|
385
|
-
const prevEntry = renaming ? p.layer.servers[prev] : undefined;
|
|
386
|
-
if (isProjectOverride(entry) || isProjectOverride(prevEntry)) {
|
|
387
|
-
return { ok: false, refusal: { code: "invalid-entry", message: `server "${name}": ${OVERRIDE_ROUTE_HINT}`, path: p.path } };
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
// A rename or a create never replaces another entry: one write, one entry.
|
|
391
|
-
if ((renaming || opts?.create === true) && p.layer.servers[name] !== undefined) {
|
|
392
|
-
return {
|
|
393
|
-
ok: false,
|
|
394
|
-
refusal: {
|
|
395
|
-
code: "name-collision",
|
|
396
|
-
message: `${p.path} already defines "${name}"${renaming ? ` (cannot rename "${prev}" onto it)` : ""}`,
|
|
397
|
-
conflict: { name, path: p.path },
|
|
398
|
-
},
|
|
399
|
-
};
|
|
400
|
-
}
|
|
401
|
-
const next = nullProto({ ...(entry as Record<string, unknown>) });
|
|
402
|
-
const refusal = validateEntry(name, next, scope, prev);
|
|
403
|
-
if (refusal) return { ok: false, refusal };
|
|
404
|
-
const servers = nullProto({ ...p.layer.servers });
|
|
405
|
-
if (renaming) delete servers[prev];
|
|
406
|
-
servers[name] = next;
|
|
407
|
-
return write(p.path, p.layer.config, servers);
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
function removeServer(name: string, scope: Scope): RemoveResult {
|
|
411
|
-
const p = prepare(name, scope);
|
|
412
|
-
if (!p.ok) return p;
|
|
413
|
-
const removed = p.layer.servers[name] as ServerEntry | undefined;
|
|
414
|
-
if (removed === undefined) return { ok: true, removed: undefined };
|
|
415
|
-
const servers = nullProto({ ...p.layer.servers });
|
|
416
|
-
delete servers[name];
|
|
417
|
-
const w = write(p.path, p.layer.config, servers);
|
|
418
|
-
return w.ok ? { ok: true, removed } : w;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
/** The kept Pi-global base for `name`, or a refusal (unparseable global wins over `no-global-server`). */
|
|
422
|
-
function keptBase(name: string): { ok: true; base: Record<string, unknown>; kept: ReadonlyMap<string, Record<string, unknown>> } | { ok: false; refusal: ConfigRefusal } {
|
|
423
|
-
const gpath = paths.globalPath();
|
|
424
|
-
const g = readLayer(configIO, gpath);
|
|
425
|
-
if (!g.ok) {
|
|
426
|
-
return { ok: false, refusal: { code: "unparseable", message: `${g.message} (pi skips this file; fix it by hand)`, path: gpath } };
|
|
427
|
-
}
|
|
428
|
-
const { kept } = keptGlobalOf(g.servers);
|
|
429
|
-
const base = kept.get(name);
|
|
430
|
-
if (!base) {
|
|
431
|
-
return {
|
|
432
|
-
ok: false,
|
|
433
|
-
refusal: {
|
|
434
|
-
code: "no-global-server",
|
|
435
|
-
message: `server "${name}" needs "command" or "url", or a global server to override`,
|
|
436
|
-
path: gpath,
|
|
437
|
-
},
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
return { ok: true, base, kept };
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
/** Everything an override write needs, or the refusal: scope, patch shape, target file, base. */
|
|
444
|
-
function planOverride(
|
|
445
|
-
name: string,
|
|
446
|
-
patch: ProjectOverridePatch,
|
|
447
|
-
scope: Scope,
|
|
448
|
-
):
|
|
449
|
-
| { ok: true; p: Extract<Prepared, { ok: true }>; existing: Record<string, unknown> | undefined; kept: ReadonlyMap<string, Record<string, unknown>> }
|
|
450
|
-
| { ok: false; refusal: ConfigRefusal } {
|
|
451
|
-
if (scope.kind !== "project") {
|
|
452
|
-
return { ok: false, refusal: { code: "invalid-entry", message: "a project override can only be written at project scope" } };
|
|
453
|
-
}
|
|
454
|
-
const bad = patchShapeRefusal(name, patch);
|
|
455
|
-
if (bad) return { ok: false, refusal: bad };
|
|
456
|
-
const p = prepare(name, scope);
|
|
457
|
-
if (!p.ok) return p;
|
|
458
|
-
const existing = existingObject(p, name);
|
|
459
|
-
if (!existing.ok) return existing;
|
|
460
|
-
if (existing.entry !== undefined && !isProjectOverride(existing.entry)) return wholeEntryRefusal(name, p.path);
|
|
461
|
-
const kb = keptBase(name); // an unparseable global file wins over no-global-server
|
|
462
|
-
if (!kb.ok) return kb;
|
|
463
|
-
return { ok: true, p, existing: existing.entry, kept: kb.kept };
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
/** Validate `next` as a pi override of its kept base, then write it (or delete the emptied entry). */
|
|
467
|
-
function commitOverride(
|
|
468
|
-
name: string,
|
|
469
|
-
p: Extract<Prepared, { ok: true }>,
|
|
470
|
-
next: Record<string, unknown>,
|
|
471
|
-
kept: ReadonlyMap<string, Record<string, unknown>>,
|
|
472
|
-
): ProjectOverrideResult {
|
|
473
|
-
const empty = Object.keys(next).length === 0;
|
|
474
|
-
const invalid = empty ? null : mergedOverrideRefusal(name, next, kept, p.path);
|
|
475
|
-
if (invalid) return { ok: false, refusal: invalid };
|
|
476
|
-
const servers = nullProto({ ...p.layer.servers });
|
|
477
|
-
if (empty) delete servers[name];
|
|
478
|
-
else servers[name] = next;
|
|
479
|
-
const w = write(p.path, p.layer.config, servers);
|
|
480
|
-
return w.ok ? { ok: true, override: empty ? undefined : { ...next } } : w;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
function setProjectOverride(name: string, patch: ProjectOverridePatch, scope: Scope): ProjectOverrideResult {
|
|
484
|
-
const plan = planOverride(name, patch, scope);
|
|
485
|
-
if (!plan.ok) return plan;
|
|
486
|
-
const next = patchedOverride(plan.existing, patch);
|
|
487
|
-
// No key change touches no file (and creates none).
|
|
488
|
-
if (deepEqual({ ...(plan.existing ?? {}) }, { ...next })) {
|
|
489
|
-
return { ok: true, override: plan.existing === undefined ? undefined : { ...next } };
|
|
490
|
-
}
|
|
491
|
-
return commitOverride(name, plan.p, next, plan.kept);
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
function setEnabled(name: string, enabled: boolean, scope: Scope): SetEnabledResult {
|
|
495
|
-
const p = prepare(name, scope);
|
|
496
|
-
if (!p.ok) return p;
|
|
497
|
-
const existing = existingObject(p, name);
|
|
498
|
-
if (!existing.ok) return existing;
|
|
499
|
-
|
|
500
|
-
// The global entry the folder layer shadows or overrides (project scope only).
|
|
501
|
-
let globalRaw: unknown;
|
|
502
|
-
if (scope.kind === "project") {
|
|
503
|
-
const g = readLayer(configIO, paths.globalPath());
|
|
504
|
-
globalRaw = g.ok ? g.servers[name] : undefined;
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
// A whole-entry layer entry (any scope): toggle its own `enabled` key.
|
|
508
|
-
if (existing.entry !== undefined && !(scope.kind === "project" && isProjectOverride(existing.entry))) {
|
|
509
|
-
const next = existing.entry;
|
|
510
|
-
if (enabled) {
|
|
511
|
-
// Enabling a folder copy that lacks the global entry's secrets would
|
|
512
|
-
// connect without credentials: let the caller choose instead.
|
|
513
|
-
if (isPlainObject(globalRaw) && next.enabled === false) {
|
|
514
|
-
const missing = folderCopyOf(globalRaw).omitted.filter((f) => !hasPath(next, f));
|
|
515
|
-
if (missing.length > 0) return { ok: true, action: "needs-choice", omitted: missing };
|
|
516
|
-
}
|
|
517
|
-
delete next.enabled;
|
|
518
|
-
} else {
|
|
519
|
-
next.enabled = false;
|
|
520
|
-
}
|
|
521
|
-
const servers = nullProto({ ...p.layer.servers });
|
|
522
|
-
servers[name] = next;
|
|
523
|
-
const w = write(p.path, p.layer.config, servers);
|
|
524
|
-
return w.ok ? { ok: true, action: "written" } : w;
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
if (scope.kind === "project" && (existing.entry !== undefined || globalRaw !== undefined)) {
|
|
528
|
-
// A global server (with or without an override): write only the smallest override.
|
|
529
|
-
const kb = keptBase(name);
|
|
530
|
-
if (!kb.ok) return kb;
|
|
531
|
-
const globalOn = kb.base.enabled !== false;
|
|
532
|
-
const r = setProjectOverride(name, { enabled: enabled === globalOn ? null : enabled }, scope);
|
|
533
|
-
return r.ok ? { ok: true, action: "written" } : r;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
return {
|
|
537
|
-
ok: false,
|
|
538
|
-
refusal: { code: "invalid-entry", message: `no MCP server named "${name}" in ${p.path}`, path: p.path },
|
|
539
|
-
};
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
/** The override a legacy copy converts to, or the refusal (not a legacy copy / invalid override keys). */
|
|
543
|
-
function legacyConversion(
|
|
544
|
-
name: string,
|
|
545
|
-
p: Extract<Prepared, { ok: true }>,
|
|
546
|
-
folder: Record<string, unknown> | undefined,
|
|
547
|
-
kb: { base: Record<string, unknown>; kept: ReadonlyMap<string, Record<string, unknown>> },
|
|
548
|
-
): { ok: true; next: Record<string, unknown> } | { ok: false; refusal: ConfigRefusal } {
|
|
549
|
-
if (folder === undefined || !isLegacyFolderCopy(p.layer.servers[name], kb.base)) {
|
|
550
|
-
return {
|
|
551
|
-
ok: false,
|
|
552
|
-
refusal: { code: "invalid-entry", message: `server "${name}" in ${p.path} is not a legacy copy of the global entry`, path: p.path },
|
|
553
|
-
};
|
|
554
|
-
}
|
|
555
|
-
const next = convertedOverride(folder, kb.base);
|
|
556
|
-
const invalid = Object.keys(next).length === 0 ? null : mergedOverrideRefusal(name, next, kb.kept, p.path);
|
|
557
|
-
return invalid ? { ok: false, refusal: invalid } : { ok: true, next };
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
function convertLegacyCopy(name: string, scope: Scope): ConvertLegacyResult {
|
|
561
|
-
if (scope.kind !== "project") {
|
|
562
|
-
return { ok: false, refusal: { code: "invalid-entry", message: "a legacy folder copy can only be converted at project scope" } };
|
|
563
|
-
}
|
|
564
|
-
const p = prepare(name, scope);
|
|
565
|
-
if (!p.ok) return p;
|
|
566
|
-
const existing = existingObject(p, name);
|
|
567
|
-
if (!existing.ok) return existing;
|
|
568
|
-
const kb = keptBase(name);
|
|
569
|
-
if (!kb.ok) return kb;
|
|
570
|
-
const plan = legacyConversion(name, p, existing.entry, kb);
|
|
571
|
-
if (!plan.ok) return plan;
|
|
572
|
-
const servers = nullProto({ ...p.layer.servers });
|
|
573
|
-
if (Object.keys(plan.next).length === 0) delete servers[name];
|
|
574
|
-
else servers[name] = plan.next;
|
|
575
|
-
const w = write(p.path, p.layer.config, servers);
|
|
576
|
-
return w.ok ? { ok: true, previous: p.layer.servers[name] as ServerEntry } : w;
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
function convertAdapterLeftovers(name: string, scope: Scope): ConfigWriteResult {
|
|
580
|
-
const p = prepare(name, scope);
|
|
581
|
-
if (!p.ok) return p;
|
|
582
|
-
const existing = existingObject(p, name);
|
|
583
|
-
if (!existing.ok) return existing;
|
|
584
|
-
const next = existing.entry;
|
|
585
|
-
if (next === undefined) {
|
|
586
|
-
return {
|
|
587
|
-
ok: false,
|
|
588
|
-
refusal: { code: "invalid-entry", message: `no MCP server named "${name}" in ${p.path}`, path: p.path },
|
|
589
|
-
};
|
|
590
|
-
}
|
|
591
|
-
if (next.disabled === true && next.enabled === undefined) next.enabled = false;
|
|
592
|
-
for (const k of ADAPTER_ONLY_KEYS) delete next[k];
|
|
593
|
-
if (next.auth !== undefined && !isPlainObject(next.auth)) delete next.auth;
|
|
594
|
-
if (next.oauth === false) delete next.oauth;
|
|
595
|
-
const servers = nullProto({ ...p.layer.servers });
|
|
596
|
-
servers[name] = next;
|
|
597
|
-
return write(p.path, p.layer.config, servers);
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
function readServerEntry(name: string, scope: Scope): ServerEntry | undefined {
|
|
601
|
-
if (!isValidServerName(name)) return undefined;
|
|
602
|
-
const l = readLayer(configIO, targetPath(scope));
|
|
603
|
-
if (!l.ok) return undefined;
|
|
604
|
-
const raw = l.servers[name];
|
|
605
|
-
return raw === undefined ? undefined : (raw as ServerEntry);
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
function readParseStatus(path: string): ParseStatus {
|
|
609
|
-
const l = readLayer(configIO, path);
|
|
610
|
-
return l.ok ? { path, ok: true } : { path, ok: false, message: l.message };
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
return {
|
|
614
|
-
targetPath,
|
|
615
|
-
readServerEntry,
|
|
616
|
-
ensureServerEntry,
|
|
617
|
-
saveServer,
|
|
618
|
-
removeServer,
|
|
619
|
-
setEnabled,
|
|
620
|
-
setProjectOverride,
|
|
621
|
-
convertLegacyCopy,
|
|
622
|
-
convertAdapterLeftovers,
|
|
623
|
-
previewEnsure,
|
|
624
|
-
readParseStatus,
|
|
625
|
-
};
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
function hasPath(entry: Record<string, unknown>, dotted: string): boolean {
|
|
629
|
-
const [head, tail] = dotted.split(".");
|
|
630
|
-
const v = entry[head];
|
|
631
|
-
if (tail === undefined) return v !== undefined;
|
|
632
|
-
return isPlainObject(v) && v[tail] !== undefined;
|
|
633
|
-
}
|
|
634
|
-
|