Skip to main content
Glama

format_code

Format code files to improve readability and maintain consistency by automatically adjusting indentation, spacing, and structure based on the programming language.

Instructions

Format code in a file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to the file to format
languageNoLanguage of the code (e.g., javascript, python, rust)

Implementation Reference

  • src/index.ts:246-269 (registration)
    Registers the 'format_code' MCP tool including its input schema, description, and annotations.
    mcpServer.registerTool({
      name: 'format_code',
      description: 'Format code in a file',
      inputSchema: {
        type: 'object',
        properties: {
          path: {
            type: 'string',
            description: 'Path to the file to format'
          },
          language: {
            type: 'string',
            description: 'Language of the code (e.g., javascript, python, rust)'
          }
        },
        required: ['path']
      },
      annotations: {
        readOnlyHint: false,
        destructiveHint: false,
        idempotentHint: true,
        openWorldHint: false
      }
    });
  • Executes the 'format_code' tool by invoking an 'edit' command with 'format' action on the EditInstanceManager for the specified language.
    case 'format_code':
      return this.editInstanceManager.executeEditCommand(sessionId, {
        type: 'edit',
        params: {
          action: 'format',
          language: operation.params.language
        }
      });
  • Implements the generic 'edit' command sent to the Edit process, which handles the 'format' action for format_code tool.
    // This is a simplified approach; in a real implementation, we would need
    // to handle different types of edits (insert, delete, replace, etc.)
    result = await instance.executeCommand(`edit ${JSON.stringify(command.params)}`);
    return { success: true, message: result };
  • Classifies 'format_code' as a complex operation for routing decisions.
    const complexOperations = [
      'interactive_edit_session',
      'format_code',
      'complex_find_replace',
      'merge_conflicts_resolution',
      'bulk_edit_operation',
      'edit_with_context_awareness'
    ];
  • Provides a REST API endpoint (/api/format) that calls the 'format_code' MCP tool.
    this.app.post('/api/format', async (req, res) => {
      try {
        const formatRequest = parseMessage({
          jsonrpc: '2.0',
          method: 'tools/call',
          params: {
            name: 'format_code',
            arguments: {
              path: req.body.path,
              language: req.body.language
            }
          },
          id: 'rest-' + Date.now()
        });
        const response = await this.mcpServer.handleMessage(formatRequest);
        res.json(response);
      } catch (error: any) {
        res.status(500).json({ error: error.message });
      }
    });

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/mixelpixx/microsoft-edit-mcp'

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