Skip to main content
Glama
tuskermanshu

Swagger MCP Server

by tuskermanshu

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
NameRequiredDescriptionDefault
contentYesTemplate content
descriptionNoTemplate description
frameworkNoFramework type (only for API client and config file templates)
idYesTemplate ID
nameYesTemplate name
typeYesTemplate type

Implementation Reference

  • 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; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/tuskermanshu/swagger-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server