Skip to main content
Glama
rodhayl
by rodhayl

formatter

Format code or apply LLM-powered syntax fixes to files locally. Choose between standard formatting and AI-assisted corrections for improved code quality.

Instructions

Run formatter or LLM-assisted syntax fixes.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
actionNoAction: run (format code), fix (LLM-powered syntax fixes)
filesNoSpecific files to process
commandNoCustom format command
checkNoCheck only, do not modify (for run action)
difficultyNoLLM fix difficulty (for fix action)
dryRunNoPreview fixes without applying (for fix action)
maxFixesNoMaximum fixes to apply (for fix action)
timeoutNoTimeout in milliseconds

Implementation Reference

  • The runFormatter method in ExecutionTools class which serves as the handler for the formatter tool, capable of detecting the formatter and executing the formatting command.
    async runFormatter(options?: {
      command?: string;
      files?: string[];
      check?: boolean; // Just check, don't modify
      timeout?: number;
    }): Promise<ExecutionResult> {
      let command = options?.command;
    
      if (!command) {
        command = await this.detectFormatCommand(options?.check);
        if (!command) {
          return {
            success: false,
            exitCode: -1,
            stdout: '',
            stderr: 'Could not detect formatter. Please provide a format command.',
            duration: 0,
            command: '',
          };
        }
      }
    
      // Append specific files if provided
      if (options?.files && options.files.length > 0) {
        // For prettier, replace glob pattern with specific files
        if (command.includes('prettier')) {
          command = command.replace(/"[^"]*"/, options.files.join(' '));
        } else {
          command = `${command} ${options.files.join(' ')}`;
        }
      }
    
      return this.executeCommand(
        command,
        this.workspaceRoot,
        options?.timeout ?? this.defaultTimeout
      );
    }
  • Helper method to automatically detect the appropriate formatter command based on the workspace files (package.json, pyproject.toml, etc.).
    private async detectFormatCommand(check: boolean = false): Promise<string | undefined> {
      const packageJsonPath = join(this.workspaceRoot, 'package.json');
    
      if (existsSync(packageJsonPath)) {
        try {
          const pkg = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
    
          // Check scripts
          const scriptName = check ? 'format:check' : 'format';
          if (pkg.scripts?.[scriptName]) {
            return `npm run ${scriptName}`;
          }
    
          // Check for Prettier config
          const prettierConfigs = [
            '.prettierrc',
            '.prettierrc.js',
            '.prettierrc.json',
            'prettier.config.js',
            'prettier.config.mjs',
            'prettier.config.cjs',
          ];
          for (const config of prettierConfigs) {
            if (existsSync(join(this.workspaceRoot, config))) {
              return check
                ? 'npx prettier --check "**/*.{ts,js,tsx,jsx,json,css,md}"'
                : 'npx prettier --write "**/*.{ts,js,tsx,jsx,json,css,md}"';
            }
          }
    
          // Check devDependencies
          const deps = { ...pkg.dependencies, ...pkg.devDependencies };
          if (deps.prettier) {
            return check ? 'npx prettier --check .' : 'npx prettier --write .';
          }
        } catch {
          // Ignore parse errors
        }
      }
    
      // Python projects
      if (existsSync(join(this.workspaceRoot, 'pyproject.toml'))) {
        return check ? 'black --check .' : 'black .';
      }
    
      // Go projects
      if (existsSync(join(this.workspaceRoot, 'go.mod'))) {
        return 'go fmt ./...';
      }
    
      // Rust projects
      if (existsSync(join(this.workspaceRoot, 'Cargo.toml'))) {
        return check ? 'cargo fmt -- --check' : 'cargo fmt';
      }
    
      return undefined;
    }

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/rodhayl/mcpLocalHelper'

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