template-save
Save or update templates for API clients, TypeScript types, and config files in Axios, Fetch, React Query, and other frameworks using the Swagger MCP Server.
Instructions
Save or update template
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | Template content | |
| description | No | Template description | |
| framework | No | Framework type (only for API client and config file templates) | |
| id | Yes | Template ID | |
| name | Yes | Template name | |
| type | Yes | Template type |
Implementation Reference
- src/tools/template-manager-tool.ts:69-84 (registration)Registration of the 'template-save' tool on the MCP server, specifying description, input schema, and handler function.// 注册保存模板工具 server.tool( TEMPLATE_SAVE_TOOL_NAME, TEMPLATE_SAVE_TOOL_DESCRIPTION, { id: z.string().describe('Template ID'), name: z.string().describe('Template name'), type: z.enum(['api-client', 'typescript-types', 'config-file']).describe('Template type'), framework: z.enum(['axios', 'fetch', 'react-query', 'swr', 'angular', 'vue']).optional().describe('Framework type (only for API client and config file templates)'), content: z.string().describe('Template content'), description: z.string().optional().describe('Template description') }, async (params) => { return await this.saveTemplate(params); } );
- Zod input schema for the 'template-save' tool parameters.id: z.string().describe('Template ID'), name: z.string().describe('Template name'), type: z.enum(['api-client', 'typescript-types', 'config-file']).describe('Template type'), framework: z.enum(['axios', 'fetch', 'react-query', 'swr', 'angular', 'vue']).optional().describe('Framework type (only for API client and config file templates)'), content: z.string().describe('Template content'), description: z.string().optional().describe('Template description') },
- Direct handler function for 'template-save' tool that constructs the template object and delegates to TemplateManager.saveCustomTemplate, handling success/error responses./** * 保存模板 */ private async saveTemplate(params: { id: string; name: string; type: string; framework?: string; content: string; description?: string; }): Promise<any> { try { const template = { id: params.id, name: params.name, type: params.type as TemplateType, framework: params.framework as FrameworkType | undefined, content: params.content, description: params.description }; const result = await this.templateManager.saveCustomTemplate(template); return { content: [ { type: 'text' as const, text: JSON.stringify({ success: true, template: result }, null, 2) } ] }; } catch (error) { console.error('[TemplateManagerTool] 保存模板失败:', error); return { content: [ { type: 'text' as const, text: JSON.stringify({ success: false, error: error instanceof Error ? error.message : String(error) }, null, 2) } ] }; } }
- Core helper function implementing the template saving logic: generates file path, writes content to disk, updates in-memory list and JSON config file.* 保存自定义模板 */ async saveCustomTemplate(template: Template): Promise<Template> { // 确保已初始化 if (!this.initialized) { await this.initialize(); } try { // 检查是否是内置模板 const existingTemplate = this.getTemplate(template.id); if (existingTemplate?.isBuiltIn) { throw new Error(`Cannot override built-in template: ${template.id}`); } // 生成保存路径 let relativePath: string; if (template.type === TemplateType.API_CLIENT) { relativePath = `api-client/${template.id}.tpl`; } else if (template.type === TemplateType.TYPESCRIPT_TYPES) { relativePath = `typescript-types/${template.id}.tpl`; } else if (template.type === TemplateType.CONFIG_FILE) { relativePath = `config/${template.id}.tpl`; } else { relativePath = `${template.id}.tpl`; } // 确保子目录存在 const dirPath = path.join(CUSTOM_TEMPLATES_DIR, path.dirname(relativePath)); await fs.mkdir(dirPath, { recursive: true }); // 保存模板内容 const templatePath = path.join(CUSTOM_TEMPLATES_DIR, relativePath); await fs.writeFile(templatePath, template.content || ''); // 更新模板配置 const newTemplate: Template = { id: template.id, name: template.name, type: template.type, framework: template.framework, path: relativePath, description: template.description, isBuiltIn: false }; // 更新内存中的自定义模板 const existingIndex = this.customTemplates.findIndex(t => t.id === template.id); if (existingIndex >= 0) { // 更新现有模板 this.customTemplates[existingIndex] = { ...newTemplate, content: template.content }; } else { // 添加新模板 this.customTemplates.push({ ...newTemplate, content: template.content }); } // 更新配置文件 await this.updateCustomTemplatesConfig(); return { ...newTemplate, content: template.content }; } catch (error) { console.error('保存自定义模板失败:', error); throw error; } }