template-save
Save or update code generation templates for API clients, TypeScript types, and configuration files in the Swagger MCP Server.
Instructions
Save or update template
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | Template ID | |
| name | Yes | Template name | |
| type | Yes | Template type | |
| framework | No | Framework type (only for API client and config file templates) | |
| content | Yes | Template content | |
| description | No | Template description |
Implementation Reference
- src/tools/template-manager-tool.ts:70-84 (registration)Registration of the 'template-save' MCP tool, including name, description, Zod input schema, and handler delegating to saveTemplate method.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 schema defining input parameters for the template-save tool: id, name, type, optional framework and description, and required content.{ 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') },
- Handler method called by the tool: constructs Template from input params, invokes TemplateManager.saveCustomTemplate, and returns formatted MCP response.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 handler implementation in TemplateManager: generates file path, saves template 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; } }
- Constants defining the tool name and description used in registration.const TEMPLATE_SAVE_TOOL_NAME = 'template-save'; const TEMPLATE_SAVE_TOOL_DESCRIPTION = 'Save or update template';