UNPKG

mcp-swagger-parser

Version:

Enterprise-grade OpenAPI/Swagger specification parser for Model Context Protocol (MCP) projects

134 lines 4.58 kB
"use strict"; /** * Endpoint extractor for OpenAPI specifications */ Object.defineProperty(exports, "__esModule", { value: true }); exports.EndpointExtractor = void 0; class EndpointExtractor { /** * Extract all endpoints from OpenAPI specification */ static extractEndpoints(spec) { const endpoints = []; if (!spec.paths) { return endpoints; } for (const [path, pathItem] of Object.entries(spec.paths)) { const methods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options', 'trace']; for (const method of methods) { const operation = pathItem[method]; if (!operation) continue; const endpoint = { path, method: method.toUpperCase(), operationId: operation.operationId, summary: operation.summary, description: operation.description, parameters: this.extractParameters(operation.parameters || []), requestBody: operation.requestBody && !this.isReferenceObject(operation.requestBody) ? operation.requestBody : undefined, responses: operation.responses || {}, tags: operation.tags, deprecated: operation.deprecated, security: operation.security }; endpoints.push(endpoint); } } return endpoints; } /** * Extract parameters from operation */ static extractParameters(parameters) { return parameters.filter(param => !this.isReferenceObject(param)); } /** * Check if object is a reference */ static isReferenceObject(obj) { return obj && typeof obj === 'object' && '$ref' in obj; } /** * Filter endpoints by criteria */ static filterEndpoints(endpoints, criteria) { return endpoints.filter(endpoint => { // Filter by HTTP method if (criteria.methods && !criteria.methods.includes(endpoint.method)) { return false; } // Filter by tags if (criteria.tags && criteria.tags.length > 0) { const endpointTags = endpoint.tags || []; const hasMatchingTag = criteria.tags.some(tag => endpointTags.includes(tag)); if (!hasMatchingTag) { return false; } } // Filter deprecated endpoints if (!criteria.includeDeprecated && endpoint.deprecated) { return false; } // Filter by operation IDs if (criteria.operationIds && criteria.operationIds.length > 0) { if (!endpoint.operationId || !criteria.operationIds.includes(endpoint.operationId)) { return false; } } return true; }); } /** * Group endpoints by tag */ static groupEndpointsByTag(endpoints) { const grouped = {}; for (const endpoint of endpoints) { const tags = endpoint.tags || ['untagged']; for (const tag of tags) { if (!grouped[tag]) { grouped[tag] = []; } grouped[tag].push(endpoint); } } return grouped; } /** * Get endpoint statistics */ static getEndpointStats(endpoints) { const stats = { total: endpoints.length, byMethod: {}, deprecated: 0, withAuth: 0, tags: new Set() }; for (const endpoint of endpoints) { // Count by method stats.byMethod[endpoint.method] = (stats.byMethod[endpoint.method] || 0) + 1; // Count deprecated if (endpoint.deprecated) { stats.deprecated++; } // Count with authentication if (endpoint.security && endpoint.security.length > 0) { stats.withAuth++; } // Collect tags if (endpoint.tags) { endpoint.tags.forEach(tag => stats.tags.add(tag)); } } return { ...stats, tags: Array.from(stats.tags) }; } } exports.EndpointExtractor = EndpointExtractor; //# sourceMappingURL=endpoint-extractor.js.map