- Extract HTTP_METHODS constant to types.ts (eliminates duplication in 3 files) - Add DEFAULT_CACHE_MAX_SIZE and DEFAULT_CACHE_TTL_MINUTES constants to cache.ts - Create schema-utils.ts with getSchemas, findSchema, getSchemaNames, getSchemaCount - Create spec-guards.ts with isOpenAPIV3, isSwaggerV2, getSpecVersion type guards - Create tool-response.ts with successResponse, errorResponse helpers - Update all tool handlers to use response helpers (~50 lines reduced) - Update parser.ts to use type guards for version detection Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
56 lines
2 KiB
TypeScript
56 lines
2 KiB
TypeScript
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<ToolResponse> {
|
|
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<string, object> }).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');
|
|
}
|
|
}
|