Skip to main content
Glama
tuskermanshu

Swagger MCP Server

by tuskermanshu

file_writer

Write content to files with automatic directory creation and append options for managing generated API code and documentation.

Instructions

Write content to the specified file path, with support for automatic directory creation

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYesComplete path of the file
contentYesContent to be written to the file
createDirsNoWhether to automatically create parent directories if they do not exist
appendNoWhether to append to an existing file instead of overwriting it
encodingNoFile encodingutf8

Implementation Reference

  • Core handler function that performs file writing: validates params, creates directories if needed, appends or overwrites content, retrieves stats, handles errors.
    async writeFile(params: FileWriterParams): Promise<object> { try { const { filePath, content, createDirs, append, encoding } = params; // 确保父目录存在 if (createDirs) { const dir = path.dirname(filePath); await fs.mkdir(dir, { recursive: true }); } // 写入文件 if (append) { await fs.appendFile(filePath, content, { encoding: encoding as BufferEncoding }); } else { await fs.writeFile(filePath, content, { encoding: encoding as BufferEncoding }); } // 获取文件信息 const stats = await fs.stat(filePath); return { success: true, filePath, size: stats.size, created: stats.birthtime, modified: stats.mtime, message: `文件已${append ? '追加' : '写入'}: ${filePath}`, }; } catch (error) { console.error('文件写入失败:', error); return { success: false, error: error instanceof Error ? error.message : String(error), filePath: params.filePath, }; } }
  • Zod schema defining input parameters for the file_writer tool: filePath, content, createDirs, append, encoding.
    const fileWriterSchema = z.object({ filePath: z.string().min(1).describe('Complete path of the file'), content: z.string().describe('Content to be written to the file'), createDirs: z.boolean().optional().default(true).describe('Whether to automatically create parent directories if they do not exist'), append: z.boolean().optional().default(false).describe('Whether to append to an existing file instead of overwriting it'), encoding: z.string().optional().default('utf8').describe('File encoding'), });
  • Class method `register` that registers the tool on the MCP server using server.tool with name, description, schema, and a wrapper async handler calling writeFile.
    register(server: McpServer): void { server.tool( this.name, this.description, this.schema.shape, async (params) => { // 验证参数 const result = await this.writeFile(params); // 返回符合MCP要求的格式 return { ...result, content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; } ); }
  • src/index.ts:63-63 (registration)
    Registers the FileWriterTool instance on the MCP server in the main index entry point.
    new FileWriterTool().register(server);
  • Registers FileWriterTool (line 75) among other tools in the registerAllTools function used by the dedicated MCP tools server.
    function registerAllTools(server: McpServer): void { const tools = [ new SwaggerParserTool(), new OptimizedSwaggerParserTool(), new TypeScriptTypesGeneratorTool(), new ApiClientGeneratorTool(), new FileWriterTool(), ]; for (const tool of tools) { tool.register(server); console.log(`✅ 已注册工具: ${tool.name}`); }

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