@oh-my-pi/pi-coding-agent 11.8.3 → 11.9.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.
@@ -0,0 +1,1223 @@
1
+ /**
2
+ * MCP Command Controller
3
+ *
4
+ * Handles /mcp subcommands for managing MCP servers.
5
+ */
6
+ import { Spacer, Text } from "@oh-my-pi/pi-tui";
7
+ import { analyzeAuthError, discoverOAuthEndpoints, MCPManager } from "../../mcp";
8
+ import { connectToServer, disconnectServer, listTools } from "../../mcp/client";
9
+ import {
10
+ addMCPServer,
11
+ getMCPConfigPath,
12
+ readMCPConfigFile,
13
+ removeMCPServer,
14
+ updateMCPServer,
15
+ } from "../../mcp/config-writer";
16
+ import { MCPOAuthFlow } from "../../mcp/oauth-flow";
17
+ import type { MCPServerConfig } from "../../mcp/types";
18
+ import type { OAuthCredential } from "../../session/auth-storage";
19
+ import { DynamicBorder } from "../components/dynamic-border";
20
+ import { MCPAddWizard } from "../components/mcp-add-wizard";
21
+ import { theme } from "../theme/theme";
22
+ import type { InteractiveModeContext } from "../types";
23
+
24
+ function withTimeout<T>(promise: Promise<T>, timeoutMs: number, message: string): Promise<T> {
25
+ const { promise: timeoutPromise, reject } = Promise.withResolvers<T>();
26
+ const timer = setTimeout(() => reject(new Error(message)), timeoutMs);
27
+ return Promise.race([promise, timeoutPromise]).finally(() => clearTimeout(timer));
28
+ }
29
+
30
+ function parseCommandArgs(argsString: string): string[] {
31
+ const args: string[] = [];
32
+ let current = "";
33
+ let inQuote: string | null = null;
34
+
35
+ for (let i = 0; i < argsString.length; i++) {
36
+ const char = argsString[i];
37
+
38
+ if (inQuote) {
39
+ if (char === inQuote) {
40
+ inQuote = null;
41
+ } else {
42
+ current += char;
43
+ }
44
+ } else if (char === '"' || char === "'") {
45
+ inQuote = char;
46
+ } else if (char === " " || char === "\t") {
47
+ if (current) {
48
+ args.push(current);
49
+ current = "";
50
+ }
51
+ } else {
52
+ current += char;
53
+ }
54
+ }
55
+
56
+ if (current) {
57
+ args.push(current);
58
+ }
59
+
60
+ return args;
61
+ }
62
+
63
+ type MCPAddScope = "user" | "project";
64
+ type MCPAddTransport = "http" | "sse";
65
+
66
+ type MCPAddParsed = {
67
+ initialName?: string;
68
+ scope: MCPAddScope;
69
+ quickConfig?: MCPServerConfig;
70
+ isCommandQuickAdd?: boolean;
71
+ hasAuthToken?: boolean;
72
+ error?: string;
73
+ };
74
+
75
+ export class MCPCommandController {
76
+ constructor(private ctx: InteractiveModeContext) {}
77
+
78
+ /**
79
+ * Handle /mcp command and route to subcommands
80
+ */
81
+ async handle(text: string): Promise<void> {
82
+ const parts = text.trim().split(/\s+/);
83
+ const subcommand = parts[1]?.toLowerCase();
84
+
85
+ if (!subcommand || subcommand === "help") {
86
+ this.#showHelp();
87
+ return;
88
+ }
89
+
90
+ switch (subcommand) {
91
+ case "add":
92
+ await this.#handleAdd(text);
93
+ break;
94
+ case "list":
95
+ await this.#handleList();
96
+ break;
97
+ case "remove":
98
+ case "rm":
99
+ await this.#handleRemove(text);
100
+ break;
101
+ case "test":
102
+ await this.#handleTest(parts[2]);
103
+ break;
104
+ case "reauth":
105
+ await this.#handleReauth(parts[2]);
106
+ break;
107
+ case "unauth":
108
+ await this.#handleUnauth(parts[2]);
109
+ break;
110
+ case "enable":
111
+ await this.#handleSetEnabled(parts[2], true);
112
+ break;
113
+ case "disable":
114
+ await this.#handleSetEnabled(parts[2], false);
115
+ break;
116
+ case "reload":
117
+ await this.#handleReload();
118
+ break;
119
+ default:
120
+ this.ctx.showError(`Unknown subcommand: ${subcommand}. Type /mcp help for usage.`);
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Show help text
126
+ */
127
+ #showHelp(): void {
128
+ const helpText = [
129
+ "",
130
+ theme.bold("MCP Server Management"),
131
+ "",
132
+ "Manage Model Context Protocol (MCP) servers for external tool integrations.",
133
+ "",
134
+ theme.fg("accent", "Commands:"),
135
+ " /mcp add Add a new MCP server (interactive wizard)",
136
+ " /mcp add <name> [--scope project|user] [--url <url> --transport http|sse] [--token <token>] [-- <command...>]",
137
+ " /mcp list List all configured MCP servers",
138
+ " /mcp remove <name> [--scope project|user] Remove an MCP server (default: project)",
139
+ " /mcp test <name> Test connection to an MCP server",
140
+ " /mcp reauth <name> Reauthorize OAuth for an MCP server",
141
+ " /mcp unauth <name> Remove OAuth auth from an MCP server",
142
+ " /mcp enable <name> Enable an MCP server",
143
+ " /mcp disable <name> Disable an MCP server",
144
+ " /mcp reload Force reload and rediscover MCP runtime tools",
145
+ " /mcp help Show this help message",
146
+ "",
147
+ ].join("\n");
148
+
149
+ this.#showMessage(helpText);
150
+ }
151
+
152
+ #parseAddCommand(text: string): MCPAddParsed {
153
+ const prefixMatch = text.match(/^\/mcp\s+add\b\s*(.*)$/i);
154
+ const rest = prefixMatch?.[1]?.trim() ?? "";
155
+ if (!rest) {
156
+ return { scope: "project" };
157
+ }
158
+
159
+ const tokens = parseCommandArgs(rest);
160
+ if (tokens.length === 0) {
161
+ return { scope: "project" };
162
+ }
163
+
164
+ let name: string | undefined;
165
+ let scope: MCPAddScope = "project";
166
+ let url: string | undefined;
167
+ let transport: MCPAddTransport = "http";
168
+ let authToken: string | undefined;
169
+ let commandTokens: string[] | undefined;
170
+
171
+ let i = 0;
172
+ if (!tokens[0].startsWith("-")) {
173
+ name = tokens[0];
174
+ i = 1;
175
+ }
176
+
177
+ while (i < tokens.length) {
178
+ const argToken = tokens[i];
179
+ if (argToken === "--") {
180
+ commandTokens = tokens.slice(i + 1);
181
+ break;
182
+ }
183
+ if (argToken === "--scope") {
184
+ const value = tokens[i + 1];
185
+ if (!value || (value !== "project" && value !== "user")) {
186
+ return { scope, error: "Invalid --scope value. Use project or user." };
187
+ }
188
+ scope = value;
189
+ i += 2;
190
+ continue;
191
+ }
192
+ if (argToken === "--url") {
193
+ const value = tokens[i + 1];
194
+ if (!value) {
195
+ return { scope, error: "Missing value for --url." };
196
+ }
197
+ url = value;
198
+ i += 2;
199
+ continue;
200
+ }
201
+ if (argToken === "--transport") {
202
+ const value = tokens[i + 1];
203
+ if (!value || (value !== "http" && value !== "sse")) {
204
+ return { scope, error: "Invalid --transport value. Use http or sse." };
205
+ }
206
+ transport = value;
207
+ i += 2;
208
+ continue;
209
+ }
210
+ if (argToken === "--token") {
211
+ const value = tokens[i + 1];
212
+ if (!value) {
213
+ return { scope, error: "Missing value for --token." };
214
+ }
215
+ authToken = value;
216
+ i += 2;
217
+ continue;
218
+ }
219
+ return { scope, error: `Unknown option: ${argToken}` };
220
+ }
221
+
222
+ const hasQuick = Boolean(url) || Boolean(commandTokens && commandTokens.length > 0);
223
+ if (!hasQuick) {
224
+ return { scope, initialName: name };
225
+ }
226
+ if (!name) {
227
+ return { scope, error: "Server name required for quick add. Usage: /mcp add <name> ..." };
228
+ }
229
+ if (url && commandTokens && commandTokens.length > 0) {
230
+ return { scope, error: "Use either --url or -- <command...>, not both." };
231
+ }
232
+ if (authToken && !url) {
233
+ return { scope, error: "--token requires --url (HTTP/SSE transport)." };
234
+ }
235
+
236
+ if (commandTokens && commandTokens.length > 0) {
237
+ const [command, ...args] = commandTokens;
238
+ const config: MCPServerConfig = {
239
+ type: "stdio",
240
+ command,
241
+ args: args.length > 0 ? args : undefined,
242
+ };
243
+ return { scope, initialName: name, quickConfig: config, isCommandQuickAdd: true };
244
+ }
245
+
246
+ const useHttpTransport = transport === "http";
247
+ let normalizedUrl = url!;
248
+ if (!/^https?:\/\//i.test(normalizedUrl)) {
249
+ normalizedUrl = `https://${normalizedUrl}`;
250
+ }
251
+ const config: MCPServerConfig = {
252
+ type: useHttpTransport ? "http" : "sse",
253
+ url: normalizedUrl,
254
+ headers: authToken ? { Authorization: `Bearer ${authToken}` } : undefined,
255
+ };
256
+ return {
257
+ scope,
258
+ initialName: name,
259
+ quickConfig: config,
260
+ isCommandQuickAdd: false,
261
+ hasAuthToken: Boolean(authToken),
262
+ };
263
+ }
264
+
265
+ /**
266
+ * Handle /mcp add - Launch interactive wizard or quick-add from args
267
+ */
268
+ async #handleAdd(text: string): Promise<void> {
269
+ const parsed = this.#parseAddCommand(text);
270
+ if (parsed.error) {
271
+ this.ctx.showError(parsed.error);
272
+ return;
273
+ }
274
+ if (parsed.quickConfig && parsed.initialName) {
275
+ let finalConfig = parsed.quickConfig;
276
+
277
+ // Quick-add with URL should still perform auth detection and OAuth flow,
278
+ // matching wizard behavior. Command quick-add intentionally skips this.
279
+ if (!parsed.isCommandQuickAdd && (finalConfig.type === "http" || finalConfig.type === "sse")) {
280
+ try {
281
+ await this.#handleTestConnection(finalConfig);
282
+ } catch (error) {
283
+ if (parsed.hasAuthToken) {
284
+ this.ctx.showError(
285
+ `Authentication failed for "${parsed.initialName}": ${error instanceof Error ? error.message : String(error)}`,
286
+ );
287
+ return;
288
+ }
289
+ const authResult = analyzeAuthError(error as Error);
290
+ if (authResult.requiresAuth) {
291
+ let oauth = authResult.authType === "oauth" ? (authResult.oauth ?? null) : null;
292
+ if (!oauth && finalConfig.url) {
293
+ try {
294
+ oauth = await discoverOAuthEndpoints(finalConfig.url);
295
+ } catch {
296
+ // Ignore discovery error and handle below.
297
+ }
298
+ }
299
+
300
+ if (!oauth) {
301
+ this.ctx.showError(
302
+ `Authentication required for "${parsed.initialName}", but OAuth endpoints could not be discovered. ` +
303
+ `Use /mcp add ${parsed.initialName} (wizard) or configure auth manually.`,
304
+ );
305
+ return;
306
+ }
307
+
308
+ try {
309
+ const credentialId = await this.#handleOAuthFlow(
310
+ oauth.authorizationUrl,
311
+ oauth.tokenUrl,
312
+ oauth.clientId ?? "",
313
+ "",
314
+ oauth.scopes ?? "",
315
+ );
316
+ finalConfig = {
317
+ ...finalConfig,
318
+ auth: {
319
+ type: "oauth",
320
+ credentialId,
321
+ },
322
+ };
323
+ } catch (oauthError) {
324
+ this.ctx.showError(
325
+ `OAuth flow failed for "${parsed.initialName}": ${oauthError instanceof Error ? oauthError.message : String(oauthError)}`,
326
+ );
327
+ return;
328
+ }
329
+ }
330
+ }
331
+ }
332
+
333
+ await this.#handleWizardComplete(parsed.initialName, finalConfig, parsed.scope);
334
+ return;
335
+ }
336
+
337
+ // Save current editor state
338
+ const done = () => {
339
+ this.ctx.editorContainer.clear();
340
+ this.ctx.editorContainer.addChild(this.ctx.editor);
341
+ this.ctx.ui.setFocus(this.ctx.editor);
342
+ };
343
+
344
+ // Create wizard with OAuth handler and connection test
345
+ const wizard = new MCPAddWizard(
346
+ async (name: string, config: MCPServerConfig, scope: "user" | "project") => {
347
+ done();
348
+ await this.#handleWizardComplete(name, config, scope);
349
+ },
350
+ () => {
351
+ done();
352
+ this.#handleWizardCancel();
353
+ },
354
+ async (authUrl: string, tokenUrl: string, clientId: string, clientSecret: string, scopes: string) => {
355
+ return await this.#handleOAuthFlow(authUrl, tokenUrl, clientId, clientSecret, scopes);
356
+ },
357
+ async (config: MCPServerConfig) => {
358
+ return await this.#handleTestConnection(config);
359
+ },
360
+ () => {
361
+ this.ctx.ui.requestRender();
362
+ },
363
+ parsed.initialName,
364
+ );
365
+
366
+ // Replace editor with wizard
367
+ this.ctx.editorContainer.clear();
368
+ this.ctx.editorContainer.addChild(wizard);
369
+ this.ctx.ui.setFocus(wizard);
370
+ this.ctx.ui.requestRender();
371
+ }
372
+
373
+ /**
374
+ * Handle OAuth authentication flow for MCP server
375
+ */
376
+ async #handleOAuthFlow(
377
+ authUrl: string,
378
+ tokenUrl: string,
379
+ clientId: string,
380
+ clientSecret: string,
381
+ scopes: string,
382
+ ): Promise<string> {
383
+ const authStorage = this.ctx.session.modelRegistry.authStorage;
384
+ let parsedAuthUrl: URL;
385
+
386
+ // Validate OAuth URLs
387
+ try {
388
+ parsedAuthUrl = new URL(authUrl);
389
+ new URL(tokenUrl);
390
+ } catch (_error) {
391
+ throw new Error(
392
+ `Invalid OAuth URLs. Please check:\n Authorization URL: ${authUrl}\n Token URL: ${tokenUrl}`,
393
+ );
394
+ }
395
+
396
+ const resolvedClientId = clientId.trim() || parsedAuthUrl.searchParams.get("client_id") || undefined;
397
+
398
+ try {
399
+ // Create OAuth flow
400
+ const flow = new MCPOAuthFlow(
401
+ {
402
+ authorizationUrl: authUrl,
403
+ tokenUrl: tokenUrl,
404
+ clientId: resolvedClientId,
405
+ clientSecret: clientSecret || undefined,
406
+ scopes: scopes || undefined,
407
+ },
408
+ {
409
+ onAuth: (info: { url: string; instructions?: string }) => {
410
+ // Show auth URL prominently in chat
411
+ this.ctx.chatContainer.addChild(new Spacer(1));
412
+ this.ctx.chatContainer.addChild(
413
+ new Text(theme.fg("accent", "━━━ OAuth Authorization Required ━━━"), 1, 0),
414
+ );
415
+ this.ctx.chatContainer.addChild(new Spacer(1));
416
+ this.ctx.chatContainer.addChild(
417
+ new Text(theme.fg("muted", "Preparing browser authorization..."), 1, 0),
418
+ );
419
+ this.ctx.chatContainer.addChild(new Spacer(1));
420
+ this.ctx.chatContainer.addChild(
421
+ new Text(
422
+ theme.fg("muted", "Waiting for authorization... (Press Ctrl+C to cancel, 5 minute timeout)"),
423
+ 1,
424
+ 0,
425
+ ),
426
+ );
427
+ this.ctx.chatContainer.addChild(new Spacer(1));
428
+ this.ctx.chatContainer.addChild(
429
+ new Text(theme.fg("accent", "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"), 1, 0),
430
+ );
431
+ this.ctx.ui.requestRender();
432
+ const isWindows = process.platform === "win32";
433
+ const isMac = process.platform === "darwin";
434
+ const isLinux = process.platform === "linux";
435
+
436
+ // Try to open browser automatically
437
+ try {
438
+ if (isWindows) {
439
+ // Windows: use URL protocol handler directly to avoid cmd quoting issues.
440
+ Bun.spawn(["rundll32.exe", "url.dll,FileProtocolHandler", info.url], {
441
+ stdout: "ignore",
442
+ stderr: "ignore",
443
+ stdin: "ignore",
444
+ });
445
+ } else if (isMac) {
446
+ // macOS: Use 'open' command
447
+ Bun.spawn(["open", info.url], {
448
+ stdout: "ignore",
449
+ stderr: "ignore",
450
+ stdin: "ignore",
451
+ });
452
+ } else if (isLinux) {
453
+ // Linux: Try xdg-open
454
+ Bun.spawn(["xdg-open", info.url], {
455
+ stdout: "ignore",
456
+ stderr: "ignore",
457
+ stdin: "ignore",
458
+ });
459
+ }
460
+
461
+ // Show confirmation that browser should open
462
+ this.ctx.chatContainer.addChild(new Spacer(1));
463
+ this.ctx.chatContainer.addChild(
464
+ new Text(theme.fg("success", "→ Opening browser automatically..."), 1, 0),
465
+ );
466
+ this.ctx.chatContainer.addChild(new Spacer(1));
467
+ this.ctx.chatContainer.addChild(
468
+ new Text(theme.fg("muted", "Alternative if browser did not open:"), 1, 0),
469
+ );
470
+ this.ctx.chatContainer.addChild(
471
+ new Text(theme.fg("success", "Copy this exact URL in your browser:"), 1, 0),
472
+ );
473
+ this.ctx.chatContainer.addChild(new Text(theme.fg("accent", info.url), 1, 0));
474
+ if (isWindows) {
475
+ const openCmd = `rundll32.exe url.dll,FileProtocolHandler "${info.url.replace(/"/g, '""')}"`;
476
+ this.ctx.chatContainer.addChild(new Spacer(1));
477
+ this.ctx.chatContainer.addChild(new Text("Windows manual open command:", 1, 0));
478
+ this.ctx.chatContainer.addChild(new Text(openCmd, 1, 0));
479
+ }
480
+ this.ctx.ui.requestRender();
481
+ } catch (_error) {
482
+ // Show error if browser doesn't open
483
+ this.ctx.chatContainer.addChild(new Spacer(1));
484
+ this.ctx.chatContainer.addChild(
485
+ new Text(theme.fg("warning", "→ Could not open browser automatically"), 1, 0),
486
+ );
487
+ this.ctx.chatContainer.addChild(
488
+ new Text(theme.fg("success", "Copy this exact URL in your browser:"), 1, 0),
489
+ );
490
+ this.ctx.chatContainer.addChild(new Text(theme.fg("accent", info.url), 1, 0));
491
+ if (isWindows) {
492
+ const openCmd = `rundll32.exe url.dll,FileProtocolHandler "${info.url.replace(/"/g, '""')}"`;
493
+ this.ctx.chatContainer.addChild(new Spacer(1));
494
+ this.ctx.chatContainer.addChild(new Text("Windows manual open command:", 1, 0));
495
+ this.ctx.chatContainer.addChild(new Text(openCmd, 1, 0));
496
+ }
497
+ this.ctx.ui.requestRender();
498
+ }
499
+ },
500
+ onProgress: (message: string) => {
501
+ this.ctx.chatContainer.addChild(new Spacer(1));
502
+ this.ctx.chatContainer.addChild(new Text(theme.fg("muted", message), 1, 0));
503
+ this.ctx.ui.requestRender();
504
+ },
505
+ },
506
+ );
507
+
508
+ // Execute OAuth flow with 5 minute timeout
509
+ const credentials = await withTimeout(flow.login(), 5 * 60 * 1000, "OAuth flow timed out after 5 minutes");
510
+
511
+ this.ctx.chatContainer.addChild(new Spacer(1));
512
+ this.ctx.chatContainer.addChild(new Text(theme.fg("success", "✓ Authorization completed in browser."), 1, 0));
513
+ this.ctx.ui.requestRender();
514
+
515
+ // Generate a unique credential ID
516
+ const credentialId = `mcp_oauth_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
517
+
518
+ // Store credentials in auth storage
519
+ const oauthCredential: OAuthCredential = {
520
+ type: "oauth",
521
+ ...credentials,
522
+ };
523
+
524
+ // Store under a synthetic provider name
525
+ await authStorage.set(credentialId, oauthCredential);
526
+
527
+ return credentialId;
528
+ } catch (error) {
529
+ const errorMsg = error instanceof Error ? error.message : String(error);
530
+
531
+ // Provide helpful error messages based on failure type
532
+ if (errorMsg.includes("timeout") || errorMsg.includes("timed out")) {
533
+ throw new Error("OAuth flow timed out. Please try again.");
534
+ } else if (errorMsg.includes("403") || errorMsg.includes("unauthorized")) {
535
+ throw new Error("OAuth authorization failed. Please check your client credentials.");
536
+ } else if (errorMsg.includes("invalid_grant")) {
537
+ throw new Error("OAuth authorization code is invalid or expired. Please try again.");
538
+ } else if (errorMsg.includes("ECONNREFUSED") || errorMsg.includes("fetch failed")) {
539
+ throw new Error("Could not connect to OAuth server. Please check the URLs and your network connection.");
540
+ } else {
541
+ throw new Error(`OAuth authentication failed: ${errorMsg}`);
542
+ }
543
+ }
544
+ }
545
+
546
+ /**
547
+ * Test connection to an MCP server.
548
+ * Throws an error if connection fails (used for auto-detection).
549
+ */
550
+ async #handleTestConnection(config: MCPServerConfig): Promise<void> {
551
+ // Create temporary connection using a test name
552
+ const testName = `test_${Date.now()}`;
553
+ let resolvedConfig: MCPServerConfig;
554
+ if (this.ctx.mcpManager) {
555
+ resolvedConfig = await this.ctx.mcpManager.prepareConfig(config);
556
+ } else {
557
+ const tempManager = new MCPManager(process.cwd());
558
+ tempManager.setAuthStorage(this.ctx.session.modelRegistry.authStorage);
559
+ resolvedConfig = await tempManager.prepareConfig(config);
560
+ }
561
+
562
+ const connection = await connectToServer(testName, resolvedConfig);
563
+ await disconnectServer(connection);
564
+ }
565
+
566
+ async #findConfiguredServer(
567
+ name: string,
568
+ ): Promise<{ filePath: string; scope: "user" | "project"; config: MCPServerConfig } | null> {
569
+ const cwd = process.cwd();
570
+ const userPath = getMCPConfigPath("user", cwd);
571
+ const projectPath = getMCPConfigPath("project", cwd);
572
+
573
+ const [userConfig, projectConfig] = await Promise.all([
574
+ readMCPConfigFile(userPath),
575
+ readMCPConfigFile(projectPath),
576
+ ]);
577
+
578
+ if (userConfig.mcpServers?.[name]) {
579
+ return { filePath: userPath, scope: "user", config: userConfig.mcpServers[name] };
580
+ }
581
+ if (projectConfig.mcpServers?.[name]) {
582
+ return { filePath: projectPath, scope: "project", config: projectConfig.mcpServers[name] };
583
+ }
584
+ return null;
585
+ }
586
+
587
+ async #removeManagedOAuthCredential(credentialId: string | undefined): Promise<void> {
588
+ if (!credentialId || !credentialId.startsWith("mcp_oauth_")) return;
589
+ await this.ctx.session.modelRegistry.authStorage.remove(credentialId);
590
+ }
591
+
592
+ #stripOAuthAuth(config: MCPServerConfig): MCPServerConfig {
593
+ const next = { ...config } as MCPServerConfig & { auth?: { type: "oauth" | "apikey"; credentialId?: string } };
594
+ delete next.auth;
595
+ return next;
596
+ }
597
+
598
+ async #resolveOAuthEndpointsFromServer(config: MCPServerConfig): Promise<{
599
+ authorizationUrl: string;
600
+ tokenUrl: string;
601
+ clientId?: string;
602
+ scopes?: string;
603
+ }> {
604
+ // First test if server actually needs auth by connecting without OAuth
605
+ let connectionSucceeded = false;
606
+ let connectionError: Error | undefined;
607
+ try {
608
+ await this.#handleTestConnection(this.#stripOAuthAuth(config));
609
+ connectionSucceeded = true;
610
+ } catch (error) {
611
+ connectionError = error as Error;
612
+ }
613
+
614
+ // Server connected fine without auth — reauth is not needed
615
+ if (connectionSucceeded) {
616
+ throw new Error("Server connection succeeded without OAuth; reauthorization is not required.");
617
+ }
618
+
619
+ // Analyze the connection error to extract OAuth endpoints
620
+ const authResult = analyzeAuthError(connectionError!);
621
+ let oauth = authResult.authType === "oauth" ? (authResult.oauth ?? null) : null;
622
+
623
+ if (!oauth && (config.type === "http" || config.type === "sse") && config.url) {
624
+ oauth = await discoverOAuthEndpoints(config.url);
625
+ }
626
+
627
+ if (!oauth) {
628
+ throw new Error("Could not discover OAuth endpoints from server response.");
629
+ }
630
+
631
+ return oauth;
632
+ }
633
+
634
+ async #waitForServerConnectionWithAnimation(
635
+ name: string,
636
+ options?: { suppressDisconnectedWarning?: boolean },
637
+ ): Promise<"connected" | "connecting" | "disconnected"> {
638
+ if (!this.ctx.mcpManager) return "disconnected";
639
+
640
+ this.ctx.chatContainer.addChild(new Spacer(1));
641
+ const statusText = new Text(theme.fg("muted", `| Connecting to "${name}"...`), 1, 0);
642
+ this.ctx.chatContainer.addChild(statusText);
643
+ this.ctx.ui.requestRender();
644
+
645
+ const frames = ["|", "/", "-", "\\"];
646
+ let frame = 0;
647
+ const interval = setInterval(() => {
648
+ statusText.setText(theme.fg("muted", `${frames[frame % frames.length]} Connecting to "${name}"...`));
649
+ frame++;
650
+ this.ctx.ui.requestRender();
651
+ }, 120);
652
+
653
+ try {
654
+ try {
655
+ await withTimeout(this.ctx.mcpManager.waitForConnection(name), 10_000, "Connection still pending");
656
+ } catch {
657
+ // Ignore timeout/errors here and use status check below.
658
+ }
659
+ const state = this.ctx.mcpManager.getConnectionStatus(name);
660
+ if (state === "connected") {
661
+ // Connection may complete after initial reload; rebind runtime MCP tools now.
662
+ await this.ctx.session.refreshMCPTools(this.ctx.mcpManager.getTools());
663
+ }
664
+ if (state === "connected") {
665
+ statusText.setText(theme.fg("success", `✓ Connected to "${name}"`));
666
+ } else if (state === "connecting") {
667
+ statusText.setText(theme.fg("muted", `◌ "${name}" is still connecting...`));
668
+ } else {
669
+ statusText.setText(
670
+ options?.suppressDisconnectedWarning
671
+ ? theme.fg("muted", `◌ Connection check complete for "${name}"`)
672
+ : theme.fg("warning", `⚠ Could not connect to "${name}" yet`),
673
+ );
674
+ }
675
+ this.ctx.ui.requestRender();
676
+ return state;
677
+ } finally {
678
+ clearInterval(interval);
679
+ }
680
+ }
681
+
682
+ async #syncManagerConnection(name: string, config: MCPServerConfig): Promise<void> {
683
+ if (!this.ctx.mcpManager) return;
684
+ if (this.ctx.mcpManager.getConnectionStatus(name) !== "disconnected") return;
685
+ await this.ctx.mcpManager.connectServers({ [name]: config }, {});
686
+ if (this.ctx.mcpManager.getConnectionStatus(name) === "connected") {
687
+ await this.ctx.session.refreshMCPTools(this.ctx.mcpManager.getTools());
688
+ }
689
+ }
690
+
691
+ async #handleWizardComplete(name: string, config: MCPServerConfig, scope: "user" | "project"): Promise<void> {
692
+ try {
693
+ // Determine file path
694
+ const cwd = process.cwd();
695
+ const filePath = getMCPConfigPath(scope, cwd);
696
+
697
+ // Add server to config
698
+ await addMCPServer(filePath, name, config);
699
+
700
+ // Reload MCP manager
701
+ await this.#reloadMCP();
702
+ const state =
703
+ config.enabled === false
704
+ ? "disconnected"
705
+ : await this.#waitForServerConnectionWithAnimation(name, { suppressDisconnectedWarning: true });
706
+ let isConnected = state === "connected";
707
+ const isConnecting = state === "connecting";
708
+
709
+ // Fallback: if manager state is still disconnected but direct test works,
710
+ // report as connected to avoid false-negative messaging.
711
+ if (!isConnected && !isConnecting && config.enabled !== false) {
712
+ try {
713
+ await this.#handleTestConnection(config);
714
+ isConnected = true;
715
+ await this.#syncManagerConnection(name, config);
716
+ } catch {
717
+ // Keep disconnected status
718
+ }
719
+ }
720
+
721
+ // Show success message
722
+ const scopeLabel = scope === "user" ? "user" : "project";
723
+ const lines = ["", theme.fg("success", `✓ Added server "${name}" to ${scopeLabel} config`), ""];
724
+
725
+ if (isConnected) {
726
+ lines.push(theme.fg("success", `✓ Successfully connected to server`));
727
+ lines.push("");
728
+ } else if (isConnecting) {
729
+ lines.push(theme.fg("muted", `◌ Server is connecting in background...`));
730
+ lines.push(theme.fg("muted", ` Run ${theme.fg("accent", `/mcp test ${name}`)} in a few seconds.`));
731
+ lines.push("");
732
+ } else {
733
+ lines.push(theme.fg("warning", `⚠ Server added but not yet connected`));
734
+ lines.push(theme.fg("muted", ` Run ${theme.fg("accent", `/mcp test ${name}`)} to test the connection.`));
735
+ lines.push("");
736
+ }
737
+
738
+ lines.push(theme.fg("muted", `Run ${theme.fg("accent", "/mcp list")} to see all configured servers.`));
739
+ lines.push("");
740
+
741
+ this.#showMessage(lines.join("\n"));
742
+ } catch (error) {
743
+ const errorMsg = error instanceof Error ? error.message : String(error);
744
+
745
+ // Provide helpful error messages
746
+ let helpText = "";
747
+ if (errorMsg.includes("EACCES") || errorMsg.includes("permission denied")) {
748
+ helpText = "\n\nTip: Check file permissions for the config directory.";
749
+ } else if (errorMsg.includes("ENOSPC")) {
750
+ helpText = "\n\nTip: Insufficient disk space.";
751
+ } else if (errorMsg.includes("already exists")) {
752
+ helpText = `\n\nTip: Use ${theme.fg("accent", "/mcp list")} to see existing servers.`;
753
+ }
754
+
755
+ this.ctx.showError(`Failed to add server: ${errorMsg}${helpText}`);
756
+ }
757
+ }
758
+
759
+ #handleWizardCancel(): void {
760
+ this.#showMessage(
761
+ [
762
+ "",
763
+ theme.fg("muted", "Server creation cancelled."),
764
+ "",
765
+ theme.fg("dim", "Tip: Press Ctrl+C or Esc anytime to cancel"),
766
+ "",
767
+ ].join("\n"),
768
+ );
769
+ }
770
+
771
+ /**
772
+ * Handle /mcp list - Show all configured servers
773
+ */
774
+ async #handleList(): Promise<void> {
775
+ try {
776
+ const cwd = process.cwd();
777
+
778
+ // Load from both user and project configs
779
+ const userPath = getMCPConfigPath("user", cwd);
780
+ const projectPath = getMCPConfigPath("project", cwd);
781
+
782
+ const [userConfig, projectConfig] = await Promise.all([
783
+ readMCPConfigFile(userPath),
784
+ readMCPConfigFile(projectPath),
785
+ ]);
786
+
787
+ const userServers = Object.keys(userConfig.mcpServers ?? {});
788
+ const projectServers = Object.keys(projectConfig.mcpServers ?? {});
789
+
790
+ if (userServers.length === 0 && projectServers.length === 0) {
791
+ this.#showMessage(
792
+ [
793
+ "",
794
+ theme.fg("muted", "No MCP servers configured."),
795
+ "",
796
+ `Use ${theme.fg("accent", "/mcp add")} to add a server.`,
797
+ "",
798
+ ].join("\n"),
799
+ );
800
+ return;
801
+ }
802
+
803
+ const lines: string[] = ["", theme.bold("Configured MCP Servers"), ""];
804
+
805
+ // Show user-level servers
806
+ if (userServers.length > 0) {
807
+ lines.push(theme.fg("accent", "User level") + theme.fg("muted", ` (~/.omp/mcp.json):`));
808
+ for (const name of userServers) {
809
+ const config = userConfig.mcpServers![name];
810
+ const type = config.type ?? "stdio";
811
+ const state =
812
+ config.enabled === false
813
+ ? "inactive"
814
+ : (this.ctx.mcpManager?.getConnectionStatus(name) ?? "disconnected");
815
+ const status =
816
+ state === "inactive"
817
+ ? theme.fg("warning", " ◌ inactive")
818
+ : state === "connected"
819
+ ? theme.fg("success", " ● connected")
820
+ : state === "connecting"
821
+ ? theme.fg("muted", " ◌ connecting")
822
+ : theme.fg("muted", " ○ not connected");
823
+ lines.push(` ${theme.fg("accent", name)}${status} ${theme.fg("dim", `[${type}]`)}`);
824
+ }
825
+ lines.push("");
826
+ }
827
+
828
+ // Show project-level servers
829
+ if (projectServers.length > 0) {
830
+ lines.push(theme.fg("accent", "Project level") + theme.fg("muted", ` (.omp/mcp.json):`));
831
+ for (const name of projectServers) {
832
+ const config = projectConfig.mcpServers![name];
833
+ const type = config.type ?? "stdio";
834
+ const state =
835
+ config.enabled === false
836
+ ? "inactive"
837
+ : (this.ctx.mcpManager?.getConnectionStatus(name) ?? "disconnected");
838
+ const status =
839
+ state === "inactive"
840
+ ? theme.fg("warning", " ◌ inactive")
841
+ : state === "connected"
842
+ ? theme.fg("success", " ● connected")
843
+ : state === "connecting"
844
+ ? theme.fg("muted", " ◌ connecting")
845
+ : theme.fg("muted", " ○ not connected");
846
+ lines.push(` ${theme.fg("accent", name)}${status} ${theme.fg("dim", `[${type}]`)}`);
847
+ }
848
+ lines.push("");
849
+ }
850
+
851
+ this.#showMessage(lines.join("\n"));
852
+ } catch (error) {
853
+ this.ctx.showError(`Failed to list servers: ${error instanceof Error ? error.message : String(error)}`);
854
+ }
855
+ }
856
+
857
+ /**
858
+ * Handle /mcp remove <name> - Remove a server
859
+ */
860
+ async #handleRemove(text: string): Promise<void> {
861
+ const match = text.match(/^\/mcp\s+(?:remove|rm)\b\s*(.*)$/i);
862
+ const rest = match?.[1]?.trim() ?? "";
863
+ const tokens = parseCommandArgs(rest);
864
+
865
+ let name: string | undefined;
866
+ let scope: "project" | "user" = "project";
867
+ let i = 0;
868
+
869
+ if (tokens.length > 0 && !tokens[0].startsWith("-")) {
870
+ name = tokens[0];
871
+ i = 1;
872
+ }
873
+
874
+ while (i < tokens.length) {
875
+ const token = tokens[i];
876
+ if (token === "--scope") {
877
+ const value = tokens[i + 1];
878
+ if (!value || (value !== "project" && value !== "user")) {
879
+ this.ctx.showError("Invalid --scope value. Use project or user.");
880
+ return;
881
+ }
882
+ scope = value;
883
+ i += 2;
884
+ continue;
885
+ }
886
+ this.ctx.showError(`Unknown option: ${token}`);
887
+ return;
888
+ }
889
+
890
+ if (!name) {
891
+ this.ctx.showError("Server name required. Usage: /mcp remove <name> [--scope project|user]");
892
+ return;
893
+ }
894
+
895
+ try {
896
+ const cwd = process.cwd();
897
+ const userPath = getMCPConfigPath("user", cwd);
898
+ const projectPath = getMCPConfigPath("project", cwd);
899
+ const filePath = scope === "user" ? userPath : projectPath;
900
+ const config = await readMCPConfigFile(filePath);
901
+ if (!config.mcpServers?.[name]) {
902
+ this.ctx.showError(`Server "${name}" not found in ${scope} config.`);
903
+ return;
904
+ }
905
+
906
+ // Disconnect if connected
907
+ if (this.ctx.mcpManager?.getConnection(name)) {
908
+ await this.ctx.mcpManager.disconnectServer(name);
909
+ }
910
+
911
+ // Remove from config
912
+ await removeMCPServer(filePath, name);
913
+
914
+ // Reload MCP manager
915
+ await this.#reloadMCP();
916
+
917
+ this.#showMessage(["", theme.fg("success", `✓ Removed server "${name}" from ${scope} config`), ""].join("\n"));
918
+ } catch (error) {
919
+ this.ctx.showError(`Failed to remove server: ${error instanceof Error ? error.message : String(error)}`);
920
+ }
921
+ }
922
+
923
+ /**
924
+ * Handle /mcp test <name> - Test connection to a server
925
+ */
926
+ async #handleTest(name: string | undefined): Promise<void> {
927
+ if (!name) {
928
+ this.ctx.showError("Server name required. Usage: /mcp test <name>");
929
+ return;
930
+ }
931
+
932
+ try {
933
+ const cwd = process.cwd();
934
+ const userPath = getMCPConfigPath("user", cwd);
935
+ const projectPath = getMCPConfigPath("project", cwd);
936
+
937
+ // Find the server config
938
+ const [userConfig, projectConfig] = await Promise.all([
939
+ readMCPConfigFile(userPath),
940
+ readMCPConfigFile(projectPath),
941
+ ]);
942
+
943
+ const config = userConfig.mcpServers?.[name] ?? projectConfig.mcpServers?.[name];
944
+
945
+ if (!config) {
946
+ this.ctx.showError(
947
+ `Server "${name}" not found.\n\nTip: Run ${theme.fg("accent", "/mcp list")} to see available servers.`,
948
+ );
949
+ return;
950
+ }
951
+ if (config.enabled === false) {
952
+ this.ctx.showError(`Server "${name}" is disabled. Run /mcp enable ${name} first.`);
953
+ return;
954
+ }
955
+
956
+ this.#showMessage(["", theme.fg("muted", `Testing connection to "${name}"...`), ""].join("\n"));
957
+
958
+ // Resolve auth config if needed
959
+ let resolvedConfig: MCPServerConfig;
960
+ if (this.ctx.mcpManager) {
961
+ resolvedConfig = await this.ctx.mcpManager.prepareConfig(config);
962
+ } else {
963
+ const tempManager = new MCPManager(process.cwd());
964
+ tempManager.setAuthStorage(this.ctx.session.modelRegistry.authStorage);
965
+ resolvedConfig = await tempManager.prepareConfig(config);
966
+ }
967
+
968
+ // Create temporary connection
969
+ const connection = await connectToServer(name, resolvedConfig);
970
+
971
+ try {
972
+ // List tools to verify connection
973
+ const tools = await listTools(connection);
974
+
975
+ const lines = [
976
+ "",
977
+ theme.fg("success", `✓ Successfully connected to "${name}"`),
978
+ "",
979
+ ` Server: ${connection.serverInfo.name} v${connection.serverInfo.version}`,
980
+ ` Tools: ${tools.length}`,
981
+ ];
982
+
983
+ // Show tool names if there are any
984
+ if (tools.length > 0 && tools.length <= 10) {
985
+ lines.push("");
986
+ lines.push(" Available tools:");
987
+ for (const tool of tools) {
988
+ lines.push(` • ${tool.name}`);
989
+ }
990
+ }
991
+
992
+ lines.push("");
993
+ await this.#syncManagerConnection(name, config);
994
+ this.#showMessage(lines.join("\n"));
995
+ } finally {
996
+ // Disconnect test connection
997
+ await disconnectServer(connection);
998
+ }
999
+ } catch (error) {
1000
+ const errorMsg = error instanceof Error ? error.message : String(error);
1001
+
1002
+ // Provide helpful error messages
1003
+ let helpText = "";
1004
+ if (errorMsg.includes("ENOENT") || errorMsg.includes("not found")) {
1005
+ helpText = "\n\nTip: Check that the command or URL is correct.";
1006
+ } else if (errorMsg.includes("EACCES")) {
1007
+ helpText = "\n\nTip: Check file/command permissions.";
1008
+ } else if (errorMsg.includes("ECONNREFUSED")) {
1009
+ helpText = "\n\nTip: Check that the server is running and the URL/port is correct.";
1010
+ } else if (errorMsg.includes("timeout")) {
1011
+ helpText = "\n\nTip: The server may be slow or unresponsive. Try increasing the timeout.";
1012
+ } else if (errorMsg.includes("401") || errorMsg.includes("403")) {
1013
+ helpText = "\n\nTip: Check your authentication credentials.";
1014
+ }
1015
+
1016
+ this.ctx.showError(`Failed to connect to "${name}": ${errorMsg}${helpText}`);
1017
+ }
1018
+ }
1019
+
1020
+ async #handleSetEnabled(name: string | undefined, enabled: boolean): Promise<void> {
1021
+ if (!name) {
1022
+ this.ctx.showError(`Server name required. Usage: /mcp ${enabled ? "enable" : "disable"} <name>`);
1023
+ return;
1024
+ }
1025
+
1026
+ try {
1027
+ const found = await this.#findConfiguredServer(name);
1028
+ if (!found) {
1029
+ this.ctx.showError(`Server "${name}" not found.`);
1030
+ return;
1031
+ }
1032
+
1033
+ if ((found.config.enabled ?? true) === enabled) {
1034
+ this.#showMessage(
1035
+ ["", theme.fg("muted", `Server "${name}" is already ${enabled ? "enabled" : "disabled"}.`), ""].join(
1036
+ "\n",
1037
+ ),
1038
+ );
1039
+ return;
1040
+ }
1041
+
1042
+ const updated: MCPServerConfig = { ...found.config, enabled };
1043
+ await updateMCPServer(found.filePath, name, updated);
1044
+ await this.#reloadMCP();
1045
+
1046
+ let status = "";
1047
+ if (enabled) {
1048
+ const state = await this.#waitForServerConnectionWithAnimation(name);
1049
+ status =
1050
+ state === "connected"
1051
+ ? theme.fg("success", "Connected")
1052
+ : state === "connecting"
1053
+ ? theme.fg("muted", "Connecting")
1054
+ : theme.fg("warning", "Not connected yet");
1055
+ }
1056
+
1057
+ const lines = [
1058
+ "",
1059
+ theme.fg("success", `✓ ${enabled ? "Enabled" : "Disabled"} "${name}" (${found.scope} config)`),
1060
+ ];
1061
+ if (status) {
1062
+ lines.push("");
1063
+ lines.push(` Status: ${status}`);
1064
+ }
1065
+ lines.push("");
1066
+ this.#showMessage(lines.join("\n"));
1067
+ } catch (error) {
1068
+ this.ctx.showError(
1069
+ `Failed to ${enabled ? "enable" : "disable"} server: ${error instanceof Error ? error.message : String(error)}`,
1070
+ );
1071
+ }
1072
+ }
1073
+
1074
+ async #handleUnauth(name: string | undefined): Promise<void> {
1075
+ if (!name) {
1076
+ this.ctx.showError("Server name required. Usage: /mcp unauth <name>");
1077
+ return;
1078
+ }
1079
+
1080
+ try {
1081
+ const found = await this.#findConfiguredServer(name);
1082
+ if (!found) {
1083
+ this.ctx.showError(`Server "${name}" not found.`);
1084
+ return;
1085
+ }
1086
+
1087
+ const currentAuth = (
1088
+ found.config as MCPServerConfig & { auth?: { type: "oauth" | "apikey"; credentialId?: string } }
1089
+ ).auth;
1090
+ if (currentAuth?.type === "oauth") {
1091
+ await this.#removeManagedOAuthCredential(currentAuth.credentialId);
1092
+ }
1093
+
1094
+ const updated = this.#stripOAuthAuth(found.config);
1095
+ await updateMCPServer(found.filePath, name, updated);
1096
+ await this.#reloadMCP();
1097
+
1098
+ this.#showMessage(
1099
+ ["", theme.fg("success", `✓ Cleared auth for "${name}" (${found.scope} config)`), ""].join("\n"),
1100
+ );
1101
+ } catch (error) {
1102
+ this.ctx.showError(`Failed to clear auth: ${error instanceof Error ? error.message : String(error)}`);
1103
+ }
1104
+ }
1105
+
1106
+ async #handleReauth(name: string | undefined): Promise<void> {
1107
+ if (!name) {
1108
+ this.ctx.showError("Server name required. Usage: /mcp reauth <name>");
1109
+ return;
1110
+ }
1111
+
1112
+ try {
1113
+ const found = await this.#findConfiguredServer(name);
1114
+ if (!found) {
1115
+ this.ctx.showError(`Server "${name}" not found.`);
1116
+ return;
1117
+ }
1118
+
1119
+ if (found.config.enabled === false) {
1120
+ this.ctx.showError(`Server "${name}" is disabled. Run /mcp enable ${name} first.`);
1121
+ return;
1122
+ }
1123
+
1124
+ const currentAuth = (
1125
+ found.config as MCPServerConfig & { auth?: { type: "oauth" | "apikey"; credentialId?: string } }
1126
+ ).auth;
1127
+ if (currentAuth?.type === "oauth") {
1128
+ await this.#removeManagedOAuthCredential(currentAuth.credentialId);
1129
+ }
1130
+
1131
+ const baseConfig = this.#stripOAuthAuth(found.config);
1132
+ const oauth = await this.#resolveOAuthEndpointsFromServer(baseConfig);
1133
+
1134
+ this.#showMessage(["", theme.fg("muted", `Reauthorizing "${name}"...`), ""].join("\n"));
1135
+
1136
+ const credentialId = await this.#handleOAuthFlow(
1137
+ oauth.authorizationUrl,
1138
+ oauth.tokenUrl,
1139
+ oauth.clientId ?? "",
1140
+ "",
1141
+ oauth.scopes ?? "",
1142
+ );
1143
+
1144
+ const updated: MCPServerConfig = {
1145
+ ...baseConfig,
1146
+ auth: {
1147
+ type: "oauth",
1148
+ credentialId,
1149
+ },
1150
+ };
1151
+ await updateMCPServer(found.filePath, name, updated);
1152
+ await this.#reloadMCP();
1153
+ const state = await this.#waitForServerConnectionWithAnimation(name);
1154
+
1155
+ const lines = [
1156
+ "",
1157
+ theme.fg("success", `✓ Reauthorized "${name}" (${found.scope} config)`),
1158
+ "",
1159
+ ` Status: ${
1160
+ state === "connected"
1161
+ ? theme.fg("success", "connected")
1162
+ : state === "connecting"
1163
+ ? theme.fg("muted", "connecting")
1164
+ : theme.fg("warning", "not connected")
1165
+ }`,
1166
+ "",
1167
+ ];
1168
+ this.#showMessage(lines.join("\n"));
1169
+ } catch (error) {
1170
+ this.ctx.showError(`Failed to reauthorize server: ${error instanceof Error ? error.message : String(error)}`);
1171
+ }
1172
+ }
1173
+
1174
+ async #handleReload(): Promise<void> {
1175
+ try {
1176
+ this.#showMessage(["", theme.fg("muted", "Reloading MCP servers and runtime tools..."), ""].join("\n"));
1177
+ await this.#reloadMCP();
1178
+ const connectedCount = this.ctx.mcpManager?.getConnectedServers().length ?? 0;
1179
+ this.#showMessage(
1180
+ ["", theme.fg("success", "✓ MCP reload complete"), ` Connected servers: ${connectedCount}`, ""].join("\n"),
1181
+ );
1182
+ } catch (error) {
1183
+ this.ctx.showError(`Failed to reload MCP: ${error instanceof Error ? error.message : String(error)}`);
1184
+ }
1185
+ }
1186
+
1187
+ /**
1188
+ * Reload MCP manager with new configs
1189
+ */
1190
+ async #reloadMCP(): Promise<void> {
1191
+ if (!this.ctx.mcpManager) {
1192
+ return;
1193
+ }
1194
+
1195
+ // Disconnect all existing servers
1196
+ await this.ctx.mcpManager.disconnectAll();
1197
+
1198
+ // Rediscover and connect
1199
+ const result = await this.ctx.mcpManager.discoverAndConnect();
1200
+ await this.ctx.session.refreshMCPTools(this.ctx.mcpManager.getTools());
1201
+
1202
+ // Show any connection errors
1203
+ if (result.errors.size > 0) {
1204
+ const errorLines = ["", theme.fg("warning", "Some servers failed to connect:"), ""];
1205
+ for (const [serverName, error] of result.errors.entries()) {
1206
+ errorLines.push(` ${serverName}: ${error}`);
1207
+ }
1208
+ errorLines.push("");
1209
+ this.#showMessage(errorLines.join("\n"));
1210
+ }
1211
+ }
1212
+
1213
+ /**
1214
+ * Show a message in the chat
1215
+ */
1216
+ #showMessage(text: string): void {
1217
+ this.ctx.chatContainer.addChild(new Spacer(1));
1218
+ this.ctx.chatContainer.addChild(new DynamicBorder());
1219
+ this.ctx.chatContainer.addChild(new Text(text, 1, 1));
1220
+ this.ctx.chatContainer.addChild(new DynamicBorder());
1221
+ this.ctx.ui.requestRender();
1222
+ }
1223
+ }