Skip to content

fix: resolve annotated tags to their commit in get_file_contents - #3936

Open
burrows99 wants to merge 2 commits into
github:mainfrom
burrows99:fix/peel-annotated-tag-ref
Open

burrows99 wants to merge 2 commits into
github:mainfrom
burrows99:fix/peel-annotated-tag-ref

Conversation

@burrows99

Copy link
Copy Markdown
Contributor

Summary

get_file_contents now resolves an annotated tag to the commit it points at, so the repo://.../sha/{sha}/contents/... URI it returns can be read back. Before, the URI named the tag object and reading it failed with 404.

Why

An annotated tag ref (refs/tags/v1.0.0 in this repo) points at a tag object, not a commit. resolveGitReference returned reference.Object.SHA as is, so for annotated tags it returned the tag object's SHA:

  • the embedded resource / resource link URI became repo://github/github-mcp-server/sha/c7fc888…/contents/go.mod, and reading it through the server's own repository_content_commit template fails with 404 Not Found (raw content does not accept tag object SHAs). For files of 1 MB or more the tool returns no content, only a resource link (and a download URL), and that link cannot be read either;
  • the "resolved refs" in the path-suggestion message report the tag object SHA as if it were the commit.

Branches and lightweight tags were fine because their ref already points at a commit.

No upstream issue is associated with this fix. Reproduced on main at 9a72d8d.

flowchart TD
    A[get_file_contents with ref v1.0.0] --> B{Annotated tag ref}
    B -->|Before| C[URI names the tag object and reads back 404]
    B -->|After| D[URI names the tagged commit and reads back the file]
Loading

What changed

  • pkg/github/repositories_helper.go: after the ref is resolved, peel annotated tags with Git.GetTag until the object is not a tag, and return that SHA. One extra API call, only for annotated tags.
  • pkg/github/repositories_test.go: Test_resolveGitReference case "annotated tag resolves to the tagged commit" (ref returns a type: tag object, tag API returns the commit).

Narrated recording in the MCP Inspector, before then after (sound on). Server run with --read-only --toolsets=repos,security_advisories:

evidence.mp4

Edge cases and unchanged behaviour checked (in the running server through the Inspector, as shown in the video):

  • ref: refs/tags/v1.0.0 (fully qualified annotated tag) -> sha/569a48d… (the tagged commit)
  • lightweight tag cli/cli v2.60.0 -> sha/44ee177…, the same commit as before
  • branch main -> sha/9a72d8d… (branch head), unchanged
  • unit test: branch, lightweight tag, heads/, tags/, PR ref and SHA cases in Test_resolveGitReference unchanged and passing

MCP impact

  • No tool or API changes
  • Tool schema or behavior changed
    get_file_contents input/output schema is unchanged; for annotated tags the returned resource URI and resolved SHA now name the commit.
  • New tool added

Prompts tested (tool changes only)

  • "Show me go.mod from github/github-mcp-server at tag v1.0.0" (then read the returned resource URI)

Security / limits

  • No security or limits impact

Tool renaming

  • I am renaming tools as part of this PR (e.g. a part of a consolidation effort)
    • I have added the new tool aliases in deprecated_tool_aliases.go
  • I am not renaming tools as part of this PR

Lint & tests

  • Linted locally with ./script/lint
    gofmt -s -l pkg/github clean; golangci-lint v2.14.0 (the pinned version) on ./pkg/github/...: 0 issues.
  • Tested locally with ./script/test
    go test ./pkg/github/ passes. The new case fails on main (expected: "commit-sha", actual: "tag-object-sha") and passes with the fix. I did not run the whole repository suite.

Docs

  • Not needed

Skills

  • Updated relevant skills or confirmed no bundled skill applies
    No bundled skill describes ref resolution.

AI assistance (Claude Code) was used to find the bug, write the fix and test, and record the video.

🤖 Generated with Claude Code

An annotated tag ref points at a tag object. resolveGitReference
returned that object's SHA as if it were a commit, so get_file_contents
built repo://.../sha/<tag object>/contents/... resource URIs that the
server's own repository resource cannot read (404), and the resolved
refs it reports name the tag object instead of the commit.

Peel annotated tags with the Git tags API so the SHA names the tagged
commit, as it already does for branches and lightweight tags.
@burrows99
burrows99 requested a review from a team as a code owner November 24, 2026 04:36
Copilot AI adaptive review requested due to automatic review settings November 24, 2026 04:36

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

Nested annotated tags trigger authenticated API requests without a depth limit, allowing repository-controlled chains to prolong file reads and exhaust the caller’s API quota.

1 open issue
What changed in this PR

This PR fixes annotated-tag resolution in get_file_contents so returned resource URIs identify the tagged commit rather than the tag object and can be read back.

Changes:

  • Follow annotated tag objects to resolve their target SHA.
  • Add a regression test for resolving an annotated tag to a commit.
File Description
pkg/​github/​repositories_test.go Adds an annotated-tag resolution regression test.
pkg/​github/​repositories_helper.go Resolves annotated tags before returning content options.

Review effort: Balanced (adaptive)

Comment thread pkg/github/repositories_helper.go Resolved
A repository can nest annotated tags arbitrarily deep, and each level costs
the caller an API request. Return an error once the chain is deeper than
maxAnnotatedTagDepth (10).
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.

2 participants