@tyvm/knowhow 0.0.116 → 0.0.118

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,742 @@
1
+ import { Command } from "commander";
2
+ import { getConfig, updateConfig } from "../config";
3
+ import { McpConfig } from "../types";
4
+ import * as fs from "fs";
5
+ import { KnowhowSimpleClient, KNOWHOW_API_URL } from "../services/KnowhowClient";
6
+ import { McpService } from "../services/Mcp";
7
+ import http from "../utils/http";
8
+
9
+ // ──────────────────────────────────────────────────────────────────────────────
10
+ // Transport inference helper
11
+ // ──────────────────────────────────────────────────────────────────────────────
12
+
13
+ function inferTransport(opts: {
14
+ url?: string;
15
+ command?: string;
16
+ transport?: string;
17
+ }): "http" | "sse" | "stdio" {
18
+ if (opts.transport) return opts.transport as "http" | "sse" | "stdio";
19
+ if (opts.url) {
20
+ if (opts.url.endsWith("/sse") || opts.url.includes("/sse?")) return "sse";
21
+ return "http";
22
+ }
23
+ return "stdio";
24
+ }
25
+
26
+ // ──────────────────────────────────────────────────────────────────────────────
27
+ // Formatting helpers
28
+ // ──────────────────────────────────────────────────────────────────────────────
29
+
30
+ function formatLocalMcpList(mcps: McpConfig[], statusMap: Record<string, { connected: boolean; toolCount: number; error?: string }> = {}) {
31
+ if (!mcps || mcps.length === 0) {
32
+ console.log("No MCP servers configured locally.");
33
+ return;
34
+ }
35
+ console.log(`\n${"─".repeat(60)}`);
36
+ console.log(" Local MCP Servers");
37
+ console.log(`${"─".repeat(60)}`);
38
+ for (const mcp of mcps) {
39
+ const status = statusMap[mcp.name];
40
+ const transport = inferTransport(mcp);
41
+ const endpoint = mcp.url ? mcp.url : mcp.command ? `${mcp.command} ${(mcp.args || []).join(" ")}`.trim() : "(none)";
42
+ let statusIcon = "○";
43
+ let statusText = "not tested";
44
+ if (status) {
45
+ if (status.connected) {
46
+ statusIcon = "✓";
47
+ statusText = `connected (${status.toolCount} tools)`;
48
+ } else {
49
+ statusIcon = "✗";
50
+ statusText = `failed: ${status.error || "unknown error"}`;
51
+ }
52
+ }
53
+ console.log(`\n ${statusIcon} ${mcp.name}`);
54
+ console.log(` transport : ${transport}`);
55
+ console.log(` endpoint : ${endpoint}`);
56
+ if (mcp.authorization_token_file) {
57
+ console.log(` auth file : ${mcp.authorization_token_file}`);
58
+ }
59
+ if (mcp.autoConnect === false) {
60
+ console.log(` auto-connect: false`);
61
+ }
62
+ if (status) {
63
+ console.log(` status : ${statusText}`);
64
+ }
65
+ }
66
+ console.log(`${"─".repeat(60)}\n`);
67
+ }
68
+
69
+ function formatRemoteMcpList(servers: any[]) {
70
+ if (!servers || servers.length === 0) {
71
+ console.log("No remote MCP servers configured for this organisation.");
72
+ return;
73
+ }
74
+ console.log(`\n${"─".repeat(60)}`);
75
+ console.log(" Remote MCP Servers (backend)");
76
+ console.log(`${"─".repeat(60)}`);
77
+ for (const s of servers) {
78
+ const transport = s.url ? (s.url.endsWith("/sse") ? "sse" : "http") : "stdio";
79
+ const endpoint = s.url ? s.url : s.command ? `${s.command} ${(s.args || []).join(" ")}`.trim() : "(none)";
80
+ const enabledIcon = s.enabled !== false ? "✓" : "○";
81
+ console.log(`\n ${enabledIcon} ${s.name} (${s.uniqueName})`);
82
+ console.log(` id : ${s.id}`);
83
+ console.log(` transport : ${transport}`);
84
+ console.log(` endpoint : ${endpoint}`);
85
+ if (s.url) {
86
+ const proxyUrl = `${KNOWHOW_API_URL}/api/mcp-proxy/${s.id}/mcp`;
87
+ console.log(` proxy url : ${proxyUrl}`);
88
+ }
89
+ if (s.enabled === false) {
90
+ console.log(` enabled : false`);
91
+ }
92
+ if (s.authConfig && typeof s.authConfig === "object") {
93
+ const ac = s.authConfig as Record<string, any>;
94
+ console.log(` auth type : ${ac.type || "unknown"}`);
95
+ if (ac.type === "basic") {
96
+ console.log(` username : secret:${ac.usernameSecretKey || "(not set)"}`);
97
+ console.log(` password : secret:${ac.passwordSecretKey || "(not set)"}`);
98
+ } else if (ac.type === "api_key") {
99
+ console.log(` api key : secret:${ac.keySecretKey || "(not set)"} (${ac.location || "header"}:${ac.keyName || "?"})`);
100
+ } else if (ac.type === "oauth2_static" || ac.type === "oauth2_dynamic") {
101
+ console.log(` oauth : ${ac.tokenUrl || ac.discoveryUrl || "(url not set)"}`);
102
+ }
103
+ }
104
+ }
105
+ console.log(`${"─".repeat(60)}\n`);
106
+ }
107
+
108
+ // ──────────────────────────────────────────────────────────────────────────────
109
+ // Test connectivity of local servers
110
+ // ──────────────────────────────────────────────────────────────────────────────
111
+
112
+ async function testLocalConnections(mcps: McpConfig[]): Promise<Record<string, { connected: boolean; toolCount: number; error?: string }>> {
113
+ const results: Record<string, { connected: boolean; toolCount: number; error?: string }> = {};
114
+ const mcpService = new McpService();
115
+
116
+ for (const mcp of mcps) {
117
+ try {
118
+ await mcpService.createStdioClients([mcp]);
119
+ await mcpService.connectAutoServers();
120
+ const tools = await mcpService.getTools();
121
+ results[mcp.name] = { connected: true, toolCount: tools.length };
122
+ await mcpService.closeTransports();
123
+ // Reset for next iteration
124
+ (mcpService as any).clients = [];
125
+ (mcpService as any).transports = [];
126
+ (mcpService as any).config = [];
127
+ (mcpService as any).connected = [];
128
+ (mcpService as any).tools = [];
129
+ } catch (err: any) {
130
+ results[mcp.name] = { connected: false, toolCount: 0, error: err.message };
131
+ }
132
+ }
133
+ return results;
134
+ }
135
+
136
+ // ──────────────────────────────────────────────────────────────────────────────
137
+ // Main command registrar
138
+ // ──────────────────────────────────────────────────────────────────────────────
139
+
140
+ export function addMcpCommands(program: Command): void {
141
+ const mcp = program
142
+ .command("mcp")
143
+ .description("Manage MCP (Model Context Protocol) servers");
144
+
145
+ // ── mcp add ────────────────────────────────────────────────────────────────
146
+ mcp
147
+ .command("add <name> [endpoint]")
148
+ .description(
149
+ "Add an MCP server.\n" +
150
+ " For HTTP/SSE servers: knowhow mcp add myserver https://example.com/mcp\n" +
151
+ " For stdio servers: knowhow mcp add myserver --command npx --args '-y,some-mcp-server'\n" +
152
+ " Remote (backend): knowhow mcp add myserver https://example.com/mcp --remote"
153
+ )
154
+ .option("--transport <transport>", "Transport type: http, sse, stdio (auto-detected if omitted)")
155
+ .option("--command <cmd>", "Command to run for stdio servers (e.g. npx)")
156
+ .option("--args <args>", "Comma-separated args for stdio command (e.g. '-y,some-mcp-server')")
157
+ .option("--env <KEY=VALUE...>", "Environment variables for stdio servers (repeatable)", collect, [])
158
+ .option("--header <Header: Value...>", "HTTP headers (stored as authorization_token_file hint)", collect, [])
159
+ .option("--auth-token-file <path>", "Path to file containing the bearer/basic auth token")
160
+ .option("--auth-scheme <scheme>", "Auth scheme: bearer or basic (default: bearer)", "bearer")
161
+ .option("--no-auto-connect", "Do not auto-connect to this server on startup")
162
+ .option("--remote", "Add to the knowhow backend instead of the local config")
163
+ .option("--unique-name <uniqueName>", "Unique name for remote MCP server (defaults to <name>)")
164
+ .option("--secret-mapping <json>", "Secret mapping JSON for remote MCP (e.g. '{\"MY_ENV\": \"secret.name.field\"}')")
165
+ .option("--auth-config <json>", "Auth config JSON for remote MCP (e.g. '{\"type\":\"basic\",\"usernameSecretKey\":\"grafana.username\",\"passwordSecretKey\":\"grafana.password\"}')")
166
+ .action(async (name: string, endpoint: string | undefined, opts: any) => {
167
+ // Build stdioArgs from --command / --args options
168
+ const stdioArgs: string[] = [];
169
+ if (opts.command) {
170
+ stdioArgs.push(opts.command);
171
+ if (opts.args) {
172
+ // Support both comma-separated and space-separated args
173
+ const parsedArgs = opts.args.includes(",")
174
+ ? opts.args.split(",").map((a: string) => a.trim())
175
+ : opts.args.split(" ").map((a: string) => a.trim()).filter(Boolean);
176
+ stdioArgs.push(...parsedArgs);
177
+ }
178
+ }
179
+
180
+ const transport = inferTransport({ url: endpoint, command: stdioArgs[0], transport: opts.transport });
181
+
182
+ if (opts.remote) {
183
+ await addRemoteMcp(name, endpoint, transport, opts, stdioArgs);
184
+ } else {
185
+ await addLocalMcp(name, endpoint, transport, opts, stdioArgs);
186
+ }
187
+ });
188
+
189
+ // ── mcp list ───────────────────────────────────────────────────────────────
190
+ mcp
191
+ .command("list")
192
+ .description("List configured MCP servers")
193
+ .option("--remote", "List MCP servers from the knowhow backend")
194
+ .option("--test", "Test connectivity and show tool counts (local only)")
195
+ .action(async (opts: any) => {
196
+ if (opts.remote) {
197
+ await listRemoteMcps();
198
+ } else {
199
+ await listLocalMcps(opts.test);
200
+ }
201
+ });
202
+
203
+ // ── mcp remove ─────────────────────────────────────────────────────────────
204
+ mcp
205
+ .command("remove <name>")
206
+ .description("Remove an MCP server from the local config")
207
+ .option("--remote", "Remove from the knowhow backend instead")
208
+ .action(async (name: string, opts: any) => {
209
+ if (opts.remote) {
210
+ await removeRemoteMcp(name);
211
+ } else {
212
+ await removeLocalMcp(name);
213
+ }
214
+ });
215
+
216
+ // ── mcp get ────────────────────────────────────────────────────────────────
217
+ mcp
218
+ .command("get <name>")
219
+ .description("Show details for a specific MCP server")
220
+ .option("--remote", "Look up from the knowhow backend")
221
+ .action(async (name: string, opts: any) => {
222
+ if (opts.remote) {
223
+ await getRemoteMcp(name);
224
+ } else {
225
+ await getLocalMcp(name);
226
+ }
227
+ });
228
+
229
+ // ── mcp secrets ────────────────────────────────────────────────────────────
230
+ // ── mcp update ─────────────────────────────────────────────────────────────
231
+ mcp
232
+ .command("update <name>")
233
+ .description("Update a remote MCP server configuration")
234
+ .option("--remote", "Update in the knowhow backend (required)")
235
+ .option("--url <url>", "New URL for the server")
236
+ .option("--auth-config <json>", "New auth config JSON")
237
+ .option("--secret-mapping <json>", "New secret mapping JSON")
238
+ .option("--env <KEY=VALUE...>", "Environment variables (repeatable)", collect, [])
239
+ .option("--enabled <bool>", "Enable or disable (true/false)")
240
+ .action(async (name: string, opts: any) => {
241
+ if (!opts.remote) {
242
+ console.error("✗ mcp update currently only supports --remote. Use mcp remove + mcp add for local updates.");
243
+ process.exit(1);
244
+ }
245
+ await updateRemoteMcp(name, opts);
246
+ });
247
+
248
+ mcp
249
+ .command("secrets")
250
+ .description("Manage remote secrets for MCP servers (requires --remote)")
251
+ .option("--list", "List all org secrets")
252
+ .option("--create <name>", "Create a secret with the given name")
253
+ .option("--value <value>", "Value for the secret (used with --create)")
254
+ .option("--value-file <path>", "Read secret value from file (used with --create)")
255
+ .option("--delete <nameOrId>", "Delete a secret by name or id")
256
+ .action(async (opts: any) => {
257
+ if (opts.list) {
258
+ await listRemoteSecrets();
259
+ } else if (opts.create) {
260
+ let value = opts.value;
261
+ if (!value && opts.valueFile) {
262
+ value = fs.readFileSync(opts.valueFile, "utf-8").trim();
263
+ }
264
+ if (!value) {
265
+ console.error("✗ --value or --value-file is required with --create");
266
+ process.exit(1);
267
+ }
268
+ await createRemoteSecret(opts.create, value);
269
+ } else if (opts.delete) {
270
+ await deleteRemoteSecret(opts.delete);
271
+ } else {
272
+ // Default: list
273
+ await listRemoteSecrets();
274
+ }
275
+ });
276
+ }
277
+
278
+ // ──────────────────────────────────────────────────────────────────────────────
279
+ // Local operations
280
+ // ──────────────────────────────────────────────────────────────────────────────
281
+
282
+ async function addLocalMcp(
283
+ name: string,
284
+ endpoint: string | undefined,
285
+ transport: "http" | "sse" | "stdio",
286
+ opts: any,
287
+ stdioArgs: string[]
288
+ ) {
289
+ const config = await getConfig();
290
+ const mcps: McpConfig[] = config.mcps || [];
291
+
292
+ if (mcps.find((m) => m.name === name)) {
293
+ console.error(`✗ MCP server '${name}' already exists. Use 'mcp remove ${name}' first.`);
294
+ process.exit(1);
295
+ }
296
+
297
+ const entry: McpConfig = { name };
298
+
299
+ if (transport === "stdio") {
300
+ if (!stdioArgs.length) {
301
+ console.error("✗ Stdio transport requires a command. Use: knowhow mcp add <name> -- <command> [args...]");
302
+ process.exit(1);
303
+ }
304
+ entry.command = stdioArgs[0];
305
+ entry.args = stdioArgs.slice(1);
306
+ if (opts.env && opts.env.length) {
307
+ entry.env = parseEnvList(opts.env);
308
+ }
309
+ } else {
310
+ // http or sse
311
+ if (!endpoint) {
312
+ console.error("✗ HTTP/SSE transport requires a URL.");
313
+ process.exit(1);
314
+ }
315
+ entry.url = endpoint;
316
+
317
+ // Auth token file
318
+ if (opts.authTokenFile) {
319
+ entry.authorization_token_file = opts.authTokenFile;
320
+ // Store the auth scheme (bearer or basic) - only needed for non-default
321
+ if (opts.authScheme && opts.authScheme !== "bearer") {
322
+ entry.authorization_scheme = opts.authScheme as "bearer" | "basic";
323
+ }
324
+ }
325
+
326
+ // Inline headers (e.g. --header "Authorization: Bearer token")
327
+ if (opts.header && opts.header.length) {
328
+ const authHeader = (opts.header as string[]).find((h) =>
329
+ h.toLowerCase().startsWith("authorization:")
330
+ );
331
+ if (authHeader) {
332
+ const tokenMatch = authHeader.match(/:\s*(?:Bearer|Basic)\s+(.+)/i);
333
+ if (tokenMatch) {
334
+ entry.authorization_token = tokenMatch[1].trim();
335
+ }
336
+ }
337
+ }
338
+ }
339
+
340
+ if (opts.autoConnect === false) {
341
+ entry.autoConnect = false;
342
+ }
343
+
344
+ mcps.push(entry);
345
+ config.mcps = mcps;
346
+ await updateConfig(config);
347
+
348
+ console.log(`✓ Added MCP server '${name}' (transport: ${transport}) to .knowhow/knowhow.json`);
349
+ if (entry.url) console.log(` URL: ${entry.url}`);
350
+ if (entry.command) console.log(` Command: ${entry.command} ${(entry.args || []).join(" ")}`);
351
+ }
352
+
353
+ async function listLocalMcps(test = false) {
354
+ const config = await getConfig();
355
+ const mcps: McpConfig[] = config.mcps || [];
356
+
357
+ let statusMap: Record<string, { connected: boolean; toolCount: number; error?: string }> = {};
358
+ if (test && mcps.length > 0) {
359
+ console.log("Testing connections…");
360
+ statusMap = await testLocalConnections(mcps);
361
+ }
362
+
363
+ formatLocalMcpList(mcps, statusMap);
364
+ }
365
+
366
+ async function removeLocalMcp(name: string) {
367
+ const config = await getConfig();
368
+ const mcps: McpConfig[] = config.mcps || [];
369
+ const idx = mcps.findIndex((m) => m.name === name);
370
+ if (idx < 0) {
371
+ console.error(`✗ MCP server '${name}' not found in local config.`);
372
+ process.exit(1);
373
+ }
374
+ mcps.splice(idx, 1);
375
+ config.mcps = mcps;
376
+ await updateConfig(config);
377
+ console.log(`✓ Removed MCP server '${name}' from .knowhow/knowhow.json`);
378
+ }
379
+
380
+ async function getLocalMcp(name: string) {
381
+ const config = await getConfig();
382
+ const mcps: McpConfig[] = config.mcps || [];
383
+ const mcp = mcps.find((m) => m.name === name);
384
+ if (!mcp) {
385
+ console.error(`✗ MCP server '${name}' not found in local config.`);
386
+ process.exit(1);
387
+ }
388
+ console.log(JSON.stringify(mcp, null, 2));
389
+ }
390
+
391
+ // ──────────────────────────────────────────────────────────────────────────────
392
+ // Remote (backend) operations
393
+ // ──────────────────────────────────────────────────────────────────────────────
394
+
395
+ async function getRemoteClient() {
396
+ const client = new KnowhowSimpleClient();
397
+ await client.checkJwt();
398
+ return client;
399
+ }
400
+
401
+ async function addRemoteMcp(
402
+ name: string,
403
+ endpoint: string | undefined,
404
+ transport: "http" | "sse" | "stdio",
405
+ opts: any,
406
+ stdioArgs: string[]
407
+ ) {
408
+ const client = await getRemoteClient();
409
+ const uniqueName = opts.uniqueName || name;
410
+
411
+ const body: Record<string, any> = {
412
+ name,
413
+ uniqueName,
414
+ command: "url",
415
+ args: [],
416
+ };
417
+
418
+ if (transport === "stdio") {
419
+ if (!stdioArgs.length) {
420
+ console.error("✗ Stdio transport requires a command. Use: knowhow mcp add --remote <name> -- <command> [args...]");
421
+ process.exit(1);
422
+ }
423
+ body.command = stdioArgs[0];
424
+ body.args = stdioArgs.slice(1);
425
+ if (opts.env && opts.env.length) {
426
+ body.env = parseEnvList(opts.env);
427
+ }
428
+ } else {
429
+ if (!endpoint) {
430
+ console.error("✗ HTTP/SSE transport requires a URL.");
431
+ process.exit(1);
432
+ }
433
+ body.url = endpoint;
434
+ body.command = "url";
435
+ body.args = [];
436
+ if (opts.env && opts.env.length) {
437
+ body.env = parseEnvList(opts.env);
438
+ }
439
+ }
440
+
441
+ // Optional secretMapping (JSON string or object)
442
+ if (opts.secretMapping) {
443
+ try {
444
+ body.secretMapping = typeof opts.secretMapping === "string" ? JSON.parse(opts.secretMapping) : opts.secretMapping;
445
+ } catch {
446
+ console.error("✗ --secret-mapping must be valid JSON.");
447
+ process.exit(1);
448
+ }
449
+ }
450
+
451
+ // Optional authConfig (JSON string or object)
452
+ if (opts.authConfig) {
453
+ try {
454
+ body.authConfig = typeof opts.authConfig === "string" ? JSON.parse(opts.authConfig) : opts.authConfig;
455
+ } catch {
456
+ console.error("✗ --auth-config must be valid JSON.");
457
+ process.exit(1);
458
+ }
459
+ }
460
+
461
+ try {
462
+ const response = await http.post(
463
+ `${KNOWHOW_API_URL}/api/org-mcp-servers`,
464
+ body,
465
+ { headers: (client as any).headers }
466
+ );
467
+ const server = (response as any).data || response;
468
+ console.log(`✓ Created remote MCP server '${name}' (id: ${server.id})`);
469
+ const proxyUrl = `${KNOWHOW_API_URL}/api/mcp-proxy/${server.id}/mcp`;
470
+ console.log(` Proxy URL: ${proxyUrl}`);
471
+ console.log(`\nTo use this MCP locally via the backend proxy, add to your config:`);
472
+ console.log(` knowhow mcp add ${name}-remote ${proxyUrl} --auth-token-file .knowhow/.jwt`);
473
+
474
+ // Remind users to create secrets if authConfig references secret keys
475
+ if (body.authConfig) {
476
+ const ac = body.authConfig;
477
+ const secretsNeeded: string[] = [];
478
+ if (ac.type === "basic") {
479
+ if (ac.usernameSecretKey) secretsNeeded.push(ac.usernameSecretKey);
480
+ if (ac.passwordSecretKey) secretsNeeded.push(ac.passwordSecretKey);
481
+ } else if (ac.type === "api_key" && ac.keySecretKey) {
482
+ secretsNeeded.push(ac.keySecretKey);
483
+ } else if (ac.type === "oauth2_static") {
484
+ if (ac.clientIdSecretKey) secretsNeeded.push(ac.clientIdSecretKey);
485
+ if (ac.clientSecretSecretKey) secretsNeeded.push(ac.clientSecretSecretKey);
486
+ }
487
+ if (secretsNeeded.length > 0) {
488
+ console.log(`\n⚠ This server uses auth secrets. Ensure the following org secrets exist:`);
489
+ for (const key of secretsNeeded) {
490
+ console.log(` knowhow mcp secrets --create ${key} --value <YOUR_VALUE>`);
491
+ }
492
+ }
493
+ }
494
+ } catch (err: any) {
495
+ const msg = err?.response?.data?.message || err.message;
496
+ console.error(`✗ Failed to create remote MCP server: ${msg}`);
497
+ process.exit(1);
498
+ }
499
+ }
500
+
501
+ async function listRemoteMcps() {
502
+ const client = await getRemoteClient();
503
+
504
+ try {
505
+ const response = await http.get(
506
+ `${KNOWHOW_API_URL}/api/org-mcp-servers`,
507
+ { headers: (client as any).headers }
508
+ );
509
+ const servers: any[] = (response as any).data || response;
510
+ formatRemoteMcpList(Array.isArray(servers) ? servers : []);
511
+ } catch (err: any) {
512
+ const msg = err?.response?.data?.message || err.message;
513
+ console.error(`✗ Failed to list remote MCP servers: ${msg}`);
514
+ process.exit(1);
515
+ }
516
+ }
517
+
518
+ async function removeRemoteMcp(nameOrId: string) {
519
+ const client = await getRemoteClient();
520
+
521
+ // First fetch the list to resolve name → id
522
+ let servers: any[] = [];
523
+ try {
524
+ const response = await http.get(
525
+ `${KNOWHOW_API_URL}/api/org-mcp-servers`,
526
+ { headers: (client as any).headers }
527
+ );
528
+ servers = (response as any).data || response;
529
+ if (!Array.isArray(servers)) servers = [];
530
+ } catch (err: any) {
531
+ console.error(`✗ Failed to fetch remote MCP servers: ${err.message}`);
532
+ process.exit(1);
533
+ }
534
+
535
+ const server = servers.find((s) => s.id === nameOrId || s.name === nameOrId || s.uniqueName === nameOrId);
536
+ if (!server) {
537
+ console.error(`✗ Remote MCP server '${nameOrId}' not found.`);
538
+ process.exit(1);
539
+ }
540
+
541
+ try {
542
+ await http.delete(
543
+ `${KNOWHOW_API_URL}/api/org-mcp-servers/${server.id}`,
544
+ { headers: (client as any).headers }
545
+ );
546
+ console.log(`✓ Removed remote MCP server '${server.name}' (id: ${server.id})`);
547
+ } catch (err: any) {
548
+ const msg = err?.response?.data?.message || err.message;
549
+ console.error(`✗ Failed to remove remote MCP server: ${msg}`);
550
+ process.exit(1);
551
+ }
552
+ }
553
+
554
+ async function getRemoteMcp(nameOrId: string) {
555
+ const client = await getRemoteClient();
556
+
557
+ let servers: any[] = [];
558
+ try {
559
+ const response = await http.get(
560
+ `${KNOWHOW_API_URL}/api/org-mcp-servers`,
561
+ { headers: (client as any).headers }
562
+ );
563
+ servers = (response as any).data || response;
564
+ if (!Array.isArray(servers)) servers = [];
565
+ } catch (err: any) {
566
+ console.error(`✗ Failed to fetch remote MCP servers: ${err.message}`);
567
+ process.exit(1);
568
+ }
569
+
570
+ const server = servers.find((s) => s.id === nameOrId || s.name === nameOrId || s.uniqueName === nameOrId);
571
+ if (!server) {
572
+ console.error(`✗ Remote MCP server '${nameOrId}' not found.`);
573
+ process.exit(1);
574
+ }
575
+
576
+ console.log(JSON.stringify(server, null, 2));
577
+ const proxyUrl = `${KNOWHOW_API_URL}/api/mcp-proxy/${server.id}/mcp`;
578
+ console.log(`\n Proxy URL: ${proxyUrl}`);
579
+ }
580
+
581
+ async function updateRemoteMcp(nameOrId: string, opts: any) {
582
+ const client = await getRemoteClient();
583
+
584
+ // First list to resolve name → id
585
+ let servers: any[] = [];
586
+ try {
587
+ const response = await http.get(
588
+ `${KNOWHOW_API_URL}/api/org-mcp-servers`,
589
+ { headers: (client as any).headers }
590
+ );
591
+ servers = (response as any).data || response;
592
+ if (!Array.isArray(servers)) servers = [];
593
+ } catch (err: any) {
594
+ console.error(`✗ Failed to fetch remote MCP servers: ${err.message}`);
595
+ process.exit(1);
596
+ }
597
+
598
+ const server = servers.find((s) => s.id === nameOrId || s.name === nameOrId || s.uniqueName === nameOrId);
599
+ if (!server) {
600
+ console.error(`✗ Remote MCP server '${nameOrId}' not found.`);
601
+ process.exit(1);
602
+ }
603
+
604
+ const body: Record<string, any> = {};
605
+ if (opts.url) body.url = opts.url;
606
+ if (opts.enabled !== undefined) body.enabled = opts.enabled === "true";
607
+ if (opts.env && opts.env.length) body.env = parseEnvList(opts.env);
608
+ if (opts.secretMapping) {
609
+ try { body.secretMapping = JSON.parse(opts.secretMapping); } catch { console.error("✗ --secret-mapping must be valid JSON."); process.exit(1); }
610
+ }
611
+ if (opts.authConfig) {
612
+ try { body.authConfig = JSON.parse(opts.authConfig); } catch { console.error("✗ --auth-config must be valid JSON."); process.exit(1); }
613
+ }
614
+
615
+ try {
616
+ const response = await http.put(
617
+ `${KNOWHOW_API_URL}/api/org-mcp-servers/${server.id}`,
618
+ body,
619
+ { headers: (client as any).headers }
620
+ );
621
+ const updated = (response as any).data || response;
622
+ console.log(`✓ Updated remote MCP server '${updated.name}' (id: ${updated.id})`);
623
+ console.log(JSON.stringify(updated, null, 2));
624
+ } catch (err: any) {
625
+ const msg = err?.response?.data?.message || err.message;
626
+ console.error(`✗ Failed to update remote MCP server: ${msg}`);
627
+ process.exit(1);
628
+ }
629
+ }
630
+
631
+ // ──────────────────────────────────────────────────────────────────────────────
632
+ // Utility
633
+ // ──────────────────────────────────────────────────────────────────────────────
634
+
635
+ function collect(value: string, previous: string[]): string[] {
636
+ return previous.concat([value]);
637
+ }
638
+
639
+ function parseEnvList(envList: string[]): Record<string, string> {
640
+ const env: Record<string, string> = {};
641
+ for (const item of envList) {
642
+ const eqIdx = item.indexOf("=");
643
+ if (eqIdx > 0) {
644
+ env[item.slice(0, eqIdx)] = item.slice(eqIdx + 1);
645
+ }
646
+ }
647
+ return env;
648
+ }
649
+
650
+ // ──────────────────────────────────────────────────────────────────────────────
651
+ // Remote secrets operations
652
+ // ──────────────────────────────────────────────────────────────────────────────
653
+
654
+ async function listRemoteSecrets() {
655
+ const client = await getRemoteClient();
656
+
657
+ try {
658
+ const response = await http.get(
659
+ `${KNOWHOW_API_URL}/api/secrets/org`,
660
+ { headers: (client as any).headers }
661
+ );
662
+ const secrets: any[] = (response as any).data || response;
663
+ const list = Array.isArray(secrets) ? secrets : [];
664
+
665
+ if (list.length === 0) {
666
+ console.log("No org secrets found.");
667
+ return;
668
+ }
669
+
670
+ console.log(`\n${"─".repeat(60)}`);
671
+ console.log(" Remote Org Secrets");
672
+ console.log(`${"─".repeat(60)}`);
673
+ for (const s of list) {
674
+ console.log(`\n • ${s.name}`);
675
+ console.log(` id : ${s.id}`);
676
+ console.log(` created : ${s.createdAt}`);
677
+ console.log(` secret path: secret.${s.name}`);
678
+ }
679
+ console.log(`${"─".repeat(60)}\n`);
680
+ } catch (err: any) {
681
+ const msg = err?.response?.data?.message || err.message;
682
+ console.error(`✗ Failed to list remote secrets: ${msg}`);
683
+ process.exit(1);
684
+ }
685
+ }
686
+
687
+ async function createRemoteSecret(name: string, value: string) {
688
+ const client = await getRemoteClient();
689
+
690
+ try {
691
+ const response = await http.post(
692
+ `${KNOWHOW_API_URL}/api/secrets/org`,
693
+ { name, value },
694
+ { headers: (client as any).headers }
695
+ );
696
+ const secret = (response as any).data || response;
697
+ console.log(`✓ Created org secret '${name}' (id: ${secret.id})`);
698
+ console.log(` Secret path: secret.${name}`);
699
+ console.log(` Use in secretMapping: { "MY_ENV_VAR": "secret.${name}" }`);
700
+ console.log(` Use in authConfig usernameSecretKey: "${name}"`);
701
+ } catch (err: any) {
702
+ const msg = err?.response?.data?.message || err.message;
703
+ console.error(`✗ Failed to create remote secret: ${msg}`);
704
+ process.exit(1);
705
+ }
706
+ }
707
+
708
+ async function deleteRemoteSecret(nameOrId: string) {
709
+ const client = await getRemoteClient();
710
+
711
+ // First list to resolve name → id
712
+ let secrets: any[] = [];
713
+ try {
714
+ const response = await http.get(
715
+ `${KNOWHOW_API_URL}/api/secrets/org`,
716
+ { headers: (client as any).headers }
717
+ );
718
+ secrets = (response as any).data || response;
719
+ if (!Array.isArray(secrets)) secrets = [];
720
+ } catch (err: any) {
721
+ console.error(`✗ Failed to fetch remote secrets: ${err.message}`);
722
+ process.exit(1);
723
+ }
724
+
725
+ const secret = secrets.find((s) => s.id === nameOrId || s.name === nameOrId);
726
+ if (!secret) {
727
+ console.error(`✗ Remote secret '${nameOrId}' not found.`);
728
+ process.exit(1);
729
+ }
730
+
731
+ try {
732
+ await http.delete(
733
+ `${KNOWHOW_API_URL}/api/secrets/org/${secret.id}`,
734
+ { headers: (client as any).headers }
735
+ );
736
+ console.log(`✓ Deleted org secret '${secret.name}' (id: ${secret.id})`);
737
+ } catch (err: any) {
738
+ const msg = err?.response?.data?.message || err.message;
739
+ console.error(`✗ Failed to delete remote secret: ${msg}`);
740
+ process.exit(1);
741
+ }
742
+ }