generate-api-client-optimized
Generate API client code from Swagger/OpenAPI documents with caching and large document support for Axios, Fetch, or React Query frameworks.
Instructions
Generate API client code from Swagger/OpenAPI document (optimized version with caching and large document support).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| swaggerUrl | Yes | Swagger/OpenAPI document URL | |
| outputDir | No | Output directory | |
| overwrite | No | Whether to overwrite existing files | |
| filePrefix | No | File prefix | |
| fileSuffix | No | File suffix | |
| clientType | No | API client technology stack | |
| generateTypeImports | No | Whether to generate type imports | |
| typesImportPath | No | Types import path | |
| groupBy | No | Grouping method | |
| includeTags | No | Include tags filter | |
| excludeTags | No | Exclude tags filter | |
| headers | No | Request headers | |
| useCache | No | Whether to use cache | |
| cacheTTLMinutes | No | Cache TTL in minutes | |
| skipValidation | No | Whether to skip validation | |
| lazyLoading | No | Whether to use lazy loading |
Implementation Reference
- Main handler function 'execute' that performs the core logic of generating optimized API client code from Swagger/OpenAPI specs using ApiClientGenerator.*/ async execute(params: z.infer<typeof this.optimizedSchema>) { try { console.log(`[ApiClientGeneratorTool] 开始生成API客户端: ${params.swaggerUrl}`); // 创建生成器实例 const generator = new ApiClientGenerator(); // 记录进度的函数 let progressUpdates: { progress: number, message: string }[] = []; const progressCallback = (progress: number, message: string) => { progressUpdates.push({ progress, message }); }; // 执行生成 const result = await generator.generate({ ...params, progressCallback } as ApiClientGeneratorOptions); // 处理结果 if (result.success) { console.log(`[ApiClientGeneratorTool] 客户端生成成功,生成了 ${result.files.length} 个文件`); return { content: [ { type: 'text' as const, text: JSON.stringify({ success: true, files: result.files, warnings: result.warnings, progress: progressUpdates }, null, 2) } ] }; } else { console.error(`[ApiClientGeneratorTool] 客户端生成失败: ${result.error}`); return { content: [ { type: 'text' as const, text: JSON.stringify({ success: false, error: result.error, progress: progressUpdates }, null, 2) } ] }; } } catch (error) { console.error(`[ApiClientGeneratorTool] 执行异常:`, error); // 返回错误结果 return { content: [ { type: 'text' as const, text: JSON.stringify({ success: false, error: error instanceof Error ? error.message : String(error) }, null, 2) } ] }; } }
- Zod schema for the optimized tool parameters, extending the base schema with caching and optimization options.// Define optimized parameter schema optimizedSchema = this.schema.extend({ /** * Whether to use cache */ useCache: z.boolean().optional().describe('Whether to use cache'), /** * Cache TTL in minutes */ cacheTTLMinutes: z.number().optional().describe('Cache TTL in minutes'), /** * Whether to skip validation */ skipValidation: z.boolean().optional().describe('Whether to skip validation'), /** * Whether to use lazy loading */ lazyLoading: z.boolean().optional().describe('Whether to use lazy loading') });
- src/tools/api-client-generator-tool.ts:125-140 (registration)Registration of the 'generate-api-client-optimized' tool on the MCP server, providing optimized defaults and delegating to the execute handler.// 注册优化版 server.tool( this.optimizedName, this.optimizedDescription, this.optimizedSchema.shape, async (params) => { // 设置优化版默认选项 const optimizedParams = { ...params, useCache: params.useCache !== false, lazyLoading: params.lazyLoading !== false, skipValidation: params.skipValidation || false }; return await this.execute(optimizedParams); } );
- src/index.ts:62-62 (registration)Main server setup where the ApiClientGeneratorTool (containing the optimized tool) is instantiated and registered.new ApiClientGeneratorTool().register(server);
- Import of the ApiClientGenerator helper class used in the handler for actual code generation.import { ApiClientGenerator, ApiClientGeneratorOptions } from '../generators/api-client-generator';