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

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

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 }],
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions formatting 'following language-specific style guides,' which implies consistency and standardization, but doesn't cover critical aspects like whether the tool modifies the input code in-place, returns a new formatted string, handles errors, or has any rate limits or permissions needed. For a tool with no annotation coverage, this is a significant gap in transparency.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence: 'Provides formatted version of code following language-specific style guides.' It is front-loaded with the core purpose and wastes no words, making it highly concise and well-structured for quick understanding.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (3 parameters, no output schema, no annotations), the description is minimally adequate. It states what the tool does but lacks details on behavior, output format, or error handling. Without annotations or an output schema, more context would be helpful, but it's not completely inadequate, scoring a 3 as the bare minimum viable.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, with clear documentation for 'code,' 'language,' and 'indent' parameters. The description adds no additional meaning beyond what the schema provides, such as examples of supported languages or style guide details. Since the schema does the heavy lifting, a baseline score of 3 is appropriate, as the description doesn't compensate but also doesn't detract.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Provides formatted version of code following language-specific style guides.' It specifies the verb ('Provides formatted version') and resource ('code'), making it understandable. However, it doesn't explicitly differentiate from sibling tools like 'lint_code' or 'validate_code', which might have overlapping functionality, preventing a perfect score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention when to choose 'format_code' over 'lint_code' or 'validate_code' from the sibling list, nor does it specify any prerequisites or exclusions. This lack of context leaves the agent without clear usage instructions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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