UNPKG

mcp-openapi-schema-explorer

Version:
63 lines 3.03 kB
import { ResourceTemplate, } from '@modelcontextprotocol/sdk/server/mcp.js'; import { RenderableComponentMap, VALID_COMPONENT_TYPES, } from '../rendering/components.js'; import { createErrorResult } from '../rendering/utils.js'; // Import shared handler utils import { formatResults, isOpenAPIV3, getValidatedComponentMap, // Import the helper } from './handler-utils.js'; // Already has .js const BASE_URI = 'openapi://'; // Removed duplicated FormattedResultItem type - now imported from handler-utils // Removed duplicated formatResults function - now imported from handler-utils // Removed duplicated isOpenAPIV3 function - now imported from handler-utils /** * Handles requests for listing component names of a specific type. * Corresponds to the `openapi://components/{type}` template. */ export class ComponentMapHandler { constructor(specLoader, formatter // Needed for context ) { this.specLoader = specLoader; this.formatter = formatter; this.handleRequest = async (uri, variables) => { const type = variables.type; const mapUriSuffix = `components/${type}`; const context = { formatter: this.formatter, baseUri: BASE_URI }; let resultItems; try { if (!VALID_COMPONENT_TYPES.includes(type)) { throw new Error(`Invalid component type: ${type}`); } const componentType = type; const spec = await this.specLoader.getTransformedSpec({ resourceType: 'schema', // Use 'schema' for now format: 'openapi', }); // Use imported type guard if (!isOpenAPIV3(spec)) { throw new Error('Only OpenAPI v3 specifications are supported'); } // --- Use helper to get validated component map --- const componentMapObj = getValidatedComponentMap(spec, componentType); // Instantiate RenderableComponentMap with the validated map const renderableMap = new RenderableComponentMap(componentMapObj, // componentMapObj retrieved safely via helper componentType, mapUriSuffix); resultItems = renderableMap.renderList(context); } catch (error) { const message = error instanceof Error ? error.message : String(error); console.error(`Error handling request ${uri.href}: ${message}`); resultItems = createErrorResult(mapUriSuffix, message); } // Use imported formatResults const contents = formatResults(context, resultItems); return { contents }; }; } getTemplate() { // TODO: Add completion logic if needed return new ResourceTemplate(`${BASE_URI}components/{type}`, { list: undefined, complete: undefined, }); } } //# sourceMappingURL=component-map-handler.js.map