forked from nicobailon/pi-mcp-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ts
More file actions
429 lines (380 loc) · 15.1 KB
/
Copy pathcommands.ts
File metadata and controls
429 lines (380 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
import type { McpExtensionState } from "./state.ts";
import type { McpAuthResult, McpConfig, ServerEntry, McpPanelCallbacks, McpPanelResult, ImportKind } from "./types.ts";
import {
ensureCompatibilityImports,
getMcpDiscoverySummary,
getServerProvenance,
previewCompatibilityImports,
previewSharedServerEntry,
previewStarterProjectConfig,
writeDirectToolsConfig,
writeSharedServerEntry,
writeStarterProjectConfig,
} from "./config.ts";
import { lazyConnect, updateMetadataCache, updateStatusBar, getFailureAgeSeconds } from "./init.ts";
import { loadMetadataCache } from "./metadata-cache.ts";
import { buildToolMetadata } from "./tool-metadata.ts";
import { supportsOAuth, authenticate, removeAuth } from "./mcp-auth-flow.ts";
import { getAuthForUrl } from "./mcp-auth.ts";
import { loadOnboardingState, markSetupCompleted as persistSetupCompleted, markSharedConfigHintShown } from "./onboarding-state.ts";
import { openPath } from "./utils.ts";
export async function showStatus(state: McpExtensionState, ctx: ExtensionContext): Promise<void> {
if (!ctx.hasUI) return;
const lines: string[] = ["MCP Server Status:", ""];
for (const name of Object.keys(state.config.mcpServers)) {
const connection = state.manager.getConnection(name);
const metadata = state.toolMetadata.get(name);
const toolCount = metadata?.length ?? 0;
const failedAgo = getFailureAgeSeconds(state, name);
let status = "not connected";
let statusIcon = "○";
let failed = false;
if (connection?.status === "connected") {
status = "connected";
statusIcon = "✓";
} else if (connection?.status === "needs-auth") {
status = "needs auth";
statusIcon = "⚠";
} else if (failedAgo !== null) {
status = `failed ${failedAgo}s ago`;
statusIcon = "✗";
failed = true;
} else if (metadata !== undefined) {
status = "cached";
}
const toolSuffix = failed ? "" : ` (${toolCount} tools${status === "cached" ? ", cached" : ""})`;
lines.push(`${statusIcon} ${name}: ${status}${toolSuffix}`);
}
if (Object.keys(state.config.mcpServers).length === 0) {
lines.push("No MCP servers configured");
lines.push("Run /mcp setup to adopt imports or scaffold a starter .mcp.json");
}
ctx.ui.notify(lines.join("\n"), "info");
}
export async function showTools(state: McpExtensionState, ctx: ExtensionContext): Promise<void> {
if (!ctx.hasUI) return;
const allTools = [...state.toolMetadata.values()].flat().map(m => m.name);
if (allTools.length === 0) {
ctx.ui.notify("No MCP tools available", "info");
return;
}
const lines = [
"MCP Tools:",
"",
...allTools.map(t => ` ${t}`),
"",
`Total: ${allTools.length} tools`,
];
ctx.ui.notify(lines.join("\n"), "info");
}
export async function reconnectServers(
state: McpExtensionState,
ctx: ExtensionContext,
targetServer?: string
): Promise<void> {
if (targetServer && !state.config.mcpServers[targetServer]) {
if (ctx.hasUI) {
ctx.ui.notify(`Server "${targetServer}" not found in config`, "error");
}
return;
}
const entries = targetServer
? [[targetServer, state.config.mcpServers[targetServer]] as [string, ServerEntry]]
: Object.entries(state.config.mcpServers);
for (const [name, definition] of entries) {
try {
await state.manager.close(name);
const connection = await state.manager.connect(name, definition);
if (connection.status === "needs-auth") {
if (ctx.hasUI) {
ctx.ui.notify(`MCP: ${name} requires OAuth. Run /mcp-auth ${name} first.`, "warning");
}
continue;
}
const prefix = state.config.settings?.toolPrefix ?? "server";
const { metadata, failedTools } = buildToolMetadata(connection.tools, connection.resources, definition, name, prefix);
state.toolMetadata.set(name, metadata);
updateMetadataCache(state, name);
state.failureTracker.delete(name);
if (ctx.hasUI) {
ctx.ui.notify(
`MCP: Reconnected to ${name} (${connection.tools.length} tools, ${connection.resources.length} resources)`,
"info"
);
if (failedTools.length > 0) {
ctx.ui.notify(`MCP: ${name} - ${failedTools.length} tools skipped`, "warning");
}
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
state.failureTracker.set(name, Date.now());
if (ctx.hasUI) {
ctx.ui.notify(`MCP: Failed to reconnect to ${name}: ${message}`, "error");
}
}
}
updateStatusBar(state);
}
export async function authenticateServer(
serverName: string,
config: McpConfig,
ctx: ExtensionContext
): Promise<McpAuthResult> {
if (!ctx.hasUI) return { ok: false, message: "OAuth authentication requires an interactive session." };
const definition = config.mcpServers[serverName];
if (!definition) {
const message = `Server "${serverName}" not found in config`;
ctx.ui.notify(message, "error");
return { ok: false, message };
}
if (!supportsOAuth(definition)) {
const message = `Server "${serverName}" does not use OAuth authentication. Set "auth": "oauth" or omit auth for auto-detection.`;
ctx.ui.notify(
`Server "${serverName}" does not use OAuth authentication.\n` +
`Set "auth": "oauth" or omit auth for auto-detection.`,
"error"
);
return { ok: false, message };
}
if (!definition.url) {
const message = `Server "${serverName}" has no URL configured (OAuth requires HTTP transport)`;
ctx.ui.notify(message, "error");
return { ok: false, message };
}
try {
ctx.ui.setStatus("mcp-auth", `Authenticating ${serverName}...`);
const status = await authenticate(serverName, definition.url, definition, {
onAuthorizationUrl: (authorizationUrl) => {
ctx.ui.notify(
`Open this URL to authenticate ${serverName}:\n\n${authorizationUrl}\n\n` +
"After approving, return to Pi; the local callback will complete automatically.",
"info"
);
},
});
if (status === "authenticated") {
const message = `OAuth authentication successful for "${serverName}"! Run /mcp reconnect ${serverName} to connect with the new token.`;
ctx.ui.notify(
`OAuth authentication successful for "${serverName}"!\n` +
`Run /mcp reconnect ${serverName} to connect with the new token.`,
"info"
);
return { ok: true, message };
}
const message = `OAuth authentication failed for "${serverName}".`;
ctx.ui.notify(message, "error");
return { ok: false, message };
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
ctx.ui.notify(`Failed to authenticate "${serverName}": ${message}`, "error");
return { ok: false, message };
} finally {
ctx.ui.setStatus("mcp-auth", undefined);
}
}
export async function logoutServer(
serverName: string,
state: McpExtensionState,
ctx: ExtensionContext
): Promise<{ ok: boolean; message: string }> {
const definition = state.config.mcpServers[serverName];
if (!definition) {
const message = `Server "${serverName}" not found in config`;
if (ctx.hasUI) ctx.ui.notify(message, "error");
return { ok: false, message };
}
await removeAuth(serverName);
await state.manager.close(serverName);
updateStatusBar(state);
const message = `OAuth credentials cleared for "${serverName}". Run /mcp-auth ${serverName} to authenticate again.`;
if (ctx.hasUI) ctx.ui.notify(message, "info");
return { ok: true, message };
}
export interface PanelFlowResult {
configChanged: boolean;
}
function buildSharedConfigNoticeLines(configOverridePath: string | undefined, cwd: string): { lines: string[]; fingerprint: string | null } {
const discovery = getMcpDiscoverySummary(configOverridePath, cwd);
const onboardingState = loadOnboardingState();
if (!discovery.hasSharedServers || onboardingState.sharedConfigHintShown) {
return { lines: [], fingerprint: null };
}
const sharedSources = discovery.sources.filter((source) => source.kind === "shared" && source.serverCount > 0);
const sourceList = sharedSources.map((source) => source.path).join(", ");
return {
lines: [
`Using standard MCP config from ${sourceList}.`,
"Pi only writes compatibility imports and adapter-specific overrides into Pi-owned files when needed.",
],
fingerprint: discovery.fingerprint,
};
}
export async function openMcpSetup(
_state: McpExtensionState,
pi: ExtensionAPI,
ctx: ExtensionContext,
configOverridePath?: string,
mode: "empty" | "setup" = "setup",
): Promise<PanelFlowResult> {
if (!ctx.hasUI) return { configChanged: false };
const discovery = getMcpDiscoverySummary(configOverridePath, ctx.cwd);
const onboardingState = loadOnboardingState();
const { createMcpSetupPanel } = await import("./mcp-setup-panel.ts");
let configChanged = false;
const callbacks = {
previewImports: (imports: ImportKind[]) => previewCompatibilityImports(imports, configOverridePath),
previewStarterProject: () => previewStarterProjectConfig(ctx.cwd),
previewRepoPrompt: () => {
const repoPrompt = getMcpDiscoverySummary(configOverridePath, ctx.cwd).repoPrompt;
if (!repoPrompt.entry || !repoPrompt.targetPath || !repoPrompt.serverName) return null;
return previewSharedServerEntry(repoPrompt.targetPath, repoPrompt.serverName, repoPrompt.entry);
},
adoptImports: async (imports: ImportKind[]) => {
const result = ensureCompatibilityImports(imports, configOverridePath);
if (result.added.length > 0) configChanged = true;
return result;
},
scaffoldProjectConfig: async () => {
const path = writeStarterProjectConfig(ctx.cwd);
configChanged = true;
return { path };
},
addRepoPrompt: async () => {
const repoPrompt = getMcpDiscoverySummary(configOverridePath, ctx.cwd).repoPrompt;
if (!repoPrompt.entry || !repoPrompt.targetPath || !repoPrompt.serverName) {
throw new Error("RepoPrompt is not available to add from this setup screen.");
}
const path = writeSharedServerEntry(repoPrompt.targetPath, repoPrompt.serverName, repoPrompt.entry);
configChanged = true;
return { path, serverName: repoPrompt.serverName };
},
openPath: async (targetPath: string) => {
await openPath(pi, targetPath);
},
markSetupCompleted: () => {
persistSetupCompleted(discovery.fingerprint);
},
};
return new Promise<PanelFlowResult>((resolve) => {
ctx.ui.custom(
(tui, _theme, keybindings, done) => {
return createMcpSetupPanel(discovery, callbacks, { mode, onboardingState, keybindings }, tui, () => {
done(undefined);
resolve({ configChanged });
});
},
{ overlay: true, overlayOptions: { anchor: "center", width: 92 } },
);
});
}
function buildMcpPanelCallbacks(
state: McpExtensionState,
config: McpConfig,
ctx: ExtensionContext,
): McpPanelCallbacks {
return {
reconnect: (serverName: string) => lazyConnect(state, serverName),
canAuthenticate: (serverName: string) => {
const definition = config.mcpServers[serverName];
return definition ? supportsOAuth(definition) : false;
},
authenticate: (serverName: string) => authenticateServer(serverName, config, ctx),
getConnectionStatus: (serverName: string) => {
const definition = config.mcpServers[serverName];
const connection = state.manager.getConnection(serverName);
if (connection?.status === "needs-auth") {
return "needs-auth";
}
if (
definition?.auth === "oauth"
&& definition.url
&& definition.oauth !== false
&& definition.oauth?.grantType !== "client_credentials"
&& !getAuthForUrl(serverName, definition.url)?.tokens
) {
return "needs-auth";
}
if (connection?.status === "connected") return "connected";
if (getFailureAgeSeconds(state, serverName) !== null) return "failed";
return "idle";
},
refreshCacheAfterReconnect: (serverName: string) => {
const freshCache = loadMetadataCache();
return freshCache?.servers?.[serverName] ?? null;
},
};
}
export async function openMcpPanel(
state: McpExtensionState,
pi: ExtensionAPI,
ctx: ExtensionContext,
configOverridePath?: string,
): Promise<PanelFlowResult> {
if (Object.keys(state.config.mcpServers).length === 0) {
return openMcpSetup(state, pi, ctx, configOverridePath, "empty");
}
const config = state.config;
const cache = loadMetadataCache();
const configPath = pi.getFlag("mcp-config") as string | undefined ?? configOverridePath;
const provenanceMap = getServerProvenance(configPath, ctx.cwd);
const { lines: noticeLines, fingerprint } = buildSharedConfigNoticeLines(configPath, ctx.cwd);
const callbacks = buildMcpPanelCallbacks(state, config, ctx);
const { createMcpPanel } = await import("./mcp-panel.ts");
let configChanged = false;
await new Promise<void>((resolve) => {
ctx.ui.custom(
(tui, _theme, keybindings, done) => {
return createMcpPanel(config, cache, provenanceMap, callbacks, tui, (result: McpPanelResult) => {
if (!result.cancelled && result.changes.size > 0) {
writeDirectToolsConfig(result.changes, provenanceMap, config);
configChanged = true;
ctx.ui.notify("Direct tools updated. Pi will reload after this panel closes.", "info");
}
done(undefined);
resolve();
}, { noticeLines, keybindings });
},
{ overlay: true, overlayOptions: { anchor: "center", width: 82 } },
);
});
if (noticeLines.length > 0 && fingerprint) {
markSharedConfigHintShown(fingerprint);
}
return { configChanged };
}
export async function openMcpAuthPanel(
state: McpExtensionState,
pi: ExtensionAPI,
ctx: ExtensionContext,
configOverridePath?: string,
): Promise<PanelFlowResult> {
if (!ctx.hasUI) return { configChanged: false };
const config = state.config;
const oauthServers = Object.entries(config.mcpServers).filter(([, definition]) => supportsOAuth(definition));
if (oauthServers.length === 0) {
ctx.ui.notify("No OAuth-capable MCP servers are configured.", "warning");
return { configChanged: false };
}
const cache = loadMetadataCache();
const configPath = pi.getFlag("mcp-config") as string | undefined ?? configOverridePath;
const provenanceMap = getServerProvenance(configPath, ctx.cwd);
const callbacks = buildMcpPanelCallbacks(state, config, ctx);
const { createMcpPanel } = await import("./mcp-panel.ts");
await new Promise<void>((resolve) => {
ctx.ui.custom(
(tui, _theme, keybindings, done) => {
return createMcpPanel(config, cache, provenanceMap, callbacks, tui, () => {
done(undefined);
resolve();
}, {
authOnly: true,
keybindings,
noticeLines: ["Select an OAuth MCP server and press Enter or ctrl+a to authenticate."],
});
},
{ overlay: true, overlayOptions: { anchor: "center", width: 82 } },
);
});
return { configChanged: false };
}