Skip to content

fix(git): auto-detect repo root from --repository#3997

Open
FazeelUsmani wants to merge 1 commit intomodelcontextprotocol:mainfrom
FazeelUsmani:fix/git-auto-detect-repo-root
Open

fix(git): auto-detect repo root from --repository#3997
FazeelUsmani wants to merge 1 commit intomodelcontextprotocol:mainfrom
FazeelUsmani:fix/git-auto-detect-repo-root

Conversation

@FazeelUsmani
Copy link
Copy Markdown

Lets mcp-server-git --repository accept any path inside a Git working tree instead of only the repo root. At startup the server walks up parents to find .git/, like git rev-parse--show-toplevel.

Fixes the case in #3029 where --repository . only works when the client is launched from the repo root. Now it works from any subdirectory, so a checked-in mcp.json works for everyone without per-developer absolute paths.

Closes #3029.

Real git walks up the tree from any subdir. The server should too, otherwise --repository. It is basically unusable in a shared config.

On the thread, @jonathanhefner and @domdomegg both said not to special-case, so this applies to any path passed to --repository.

How Has This Been Tested?

  • Unit tests for resolve_repo_root: root path, nested subdir, non-repo path, and Path(".") via monkeypatch.chdir.
  • Two integration tests that drive serve() with a stubbed stdio_server to confirm the resolver is actually wired into startup and the rewrite gets logged.
  • Local smoke test: ran mcp-server-git --repository . from a few nested subdirs and confirmed it picks up the right repo.

Breaking Changes

No impact for configs that already point at the repo root.

One behavior change: --repository /repo/non_repo_subdir used to fail with "not a valid Git repository". Now it resolves upward to /repo and starts. If the resolved path differs from the input, it logs it at INFO so it's visible. The README documents this and notes that the flag alone doesn't enforce subtree scoping.

The error message also changed from "is not a valid Git repository" to "is not inside a Git repository".

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Protocol Documentation
  • My changes follow MCP security best practices
  • I have updated the server's README accordingly
  • I have tested this with an LLM client
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have documented all environment variables and configuration options

Additional context

Kept the fix scoped to the --repository CLI flag. Applying the same parent-walking to per-tool repo_path would break git_add semantics: repo.git.add(".") runs at the repo root, so passing repo_path=/repo/subdir with files=["."] would stage the whole repo instead of just the subdir. Leaving repo_path strict avoids that. The README says so explicitly.

Walk parent directories when --repository points inside a working tree,
the same way `git rev-parse --show-toplevel` does. Makes `--repository .`
work from any subdirectory of the repo so a checked-in config works for
every developer regardless of where they launch from.

If the resolved path differs from the input, the rewrite is logged at
startup. Per-tool repo_path arguments are unchanged.

Closes modelcontextprotocol#3029
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp-server-git: Make --repository . auto-detect git root like standard git commands

1 participant