@blackbelt-technology/pi-dashboard-apple-tools 0.7.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 +68 -0
- package/README.md +61 -0
- package/config.schema.json +13 -0
- package/package.json +93 -0
- package/src/bin/install.ts +31 -0
- package/src/client/index.tsx +202 -0
- package/src/detect.ts +83 -0
- package/src/doctor.ts +50 -0
- package/src/env.ts +146 -0
- package/src/install.ts +244 -0
- package/src/mcp-config.ts +285 -0
- package/src/reconcile.ts +29 -0
- package/src/server/index.ts +200 -0
package/src/reconcile.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side path reconciliation (Decision 1). When the server's check
|
|
3
|
+
* discovers imcp-server at a non-default candidate, persist it to the
|
|
4
|
+
* `imcpServerPath` config key — but ONLY when the configured value is unset or
|
|
5
|
+
* still at the schema default. An explicit operator override is never
|
|
6
|
+
* overwritten (its file may legitimately be absent at check time).
|
|
7
|
+
*
|
|
8
|
+
* Pure function so the guard is unit-testable (#E33). The actual write is done
|
|
9
|
+
* by the caller via `updatePluginConfig` (server owns the store).
|
|
10
|
+
*
|
|
11
|
+
* See change: add-apple-tools-imcp-plugin.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** The canonical /Applications location, also the configSchema default. */
|
|
15
|
+
export const DEFAULT_IMCP_PATH = "/Applications/iMCP.app/Contents/MacOS/imcp-server";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Should the discovered path be written back to `imcpServerPath`?
|
|
19
|
+
* True only when discovery found a real, different path AND the configured
|
|
20
|
+
* value is unset/empty or still at the default. False for an explicit override.
|
|
21
|
+
*/
|
|
22
|
+
export function shouldReconcilePath(
|
|
23
|
+
configured: string | undefined,
|
|
24
|
+
discovered: string | null,
|
|
25
|
+
): discovered is string {
|
|
26
|
+
if (!discovered) return false;
|
|
27
|
+
if (discovered === configured) return false;
|
|
28
|
+
return configured === undefined || configured === "" || configured === DEFAULT_IMCP_PATH;
|
|
29
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* apple-tools · SERVER entry.
|
|
3
|
+
*
|
|
4
|
+
* Exposes the iMCP provisioning state (shared write-suppressed checker) and the
|
|
5
|
+
* run-installer action to the dashboard. Owns two things the CLI cannot:
|
|
6
|
+
* - server-side path reconciliation into the server-owned plugin config store
|
|
7
|
+
* (Decision 1) — persists a discovered non-default path to `imcpServerPath`,
|
|
8
|
+
* - the server enable/disable write to the project-local `.pi/mcp.json`.
|
|
9
|
+
*
|
|
10
|
+
* See change: add-apple-tools-imcp-plugin.
|
|
11
|
+
*/
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import type { ServerPluginContext } from "@blackbelt-technology/dashboard-plugin-runtime/server";
|
|
14
|
+
import { createInstallerEnv } from "../env.js";
|
|
15
|
+
import { runInstaller, type TerminalState } from "../install.js";
|
|
16
|
+
import { readImcpEntry, setDirectTools, setServerDisabled } from "../mcp-config.js";
|
|
17
|
+
import { DEFAULT_IMCP_PATH, shouldReconcilePath } from "../reconcile.js";
|
|
18
|
+
|
|
19
|
+
const PLUGIN_ID = "apple-tools";
|
|
20
|
+
const HOST_KNOWN_FOLDERS = "host.knownFolderCwds";
|
|
21
|
+
/** Status readout TTL — the traversal shells out to `sw_vers`/`which`, so a
|
|
22
|
+
* polling panel must not re-run it per request. Mirrors the 30s requirement-
|
|
23
|
+
* probe cache. */
|
|
24
|
+
const STATUS_TTL_MS = 10_000;
|
|
25
|
+
|
|
26
|
+
interface AppleToolsConfig {
|
|
27
|
+
imcpServerPath?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface StatusReadout {
|
|
31
|
+
platform: string;
|
|
32
|
+
state: TerminalState;
|
|
33
|
+
message: string;
|
|
34
|
+
resolvedPath?: string;
|
|
35
|
+
imcpServerPath: string;
|
|
36
|
+
/**
|
|
37
|
+
* Adapter-owned fields, read from `~/.pi/agent/mcp.json` (the source of
|
|
38
|
+
* truth) rather than our plugin config store — the adapter, not us, consumes
|
|
39
|
+
* them, and a project layer may override `disabled`.
|
|
40
|
+
*/
|
|
41
|
+
directTools: string[];
|
|
42
|
+
disabled: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* True when iMCP.app is actually on disk. The dashboard can only perform the
|
|
45
|
+
* fast config-write half of provisioning; when this is false the operator
|
|
46
|
+
* must run the CLI (which owns the long, network-bound brew install), so the
|
|
47
|
+
* panel surfaces that instead of offering a button that would refuse.
|
|
48
|
+
*/
|
|
49
|
+
appPresent: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function registerPlugin(ctx: ServerPluginContext): Promise<void> {
|
|
53
|
+
ctx.logger.info("apple-tools server entry activated");
|
|
54
|
+
|
|
55
|
+
// cwd allow-list for the project-local `disabled` write. Without this, a
|
|
56
|
+
// browser payload could name ANY directory and the writer would mkdir -p it.
|
|
57
|
+
// Same guard shape as kb-plugin's `isAllowedCwd`.
|
|
58
|
+
const hostKnown = ctx.consume<() => string[]>(HOST_KNOWN_FOLDERS);
|
|
59
|
+
const knownCwds = (): string[] => {
|
|
60
|
+
if (hostKnown) return hostKnown();
|
|
61
|
+
return (ctx.sessionManager.listAll() as Array<{ cwd?: string }>)
|
|
62
|
+
.map((s) => s.cwd)
|
|
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);
|
|
67
|
+
|
|
68
|
+
let statusCache: { at: number; value: StatusReadout } | null = null;
|
|
69
|
+
|
|
70
|
+
function computeStatus(): StatusReadout {
|
|
71
|
+
const cfg = ctx.getPluginConfig<AppleToolsConfig>() ?? {};
|
|
72
|
+
const configured = cfg.imcpServerPath;
|
|
73
|
+
const env = createInstallerEnv({
|
|
74
|
+
...(configured ? { overridePath: configured } : {}),
|
|
75
|
+
});
|
|
76
|
+
const result = runInstaller(env, { check: true });
|
|
77
|
+
const entry = readImcpEntry(env.configIO, env.mcpJsonPath);
|
|
78
|
+
return {
|
|
79
|
+
platform: env.platform,
|
|
80
|
+
state: result.state,
|
|
81
|
+
message: result.message,
|
|
82
|
+
...(result.resolvedPath ? { resolvedPath: result.resolvedPath } : {}),
|
|
83
|
+
imcpServerPath: configured ?? DEFAULT_IMCP_PATH,
|
|
84
|
+
directTools: entry.directTools,
|
|
85
|
+
disabled: entry.disabled,
|
|
86
|
+
// Reported by the traversal itself: false when the state is a prediction.
|
|
87
|
+
appPresent: result.appPresent,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Narrowed to the only value reconciliation needs — see the review note on
|
|
92
|
+
* the discarded `cachedStatus()` round-trip. */
|
|
93
|
+
async function reconcile(resolvedPath: string | undefined): Promise<void> {
|
|
94
|
+
const cfg = ctx.getPluginConfig<AppleToolsConfig>() ?? {};
|
|
95
|
+
if (shouldReconcilePath(cfg.imcpServerPath, resolvedPath ?? null)) {
|
|
96
|
+
await ctx.updatePluginConfig<AppleToolsConfig>({ imcpServerPath: resolvedPath });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function cachedStatus(now: number = Date.now()): StatusReadout {
|
|
101
|
+
if (statusCache && now - statusCache.at < STATUS_TTL_MS) return statusCache.value;
|
|
102
|
+
const value = computeStatus();
|
|
103
|
+
statusCache = { at: now, value };
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// GET is read-only (no reconciliation write — a prefetch/refresh must not
|
|
108
|
+
// mutate the server-owned config store). Reconciliation runs on the explicit
|
|
109
|
+
// run-installer action instead.
|
|
110
|
+
ctx.fastify.get(`/api/${PLUGIN_ID}/status`, async () => cachedStatus());
|
|
111
|
+
|
|
112
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: flat action switch.
|
|
113
|
+
ctx.registerBrowserHandler("plugin_action", (msg) => {
|
|
114
|
+
const m = msg as { pluginId?: string; action?: string; payload?: Record<string, unknown> };
|
|
115
|
+
if (m.pluginId !== PLUGIN_ID) return;
|
|
116
|
+
switch (m.action) {
|
|
117
|
+
case "run-installer": {
|
|
118
|
+
const cfg = ctx.getPluginConfig<AppleToolsConfig>() ?? {};
|
|
119
|
+
const env = createInstallerEnv({
|
|
120
|
+
...(cfg.imcpServerPath ? { overridePath: cfg.imcpServerPath } : {}),
|
|
121
|
+
});
|
|
122
|
+
// The state machine is synchronous by design (pure + unit-testable).
|
|
123
|
+
// Running its INSTALL branch here would `execFileSync(brew, …)` with a
|
|
124
|
+
// 10-minute timeout on the Fastify event loop, freezing every session's
|
|
125
|
+
// WebSocket and every other plugin's HTTP for the duration.
|
|
126
|
+
//
|
|
127
|
+
// So the server only ever performs the FAST half of provisioning (the
|
|
128
|
+
// two config writes, which run when iMCP is already on disk). When the
|
|
129
|
+
// app is absent — the only branch that shells out to brew — it refuses
|
|
130
|
+
// and directs the operator to the CLI, which owns the long, network-
|
|
131
|
+
// bound install. See the security/perf review of this change.
|
|
132
|
+
const probe = runInstaller(env, { check: true });
|
|
133
|
+
if (!probe.appPresent) {
|
|
134
|
+
// The panel reads `appPresent` from the status readout and renders the
|
|
135
|
+
// CLI instruction instead of the button, so this is defence in depth.
|
|
136
|
+
ctx.logger.warn(
|
|
137
|
+
"apple-tools run-installer: iMCP is not installed. Run `pi-apple-tools-install` " +
|
|
138
|
+
"in a terminal — the dashboard does not run `brew` in-process.",
|
|
139
|
+
);
|
|
140
|
+
statusCache = null;
|
|
141
|
+
break;
|
|
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;
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export default registerPlugin;
|