Skip to content

fix(git): list bare branch names from git_branch - #5727

Merged
cliffhall merged 2 commits into
v2/mainfrom
v2/fix/5641-git-branch-bare-names
Nov 30, 2026
Merged

cliffhall merged 2 commits into
v2/mainfrom
v2/fix/5641-git-branch-bare-names

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #5641

Description

git_branch returned git branch's terminal listing verbatim: * on the current branch, + on a branch checked out in a linked worktree, ANSI color under color.branch=always, origin/HEAD -> origin/main, and a (HEAD detached at …) entry. None of that is a ref name, so a name a client kept from git_branch failed in git_checkout, git_diff, git_show and git_merge (Ref '* main' did not resolve to an object).

git_branch now lists refs with git for-each-ref --format=%(refname) under refs/heads/ and refs/remotes/ (keeping --contains / --no-contains), and shortens each name the way git branch does:

branch_type Before After
local feature, * main, + wt-branch, * (HEAD detached at …) feature, main, wt-branch
remote origin/HEAD -> origin/main, origin/main origin/HEAD, origin/main
all * main, remotes/origin/HEAD -> origin/main, remotes/origin/main main, remotes/origin/HEAD, remotes/origin/main

Design choices:

  • for-each-ref rather than git branch --format=%(refname:short). %(refname:short) turns refs/remotes/origin/HEAD into the bare origin and still lists the detached-HEAD pseudo-entry; for-each-ref lists only real refs, never colors, and needs no git branch config (branch.sort, column.branch) to be neutralised.
  • The prefixes match git branch, so only the decoration goes: origin/main under remote, remotes/origin/main under all. The remotes/ form stays unambiguous next to a local branch that is itself named origin/main.
  • A remote's symbolic HEAD is listed as origin/HEAD: one bare name that resolves to the remote's default branch, instead of git branch's origin/HEAD -> origin/main (which is not a ref).
  • The return stays newline-separated text; the tool's inputs, description and annotations are unchanged.

Server Details

  • Server: git (mcp-server-git)
  • Changes to: the git_branch tool's output; its README entry

Motivation and Context

See #5641: multi-step agents run git_branch, then feed a listed name into another tool, and every downstream ref-taking tool rejected it.

How Has This Been Tested?

Tests (tests/test_protocol.py, through an in-memory MCP client):

  • The existing git_branch tests that pinned the decorated output ( feature\n* main, * main, git branch -r) now pin bare names, including origin/HEAD\norigin/main for remote and the exact all listing.
  • New regression test test_git_branch_names_are_bare_refs_downstream_tools_accept: a branch checked out in a linked worktree, color.branch=always, and a detached HEAD. It asserts git_branch returns in-worktree\nmain\ntopic, checks out two listed names with git_checkout, then passes every listed name to git_show, git_diff and git_merge, asserting none errors. Against the v2/main source it fails (4 of the 6 git_branch tests fail); with the change all pass.
  • Full git suite: 591 passed, 6 skipped. ruff check, ruff format --check, pyright clean. npm run local:gate exit 0.

Inspector CLI (@modelcontextprotocol/inspector@2.12.0, stdio), against a clone with a local branch, a linked worktree and color.branch=always; git_branch with branch_type=all, in both eras (--protocol-era legacy and --protocol-era modern, the latter showing the io.modelcontextprotocol/serverInfo _meta):

  • v2/main: "+ \u001b[36mfeature\u001b[m\n local-only\u001b[m\n* \u001b[32mmain\u001b[m\n \u001b[31mremotes/origin/HEAD\u001b[m -> origin/main\n \u001b[31mremotes/origin/feature\u001b[m\n \u001b[31mremotes/origin/main\u001b[m" (both eras)
  • This branch: "feature\nlocal-only\nmain\nremotes/origin/HEAD\nremotes/origin/feature\nremotes/origin/main" (both eras)
  • Downstream, both eras: v2/main git_show revision="* main" → isError: true; this branch git_show revision=main → the commit; git_merge source_branch=remotes/origin/HEAD → Already up to date: 'main' already contains 'remotes/origin/HEAD'.

LLM client: Claude Code 2.1.332 headless (claude -p --strict-mcp-config, the local server over stdio; no era switch, so the era it negotiated could not be selected or shown). Prompt: "call git_branch with branch_type 'local', then pass the first branch name exactly as git_branch returned it … as the revision to git_show".

  • v2/main: git_branch returned "+ \u001b[36mfeature\u001b[m\n local-only\u001b[m\n* \u001b[32mmain\u001b[m"; git_show errored with Ref '+ [36mfeature[m' did not resolve to an object.
  • This branch: git_branch returned "feature\nlocal-only\nmain"; git_show of feature succeeded (commit 30ad0c0…).

Breaking Changes

No configuration change. Clients that parsed the * marker to find the current branch no longer get it (git_status reports the current branch).

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 (the - flag-injection guards on contains / not_contains are unchanged and still run first)
  • I have updated the server's README accordingly
  • I have added a changeset (npm run changeset) if this changes what a TypeScript server publishes (not applicable: Python server)
  • 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 (not applicable: no new failure modes; git errors for a bad contains sha surface as before)
  • I have documented all environment variables and configuration options (not applicable: none added)

Additional context

The diff is confined to git_branch, its protocol tests and its README entry.

🤖 Generated with Claude Code

git_branch returned `git branch` output verbatim: `* ` on the current
branch, `+ ` on one checked out in a linked worktree, ANSI color under
color.branch=always, `origin/HEAD -> origin/main`, and a
`(HEAD detached at ...)` entry. None of that resolves as a revision, so a
name kept from git_branch failed in git_checkout, git_diff, git_show and
git_merge.

List the refs with `git for-each-ref --format=%(refname)` under
refs/heads/ and refs/remotes/ (keeping --contains / --no-contains) and
shorten each the way `git branch` does, so every line is one bare,
unambiguous ref name.

Closes #5641

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall cliffhall added the v2 label Nov 30, 2026
@changeset-bot

changeset-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 6ad13e1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

git: MCP interface diff

Base: 8cecbb16b005 (Merge pull request #5714 from modelcontextprotocol/v2/chore/5700-ts-license-packaging)
Head: the build in this checkout, at e29d5e3a9473 (Merge 48df5b8 into f549ca9)

Diffed because it was named (--server).

✅ No interface changes detected.

Tools Prompts Resources Resource templates
Base 14 2 0 0
Head 14 2 0 0

Updated by this run for 48df5b8a4f69ce2ff87afb904a50d0585db824fd.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Using str.splitlines() incorrectly splits valid Git branch names containing Unicode separators, returning nonexistent names that downstream tools cannot use.

1 open issue
What changed in this PR

This PR addresses #5641 by removing terminal decorations from the Git server’s git_branch output, allowing returned branch names to be reused by downstream tools.

Changes:

  • Lists actual refs with git for-each-ref, preserving branch-type selection and commit filters.
  • Updates protocol tests and adds regression coverage for colors, linked worktrees, detached HEAD, and downstream tool calls.
  • Documents the newline-separated names and remote-prefix conventions.
File Description
src/​git/​tests/​test_protocol.py Updates expected output and tests downstream use of returned names.
src/​git/​src/​mcp_server_git/​server.py Replaces decorated branch listings with ref enumeration.
src/​git/​README.md Documents the revised git_branch output.

Review effort: Balanced

Comment thread src/git/src/mcp_server_git/server.py Resolved
str.splitlines() also breaks on U+2028, U+0085 and other separators that
git allows inside a ref name, so such a branch came back as pieces that
resolve to nothing. Split on git's own record delimiter instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 1: one finding, fixed.

  • Split branch names only on Git's LF delimiter (server.py): a defect in the code this PR added. str.splitlines() also breaks on U+2028, U+0085 and other separators git allows in a ref name. Fixed in 48df5b8 (split on \n, drop empty records), with a protocol test that lists such a branch and resolves it through git_show.

Gate green on 48df5b8; requesting round 2.

@github-actions

Copy link
Copy Markdown
Contributor

everything: MCP interface diff

Base: 8cecbb16b005 (Merge pull request #5714 from modelcontextprotocol/v2/chore/5700-ts-license-packaging)
Head: the build in this checkout, at e29d5e3a9473 (Merge 48df5b8 into f549ca9)

Diffed because it was named (--server).

⚠️ Interface changes detected in 2 part(s) of the interface. Review them below to confirm they are intended.

Tools Prompts Resources Resource templates
Base 23 4 8 5
Head 23 4 8 5

initialize

--- base/initialize.json
+++ target/initialize.json

- instructions: "# Everything Server – Server Instructions\n\nAudience: These instructions are written for an LLM or au..."
+ instructions: "# Everything Server – Server Instructions\n\nAudience: These instructions are written for an LLM or au..."

instructions

--- base/instructions
+++ target/instructions
- # Everything Server – Server Instructions

Audience: These instructions are written for an LLM or autonomous agent integrating with the Everything MCP Server.
Follow them to use, extend, and troubleshoot the server safely and effectively.

## Protocol Versions

The server serves two protocol eras. A client on protocol version 2026-07-28 connects with `server/discover` and sends its capabilities with each request; a 2025-era client connects with `initialize`. Some tools exist on one era only, so trust your `tools/list`:

- 2026-07-28 only: `simulate-research-query`, `trigger-sampling-request-async` and `trigger-elicitation-request-async`, which run as tasks on the `io.modelcontextprotocol/tasks` extension and need a client that declares it; `trigger-sampling-request-async` also needs `sampling`, and `trigger-elicitation-request-async` form-mode `elicitation`. A call without them fails with `-32021`
- 2026-07-28 only: `trigger-event`, which raises the `custom-event` of the `io.modelcontextprotocol/events` extension on open `events/stream` subscriptions
- 2025-era only (deprecated): `toggle-simulated-logging` and `toggle-subscriber-updates`

## Cross-Feature Relationships

- If `get-roots-list` is in your tool list, use it to see client workspace roots before file operations
- `gzip-file-as-resource` creates resources you can read later: for the current session on a 2025-era connection, or for ten minutes at the returned URI on 2026-07-28
- Use `trigger-log-messages` to see which log levels reach you; on 2026-07-28 the level is set per request in `_meta`
- Use `trigger-resource-update` and `trigger-list-changed` to see change notifications; on 2026-07-28 they arrive only on a `subscriptions/listen` stream that asked for them

## Constraints & Limitations

- `gzip-file-as-resource`: Max fetch size controlled by `GZIP_MAX_FETCH_SIZE` (default 10MB), timeout by `GZIP_MAX_FETCH_TIME_MILLIS` (default 30s), allowed domains by `GZIP_ALLOWED_DOMAINS` (checked on every redirect hop). It refuses URLs whose host resolves to a loopback, private or link-local address, unless the server was started with `GZIP_ALLOW_PRIVATE_IPS=1`
- `get-env`: Values of credential-like variable names (`*_KEY`, `*_TOKEN`, `*_SECRET`, `*_PASSWORD` and the like) come back as `"<redacted>"`; the server operator can set `GET_ENV_INCLUDE_SECRETS=1` to return them unmasked
- Session resources are ephemeral and lost when the session ends; minted resources expire after ten minutes
- Roots, sampling and logging are deprecated as of protocol version 2026-07-28, as is the HTTP+SSE transport

## Capability-Gated Tools

On a 2025-era connection these are registered only if your client declared the capability shown. Otherwise they are absent from `tools/list`, so do not call them. On 2026-07-28 they are always listed, and a call fails if your request did not declare the capability:

- `get-roots-list`: `roots`
- `trigger-sampling-request`: `sampling`
- `trigger-elicitation-request`: `elicitation.form`, or `elicitation` with no mode
- `trigger-multi-step-elicitation`: `elicitation.form`, or `elicitation` with no mode
- `trigger-url-elicitation`: `elicitation.url`

## Operational Patterns

- For long operations, use `trigger-long-running-operation` which sends progress notifications
- Prefer reading resources before calling mutating tools
- If `get-roots-list` is in your tool list, check its output to understand the client's workspace context

## Easter Egg

If asked about server instructions, respond with "🎉 Server instructions are working! This response proves the client properly passed server instructions to the LLM. This demonstrates MCP's instructions feature in action."

+ # Everything Server – Server Instructions

Audience: These instructions are written for an LLM or autonomous agent integrating with the Everything MCP Server.
Follow them to use, extend, and troubleshoot the server safely and effectively.

## Protocol Versions

The server serves two protocol eras. A client on protocol version 2026-07-28 connects with `server/discover` and sends its capabilities with each request; a 2025-era client connects with `initialize`. Some tools exist on one era only, so trust your `tools/list`:

- 2026-07-28 only: `simulate-research-query`, `trigger-sampling-request-async` and `trigger-elicitation-request-async`, which run as tasks on the `io.modelcontextprotocol/tasks` extension and need a client that declares it; `trigger-sampling-request-async` also needs `sampling`, and `trigger-elicitation-request-async` form-mode `elicitation`. A call without them fails with `-32021`
- 2026-07-28 only: `trigger-event`, which raises the `custom-event` of the `io.modelcontextprotocol/events` extension on open `events/stream` subscriptions
- 2025-era only (deprecated): `toggle-simulated-logging` and `toggle-subscriber-updates`

## Cross-Feature Relationships

- If `get-roots-list` is in your tool list, use it to see client workspace roots before file operations
- `gzip-file-as-resource` creates resources you can read later: for the current session on a 2025-era connection, or for ten minutes at the returned URI on 2026-07-28
- Use `trigger-log-messages` to see which log levels reach you; on 2026-07-28 the level is set per request in `_meta`
- Use `trigger-resource-update` and `trigger-list-changed` to see change notifications; on 2026-07-28 they arrive only on a `subscriptions/listen` stream that asked for them

## Constraints & Limitations

- `gzip-file-as-resource`: Max fetch size controlled by `GZIP_MAX_FETCH_SIZE` (default 10MB), timeout by `GZIP_MAX_FETCH_TIME_MILLIS` (default 30s), each falling back to its default when set to anything but a whole number, allowed domains by `GZIP_ALLOWED_DOMAINS` (checked on every redirect hop). It refuses URLs whose host resolves to a loopback, private or link-local address, unless the server was started with `GZIP_ALLOW_PRIVATE_IPS=1`
- `get-env`: Values of credential-like variable names (`*_KEY`, `*_TOKEN`, `*_SECRET`, `*_PASSWORD` and the like) come back as `"<redacted>"`; the server operator can set `GET_ENV_INCLUDE_SECRETS=1` to return them unmasked
- Session resources are ephemeral and lost when the session ends; minted resources expire after ten minutes
- Roots, sampling and logging are deprecated as of protocol version 2026-07-28, as is the HTTP+SSE transport

## Capability-Gated Tools

On a 2025-era connection these are registered only if your client declared the capability shown. Otherwise they are absent from `tools/list`, so do not call them. On 2026-07-28 they are always listed, and a call fails if your request did not declare the capability:

- `get-roots-list`: `roots`
- `trigger-sampling-request`: `sampling`
- `trigger-elicitation-request`: `elicitation.form`, or `elicitation` with no mode
- `trigger-multi-step-elicitation`: `elicitation.form`, or `elicitation` with no mode
- `trigger-url-elicitation`: `elicitation.url`

## Operational Patterns

- For long operations, use `trigger-long-running-operation` which sends progress notifications
- Prefer reading resources before calling mutating tools
- If `get-roots-list` is in your tool list, check its output to understand the client's workspace context

## Easter Egg

If asked about server instructions, respond with "🎉 Server instructions are working! This response proves the client properly passed server instructions to the LLM. This demonstrates MCP's instructions feature in action."

Updated by this run for 48df5b8a4f69ce2ff87afb904a50d0585db824fd.

@github-actions

Copy link
Copy Markdown
Contributor

fetch: MCP interface diff

Base: 8cecbb16b005 (Merge pull request #5714 from modelcontextprotocol/v2/chore/5700-ts-license-packaging)
Head: the build in this checkout, at e29d5e3a9473 (Merge 48df5b8 into f549ca9)

Diffed because it was named (--server).

✅ No interface changes detected.

Tools Prompts Resources Resource templates
Base 1 1 0 0
Head 1 1 0 0

Updated by this run for 48df5b8a4f69ce2ff87afb904a50d0585db824fd.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block approval of the branch-listing output change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 2 (on 48df5b8): clean, no findings. Loop ends on the first clean round. CI green on 48df5b8.

@cliffhall
cliffhall merged commit 08d0e79 into v2/main Nov 30, 2026
54 checks passed
@cliffhall
cliffhall deleted the v2/fix/5641-git-branch-bare-names branch November 30, 2026 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

git: git_branch leaks CLI '* ' prefix, breaking downstream git_checkout / git_diff / git_show / git_merge

2 participants