|
| 1 | +import type { Command } from "commander"; |
| 2 | +import { |
| 3 | + listProjectsOperation, |
| 4 | + listProjectServersOperation, |
| 5 | + PlatformApiError, |
| 6 | + showServersOperation, |
| 7 | +} from "@mcpjam/sdk/platform"; |
| 8 | +import { |
| 9 | + formatProjectServersHuman, |
| 10 | + formatProjectsHuman, |
| 11 | + formatShowServersHuman, |
| 12 | +} from "../lib/projects-render.js"; |
| 13 | +import { writeResult } from "../lib/output.js"; |
| 14 | +import { buildPlatformClient, toCliError } from "../lib/platform-client.js"; |
| 15 | +import { getGlobalOptions } from "../lib/server-config.js"; |
| 16 | + |
| 17 | +type PlatformOptions = { |
| 18 | + apiKey?: string; |
| 19 | + apiUrl?: string; |
| 20 | + project?: string; |
| 21 | +}; |
| 22 | + |
| 23 | +function addPlatformOptions(command: Command): Command { |
| 24 | + return command |
| 25 | + .option("--api-key <key>", "MCPJam sk_ API key (overrides MCPJAM_API_KEY)") |
| 26 | + .option( |
| 27 | + "--api-url <url>", |
| 28 | + "MCPJam API base URL (defaults to https://app.mcpjam.com/api/v1)", |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +async function runPlatformCommand<TOutput>( |
| 33 | + options: PlatformOptions, |
| 34 | + timeoutMs: number, |
| 35 | + execute: (context: { |
| 36 | + client: ReturnType<typeof buildPlatformClient>["client"]; |
| 37 | + signal: AbortSignal; |
| 38 | + }) => Promise<TOutput>, |
| 39 | +): Promise<TOutput> { |
| 40 | + const controller = new AbortController(); |
| 41 | + const timeoutHandle = setTimeout(() => { |
| 42 | + controller.abort( |
| 43 | + new PlatformApiError( |
| 44 | + `Request timed out after ${timeoutMs}ms`, |
| 45 | + "TIMEOUT", |
| 46 | + { |
| 47 | + status: 0, |
| 48 | + }, |
| 49 | + ), |
| 50 | + ); |
| 51 | + }, timeoutMs); |
| 52 | + timeoutHandle.unref?.(); |
| 53 | + |
| 54 | + try { |
| 55 | + const { client } = buildPlatformClient({ ...options, timeoutMs }); |
| 56 | + return await execute({ client, signal: controller.signal }); |
| 57 | + } catch (error) { |
| 58 | + // When OUR deadline fired, surface the armed TIMEOUT error: depending |
| 59 | + // on the fetch implementation, the rejection may be a bare AbortError |
| 60 | + // that would otherwise map to INTERNAL_ERROR. |
| 61 | + if ( |
| 62 | + controller.signal.aborted && |
| 63 | + controller.signal.reason instanceof PlatformApiError |
| 64 | + ) { |
| 65 | + throw toCliError(controller.signal.reason); |
| 66 | + } |
| 67 | + throw toCliError(error); |
| 68 | + } finally { |
| 69 | + clearTimeout(timeoutHandle); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export function registerProjectsCommands(program: Command): void { |
| 74 | + const projects = program |
| 75 | + .command("projects") |
| 76 | + .description("Operate the MCP servers saved in your hosted MCPJam projects"); |
| 77 | + |
| 78 | + addPlatformOptions( |
| 79 | + projects.command("list").description("List the projects you can access"), |
| 80 | + ).action(async (options: PlatformOptions, command) => { |
| 81 | + const globalOptions = getGlobalOptions(command); |
| 82 | + const result = await runPlatformCommand( |
| 83 | + options, |
| 84 | + globalOptions.timeout, |
| 85 | + ({ client, signal }) => |
| 86 | + listProjectsOperation.execute({}, { client, signal }), |
| 87 | + ); |
| 88 | + |
| 89 | + if (globalOptions.format === "human") { |
| 90 | + process.stdout.write(`${formatProjectsHuman(result.items)}\n`); |
| 91 | + } else { |
| 92 | + // Operation payload verbatim — keeps pagination fields like |
| 93 | + // nextCursor, matching the sibling commands and the MCP tool. |
| 94 | + writeResult(result, globalOptions.format); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + addPlatformOptions( |
| 99 | + projects |
| 100 | + .command("servers") |
| 101 | + .description("List the servers saved in a project") |
| 102 | + .option( |
| 103 | + "--project <id-or-name>", |
| 104 | + "Project name or ID (defaults to the most recently updated project)", |
| 105 | + ), |
| 106 | + ).action(async (options: PlatformOptions, command) => { |
| 107 | + const globalOptions = getGlobalOptions(command); |
| 108 | + const result = await runPlatformCommand( |
| 109 | + options, |
| 110 | + globalOptions.timeout, |
| 111 | + ({ client, signal }) => |
| 112 | + listProjectServersOperation.execute( |
| 113 | + { project: options.project }, |
| 114 | + { client, signal }, |
| 115 | + ), |
| 116 | + ); |
| 117 | + |
| 118 | + if (globalOptions.format === "human") { |
| 119 | + process.stdout.write(`${formatProjectServersHuman(result)}\n`); |
| 120 | + } else { |
| 121 | + writeResult(result, globalOptions.format); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + addPlatformOptions( |
| 126 | + projects |
| 127 | + .command("status") |
| 128 | + .description( |
| 129 | + "Health-check every server in a project (hosted doctor per server)", |
| 130 | + ) |
| 131 | + .option( |
| 132 | + "--project <id-or-name>", |
| 133 | + "Project name or ID (defaults to the most recently updated project)", |
| 134 | + ), |
| 135 | + ).action(async (options: PlatformOptions, command) => { |
| 136 | + const globalOptions = getGlobalOptions(command); |
| 137 | + const payload = await runPlatformCommand( |
| 138 | + options, |
| 139 | + globalOptions.timeout, |
| 140 | + ({ client, signal }) => |
| 141 | + showServersOperation.execute( |
| 142 | + { project: options.project }, |
| 143 | + { client, signal }, |
| 144 | + ), |
| 145 | + ); |
| 146 | + |
| 147 | + if (globalOptions.format === "human") { |
| 148 | + process.stdout.write(`${formatShowServersHuman(payload)}\n`); |
| 149 | + } else { |
| 150 | + writeResult(payload, globalOptions.format); |
| 151 | + } |
| 152 | + // Exit 0 even with unreachable servers: this is a status report, not an |
| 153 | + // assertion. CI gating can parse the summary from the JSON payload. |
| 154 | + }); |
| 155 | +} |
0 commit comments