Skip to main content
Glama
MrOrz

git-commit-aider MCP Server

by MrOrz

commit_staged

Stage and commit changes with a custom message while appending "(aider)" to the committer name, enabling clear tracking of AI contributions in your codebase.

Instructions

Commit staged changes with a specific message, appending "(aider)" to the committer name.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cwdNoOptional: The working directory for the git command (defaults to the workspace root).
messageYesThe commit message.

Implementation Reference

  • Handler for executing the commit_staged tool. Validates input, retrieves committer info from env or git config, appends '(aider)' to name, and executes 'git commit -m <message> --author=<aider_name> <email>'.
    server.setRequestHandler(CallToolRequestSchema, async (request) => {
        if (request.params.name !== 'commit_staged') {
            throw new McpError(
                ErrorCode.MethodNotFound,
                `Unknown tool: ${request.params.name}`
            );
        }
    
        if (!isValidCommitArgs(request.params.arguments)) {
            throw new McpError(
                ErrorCode.InvalidParams,
                'Invalid arguments for commit_staged. Requires "message" (string) and optional "cwd" (string).'
            );
        }
    
        const { message, cwd } = request.params.arguments;
        const executionOptions = { cwd: cwd || '' };
    
        try {
            let committerName: string;
            let committerEmail: string;
    
            // Get committer name: prioritize env var, fallback to git config
            const envName = process.env.GIT_COMMITTER_NAME;
            if (envName) {
                committerName = envName;
                console.error('Using committer name from environment variable.');
            } else {
                console.error('GIT_COMMITTER_NAME not set, falling back to git config user.name.');
                try {
                    const { stdout: nameOutput } = await execa('git', ['config', 'user.name'], executionOptions);
                    committerName = nameOutput.trim();
                    if (!committerName) {
                        throw new Error('Git user.name is empty.');
                    }
                    console.error(`Using committer name from git config: ${committerName}`);
                } catch (configError: any) {
                    const configErrorMessage = configError?.stderr || configError?.stdout || configError?.shortMessage || configError?.message || 'Unknown error getting git config user.name.';
                    throw new McpError(
                        ErrorCode.InternalError,
                        `Failed to get git user.name: ${configErrorMessage}. Please ensure git user.name is configured or set the GIT_COMMITTER_NAME environment variable.`
                    );
                }
            }
    
            // Get committer email: prioritize env var, fallback to git config
            const envEmail = process.env.GIT_COMMITTER_EMAIL;
            if (envEmail) {
                committerEmail = envEmail;
                console.error('Using committer email from environment variable.');
            } else {
                console.error('GIT_COMMITTER_EMAIL not set, falling back to git config user.email.');
                try {
                    const { stdout: emailOutput } = await execa('git', ['config', 'user.email'], executionOptions);
                    committerEmail = emailOutput.trim();
                     if (!committerEmail) {
                        throw new Error('Git user.email is empty.');
                    }
                    console.error(`Using committer email from git config: ${committerEmail}`);
                } catch (configError: any) {
                    const configErrorMessage = configError?.stderr || configError?.stdout || configError?.shortMessage || configError?.message || 'Unknown error getting git config user.email.';
                     throw new McpError(
                        ErrorCode.InternalError,
                        `Failed to get git user.email: ${configErrorMessage}. Please ensure git user.email is configured or set the GIT_COMMITTER_EMAIL environment variable.`
                    );
                }
            }
    
            // Construct the aider committer string
            const aiderName = `${committerName} (aider)`;
            const authorString = `${aiderName} <${committerEmail}>`;
    
            // Execute the git commit command
            console.error(`Executing: git commit -m "${message}" --author="${authorString}"`);
            const { stdout, stderr } = await execa(
                'git',
                ['commit', '-m', message, `--author=${authorString}`],
                executionOptions
            );
    
            return {
                content: [
                    {
                        type: 'text',
                        text: `Commit successful:\n${stdout}\n${stderr ? `Stderr:\n${stderr}` : ''}`,
                    },
                ],
            };
        } catch (error: any) {
             // Handle McpError specifically if thrown from git config fallback
             if (error instanceof McpError) {
                 return {
                     content: [{ type: 'text', text: error.message }],
                     isError: true,
                 };
             }
    
             // Handle other errors (likely from the commit command itself)
             const errorMessage = error?.stderr || error?.stdout || error?.shortMessage || error?.message || 'Unknown error during git commit.';
             // Check for specific git errors like "nothing to commit"
             if (errorMessage.includes('nothing to commit, working tree clean') || errorMessage.includes('no changes added to commit')) {
                 return {
                     content: [{ type: 'text', text: `Git commit skipped: ${errorMessage}` }],
                     isError: false, // Not technically an error in execution, just nothing happened
                 };
             }
             // Return a more specific error message if possible
             return {
                 content: [{ type: 'text', text: `Git commit failed: ${errorMessage}` }],
                 isError: true,
             };
        }
    });
  • src/index.ts:38-59 (registration)
    Registration of the commit_staged tool via ListToolsRequestSchema handler, including name, description, and input schema.
    server.setRequestHandler(ListToolsRequestSchema, async () => ({
        tools: [
            {
                name: 'commit_staged',
                description: 'Commit staged changes with a specific message, appending "(aider)" to the committer name.',
                inputSchema: {
                    type: 'object',
                    properties: {
                        message: {
                            type: 'string',
                            description: 'The commit message.',
                        },
                        cwd: {
                            type: 'string',
                            description: 'Optional: The working directory for the git command (defaults to the workspace root).',
                        }
                    },
                    required: ['message'],
                },
            },
        ],
    }));
  • Helper function to validate commit_staged arguments: requires 'message' string and optional 'cwd' string.
    // Helper function to validate arguments for the commit_staged tool
    const isValidCommitArgs = (
        args: any
    ): args is { message: string; cwd?: string } =>
        typeof args === 'object' &&
        args !== null &&
        typeof args.message === 'string' &&
        (args.cwd === undefined || typeof args.cwd === 'string');
Install Server

Other Tools

Related 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/MrOrz/mcp-git-commit-aider'

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