import { z } from 'zod'; import { parseSpec } from '../lib/parser.js'; import { formatSchema } from '../utils/format.js'; import { findSchema, getSchemaNames } from '../lib/schema-utils.js'; import { successResponse, errorResponse } from '../lib/tool-response.js'; import type { ToolResponse } from '../lib/tool-response.js'; import type { SchemaInfo } from '../lib/types.js'; export const schemaToolName = 'get-schema'; export const schemaToolDescription = 'Get details of a component schema from an OpenAPI spec. Returns the full schema definition with resolved references.'; export const schemaToolSchema = { path: z.string().describe('Path to the OpenAPI/Swagger spec file'), schemaName: z.string().describe('Name of the schema in components/schemas (or definitions for Swagger 2.0)'), noCache: z.boolean().optional().describe('Bypass cache and parse fresh'), }; export async function schemaToolHandler({ path, schemaName, noCache }: { path: string; schemaName: string; noCache?: boolean; }): Promise { try { const { spec } = await parseSpec(path, { noCache }); const schema = findSchema(spec, schemaName); if (!schema) { const available = getSchemaNames(spec); return { content: [{ type: 'text', text: `Schema '${schemaName}' not found. Available schemas: ${available.join(', ')}` }], structuredContent: { success: false, error: `Schema '${schemaName}' not found`, availableSchemas: available, }, }; } const schemaInfo: SchemaInfo = { name: schemaName, type: (schema as { type?: string }).type, description: (schema as { description?: string }).description, properties: (schema as { properties?: Record }).properties, required: (schema as { required?: string[] }).required, schema, }; const text = formatSchema(schemaInfo); return successResponse(text, { schema: schemaInfo }); } catch (err) { return errorResponse((err as Error).message, 'getting schema'); } }