Skip to main content
Glama
bsreeram08

Git Repo Browser MCP

git_blame

Identify which commit introduced each line in a file to track changes and understand code evolution in Git repositories.

Instructions

Get blame information for a file.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
repo_pathYesThe path to the local Git repository
file_pathYesPath to the file
revNoRevision to blame (default: HEAD)HEAD

Implementation Reference

  • The core handler function that runs `git blame --line-porcelain`, parses the detailed output, and returns structured blame information for each line including commit hash, author, time, and content.
    export async function handleGitBlame({ repo_path, file_path, rev = "HEAD" }) {
      try {
        const git = simpleGit(repo_path);
    
        // Run git blame
        const blameResult = await git.raw([
          "blame",
          "--line-porcelain",
          rev,
          "--",
          file_path,
        ]);
    
        // Parse the output
        const lines = blameResult.split("\n");
        const blameInfo = [];
    
        let currentCommit = null;
    
        for (let i = 0; i < lines.length; i++) {
          const line = lines[i];
    
          // Start of a new blame entry
          if (line.match(/^[0-9a-f]{40}/)) {
            if (currentCommit) {
              blameInfo.push(currentCommit);
            }
    
            const parts = line.split(" ");
            currentCommit = {
              hash: parts[0],
              originalLine: parseInt(parts[1]),
              finalLine: parseInt(parts[2]),
              lineCount: parseInt(parts[3] || 1),
              author: "",
              authorMail: "",
              authorTime: 0,
              subject: "",
              content: "",
            };
          } else if (line.startsWith("author ") && currentCommit) {
            currentCommit.author = line.substring(7);
          } else if (line.startsWith("author-mail ") && currentCommit) {
            currentCommit.authorMail = line.substring(12).replace(/[<>]/g, "");
          } else if (line.startsWith("author-time ") && currentCommit) {
            currentCommit.authorTime = parseInt(line.substring(12));
          } else if (line.startsWith("summary ") && currentCommit) {
            currentCommit.subject = line.substring(8);
          } else if (line.startsWith("\t") && currentCommit) {
            // This is the content line
            currentCommit.content = line.substring(1);
            blameInfo.push(currentCommit);
            currentCommit = null;
          }
        }
    
        // Add the last commit if there is one
        if (currentCommit) {
          blameInfo.push(currentCommit);
        }
    
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(
                {
                  success: true,
                  file: file_path,
                  blame: blameInfo,
                },
                null,
                2
              ),
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(
                { error: `Failed to get blame information: ${error.message}` },
                null,
                2
              ),
            },
          ],
          isError: true,
        };
      }
    }
  • Tool schema definition specifying the name, description, and input parameters (repo_path, file_path, rev) for the git_blame tool, used in ListTools response.
    {
      name: "git_blame",
      description: "Get blame information for a file.",
      inputSchema: {
        type: "object",
        properties: {
          repo_path: {
            type: "string",
            description: "The path to the local Git repository",
          },
          file_path: {
            type: "string",
            description: "Path to the file",
          },
          rev: {
            type: "string",
            description: "Revision to blame (default: HEAD)",
            default: "HEAD",
          },
        },
        required: ["repo_path", "file_path"],
      },
    },
  • src/server.js:898-927 (registration)
    Maps the tool name 'git_blame' to its handler function handleGitBlame in the central handlersMap used by the MCP server to dispatch tool calls.
    this.handlersMap = {
      // Primary handlers
      git_directory_structure: handleGitDirectoryStructure,
      git_read_files: handleGitReadFiles,
      git_branch_diff: handleGitBranchDiff,
      git_commit_history: handleGitCommitHistory,
      git_commits_details: handleGitCommitsDetails,
      git_local_changes: handleGitLocalChanges,
      git_search_code: handleGitSearchCode,
      git_commit: handleGitCommit,
      git_track: handleGitTrack,
      git_checkout_branch: handleGitCheckoutBranch,
      git_delete_branch: handleGitDeleteBranch,
      git_merge_branch: handleGitMergeBranch,
      git_push: handleGitPush,
      git_pull: handleGitPull,
      git_stash: handleGitStash,
      git_create_tag: handleGitCreateTag,
      git_rebase: handleGitRebase,
      git_config: handleGitConfig,
      git_reset: handleGitReset,
      git_archive: handleGitArchive,
      git_attributes: handleGitAttributes,
      git_blame: handleGitBlame,
      git_clean: handleGitClean,
      git_hooks: handleGitHooks,
      git_lfs: handleGitLFS,
      git_lfs_fetch: handleGitLFSFetch,
      git_revert: handleGitRevert,
    };
  • Imports handleGitBlame from other-operations.js into the index barrel file for centralized access.
    import {
      handleGitArchive,
      handleGitAttributes,
      handleGitBlame,
      handleGitClean,
      handleGitHooks,
      handleGitLFS,
      handleGitLFSFetch,
      handleGitRevert,
    } from "./other-operations.js";
  • Re-exports handleGitBlame from the handlers index for convenient import in server.js.
    // Other operations
    handleGitArchive,
    handleGitAttributes,
    handleGitBlame,
    handleGitClean,
    handleGitHooks,
    handleGitLFS,
    handleGitLFSFetch,
    handleGitRevert,

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/bsreeram08/git-commands-mcp'

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