Skip to main content
Glama
bsreeram08

Git Repo Browser MCP

git_stash

Save uncommitted changes temporarily to switch branches or clean your working directory, then restore them later when needed.

Instructions

Create or apply a stash.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
repo_pathYesThe path to the local Git repository
actionNoStash action (save, pop, apply, list, drop)save
messageNoStash message (for save action)
indexNoStash index (for pop, apply, drop actions)

Implementation Reference

  • Core handler function implementing git stash operations (save, pop, apply, list, drop) using simpleGit.
    export async function handleGitStash({
      repo_path,
      action = "save",
      message = "",
      index = 0,
    }) {
      try {
        const git = simpleGit(repo_path);
    
        let result;
        switch (action) {
          case "save":
            result = await git.stash(["save", message]);
            return {
              content: [
                {
                  type: "text",
                  text: JSON.stringify(
                    {
                      success: true,
                      message: "Changes stashed successfully",
                      stash_message: message,
                    },
                    null,
                    2
                  ),
                },
              ],
            };
    
          case "pop":
            result = await git.stash(["pop", index.toString()]);
            return {
              content: [
                {
                  type: "text",
                  text: JSON.stringify(
                    {
                      success: true,
                      message: `Applied and dropped stash@{${index}}`,
                    },
                    null,
                    2
                  ),
                },
              ],
            };
    
          case "apply":
            result = await git.stash(["apply", index.toString()]);
            return {
              content: [
                {
                  type: "text",
                  text: JSON.stringify(
                    {
                      success: true,
                      message: `Applied stash@{${index}}`,
                    },
                    null,
                    2
                  ),
                },
              ],
            };
    
          case "list":
            result = await git.stash(["list"]);
            // Parse the stash list
            const stashList = result
              .trim()
              .split("\n")
              .filter((line) => line.trim() !== "")
              .map((line) => {
                const match = line.match(/stash@\{(\d+)\}: (.*)/);
                if (match) {
                  return {
                    index: parseInt(match[1]),
                    description: match[2],
                  };
                }
                return null;
              })
              .filter((item) => item !== null);
    
            return {
              content: [
                {
                  type: "text",
                  text: JSON.stringify(
                    {
                      success: true,
                      stashes: stashList,
                    },
                    null,
                    2
                  ),
                },
              ],
            };
    
          case "drop":
            result = await git.stash(["drop", index.toString()]);
            return {
              content: [
                {
                  type: "text",
                  text: JSON.stringify(
                    {
                      success: true,
                      message: `Dropped stash@{${index}}`,
                    },
                    null,
                    2
                  ),
                },
              ],
            };
    
          default:
            return {
              content: [
                {
                  type: "text",
                  text: JSON.stringify(
                    { error: `Unknown stash action: ${action}` },
                    null,
                    2
                  ),
                },
              ],
              isError: true,
            };
        }
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(
                { error: `Failed to perform stash operation: ${error.message}` },
                null,
                2
              ),
            },
          ],
          isError: true,
        };
      }
    }
  • Tool schema definition including input parameters and validation for git_stash.
      name: "git_stash",
      description: "Create or apply a stash.",
      inputSchema: {
        type: "object",
        properties: {
          repo_path: {
            type: "string",
            description: "The path to the local Git repository",
          },
          action: {
            type: "string",
            description: "Stash action (save, pop, apply, list, drop)",
            default: "save",
            enum: ["save", "pop", "apply", "list", "drop"],
          },
          message: {
            type: "string",
            description: "Stash message (for save action)",
            default: "",
          },
          index: {
            type: "integer",
            description: "Stash index (for pop, apply, drop actions)",
            default: 0,
          },
        },
        required: ["repo_path"],
      },
    },
  • src/server.js:914-914 (registration)
    Registers the 'git_stash' tool name to the handleGitStash function in the central handlersMap.
    git_stash: handleGitStash,
  • Imports the handleGitStash handler from its implementation file for re-export.
    import { handleGitStash } from "./stash-operations.js";
  • Re-exports handleGitStash from handlers/index.js to make it available to server.js.
    handleGitStash,

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