@aexol/spectral 0.2.3 → 0.2.6
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/dist/cli.js +18 -49
- package/dist/commands/login-oauth.js +116 -0
- package/dist/commands/serve.js +1 -0
- package/dist/config.js +5 -1
- package/dist/mcp/agent-dir.js +18 -0
- package/dist/mcp/app-bridge.bundle.js +67 -0
- package/dist/mcp/commands.js +263 -0
- package/dist/mcp/config.js +532 -0
- package/dist/mcp/consent-manager.js +59 -0
- package/dist/mcp/direct-tools.js +354 -0
- package/dist/mcp/errors.js +165 -0
- package/dist/mcp/glimpse-ui.js +67 -0
- package/dist/mcp/host-html-template.js +412 -0
- package/dist/mcp/index.js +291 -0
- package/dist/mcp/init.js +280 -0
- package/dist/mcp/lifecycle.js +79 -0
- package/dist/mcp/logger.js +130 -0
- package/dist/mcp/mcp-auth-flow.js +283 -0
- package/dist/mcp/mcp-auth.js +226 -0
- package/dist/mcp/mcp-callback-server.js +225 -0
- package/dist/mcp/mcp-oauth-provider.js +243 -0
- package/dist/mcp/mcp-panel.js +646 -0
- package/dist/mcp/mcp-setup-panel.js +485 -0
- package/dist/mcp/metadata-cache.js +158 -0
- package/dist/mcp/npx-resolver.js +385 -0
- package/dist/mcp/oauth-handler.js +54 -0
- package/dist/mcp/onboarding-state.js +56 -0
- package/dist/mcp/proxy-modes.js +714 -0
- package/dist/mcp/resource-tools.js +14 -0
- package/dist/mcp/sampling-handler.js +206 -0
- package/dist/mcp/server-manager.js +301 -0
- package/dist/mcp/state.js +1 -0
- package/dist/mcp/tool-metadata.js +128 -0
- package/dist/mcp/tool-registrar.js +43 -0
- package/dist/mcp/types.js +93 -0
- package/dist/mcp/ui-resource-handler.js +113 -0
- package/dist/mcp/ui-server.js +522 -0
- package/dist/mcp/ui-session.js +306 -0
- package/dist/mcp/ui-stream-types.js +58 -0
- package/dist/mcp/utils.js +104 -0
- package/dist/mcp/vitest.config.js +13 -0
- package/dist/relay/machine-store.js +4 -0
- package/dist/relay/registration.js +12 -7
- package/package.json +9 -3
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
import { matchesKey, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
|
|
2
|
+
const DEFAULT_THEME = {
|
|
3
|
+
border: "2",
|
|
4
|
+
title: "36",
|
|
5
|
+
selected: "32",
|
|
6
|
+
hint: "2",
|
|
7
|
+
success: "32",
|
|
8
|
+
warning: "33",
|
|
9
|
+
muted: "2;3",
|
|
10
|
+
};
|
|
11
|
+
function fg(code, text) {
|
|
12
|
+
return code ? `\x1b[${code}m${text}\x1b[0m` : text;
|
|
13
|
+
}
|
|
14
|
+
function wrapText(text, width) {
|
|
15
|
+
if (width <= 8)
|
|
16
|
+
return [text];
|
|
17
|
+
const words = text.split(/\s+/).filter(Boolean);
|
|
18
|
+
const lines = [];
|
|
19
|
+
let current = "";
|
|
20
|
+
for (const word of words) {
|
|
21
|
+
const candidate = current ? `${current} ${word}` : word;
|
|
22
|
+
if (visibleWidth(candidate) <= width) {
|
|
23
|
+
current = candidate;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (current)
|
|
27
|
+
lines.push(current);
|
|
28
|
+
current = word;
|
|
29
|
+
}
|
|
30
|
+
if (current)
|
|
31
|
+
lines.push(current);
|
|
32
|
+
return lines.length > 0 ? lines : [""];
|
|
33
|
+
}
|
|
34
|
+
export class McpSetupPanel {
|
|
35
|
+
discovery;
|
|
36
|
+
callbacks;
|
|
37
|
+
options;
|
|
38
|
+
done;
|
|
39
|
+
screen;
|
|
40
|
+
actionCursor = 0;
|
|
41
|
+
importCursor = 0;
|
|
42
|
+
pathCursor = 0;
|
|
43
|
+
selectedImports = new Set();
|
|
44
|
+
busy = false;
|
|
45
|
+
notice = null;
|
|
46
|
+
tui;
|
|
47
|
+
t = DEFAULT_THEME;
|
|
48
|
+
inactivityTimeout = null;
|
|
49
|
+
static INACTIVITY_MS = 60_000;
|
|
50
|
+
constructor(discovery, callbacks, options, tui, done) {
|
|
51
|
+
this.discovery = discovery;
|
|
52
|
+
this.callbacks = callbacks;
|
|
53
|
+
this.options = options;
|
|
54
|
+
this.done = done;
|
|
55
|
+
this.tui = tui;
|
|
56
|
+
this.screen = options.mode;
|
|
57
|
+
for (const entry of discovery.imports) {
|
|
58
|
+
this.selectedImports.add(entry.kind);
|
|
59
|
+
}
|
|
60
|
+
this.resetInactivityTimeout();
|
|
61
|
+
}
|
|
62
|
+
resetInactivityTimeout() {
|
|
63
|
+
if (this.inactivityTimeout)
|
|
64
|
+
clearTimeout(this.inactivityTimeout);
|
|
65
|
+
this.inactivityTimeout = setTimeout(() => {
|
|
66
|
+
this.cleanup();
|
|
67
|
+
this.done();
|
|
68
|
+
}, McpSetupPanel.INACTIVITY_MS);
|
|
69
|
+
}
|
|
70
|
+
cleanup() {
|
|
71
|
+
if (this.inactivityTimeout) {
|
|
72
|
+
clearTimeout(this.inactivityTimeout);
|
|
73
|
+
this.inactivityTimeout = null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
getActions() {
|
|
77
|
+
const actions = [];
|
|
78
|
+
if (this.screen === "empty") {
|
|
79
|
+
actions.push({ id: "run-setup", label: "Run setup", description: "Inspect detected configs, adopt imports, and scaffold a minimal `.mcp.json`." });
|
|
80
|
+
}
|
|
81
|
+
if (this.discovery.imports.length > 0) {
|
|
82
|
+
actions.push({ id: "adopt-imports", label: "Adopt detected compatibility imports", description: `Choose which host-specific MCP configs Pi should import into its own override file. ${this.discovery.imports.length} source${this.discovery.imports.length === 1 ? "" : "s"} found.` });
|
|
83
|
+
}
|
|
84
|
+
actions.push({ id: "view-example", label: "View example `.mcp.json`", description: "Preview a working shared MCP config you can paste or adapt." });
|
|
85
|
+
if (!this.discovery.sources.some((source) => source.id === "shared-project" && source.exists)) {
|
|
86
|
+
actions.push({ id: "scaffold-project", label: "Scaffold project `.mcp.json`", description: "Write a minimal project config using the standard shared MCP file path, then reload Pi." });
|
|
87
|
+
}
|
|
88
|
+
actions.push({ id: "show-precedence", label: "Explain config precedence", description: "Show the read order and where Pi writes compatibility settings." });
|
|
89
|
+
if (this.getDetectedPaths().length > 0) {
|
|
90
|
+
actions.push({ id: "open-paths", label: "Open detected config paths", description: "Browse the actual config files that Pi discovered on this machine." });
|
|
91
|
+
}
|
|
92
|
+
if (!this.discovery.repoPrompt.configured && this.discovery.repoPrompt.executablePath && this.discovery.repoPrompt.targetPath && this.discovery.repoPrompt.entry && this.discovery.repoPrompt.serverName) {
|
|
93
|
+
actions.push({ id: "add-repoprompt", label: "Add RepoPrompt to shared MCP config", description: "Write a standard MCP entry for RepoPrompt to the recommended shared target, then reload MCP in-session." });
|
|
94
|
+
}
|
|
95
|
+
actions.push({ id: "close", label: "Close", description: "Exit the onboarding flow." });
|
|
96
|
+
return actions;
|
|
97
|
+
}
|
|
98
|
+
getDetectedPaths() {
|
|
99
|
+
const paths = [
|
|
100
|
+
...this.discovery.sources.filter((source) => source.exists).map((source) => source.path),
|
|
101
|
+
...this.discovery.imports.map((entry) => entry.path),
|
|
102
|
+
];
|
|
103
|
+
return [...new Set(paths)];
|
|
104
|
+
}
|
|
105
|
+
getSelectedAction() {
|
|
106
|
+
const actions = this.getActions();
|
|
107
|
+
return actions[this.actionCursor] ?? null;
|
|
108
|
+
}
|
|
109
|
+
handleInput(data) {
|
|
110
|
+
this.resetInactivityTimeout();
|
|
111
|
+
if (!this.busy)
|
|
112
|
+
this.notice = null;
|
|
113
|
+
if (matchesKey(data, "ctrl+c")) {
|
|
114
|
+
this.cleanup();
|
|
115
|
+
this.done();
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (matchesKey(data, "escape")) {
|
|
119
|
+
if (this.screen === "imports" || this.screen === "paths") {
|
|
120
|
+
this.screen = this.discovery.hasAnyConfig ? "setup" : "empty";
|
|
121
|
+
this.tui.requestRender();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
this.cleanup();
|
|
125
|
+
this.done();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (this.busy)
|
|
129
|
+
return;
|
|
130
|
+
if (this.screen === "imports") {
|
|
131
|
+
this.handleImportsInput(data);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (this.screen === "paths") {
|
|
135
|
+
this.handlePathsInput(data);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const actions = this.getActions();
|
|
139
|
+
if (matchesKey(data, "up")) {
|
|
140
|
+
this.actionCursor = Math.max(0, this.actionCursor - 1);
|
|
141
|
+
this.tui.requestRender();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (matchesKey(data, "down")) {
|
|
145
|
+
this.actionCursor = Math.min(actions.length - 1, this.actionCursor + 1);
|
|
146
|
+
this.tui.requestRender();
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (matchesKey(data, "return")) {
|
|
150
|
+
const selected = this.getSelectedAction();
|
|
151
|
+
if (selected)
|
|
152
|
+
void this.runAction(selected.id);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
handleImportsInput(data) {
|
|
156
|
+
const imports = this.discovery.imports;
|
|
157
|
+
if (matchesKey(data, "up")) {
|
|
158
|
+
this.importCursor = Math.max(0, this.importCursor - 1);
|
|
159
|
+
this.tui.requestRender();
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (matchesKey(data, "down")) {
|
|
163
|
+
this.importCursor = Math.min(imports.length - 1, this.importCursor + 1);
|
|
164
|
+
this.tui.requestRender();
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (matchesKey(data, "space")) {
|
|
168
|
+
const current = imports[this.importCursor];
|
|
169
|
+
if (!current)
|
|
170
|
+
return;
|
|
171
|
+
if (this.selectedImports.has(current.kind)) {
|
|
172
|
+
this.selectedImports.delete(current.kind);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
this.selectedImports.add(current.kind);
|
|
176
|
+
}
|
|
177
|
+
this.tui.requestRender();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (matchesKey(data, "return")) {
|
|
181
|
+
void this.applySelectedImports();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
handlePathsInput(data) {
|
|
185
|
+
const paths = this.getDetectedPaths();
|
|
186
|
+
if (matchesKey(data, "up")) {
|
|
187
|
+
this.pathCursor = Math.max(0, this.pathCursor - 1);
|
|
188
|
+
this.tui.requestRender();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (matchesKey(data, "down")) {
|
|
192
|
+
this.pathCursor = Math.min(paths.length - 1, this.pathCursor + 1);
|
|
193
|
+
this.tui.requestRender();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (matchesKey(data, "return")) {
|
|
197
|
+
const selected = paths[this.pathCursor];
|
|
198
|
+
if (!selected)
|
|
199
|
+
return;
|
|
200
|
+
void this.runBusy(async () => {
|
|
201
|
+
await this.callbacks.openPath(selected);
|
|
202
|
+
this.notice = { text: `Opened ${selected}`, tone: "success" };
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
async runAction(action) {
|
|
207
|
+
if (action === "run-setup") {
|
|
208
|
+
this.screen = "setup";
|
|
209
|
+
this.actionCursor = 0;
|
|
210
|
+
this.tui.requestRender();
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (action === "adopt-imports") {
|
|
214
|
+
this.screen = "imports";
|
|
215
|
+
this.importCursor = 0;
|
|
216
|
+
this.tui.requestRender();
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
if (action === "open-paths") {
|
|
220
|
+
this.screen = "paths";
|
|
221
|
+
this.pathCursor = 0;
|
|
222
|
+
this.tui.requestRender();
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (action === "scaffold-project") {
|
|
226
|
+
await this.runBusy(async () => {
|
|
227
|
+
const result = await this.callbacks.scaffoldProjectConfig();
|
|
228
|
+
this.callbacks.markSetupCompleted();
|
|
229
|
+
this.notice = { text: `Wrote starter config to ${result.path}. Pi will reload after this panel closes.`, tone: "success" };
|
|
230
|
+
});
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (action === "add-repoprompt") {
|
|
234
|
+
await this.runBusy(async () => {
|
|
235
|
+
const result = await this.callbacks.addRepoPrompt();
|
|
236
|
+
this.callbacks.markSetupCompleted();
|
|
237
|
+
this.notice = { text: `Added ${result.serverName} to ${result.path}. Pi will reload after this panel closes.`, tone: "success" };
|
|
238
|
+
});
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (action === "close") {
|
|
242
|
+
this.cleanup();
|
|
243
|
+
this.done();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
this.notice = { text: "Review the details below. Press Enter on an action with a side effect to apply it.", tone: "muted" };
|
|
247
|
+
this.tui.requestRender();
|
|
248
|
+
}
|
|
249
|
+
async applySelectedImports() {
|
|
250
|
+
const selected = this.discovery.imports.filter((entry) => this.selectedImports.has(entry.kind)).map((entry) => entry.kind);
|
|
251
|
+
if (selected.length === 0) {
|
|
252
|
+
this.notice = { text: "Select at least one compatibility import first.", tone: "warning" };
|
|
253
|
+
this.tui.requestRender();
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
await this.runBusy(async () => {
|
|
257
|
+
const result = await this.callbacks.adoptImports(selected);
|
|
258
|
+
this.callbacks.markSetupCompleted();
|
|
259
|
+
this.notice = result.added.length > 0
|
|
260
|
+
? { text: `Added ${result.added.join(", ")} to ${result.path}. Pi will reload after this panel closes.`, tone: "success" }
|
|
261
|
+
: { text: `No changes needed in ${result.path}.`, tone: "muted" };
|
|
262
|
+
this.screen = this.discovery.hasAnyConfig ? "setup" : "empty";
|
|
263
|
+
this.actionCursor = 0;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
async runBusy(fn) {
|
|
267
|
+
this.busy = true;
|
|
268
|
+
this.notice = { text: "Working...", tone: "muted" };
|
|
269
|
+
this.tui.requestRender();
|
|
270
|
+
try {
|
|
271
|
+
await fn();
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
this.notice = {
|
|
275
|
+
text: error instanceof Error ? error.message : String(error),
|
|
276
|
+
tone: "warning",
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
finally {
|
|
280
|
+
this.busy = false;
|
|
281
|
+
this.tui.requestRender();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
render(width) {
|
|
285
|
+
const innerW = Math.max(40, width - 2);
|
|
286
|
+
const lines = [];
|
|
287
|
+
const border = fg(this.t.border, "─".repeat(innerW));
|
|
288
|
+
lines.push(`┌${border}┐`);
|
|
289
|
+
lines.push(this.padLine(fg(this.t.title, "MCP setup"), innerW));
|
|
290
|
+
lines.push(this.padLine(this.discoverySummaryLine(), innerW));
|
|
291
|
+
lines.push(this.padLine(fg(this.t.muted, this.secondarySummaryLine()), innerW));
|
|
292
|
+
lines.push(this.padLine("", innerW));
|
|
293
|
+
if (this.notice) {
|
|
294
|
+
const tone = this.notice.tone === "success" ? this.t.success : this.notice.tone === "warning" ? this.t.warning : this.t.hint;
|
|
295
|
+
for (const line of wrapText(this.notice.text, innerW - 6)) {
|
|
296
|
+
lines.push(this.padLine(fg(tone, line), innerW));
|
|
297
|
+
}
|
|
298
|
+
lines.push(this.padLine("", innerW));
|
|
299
|
+
}
|
|
300
|
+
lines.push(`├${border}┤`);
|
|
301
|
+
if (this.screen === "imports") {
|
|
302
|
+
lines.push(...this.renderImports(innerW));
|
|
303
|
+
}
|
|
304
|
+
else if (this.screen === "paths") {
|
|
305
|
+
lines.push(...this.renderPaths(innerW));
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
lines.push(...this.renderActions(innerW));
|
|
309
|
+
}
|
|
310
|
+
lines.push(`└${border}┘`);
|
|
311
|
+
return lines;
|
|
312
|
+
}
|
|
313
|
+
renderActions(innerW) {
|
|
314
|
+
const lines = [];
|
|
315
|
+
const actions = this.getActions();
|
|
316
|
+
for (let index = 0; index < actions.length; index++) {
|
|
317
|
+
const action = actions[index];
|
|
318
|
+
const selected = index === this.actionCursor;
|
|
319
|
+
const cursor = selected ? fg(this.t.selected, "›") : " ";
|
|
320
|
+
lines.push(this.padLine(`${cursor} ${truncateToWidth(action.label, innerW - 4)}`, innerW));
|
|
321
|
+
}
|
|
322
|
+
lines.push(this.padLine("", innerW));
|
|
323
|
+
const preview = this.getActionPreview(this.getSelectedAction()?.id ?? "view-example");
|
|
324
|
+
for (const line of preview) {
|
|
325
|
+
lines.push(this.padLine(line, innerW));
|
|
326
|
+
}
|
|
327
|
+
lines.push(this.padLine("", innerW));
|
|
328
|
+
lines.push(this.padLine(fg(this.t.muted, "Enter selects, Esc goes back, Ctrl+C closes."), innerW));
|
|
329
|
+
return lines;
|
|
330
|
+
}
|
|
331
|
+
renderImports(innerW) {
|
|
332
|
+
const lines = [];
|
|
333
|
+
lines.push(this.padLine("Select compatibility imports. Space toggles, Enter saves, Esc goes back.", innerW));
|
|
334
|
+
lines.push(this.padLine("", innerW));
|
|
335
|
+
for (let index = 0; index < this.discovery.imports.length; index++) {
|
|
336
|
+
const entry = this.discovery.imports[index];
|
|
337
|
+
const selected = this.selectedImports.has(entry.kind) ? "[x]" : "[ ]";
|
|
338
|
+
const cursor = index === this.importCursor ? fg(this.t.selected, "›") : " ";
|
|
339
|
+
lines.push(this.padLine(`${cursor} ${selected} ${entry.kind} ${entry.path}`, innerW));
|
|
340
|
+
}
|
|
341
|
+
lines.push(this.padLine("", innerW));
|
|
342
|
+
const selected = this.discovery.imports.filter((entry) => this.selectedImports.has(entry.kind)).map((entry) => entry.kind);
|
|
343
|
+
const preview = this.callbacks.previewImports(selected);
|
|
344
|
+
for (const line of this.formatWritePreview("Compatibility import write preview", preview)) {
|
|
345
|
+
lines.push(this.padLine(line, innerW));
|
|
346
|
+
}
|
|
347
|
+
return lines;
|
|
348
|
+
}
|
|
349
|
+
renderPaths(innerW) {
|
|
350
|
+
const lines = [];
|
|
351
|
+
lines.push(this.padLine("Select a detected config path to open. Enter opens it, Esc goes back.", innerW));
|
|
352
|
+
lines.push(this.padLine("", innerW));
|
|
353
|
+
const paths = this.getDetectedPaths();
|
|
354
|
+
for (let index = 0; index < paths.length; index++) {
|
|
355
|
+
const cursor = index === this.pathCursor ? fg(this.t.selected, "›") : " ";
|
|
356
|
+
lines.push(this.padLine(`${cursor} ${paths[index]}`, innerW));
|
|
357
|
+
}
|
|
358
|
+
return lines;
|
|
359
|
+
}
|
|
360
|
+
discoverySummaryLine() {
|
|
361
|
+
if (!this.discovery.hasAnyConfig) {
|
|
362
|
+
return fg(this.t.warning, this.options.onboardingState.setupCompleted
|
|
363
|
+
? "No MCP servers are active right now."
|
|
364
|
+
: "No MCP config is active yet.");
|
|
365
|
+
}
|
|
366
|
+
if (this.discovery.totalServerCount === 0 && (this.discovery.imports.length > 0 || !!this.discovery.repoPrompt.executablePath)) {
|
|
367
|
+
return fg(this.t.warning, "Pi found MCP-related setup options, but none are active in Pi yet.");
|
|
368
|
+
}
|
|
369
|
+
const shared = this.discovery.sources.filter((source) => source.kind === "shared" && source.serverCount > 0).length;
|
|
370
|
+
const piOwned = this.discovery.sources.filter((source) => source.kind === "pi" && source.serverCount > 0).length;
|
|
371
|
+
return fg(this.t.hint, `Detected ${this.discovery.totalServerCount} configured servers across ${shared} shared and ${piOwned} Pi-owned source${shared + piOwned === 1 ? "" : "s"}.`);
|
|
372
|
+
}
|
|
373
|
+
secondarySummaryLine() {
|
|
374
|
+
if (!this.discovery.hasAnyConfig) {
|
|
375
|
+
return "Create a shared `.mcp.json`, adopt host imports, or quick-add RepoPrompt from this screen.";
|
|
376
|
+
}
|
|
377
|
+
if (this.discovery.totalServerCount === 0 && this.discovery.imports.length > 0) {
|
|
378
|
+
return `Detected ${this.discovery.imports.length} compatibility import source${this.discovery.imports.length === 1 ? "" : "s"}. Adopt them into Pi or inspect the underlying files.`;
|
|
379
|
+
}
|
|
380
|
+
return "Shared MCP files are preferred. Pi-owned files are only for compatibility imports and adapter-specific overrides.";
|
|
381
|
+
}
|
|
382
|
+
getActionPreview(action) {
|
|
383
|
+
switch (action) {
|
|
384
|
+
case "run-setup":
|
|
385
|
+
return this.formatPreview([
|
|
386
|
+
"Run setup to adopt host-specific imports, inspect detected paths, and scaffold a minimal `.mcp.json` if needed.",
|
|
387
|
+
]);
|
|
388
|
+
case "adopt-imports":
|
|
389
|
+
return this.formatWritePreview("Compatibility import write preview", this.callbacks.previewImports(this.discovery.imports.filter((entry) => this.selectedImports.has(entry.kind)).map((entry) => entry.kind)), [
|
|
390
|
+
`Detected imports: ${this.discovery.imports.map((entry) => `${entry.kind} (${entry.serverCount} servers)`).join(", ")}`,
|
|
391
|
+
"Selected imports are written into the Pi agent dir config as Pi-owned compatibility state.",
|
|
392
|
+
]);
|
|
393
|
+
case "view-example":
|
|
394
|
+
return this.formatPreview([
|
|
395
|
+
"Example shared `.mcp.json`:",
|
|
396
|
+
"{",
|
|
397
|
+
' "mcpServers": {',
|
|
398
|
+
' "chrome-devtools": {',
|
|
399
|
+
' "command": "npx",',
|
|
400
|
+
' "args": ["-y", "chrome-devtools-mcp@latest"]',
|
|
401
|
+
" }",
|
|
402
|
+
" }",
|
|
403
|
+
"}",
|
|
404
|
+
"",
|
|
405
|
+
"Use Scaffold project `.mcp.json` when you want a safe empty shell instead of a live example server.",
|
|
406
|
+
]);
|
|
407
|
+
case "show-precedence":
|
|
408
|
+
return this.formatPreview([
|
|
409
|
+
"Read order:",
|
|
410
|
+
"1. ~/.config/mcp/mcp.json",
|
|
411
|
+
"2. <Pi agent dir>/mcp.json",
|
|
412
|
+
"3. .mcp.json",
|
|
413
|
+
"4. .pi/mcp.json",
|
|
414
|
+
"Pi writes compatibility imports and adapter-only overrides to Pi-owned files.",
|
|
415
|
+
]);
|
|
416
|
+
case "open-paths":
|
|
417
|
+
return this.formatPreview(this.getDetectedPaths().length > 0
|
|
418
|
+
? ["Detected paths:", ...this.getDetectedPaths()]
|
|
419
|
+
: ["No config paths were detected."]);
|
|
420
|
+
case "add-repoprompt": {
|
|
421
|
+
const repoPrompt = this.discovery.repoPrompt;
|
|
422
|
+
const preview = this.callbacks.previewRepoPrompt();
|
|
423
|
+
if (!preview) {
|
|
424
|
+
return this.formatPreview(["RepoPrompt is not available to add from this setup screen."]);
|
|
425
|
+
}
|
|
426
|
+
return this.formatWritePreview("RepoPrompt write preview", preview, [
|
|
427
|
+
`Executable: ${repoPrompt.executablePath ?? "not found"}`,
|
|
428
|
+
`Target: ${repoPrompt.targetPath ?? "n/a"}`,
|
|
429
|
+
`Server name: ${repoPrompt.serverName ?? "repoprompt"}`,
|
|
430
|
+
]);
|
|
431
|
+
}
|
|
432
|
+
case "scaffold-project":
|
|
433
|
+
return this.formatWritePreview("Starter project `.mcp.json` write preview", this.callbacks.previewStarterProject(), [
|
|
434
|
+
"This writes a minimal `.mcp.json` in the current project using the shared MCP layout.",
|
|
435
|
+
"It intentionally avoids adding a fake placeholder server that would fail on first reload.",
|
|
436
|
+
]);
|
|
437
|
+
case "close":
|
|
438
|
+
default:
|
|
439
|
+
return this.formatPreview(["Close the setup flow."]);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
formatPreview(lines) {
|
|
443
|
+
const preview = [];
|
|
444
|
+
for (const line of lines) {
|
|
445
|
+
preview.push(...wrapText(line, 74));
|
|
446
|
+
}
|
|
447
|
+
return preview;
|
|
448
|
+
}
|
|
449
|
+
formatWritePreview(title, preview, intro = []) {
|
|
450
|
+
const lines = [];
|
|
451
|
+
for (const line of intro) {
|
|
452
|
+
lines.push(...wrapText(line, 74));
|
|
453
|
+
}
|
|
454
|
+
if (intro.length > 0)
|
|
455
|
+
lines.push("");
|
|
456
|
+
lines.push(...wrapText(`${title}: ${preview.path}`, 74));
|
|
457
|
+
lines.push(...wrapText(preview.existed ? "Existing file detected. Showing exact before/after diff." : "New file will be created. Showing exact content diff.", 74));
|
|
458
|
+
lines.push("");
|
|
459
|
+
const diffLines = preview.diffText.split("\n");
|
|
460
|
+
const maxLines = 18;
|
|
461
|
+
const shown = diffLines.slice(0, maxLines);
|
|
462
|
+
for (const line of shown) {
|
|
463
|
+
lines.push(...wrapText(line, 74));
|
|
464
|
+
}
|
|
465
|
+
if (diffLines.length > maxLines) {
|
|
466
|
+
lines.push(...wrapText(`… ${diffLines.length - maxLines} more diff line${diffLines.length - maxLines === 1 ? "" : "s"}`, 74));
|
|
467
|
+
}
|
|
468
|
+
return lines;
|
|
469
|
+
}
|
|
470
|
+
padLine(text, innerW) {
|
|
471
|
+
const inset = 2;
|
|
472
|
+
const contentW = Math.max(0, innerW - inset * 2);
|
|
473
|
+
const fitted = truncateToWidth(text, contentW, "…", true);
|
|
474
|
+
const plainWidth = visibleWidth(fitted);
|
|
475
|
+
const padding = Math.max(0, contentW - plainWidth);
|
|
476
|
+
return `│${" ".repeat(inset)}${fitted}${" ".repeat(padding)}${" ".repeat(inset)}│`;
|
|
477
|
+
}
|
|
478
|
+
invalidate() { }
|
|
479
|
+
dispose() {
|
|
480
|
+
this.cleanup();
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
export function createMcpSetupPanel(discovery, callbacks, options, tui, done) {
|
|
484
|
+
return new McpSetupPanel(discovery, callbacks, options, tui, done);
|
|
485
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// metadata-cache.ts - Persistent MCP metadata cache
|
|
2
|
+
import { existsSync, readFileSync, writeFileSync, renameSync, mkdirSync } from "node:fs";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
import { getAgentPath } from "./agent-dir.js";
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
import { getToolUiResourceUri } from "@modelcontextprotocol/ext-apps/app-bridge";
|
|
7
|
+
import { formatToolName, isToolExcluded } from "./types.js";
|
|
8
|
+
import { resourceNameToToolName } from "./resource-tools.js";
|
|
9
|
+
import { extractToolUiStreamMode, interpolateEnvRecord, resolveBearerToken, resolveConfigPath } from "./utils.js";
|
|
10
|
+
const CACHE_VERSION = 1;
|
|
11
|
+
const CACHE_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
12
|
+
export function getMetadataCachePath() {
|
|
13
|
+
return getAgentPath("mcp-cache.json");
|
|
14
|
+
}
|
|
15
|
+
export function loadMetadataCache() {
|
|
16
|
+
const cachePath = getMetadataCachePath();
|
|
17
|
+
if (!existsSync(cachePath))
|
|
18
|
+
return null;
|
|
19
|
+
try {
|
|
20
|
+
const raw = JSON.parse(readFileSync(cachePath, "utf-8"));
|
|
21
|
+
if (!raw || typeof raw !== "object")
|
|
22
|
+
return null;
|
|
23
|
+
if (raw.version !== CACHE_VERSION)
|
|
24
|
+
return null;
|
|
25
|
+
if (!raw.servers || typeof raw.servers !== "object")
|
|
26
|
+
return null;
|
|
27
|
+
return raw;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function saveMetadataCache(cache) {
|
|
34
|
+
const cachePath = getMetadataCachePath();
|
|
35
|
+
const dir = dirname(cachePath);
|
|
36
|
+
mkdirSync(dir, { recursive: true });
|
|
37
|
+
let merged = { version: CACHE_VERSION, servers: {} };
|
|
38
|
+
try {
|
|
39
|
+
if (existsSync(cachePath)) {
|
|
40
|
+
const existing = JSON.parse(readFileSync(cachePath, "utf-8"));
|
|
41
|
+
if (existing && existing.version === CACHE_VERSION && existing.servers) {
|
|
42
|
+
merged.servers = { ...existing.servers };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// Ignore parse errors and proceed with empty cache
|
|
48
|
+
}
|
|
49
|
+
merged.version = CACHE_VERSION;
|
|
50
|
+
merged.servers = { ...merged.servers, ...cache.servers };
|
|
51
|
+
const tmpPath = `${cachePath}.${process.pid}.tmp`;
|
|
52
|
+
writeFileSync(tmpPath, JSON.stringify(merged, null, 2), "utf-8");
|
|
53
|
+
renameSync(tmpPath, cachePath);
|
|
54
|
+
}
|
|
55
|
+
export function computeServerHash(definition) {
|
|
56
|
+
// Hash only fields that affect server identity and tool/resource output.
|
|
57
|
+
// Exclude lifecycle, idleTimeout, debug — those are runtime behavior settings
|
|
58
|
+
// that don't change which tools a server exposes.
|
|
59
|
+
const identity = {
|
|
60
|
+
command: definition.command,
|
|
61
|
+
args: definition.args,
|
|
62
|
+
env: interpolateEnvRecord(definition.env),
|
|
63
|
+
cwd: resolveConfigPath(definition.cwd),
|
|
64
|
+
url: definition.url,
|
|
65
|
+
headers: interpolateEnvRecord(definition.headers),
|
|
66
|
+
auth: definition.auth,
|
|
67
|
+
bearerToken: resolveBearerToken(definition),
|
|
68
|
+
bearerTokenEnv: definition.bearerTokenEnv,
|
|
69
|
+
exposeResources: definition.exposeResources,
|
|
70
|
+
excludeTools: definition.excludeTools,
|
|
71
|
+
};
|
|
72
|
+
const normalized = stableStringify(identity);
|
|
73
|
+
return createHash("sha256").update(normalized).digest("hex");
|
|
74
|
+
}
|
|
75
|
+
export function isServerCacheValid(entry, definition, maxAgeMs = CACHE_MAX_AGE_MS) {
|
|
76
|
+
if (!entry || entry.configHash !== computeServerHash(definition))
|
|
77
|
+
return false;
|
|
78
|
+
if (!entry.cachedAt || typeof entry.cachedAt !== "number")
|
|
79
|
+
return false;
|
|
80
|
+
if (maxAgeMs > 0 && Date.now() - entry.cachedAt > maxAgeMs)
|
|
81
|
+
return false;
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
export function reconstructToolMetadata(serverName, entry, prefix, definition) {
|
|
85
|
+
const metadata = [];
|
|
86
|
+
for (const tool of entry.tools ?? []) {
|
|
87
|
+
if (!tool?.name)
|
|
88
|
+
continue;
|
|
89
|
+
if (isToolExcluded(tool.name, serverName, prefix, definition.excludeTools)) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
metadata.push({
|
|
93
|
+
name: formatToolName(tool.name, serverName, prefix),
|
|
94
|
+
originalName: tool.name,
|
|
95
|
+
description: tool.description ?? "",
|
|
96
|
+
inputSchema: tool.inputSchema,
|
|
97
|
+
uiResourceUri: tool.uiResourceUri,
|
|
98
|
+
uiStreamMode: tool.uiStreamMode,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (definition.exposeResources !== false) {
|
|
102
|
+
for (const resource of entry.resources ?? []) {
|
|
103
|
+
if (!resource?.name || !resource?.uri)
|
|
104
|
+
continue;
|
|
105
|
+
const baseName = `get_${resourceNameToToolName(resource.name)}`;
|
|
106
|
+
if (isToolExcluded(baseName, serverName, prefix, definition.excludeTools)) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
metadata.push({
|
|
110
|
+
name: formatToolName(baseName, serverName, prefix),
|
|
111
|
+
originalName: baseName,
|
|
112
|
+
description: resource.description ?? `Read resource: ${resource.uri}`,
|
|
113
|
+
resourceUri: resource.uri,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return metadata;
|
|
118
|
+
}
|
|
119
|
+
export function serializeTools(tools) {
|
|
120
|
+
return tools
|
|
121
|
+
.filter(t => t?.name)
|
|
122
|
+
.map(t => ({
|
|
123
|
+
name: t.name,
|
|
124
|
+
description: t.description,
|
|
125
|
+
inputSchema: t.inputSchema,
|
|
126
|
+
uiResourceUri: tryGetToolUiResourceUri(t),
|
|
127
|
+
uiStreamMode: extractToolUiStreamMode(t._meta),
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
export function serializeResources(resources) {
|
|
131
|
+
return resources
|
|
132
|
+
.filter(r => r?.name && r?.uri)
|
|
133
|
+
.map(r => ({
|
|
134
|
+
uri: r.uri,
|
|
135
|
+
name: r.name,
|
|
136
|
+
description: r.description,
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
function stableStringify(value) {
|
|
140
|
+
if (value === null || value === undefined || typeof value !== "object") {
|
|
141
|
+
const serialized = JSON.stringify(value);
|
|
142
|
+
return serialized === undefined ? "undefined" : serialized;
|
|
143
|
+
}
|
|
144
|
+
if (Array.isArray(value)) {
|
|
145
|
+
return `[${value.map(v => stableStringify(v)).join(",")}]`;
|
|
146
|
+
}
|
|
147
|
+
const obj = value;
|
|
148
|
+
const keys = Object.keys(obj).sort();
|
|
149
|
+
return `{${keys.map(k => `${JSON.stringify(k)}:${stableStringify(obj[k])}`).join(",")}}`;
|
|
150
|
+
}
|
|
151
|
+
function tryGetToolUiResourceUri(tool) {
|
|
152
|
+
try {
|
|
153
|
+
return getToolUiResourceUri({ _meta: tool._meta });
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
}
|