mcp-swagger-parser
Version:
Enterprise-grade OpenAPI/Swagger specification parser for Model Context Protocol (MCP) projects
71 lines • 2.47 kB
JavaScript
;
/**
* OpenAPI specification normalizer
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Normalizer = void 0;
const swagger_parser_1 = __importDefault(require("@apidevtools/swagger-parser"));
const index_1 = require("../errors/index");
class Normalizer {
constructor(config) {
this.config = config;
}
/**
* Normalize OpenAPI specification
*/
async normalize(spec) {
let normalizedSpec = { ...spec };
// Resolve references if enabled
if (this.config.resolveReferences) {
try {
normalizedSpec = await swagger_parser_1.default.dereference(normalizedSpec);
}
catch (error) {
if (this.config.strictMode) {
throw new index_1.OpenAPIParseError(`Failed to resolve references: ${error instanceof Error ? error.message : String(error)}`, index_1.ERROR_CODES.REFERENCE_ERROR, undefined, error instanceof Error ? error : undefined);
}
// In non-strict mode, continue with unresolved references
}
}
// Additional normalization steps could be added here
normalizedSpec = this.normalizeServers(normalizedSpec);
normalizedSpec = this.normalizePaths(normalizedSpec);
return normalizedSpec;
}
/**
* Normalize server configurations
*/
normalizeServers(spec) {
if (!spec.servers || spec.servers.length === 0) {
// Add default server if none specified
spec.servers = [{
url: 'http://localhost',
description: 'Default server'
}];
}
return spec;
}
/**
* Normalize path definitions
*/
normalizePaths(spec) {
if (!spec.paths) {
return spec;
}
const normalizedPaths = {};
for (const [path, pathItem] of Object.entries(spec.paths)) {
// Ensure path starts with forward slash
const normalizedPath = path.startsWith('/') ? path : `/${path}`;
normalizedPaths[normalizedPath] = pathItem;
}
return {
...spec,
paths: normalizedPaths
};
}
}
exports.Normalizer = Normalizer;
//# sourceMappingURL=normalizer.js.map