@blackbelt-technology/pi-dashboard-apple-tools 0.8.0 → 0.10.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/.pi/skills/apple-tools/SKILL.md +8 -7
- package/README.md +16 -11
- package/package.json +11 -8
- package/src/bin/install.ts +4 -4
- package/src/client/AppleToolsSettings.tsx +403 -0
- package/src/client/index.tsx +7 -198
- package/src/env.ts +26 -76
- package/src/install.ts +38 -33
- package/src/server/index.ts +68 -109
- package/src/mcp-config.ts +0 -285
package/src/server/index.ts
CHANGED
|
@@ -2,22 +2,29 @@
|
|
|
2
2
|
* apple-tools · SERVER entry.
|
|
3
3
|
*
|
|
4
4
|
* Exposes the iMCP provisioning state (shared write-suppressed checker) and the
|
|
5
|
-
* run-installer action to the dashboard.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* run-installer action to the dashboard. Consumes the `mcp-client.config`
|
|
6
|
+
* service provided by the `mcp-client` plugin (declared via `dependsOn`) for
|
|
7
|
+
* the mcp.json readout; owns reconciliation of a discovered non-default path
|
|
8
|
+
* into the server-owned plugin config store (Decision 1).
|
|
9
9
|
*
|
|
10
|
-
*
|
|
10
|
+
* The MCP server enable/disable + exposure controls live in the mcp-client
|
|
11
|
+
* plugin's own generic surface; this plugin no longer writes mcp.json.
|
|
12
|
+
*
|
|
13
|
+
* See changes: add-apple-tools-imcp-plugin, extract-mcp-client-plugin,
|
|
14
|
+
* migrate-mcp-to-pi-builtin.
|
|
11
15
|
*/
|
|
12
|
-
import { join } from "node:path";
|
|
13
16
|
import type { ServerPluginContext } from "@blackbelt-technology/dashboard-plugin-runtime/server";
|
|
17
|
+
import {
|
|
18
|
+
displayExposure,
|
|
19
|
+
type McpClientConfigService,
|
|
20
|
+
} from "@blackbelt-technology/pi-dashboard-mcp-client-plugin/core";
|
|
14
21
|
import { createInstallerEnv } from "../env.js";
|
|
15
22
|
import { runInstaller, type TerminalState } from "../install.js";
|
|
16
|
-
import { readImcpEntry, setDirectTools, setServerDisabled } from "../mcp-config.js";
|
|
17
23
|
import { DEFAULT_IMCP_PATH, shouldReconcilePath } from "../reconcile.js";
|
|
18
24
|
|
|
19
25
|
const PLUGIN_ID = "apple-tools";
|
|
20
|
-
|
|
26
|
+
/** The service key the `mcp-client` plugin provides. */
|
|
27
|
+
const SERVICE_KEY = "mcp-client.config";
|
|
21
28
|
/** Status readout TTL — the traversal shells out to `sw_vers`/`which`, so a
|
|
22
29
|
* polling panel must not re-run it per request. Mirrors the 30s requirement-
|
|
23
30
|
* probe cache. */
|
|
@@ -34,12 +41,12 @@ interface StatusReadout {
|
|
|
34
41
|
resolvedPath?: string;
|
|
35
42
|
imcpServerPath: string;
|
|
36
43
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
44
|
+
* pi built-in MCP fields of the iMCP entry, read from the Pi-global
|
|
45
|
+
* `mcp.json` through the mcp-client service (the source of truth) rather
|
|
46
|
+
* than our plugin config. `exposure` is the display value (default codemode).
|
|
40
47
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
exposure: string;
|
|
49
|
+
enabled: boolean;
|
|
43
50
|
/**
|
|
44
51
|
* True when iMCP.app is actually on disk. The dashboard can only perform the
|
|
45
52
|
* fast config-write half of provisioning; when this is false the operator
|
|
@@ -52,18 +59,14 @@ interface StatusReadout {
|
|
|
52
59
|
export async function registerPlugin(ctx: ServerPluginContext): Promise<void> {
|
|
53
60
|
ctx.logger.info("apple-tools server entry activated");
|
|
54
61
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
.filter((c): c is string => typeof c === "string" && c.length > 0);
|
|
64
|
-
};
|
|
65
|
-
const isAllowedCwd = (cwd: string | undefined): cwd is string =>
|
|
66
|
-
typeof cwd === "string" && cwd.length > 0 && knownCwds().includes(cwd);
|
|
62
|
+
const consumed = ctx.consume<McpClientConfigService>(SERVICE_KEY);
|
|
63
|
+
if (!consumed) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
"apple-tools requires the `mcp-client.config` service (plugin id `mcp-client`); " +
|
|
66
|
+
"ensure it is enabled and loaded before apple-tools (`dependsOn: [\"mcp-client\"]`).",
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
const mcpClient: McpClientConfigService = consumed;
|
|
67
70
|
|
|
68
71
|
let statusCache: { at: number; value: StatusReadout } | null = null;
|
|
69
72
|
|
|
@@ -74,15 +77,16 @@ export async function registerPlugin(ctx: ServerPluginContext): Promise<void> {
|
|
|
74
77
|
...(configured ? { overridePath: configured } : {}),
|
|
75
78
|
});
|
|
76
79
|
const result = runInstaller(env, { check: true });
|
|
77
|
-
const entry =
|
|
80
|
+
const entry = mcpClient.readServerEntry("iMCP", { kind: "global" });
|
|
78
81
|
return {
|
|
79
82
|
platform: env.platform,
|
|
80
83
|
state: result.state,
|
|
81
84
|
message: result.message,
|
|
82
85
|
...(result.resolvedPath ? { resolvedPath: result.resolvedPath } : {}),
|
|
83
86
|
imcpServerPath: configured ?? DEFAULT_IMCP_PATH,
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
exposure: displayExposure(entry?.exposure),
|
|
88
|
+
// pi: only a literal `enabled: false` disables.
|
|
89
|
+
enabled: entry?.enabled !== false,
|
|
86
90
|
// Reported by the traversal itself: false when the state is a prediction.
|
|
87
91
|
appPresent: result.appPresent,
|
|
88
92
|
};
|
|
@@ -109,91 +113,46 @@ export async function registerPlugin(ctx: ServerPluginContext): Promise<void> {
|
|
|
109
113
|
// run-installer action instead.
|
|
110
114
|
ctx.fastify.get(`/api/${PLUGIN_ID}/status`, async () => cachedStatus());
|
|
111
115
|
|
|
112
|
-
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: flat action switch.
|
|
113
116
|
ctx.registerBrowserHandler("plugin_action", (msg) => {
|
|
114
117
|
const m = msg as { pluginId?: string; action?: string; payload?: Record<string, unknown> };
|
|
115
118
|
if (m.pluginId !== PLUGIN_ID) return;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
const result = runInstaller(env, { check: false });
|
|
144
|
-
ctx.logger.info(`apple-tools run-installer → ${result.state}`);
|
|
145
|
-
statusCache = null; // invalidate on mutation (#F7)
|
|
146
|
-
// Pass only what reconcile reads. Calling cachedStatus() here would
|
|
147
|
-
// miss the just-cleared cache, run a THIRD sync traversal (sw_vers +
|
|
148
|
-
// which) on the event loop, discard the result, and re-seed the cache
|
|
149
|
-
// with a pre-reconcile readout served for the next 10s.
|
|
150
|
-
reconcile(result.resolvedPath).catch((e) =>
|
|
151
|
-
ctx.logger.warn(`apple-tools reconcile failed: ${(e as Error).message}`),
|
|
152
|
-
);
|
|
153
|
-
break;
|
|
154
|
-
}
|
|
155
|
-
case "set-disabled": {
|
|
156
|
-
// Enable/disable the iMCP server. BOTH levels are supported:
|
|
157
|
-
// scope "global" → ~/.pi/agent/mcp.json (default; the global panel)
|
|
158
|
-
// scope "project" → <cwd>/.pi/mcp.json (highest-precedence override)
|
|
159
|
-
const payload = m.payload ?? {};
|
|
160
|
-
const disabled = payload.disabled === true;
|
|
161
|
-
const scope = payload.scope === "project" ? "project" : "global";
|
|
162
|
-
const env = createInstallerEnv();
|
|
163
|
-
let target: string;
|
|
164
|
-
if (scope === "project") {
|
|
165
|
-
const cwd = typeof payload.cwd === "string" ? payload.cwd : undefined;
|
|
166
|
-
// Browser-supplied write path — it MUST be a known folder cwd, else
|
|
167
|
-
// this is an arbitrary mkdir -p + file write as the server user.
|
|
168
|
-
if (!isAllowedCwd(cwd)) {
|
|
169
|
-
ctx.logger.warn(`apple-tools set-disabled: cwd not allowed (${cwd ?? "missing"})`);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
target = join(cwd, ".pi", "mcp.json");
|
|
173
|
-
} else {
|
|
174
|
-
target = env.mcpJsonPath;
|
|
175
|
-
}
|
|
176
|
-
const r = setServerDisabled(env.configIO, target, disabled);
|
|
177
|
-
if (!r.ok) ctx.logger.warn(`apple-tools set-disabled failed: ${r.message}`);
|
|
178
|
-
else ctx.logger.info(`apple-tools set-disabled scope=${scope} disabled=${disabled}`);
|
|
179
|
-
statusCache = null;
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
case "set-direct-tools": {
|
|
183
|
-
// Adapter-owned per-server `directTools` filter on the global entry.
|
|
184
|
-
const payload = m.payload ?? {};
|
|
185
|
-
const tools = Array.isArray(payload.tools)
|
|
186
|
-
? (payload.tools as unknown[]).filter((t): t is string => typeof t === "string")
|
|
187
|
-
: [];
|
|
188
|
-
const env = createInstallerEnv();
|
|
189
|
-
const r = setDirectTools(env.configIO, env.mcpJsonPath, tools);
|
|
190
|
-
if (!r.ok) ctx.logger.warn(`apple-tools set-direct-tools failed: ${r.message}`);
|
|
191
|
-
statusCache = null;
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
default:
|
|
195
|
-
break;
|
|
119
|
+
if (m.action !== "run-installer") return;
|
|
120
|
+
|
|
121
|
+
const cfg = ctx.getPluginConfig<AppleToolsConfig>() ?? {};
|
|
122
|
+
const env = createInstallerEnv({
|
|
123
|
+
...(cfg.imcpServerPath ? { overridePath: cfg.imcpServerPath } : {}),
|
|
124
|
+
});
|
|
125
|
+
// The state machine is synchronous by design (pure + unit-testable).
|
|
126
|
+
// Running its INSTALL branch here would `execFileSync(brew, …)` with a
|
|
127
|
+
// 10-minute timeout on the Fastify event loop, freezing every session's
|
|
128
|
+
// WebSocket and every other plugin's HTTP for the duration.
|
|
129
|
+
//
|
|
130
|
+
// So the server only ever performs the FAST half of provisioning (the
|
|
131
|
+
// two config writes, which run when iMCP is already on disk). When the
|
|
132
|
+
// app is absent — the only branch that shells out to brew — it refuses
|
|
133
|
+
// and directs the operator to the CLI, which owns the long, network-
|
|
134
|
+
// bound install. See the security/perf review of this change.
|
|
135
|
+
const probe = runInstaller(env, { check: true });
|
|
136
|
+
if (!probe.appPresent) {
|
|
137
|
+
// The panel reads `appPresent` from the status readout and renders the
|
|
138
|
+
// CLI instruction instead of the button, so this is defence in depth.
|
|
139
|
+
ctx.logger.warn(
|
|
140
|
+
"apple-tools run-installer: iMCP is not installed. Run `pi-apple-tools-install` " +
|
|
141
|
+
"in a terminal — the dashboard does not run `brew` in-process.",
|
|
142
|
+
);
|
|
143
|
+
statusCache = null;
|
|
144
|
+
return;
|
|
196
145
|
}
|
|
146
|
+
const result = runInstaller(env, { check: false });
|
|
147
|
+
ctx.logger.info(`apple-tools run-installer → ${result.state}`);
|
|
148
|
+
statusCache = null; // invalidate on mutation (#F7)
|
|
149
|
+
// Pass only what reconcile reads. Calling cachedStatus() here would
|
|
150
|
+
// miss the just-cleared cache, run a THIRD sync traversal (sw_vers +
|
|
151
|
+
// which) on the event loop, discard the result, and re-seed the cache
|
|
152
|
+
// with a pre-reconcile readout served for the next 10s.
|
|
153
|
+
reconcile(result.resolvedPath).catch((e) =>
|
|
154
|
+
ctx.logger.warn(`apple-tools reconcile failed: ${(e as Error).message}`),
|
|
155
|
+
);
|
|
197
156
|
});
|
|
198
157
|
}
|
|
199
158
|
|
package/src/mcp-config.ts
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Merge-only, atomic config writers for the two user-owned files the installer
|
|
3
|
-
* touches: `~/.pi/agent/mcp.json` (the `mcpServers.iMCP` entry) and
|
|
4
|
-
* `~/.pi/agent/settings.json` (the `packages[]` adapter entry).
|
|
5
|
-
*
|
|
6
|
-
* Discipline (see change: add-apple-tools-imcp-plugin, Decision 4):
|
|
7
|
-
* - read → deep-merge exactly one key → write. Never clobber siblings.
|
|
8
|
-
* - refuse to write a present-but-unparseable file (surface, don't "fix").
|
|
9
|
-
* - write via temp-file + atomic rename; never leave a truncated config.
|
|
10
|
-
* - never interpolate any probed value into a shell string.
|
|
11
|
-
*
|
|
12
|
-
* All filesystem access is injected (`ConfigIO`) so the suite runs on Linux CI
|
|
13
|
-
* without touching real user config.
|
|
14
|
-
*/
|
|
15
|
-
import { sourcesMatch } from "@blackbelt-technology/pi-dashboard-shared/source-matching.js";
|
|
16
|
-
|
|
17
|
-
/** Injected filesystem surface. `readFile` returns null when the file is absent. */
|
|
18
|
-
export interface ConfigIO {
|
|
19
|
-
readFile: (path: string) => string | null;
|
|
20
|
-
/** Atomic write (temp-file + rename). Throws an Error whose `.code` may be EACCES/ENOSPC. */
|
|
21
|
-
writeFileAtomic: (path: string, content: string) => void;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/** Outcome of a config write; `state` is set only on a terminal failure. */
|
|
25
|
-
export type ConfigWriteResult =
|
|
26
|
-
| { ok: true }
|
|
27
|
-
| { ok: false; state: "CONFIG_UNPARSEABLE" | "CONFIG_WRITE_FAILED"; message: string };
|
|
28
|
-
|
|
29
|
-
/** The npm source string appended to settings.json packages[] for the adapter. */
|
|
30
|
-
export const ADAPTER_PACKAGE_SOURCE = "npm:pi-mcp-adapter";
|
|
31
|
-
|
|
32
|
-
function parseOrError(
|
|
33
|
-
io: ConfigIO,
|
|
34
|
-
path: string,
|
|
35
|
-
): { parsed: Record<string, unknown> } | { error: ConfigWriteResult } {
|
|
36
|
-
const raw = io.readFile(path);
|
|
37
|
-
if (raw === null || raw.trim() === "") return { parsed: {} };
|
|
38
|
-
try {
|
|
39
|
-
const val = JSON.parse(raw);
|
|
40
|
-
if (val === null || typeof val !== "object" || Array.isArray(val)) {
|
|
41
|
-
return {
|
|
42
|
-
error: {
|
|
43
|
-
ok: false,
|
|
44
|
-
state: "CONFIG_UNPARSEABLE",
|
|
45
|
-
message: `${path} is not a JSON object`,
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
return { parsed: val as Record<string, unknown> };
|
|
50
|
-
} catch (e) {
|
|
51
|
-
return {
|
|
52
|
-
error: {
|
|
53
|
-
ok: false,
|
|
54
|
-
state: "CONFIG_UNPARSEABLE",
|
|
55
|
-
message: `${path} contains invalid JSON: ${(e as Error).message}`,
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Read `mcpServers` as an object. A present-but-wrong-typed value (array,
|
|
63
|
-
* string) is refused rather than coerced to `{}` — coercing would silently
|
|
64
|
-
* drop the user's data on the next write.
|
|
65
|
-
*/
|
|
66
|
-
function readServers(
|
|
67
|
-
config: Record<string, unknown>,
|
|
68
|
-
path: string,
|
|
69
|
-
): { servers: Record<string, unknown> } | { error: ConfigWriteResult } {
|
|
70
|
-
const raw = config.mcpServers;
|
|
71
|
-
if (raw === undefined) return { servers: {} };
|
|
72
|
-
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
73
|
-
return {
|
|
74
|
-
error: {
|
|
75
|
-
ok: false,
|
|
76
|
-
state: "CONFIG_UNPARSEABLE",
|
|
77
|
-
message: `${path}: "mcpServers" must be an object`,
|
|
78
|
-
},
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
return { servers: raw as Record<string, unknown> };
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Read the existing `mcpServers.iMCP` entry. A present-but-wrong-typed value
|
|
86
|
-
* (scalar, null, array) is REFUSED rather than coerced to `{}` — coercing would
|
|
87
|
-
* destroy the operator's parseable value on the next write.
|
|
88
|
-
*/
|
|
89
|
-
function readExistingImcp(
|
|
90
|
-
servers: Record<string, unknown>,
|
|
91
|
-
path: string,
|
|
92
|
-
): { existing: Record<string, unknown> } | { error: ConfigWriteResult } {
|
|
93
|
-
const raw = servers.iMCP;
|
|
94
|
-
if (raw === undefined) return { existing: {} };
|
|
95
|
-
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
96
|
-
return {
|
|
97
|
-
error: {
|
|
98
|
-
ok: false,
|
|
99
|
-
state: "CONFIG_UNPARSEABLE",
|
|
100
|
-
message: `${path}: "mcpServers.iMCP" must be an object`,
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
return { existing: raw as Record<string, unknown> };
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function writeOrError(io: ConfigIO, path: string, obj: unknown): ConfigWriteResult {
|
|
108
|
-
try {
|
|
109
|
-
io.writeFileAtomic(path, `${JSON.stringify(obj, null, 2)}\n`);
|
|
110
|
-
return { ok: true };
|
|
111
|
-
} catch (e) {
|
|
112
|
-
return {
|
|
113
|
-
ok: false,
|
|
114
|
-
state: "CONFIG_WRITE_FAILED",
|
|
115
|
-
message: `failed to write ${path}: ${(e as Error).message}`,
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Ensure `mcpServers.iMCP = { command: <imcpServerPath> }` in mcp.json,
|
|
122
|
-
* preserving every other server and unknown top-level key. imcp-server is a
|
|
123
|
-
* stdio server that takes no args — `command` alone is the full contract
|
|
124
|
-
* (verified against pi-mcp-adapter's documented mcpServers shape).
|
|
125
|
-
*/
|
|
126
|
-
export function ensureMcpEntry(
|
|
127
|
-
io: ConfigIO,
|
|
128
|
-
mcpJsonPath: string,
|
|
129
|
-
imcpServerPath: string,
|
|
130
|
-
): ConfigWriteResult {
|
|
131
|
-
const r = parseOrError(io, mcpJsonPath);
|
|
132
|
-
if ("error" in r) return r.error;
|
|
133
|
-
const config = r.parsed;
|
|
134
|
-
const sr = readServers(config, mcpJsonPath);
|
|
135
|
-
if ("error" in sr) return sr.error;
|
|
136
|
-
const { servers } = sr;
|
|
137
|
-
const er = readExistingImcp(servers, mcpJsonPath);
|
|
138
|
-
if ("error" in er) return er.error;
|
|
139
|
-
const { existing } = er;
|
|
140
|
-
// Merge-only: preserve any operator-added fields on the iMCP entry (e.g. a
|
|
141
|
-
// `disabled` flag), overriding just `command` with the re-discovered path.
|
|
142
|
-
const merged = { ...existing, command: imcpServerPath };
|
|
143
|
-
const next = { ...config, mcpServers: { ...servers, iMCP: merged } };
|
|
144
|
-
return writeOrError(io, mcpJsonPath, next);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Append the adapter to settings.json `packages[]` if absent. Presence is
|
|
149
|
-
* detected with the cross-kind `sourcesMatch()` (npm ↔ git ↔ raw), so a
|
|
150
|
-
* user who installed pi-mcp-adapter from git receives no duplicate npm entry.
|
|
151
|
-
* Never reorders or removes existing entries.
|
|
152
|
-
*/
|
|
153
|
-
export function ensureAdapterPackage(io: ConfigIO, settingsJsonPath: string): ConfigWriteResult {
|
|
154
|
-
const r = parseOrError(io, settingsJsonPath);
|
|
155
|
-
if ("error" in r) return r.error;
|
|
156
|
-
const config = r.parsed;
|
|
157
|
-
if (config.packages !== undefined && !Array.isArray(config.packages)) {
|
|
158
|
-
return {
|
|
159
|
-
ok: false,
|
|
160
|
-
state: "CONFIG_UNPARSEABLE",
|
|
161
|
-
message: `${settingsJsonPath}: "packages" must be an array`,
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
const packages = Array.isArray(config.packages) ? (config.packages as unknown[]) : [];
|
|
165
|
-
const already = packages.some(
|
|
166
|
-
(p) => typeof p === "string" && sourcesMatch(p, ADAPTER_PACKAGE_SOURCE),
|
|
167
|
-
);
|
|
168
|
-
if (already) return { ok: true };
|
|
169
|
-
const next = { ...config, packages: [...packages, ADAPTER_PACKAGE_SOURCE] };
|
|
170
|
-
return writeOrError(io, settingsJsonPath, next);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Read-only structural validation of a config file, using the SAME rules the
|
|
175
|
-
* writers enforce. Check mode calls this so `--check` can never report a
|
|
176
|
-
* healthy state that write mode would fail on (parity: invalid JSON, non-object
|
|
177
|
-
* root, malformed `mcpServers` / `mcpServers.iMCP` / `packages`).
|
|
178
|
-
* Returns null when the file would be writable.
|
|
179
|
-
*/
|
|
180
|
-
export function validateConfigShape(
|
|
181
|
-
io: ConfigIO,
|
|
182
|
-
path: string,
|
|
183
|
-
kind: "mcp" | "settings",
|
|
184
|
-
): Extract<ConfigWriteResult, { ok: false }> | null {
|
|
185
|
-
const r = parseOrError(io, path);
|
|
186
|
-
if ("error" in r) return r.error as Extract<ConfigWriteResult, { ok: false }>;
|
|
187
|
-
if (kind === "mcp") {
|
|
188
|
-
const sr = readServers(r.parsed, path);
|
|
189
|
-
if ("error" in sr) return sr.error as Extract<ConfigWriteResult, { ok: false }>;
|
|
190
|
-
const er = readExistingImcp(sr.servers, path);
|
|
191
|
-
if ("error" in er) return er.error as Extract<ConfigWriteResult, { ok: false }>;
|
|
192
|
-
} else if (r.parsed.packages !== undefined && !Array.isArray(r.parsed.packages)) {
|
|
193
|
-
return {
|
|
194
|
-
ok: false,
|
|
195
|
-
state: "CONFIG_UNPARSEABLE",
|
|
196
|
-
message: `${path}: "packages" must be an array`,
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
return null;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/** Read the iMCP entry's operator-facing fields from a given mcp.json layer. */
|
|
203
|
-
export function readImcpEntry(
|
|
204
|
-
io: ConfigIO,
|
|
205
|
-
mcpJsonPath: string,
|
|
206
|
-
): { disabled: boolean; directTools: string[] } {
|
|
207
|
-
const r = parseOrError(io, mcpJsonPath);
|
|
208
|
-
if ("error" in r) return { disabled: false, directTools: [] };
|
|
209
|
-
const servers = r.parsed.mcpServers;
|
|
210
|
-
if (!servers || typeof servers !== "object" || Array.isArray(servers)) {
|
|
211
|
-
return { disabled: false, directTools: [] };
|
|
212
|
-
}
|
|
213
|
-
const entry = (servers as Record<string, unknown>).iMCP;
|
|
214
|
-
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
|
|
215
|
-
return { disabled: false, directTools: [] };
|
|
216
|
-
}
|
|
217
|
-
const e = entry as Record<string, unknown>;
|
|
218
|
-
return {
|
|
219
|
-
// pi-mcp-adapter's isServerDisabled: only a literal `true` disables.
|
|
220
|
-
disabled: e.disabled === true,
|
|
221
|
-
directTools: Array.isArray(e.directTools)
|
|
222
|
-
? (e.directTools as unknown[]).filter((t): t is string => typeof t === "string")
|
|
223
|
-
: [],
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/** Merge a patch into `mcpServers.iMCP`; an `undefined` value removes the key. */
|
|
228
|
-
function patchImcpEntry(
|
|
229
|
-
io: ConfigIO,
|
|
230
|
-
mcpJsonPath: string,
|
|
231
|
-
patch: Record<string, unknown>,
|
|
232
|
-
): ConfigWriteResult {
|
|
233
|
-
const r = parseOrError(io, mcpJsonPath);
|
|
234
|
-
if ("error" in r) return r.error;
|
|
235
|
-
const config = r.parsed;
|
|
236
|
-
const sr = readServers(config, mcpJsonPath);
|
|
237
|
-
if ("error" in sr) return sr.error;
|
|
238
|
-
const { servers } = sr;
|
|
239
|
-
const er = readExistingImcp(servers, mcpJsonPath);
|
|
240
|
-
if ("error" in er) return er.error;
|
|
241
|
-
const merged: Record<string, unknown> = { ...er.existing };
|
|
242
|
-
for (const [k, v] of Object.entries(patch)) {
|
|
243
|
-
if (v === undefined) delete merged[k];
|
|
244
|
-
else merged[k] = v;
|
|
245
|
-
}
|
|
246
|
-
const next = { ...config, mcpServers: { ...servers, iMCP: merged } };
|
|
247
|
-
return writeOrError(io, mcpJsonPath, next);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Write the iMCP `disabled` override into a given mcp.json layer. BOTH levels
|
|
252
|
-
* are supported and use the identical merge discipline:
|
|
253
|
-
* - global → `~/.pi/agent/mcp.json` (the layer the installer writes `command`
|
|
254
|
-
* to; the target for the dashboard's global settings panel)
|
|
255
|
-
* - project → `<cwd>/.pi/mcp.json` (the adapter's highest-precedence layer,
|
|
256
|
-
* so a project override folds over the global value without mutating it)
|
|
257
|
-
*
|
|
258
|
-
* Mirrors `writeProjectServerDisabledOverride` in the installed pi-mcp-adapter
|
|
259
|
-
* (`config.ts`): disabling writes `disabled: true`; ENABLING removes the key
|
|
260
|
-
* rather than writing `false`. `isServerDisabled` treats only a literal `true`
|
|
261
|
-
* as disabled (`types.ts:395`). Verified against adapter v2.19.0 (task 7.8).
|
|
262
|
-
*/
|
|
263
|
-
export function setServerDisabled(
|
|
264
|
-
io: ConfigIO,
|
|
265
|
-
mcpJsonPath: string,
|
|
266
|
-
disabled: boolean,
|
|
267
|
-
): ConfigWriteResult {
|
|
268
|
-
return patchImcpEntry(io, mcpJsonPath, { disabled: disabled ? true : undefined });
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Write the adapter's per-server `directTools` filter onto the iMCP entry
|
|
273
|
-
* (`ServerEntry.directTools?: boolean | string[]`, consumed by the adapter's
|
|
274
|
-
* `direct-tools.ts`). An empty selection removes the key rather than writing
|
|
275
|
-
* `[]`, which the adapter would read as "promote nothing".
|
|
276
|
-
*/
|
|
277
|
-
export function setDirectTools(
|
|
278
|
-
io: ConfigIO,
|
|
279
|
-
mcpJsonPath: string,
|
|
280
|
-
tools: string[],
|
|
281
|
-
): ConfigWriteResult {
|
|
282
|
-
return patchImcpEntry(io, mcpJsonPath, {
|
|
283
|
-
directTools: tools.length > 0 ? tools : undefined,
|
|
284
|
-
});
|
|
285
|
-
}
|