Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1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 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 7x 7x 7x 7x 7x 7x 7x 7x 7x 14x | "use strict";
/**
* Start-up preparation: turn an OpenAPI document and an existing
* typescript-fetch SDK into everything the server needs, once per process.
*
* Stages (each logged as `prepare.stage.completed`):
*
* 1. `inputs`: validate and load the OpenAPI document; resolve the SDK
* directory in place or extract an archive into the workspace.
* 2. `compatibility`: check generator target, version, options and layout,
* and that runtime dependencies are already installed (never installs).
* 3. `sdk`: declarations (emitted for source SDKs, copied for compiled ones)
* and one ES-module execution bundle, written to the workspace.
* 4. `index`: SDK methods (from declarations and implementation) mapped to
* OpenAPI operations, the in-memory search index and the `sdk` binding
* declaration.
*
* The bridge and executor (stage 5) are assembled by the runtime service.
* Nothing is written to the supplied SDK; no package manager, lifecycle
* script or SDK build script is run.
*
* @module prepare/prepare
*/
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { OagMcpError } from "../errors.js";
import type { Logs } from "../logging/logger.js";
import { MethodSearchIndex } from "../search/search-index.js";
import type {
MethodRecord,
OperationBinding,
PreparedSdkInfo,
SpecSummary,
} from "../types.js";
import { sha256File } from "../util/hash.js";
import {
dependencyVersion,
oagMcpVersion,
packageRoot,
} from "../util/version.js";
import { SdkAnalyzer, scanImplementation } from "./analyze.js";
import {
bundleSdk,
copyDeclarations,
copyTsLibs,
emitDeclarations,
} from "./bundle.js";
import { type SdkInspection, inspectSdk } from "./compat.js";
import { buildRecords, hintsForOperation } from "./method-records.js";
import {
type StagedSdk,
missingDependencies,
stageSdkInput,
} from "./sdk-input.js";
import { loadSpec } from "./spec.js";
import {
WORKSPACE_PATHS,
type Workspace,
bindingDeclaration,
createWorkspace,
} from "./workspace.js";
/** Start-up preparation stages, in order. */
export const PREPARE_STAGES = [
"inputs",
"compatibility",
"sdk",
"index",
"executor",
] as const;
/** A preparation stage name. */
export type PrepareStage = (typeof PREPARE_STAGES)[number];
/** Options for {@link prepareSdk}. */
export interface PrepareOptions {
/** OpenAPI 3.0/3.1 document. */
spec: string;
/** typescript-fetch SDK directory or npm package archive (.tgz). */
sdk: string;
sdkLayout?: "auto" | "source" | "compiled";
/** Generator version for SDKs lacking .openapi-generator/VERSION. */
sdkOagVersion?: string;
allowUntestedOagVersion?: boolean;
/** Parent of the private workspace, default: OS temporary directory. */
workspaceRoot?: string;
/** Cancels preparation between stages (e.g. on SIGTERM during start-up). */
signal?: AbortSignal;
logs: Logs;
}
/** A prepared SDK held by one server process. */
export interface PreparedSdk {
info: PreparedSdkInfo;
/** Private workspace with sdk/ and executor/ (removed by cleanup). */
workspaceDir: string;
records: MethodRecord[];
bindings: OperationBinding[];
index: MethodSearchIndex;
warnings: string[];
/** Remove the workspace. Never throws; returns whether it succeeded. */
cleanup(): boolean;
}
/**
* Run one preparation stage with logging.
*
* @param {Object} logs - logs
* @param {String} stage - stage name
* @param {Function} fn - stage body
* @return {Promise} stage result
*/
export async function runStage<T>(
logs: Logs,
stage: PrepareStage,
fn: () => T | Promise<T>,
signal?: AbortSignal,
): Promise<T> {
if (signal?.aborted) {
logs.op("info", "prepare.aborted", { stage });
throw new OagMcpError(
"STARTUP_ABORTED",
`Start-up was cancelled before the ${stage} stage`,
{ hint: "The server received a shutdown signal while preparing." },
);
}
const started = Date.now();
logs.op("debug", "prepare.stage.started", { stage });
try {
const out = await fn();
logs.op("info", "prepare.stage.completed", {
stage,
durationMs: Date.now() - started,
outcome: "success",
});
return out;
} catch (err) {
const code = err instanceof OagMcpError ? err.code : "UNEXPECTED";
if (stage === "compatibility") {
logs.op("error", "prepare.compat.failed", {
errorCode: code,
outcome: "failure",
});
}
logs.op("error", "prepare.stage.failed", {
stage,
errorCode: code,
durationMs: Date.now() - started,
outcome: "failure",
});
throw err;
}
}
/**
* Stage 1: inputs.
*
* @private
*/
async function _inputs(
opts: PrepareOptions,
ws: Workspace,
): Promise<{ specPath: string; spec: SpecSummary; staged: StagedSdk }> {
if (!opts.spec) {
throw new OagMcpError("CLI_ARGS_INVALID", "--spec is required", {
hint: "Usage: oag-mcp serve --spec ./openapi.yaml --sdk ./generated-sdk",
});
}
if (!opts.sdk) {
throw new OagMcpError("CLI_ARGS_INVALID", "--sdk is required", {
hint: "Pass an existing OpenAPI Generator typescript-fetch SDK directory or .tgz package. oag-mcp does not generate SDKs.",
});
}
const specPath = path.resolve(opts.spec);
if (!fs.existsSync(specPath) || !fs.statSync(specPath).isFile()) {
throw new OagMcpError(
"SPEC_NOT_FOUND",
`OpenAPI document ${opts.spec} does not exist`,
{ hint: "Pass --spec <file> with an OpenAPI 3.0/3.1 YAML or JSON file." },
);
}
const spec = await loadSpec(specPath);
opts.logs.op("info", "prepare.spec.loaded", {
openapiVersion: spec.openapiVersion,
operations: spec.operations.length,
securitySchemes: Object.keys(spec.securitySchemes).length,
});
const staged = await stageSdkInput(
opts.sdk,
path.join(ws.dir, WORKSPACE_PATHS.input),
);
opts.logs.op("info", "prepare.sdk.resolved", { sdkKind: staged.kind });
return { specPath, spec, staged };
}
/**
* Stage 2: compatibility.
*
* @private
*/
function _compatibility(
opts: PrepareOptions,
staged: StagedSdk,
): SdkInspection {
const insp = inspectSdk(staged.root, {
layout: opts.sdkLayout,
declaredOagVersion: opts.sdkOagVersion,
allowUntestedOagVersion: opts.allowUntestedOagVersion,
});
const missing = missingDependencies(staged.root, insp.pkg?.dependencies);
if (missing.length > 0) {
const how =
staged.kind === "archive"
? "Extract the archive (e.g. `tar -xzf <file>.tgz`), run `npm install --omit=dev --ignore-scripts` in the extracted `package/` directory, and pass that directory to --sdk."
: `Run \`npm install --omit=dev --ignore-scripts\` in ${opts.sdk}, then restart oag-mcp.`;
throw new OagMcpError(
"SDK_DEPENDENCIES_MISSING",
`SDK runtime dependencies are not installed: ${missing.join(", ")}`,
{
hint: `oag-mcp never installs packages or runs package scripts. ${how}`,
details: { missing },
},
);
}
opts.logs.op("info", "prepare.compat.checked", {
layout: insp.layout,
oagVersion: insp.oagVersion,
oagVersionTested: insp.oagVersionTested,
apiFiles: insp.apiFiles.length,
outcome: "success",
});
return insp;
}
/**
* Stage 3: declarations, execution bundle, TypeScript libs and the runner.
*
* @private
*/
async function _sdk(
insp: SdkInspection,
staged: StagedSdk,
ws: Workspace,
logs: Logs,
): Promise<{ typesDir: string; warnings: string[] }> {
const warnings: string[] = [];
const typesDir = path.join(ws.dir, WORKSPACE_PATHS.types);
if (insp.layout === "source") {
const diags = emitDeclarations(insp.entry, typesDir);
if (diags.length > 0) {
warnings.push(
`SDK declarations emitted with ${diags.length} TypeScript diagnostic(s): ${diags[0]}`,
);
}
} else {
copyDeclarations(insp.typesDir!, typesDir);
}
const classNames = [...scanImplementation(insp.apiFiles).keys()];
const bundle = await bundleSdk({
entry: insp.entry,
root: staged.root,
outFile: path.join(ws.dir, WORKSPACE_PATHS.bundle),
shimDir: path.join(ws.dir, WORKSPACE_PATHS.shim),
classNames,
});
logs.op("info", "prepare.sdk.bundled", {
bytes: bundle.bytes,
exports: bundle.exports.length,
});
copyTsLibs(path.join(ws.dir, WORKSPACE_PATHS.tsLib));
fs.mkdirSync(path.dirname(path.join(ws.dir, WORKSPACE_PATHS.runner)), {
recursive: true,
});
fs.copyFileSync(
path.join(packageRoot(), "assets", "deno-runner.js"),
path.join(ws.dir, WORKSPACE_PATHS.runner),
);
return { typesDir, warnings };
}
/**
* Stage 4: method index and the `sdk` binding declaration.
*
* @private
*/
function _index(
spec: SpecSummary,
insp: SdkInspection,
typesDir: string,
ws: Workspace,
logs: Logs,
): {
records: MethodRecord[];
bindings: OperationBinding[];
unmapped: Array<{ key: string; operationId?: string }>;
index: MethodSearchIndex;
warnings: string[];
} {
const impl = scanImplementation(insp.apiFiles);
const analyzer = new SdkAnalyzer(path.join(typesDir, "index.d.ts"));
const byKey = new Map(spec.operations.map((o) => [o.key, o]));
const methods = analyzer.analyze(impl, (m, p) =>
hintsForOperation(byKey.get(`${m} ${p}`)),
);
const { records, bindings, unmapped } = buildRecords(spec, methods);
const warnings = unmapped.map(
(u) =>
`Operation ${u.key}${u.operationId ? ` (${u.operationId})` : ""} has no SDK method and is not available`,
);
fs.writeFileSync(
path.join(ws.dir, WORKSPACE_PATHS.binding),
bindingDeclaration(records),
);
const index = new MethodSearchIndex(records);
logs.op("info", "prepare.index.created", {
methods: records.length,
apiClasses: new Set(records.map((r) => r.className)).size,
unmappedOperations: unmapped.length,
});
return { records, bindings, unmapped, index, warnings };
}
/**
* Prepare the SDK for serving: stages 1–4. On failure the workspace is
* removed before the error is rethrown.
*
* @param {Object} opts - preparation options
* @return {Promise} prepared SDK
*/
export async function prepareSdk(opts: PrepareOptions): Promise<PreparedSdk> {
const { logs } = opts;
const started = Date.now();
const prepareId = crypto.randomUUID();
logs.op("info", "prepare.started", { prepareId });
const ws = createWorkspace({ root: opts.workspaceRoot, logs });
try {
const { specPath, spec, staged } = await runStage(logs, "inputs", () =>
_inputs(opts, ws),
);
const insp = await runStage(logs, "compatibility", () =>
_compatibility(opts, staged),
);
const sdk = await runStage(logs, "sdk", () => _sdk(insp, staged, ws, logs));
const idx = await runStage(logs, "index", () =>
_index(spec, insp, sdk.typesDir, ws, logs),
);
const warnings = [...insp.warnings, ...sdk.warnings, ...idx.warnings];
for (const w of warnings) {
logs.op("warn", "prepare.warning", { warning: w });
}
const info: PreparedSdkInfo = {
prepareId,
preparedAt: new Date().toISOString(),
oagMcpVersion: oagMcpVersion(),
target: {
generator: "typescript-fetch",
oagVersion: insp.oagVersion,
oagVersionTested: insp.oagVersionTested,
options: insp.detectedOptions,
sdkLayout: insp.layout,
sdkPackage: insp.pkg
? { name: insp.pkg.name, version: insp.pkg.version }
: undefined,
},
tools: {
typescript: dependencyVersion("typescript"),
esbuild: dependencyVersion("esbuild"),
},
inputs: {
spec: {
fileName: path.basename(specPath),
sha256: sha256File(specPath),
},
sdk: { kind: staged.kind, sha256: staged.sha256 },
},
api: {
title: spec.title,
version: spec.apiVersion,
description: spec.description,
servers: spec.servers,
tags: spec.tags,
},
securitySchemes: spec.securitySchemes,
operations: idx.bindings,
unmappedOperations: idx.unmapped,
};
logs.op("info", "prepare.sdk.prepared", {
prepareId,
methods: idx.records.length,
oagVersion: insp.oagVersion,
sdkLayout: insp.layout,
sdkKind: staged.kind,
specSha256: info.inputs.spec.sha256,
sdkSha256: staged.sha256,
durationMs: Date.now() - started,
});
return {
info,
workspaceDir: ws.dir,
records: idx.records,
bindings: idx.bindings,
index: idx.index,
warnings,
cleanup: ws.cleanup,
};
} catch (err) {
logs.op("error", "prepare.failed", {
prepareId,
errorCode: err instanceof OagMcpError ? err.code : "UNEXPECTED",
durationMs: Date.now() - started,
outcome: "failure",
});
ws.cleanup();
throw err;
}
}
|