format_code
Format code files automatically using Prettier to ensure consistent styling and improve readability in development workflows.
Instructions
Format code files using Prettier
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | Yes | File paths to format | |
| options | No | Prettier options |
Implementation Reference
- src/utils/linting-utils.ts:101-125 (handler)Core handler function that executes the formatting logic using Prettier for a single file, returning original and formatted content.async formatCode(filePath: string, options?: prettier.Options): Promise<FormatResult> { try { const content = readFileSync(filePath, 'utf-8'); const formatted = await prettier.format(content, { filepath: filePath, ...options, }); return { file: filePath, formatted: content !== formatted, original: content, formattedContent: formatted, }; } catch (error) { // If Prettier fails, return unformatted const content = readFileSync(filePath, 'utf-8'); return { file: filePath, formatted: false, original: content, formattedContent: content, }; } }
- src/tools/linting.ts:99-106 (handler)Tool dispatcher handler case for 'format_code' that handles multiple files by mapping over them and calling the formatCode utility.case 'format_code': { const files = params.files as string[]; const options = params.options as Record<string, unknown>; const results = await Promise.all( files.map((file) => lintingUtils.formatCode(file, options)) ); return results; }
- src/tools/linting.ts:25-39 (schema)Input schema defining the expected parameters for the format_code tool: array of file paths (required) and optional Prettier options.inputSchema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' }, description: 'File paths to format', }, options: { type: 'object', description: 'Prettier options', }, }, required: ['files'], },
- src/tools/linting.ts:22-40 (registration)Registration of the 'format_code' tool in the lintingTools array, including name, description, and schema.{ name: 'format_code', description: 'Format code files using Prettier', inputSchema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' }, description: 'File paths to format', }, options: { type: 'object', description: 'Prettier options', }, }, required: ['files'], }, },