Skip to main content
Glama

format_code

Format code to follow language-specific style guides by specifying programming language and indentation preferences for consistent, readable code.

Instructions

Provides formatted version of code following language-specific style guides.

Input Schema

NameRequiredDescriptionDefault
codeYesThe code to format
languageYesProgramming language
indentNoIndentation size (default: 2 for JS, 4 for Python)

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "code": { "description": "The code to format", "type": "string" }, "indent": { "description": "Indentation size (default: 2 for JS, 4 for Python)", "type": "number" }, "language": { "description": "Programming language", "type": "string" } }, "required": [ "code", "language" ], "type": "object" }

Implementation Reference

  • The formatCodeHandler function implements the core logic of the 'format_code' tool. It performs basic code formatting (tab to spaces, remove trailing whitespace) and generates a markdown report with formatting analysis, suggestions, and style guide references based on the input language.
    export function formatCodeHandler(args: any) { const { code, language, indent } = args; const defaultIndent = language === "python" ? 4 : 2; const indentSize = indent || defaultIndent; const indentStr = " ".repeat(indentSize); let formatted = code; const suggestions: string[] = []; // Basic formatting formatted = formatted.replace(/\t/g, indentStr); formatted = formatted.replace(/[ \t]+$/gm, ""); // Remove trailing whitespace // Language-specific suggestions if (language === "javascript" || language === "typescript") { if (!formatted.endsWith("\n")) { suggestions.push("Add newline at end of file"); } if (formatted.includes("{ }")) { suggestions.push("Empty blocks should be on separate lines"); } } if (language === "python") { if (formatted.includes(" #")) { suggestions.push("Add two spaces before inline comments"); } } const result = `# Format Analysis: ${language} ## Formatting Applied - Converted tabs to ${indentSize} spaces - Removed trailing whitespace - Normalized line endings ## Additional Suggestions ${suggestions.length > 0 ? suggestions.map(s => `- ${s}`).join("\n") : "✅ Code follows formatting guidelines"} ## Style Guide Reference - ${language === "python" ? "PEP 8" : language === "javascript" || language === "typescript" ? "Prettier/ESLint" : "Standard conventions"} `; return { content: [{ type: "text", text: result }] }; }
  • Zod schema definition for the 'format_code' tool, specifying input parameters: code (string), language (string), and optional indent (number).
    export const formatCodeSchema = { name: "format_code", description: "Provides formatted version of code following language-specific style guides.", inputSchema: z.object({ code: z.string().describe("The code to format"), language: z.string().describe("Programming language"), indent: z.number().optional().describe("Indentation size (default: 2 for JS, 4 for Python)") }) };
  • src/index.ts:105-105 (registration)
    Registration of the 'format_code' tool in the main toolRegistry Map used by the stdio MCP server.
    ["format_code", { schema: formatCodeSchema, handler: formatCodeHandler }],
  • src/server.ts:105-105 (registration)
    Registration of the 'format_code' tool in the toolRegistry Map used by the HTTP MCP server.
    ["format_code", { schema: formatCodeSchema, handler: formatCodeHandler }],

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/millsydotdev/Code-MCP'

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