diff --git a/src/lib/parser.ts b/src/lib/parser.ts index acd03be..e156b53 100644 --- a/src/lib/parser.ts +++ b/src/lib/parser.ts @@ -4,13 +4,30 @@ import type { ParsedSpec, OpenAPISpec } from './types.js'; const HTTP_METHODS = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'] as const; -export async function parseSpec(specPath: string): Promise<{ +export async function parseSpec(specPath: string, options?: { dereference?: boolean }): Promise<{ spec: OpenAPISpec; metadata: ParsedSpec; + dereferenced: boolean; }> { - const spec = await SwaggerParser.dereference(specPath); + const shouldDereference = options?.dereference !== false; + + let spec: OpenAPISpec; + let dereferenced = false; + + if (shouldDereference) { + try { + spec = await SwaggerParser.dereference(specPath); + dereferenced = true; + } catch { + // Fallback to parse without dereferencing if refs are broken + spec = await SwaggerParser.parse(specPath); + } + } else { + spec = await SwaggerParser.parse(specPath); + } + const metadata = extractMetadata(spec); - return { spec, metadata }; + return { spec, metadata, dereferenced }; } export async function bundleSpec(specPath: string): Promise { diff --git a/src/tools/parse.ts b/src/tools/parse.ts index cfe3ea5..fc0264c 100644 --- a/src/tools/parse.ts +++ b/src/tools/parse.ts @@ -15,15 +15,19 @@ export async function parseToolHandler({ path }: { path: string }): Promise<{ structuredContent: Record; }> { try { - const { spec, metadata } = await parseSpec(path); + const { spec, metadata, dereferenced } = await parseSpec(path); - const text = formatMetadata(metadata); + let text = formatMetadata(metadata); + if (!dereferenced) { + text += '\n\n**Warning**: Spec has broken $refs. Parsed without dereferencing.'; + } return { content: [{ type: 'text', text }], structuredContent: { success: true, metadata, + dereferenced, spec, }, };