Skip to main content
Glama

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
NameRequiredDescriptionDefault
filesYesFile paths to format
optionsNoPrettier options

Implementation Reference

  • 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, }; } }
  • 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; }
  • 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'], },
  • 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'], }, },

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/code-alchemist01/development-tools-mcp-Server'

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