Skip to content

Conversation

@tommaso-moro
Copy link
Contributor

@tommaso-moro tommaso-moro commented Aug 11, 2025

Closes: #728

Overview

This PR significantly improves how we handle refs in the get_file_contents tool, after users reported the tool being often broken from bad ref handling.

The problem

Despite the tool description, models can pass as argument to the tool a ref formatted either as fully qualified (e.g. refs/heads/beanch_name) or as short user-friendly names (e.g. branch_name).

The get_repository_content endpoint from the Github API is able to handle both flexibly:
Screenshot 2025-08-11 at 11 47 24
Screenshot 2025-08-11 at 11 47 39

However, the get_reference endpoint doesn't (it requires a ref formatted in a specific way):
Screenshot 2025-08-11 at 11 51 42
Screenshot 2025-08-11 at 11 51 33

Our previous logic would fail often because it uses both endpoints but it was not handling user-friendly refs (e.g. branch_name, heads/branch_name, tag_name), which are often provided by models to the tool. This would thus break the tool.

The Solution

This PR introduces new, robust logic to resolve a ref by identifying the format in which it was provided and constructing a fully qualified ref accordingly. In short, the new resolveGitReference function now handles:

  • Fully-Qualified refs: refs/heads/main
  • Partially-Qualified refs: heads/main
  • Short Name refs: main (discovering automatically if it's a branch or a tag)

This ensures that the tool can handle the most common ways in which the ref input is provided by the user.

Additionally, I added:

  • Comprehensive tests that cover all new logic paths.
  • I have added quite detailed comments to the function itself to make the resolution logic, which can be tedious, much easier to follow

Demo
Screenshot 2025-08-11 at 11 43 38
Screenshot 2025-08-11 at 11 39 53
Screenshot 2025-08-07 at 17 55 20

@tommaso-moro tommaso-moro changed the title Tommy/improve ref handling Tommy/improve-ref-handling Aug 11, 2025
@tommaso-moro tommaso-moro marked this pull request as ready for review August 11, 2025 11:02
@tommaso-moro tommaso-moro requested a review from a team as a code owner August 11, 2025 11:02
Copilot AI review requested due to automatic review settings August 11, 2025 11:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR significantly improves the ref handling in the get_file_contents tool to better handle different ref formats that models commonly provide. The issue was that while the GitHub API's content endpoint can handle both fully-qualified refs (refs/heads/branch_name) and short names (branch_name), the reference endpoint requires fully-qualified refs, causing the tool to fail frequently.

Key changes:

  • Enhanced resolveGitReference function with robust logic to handle fully-qualified, partially-qualified, and short name refs
  • Added comprehensive test coverage for all ref resolution scenarios
  • Improved error handling and logging for better diagnostics

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
pkg/github/repositories.go Enhanced resolveGitReference function with improved ref resolution logic and detailed documentation
pkg/github/repositories_test.go Added comprehensive test cases covering all ref format scenarios and error conditions

Copy link
Contributor

@almaleksia almaleksia left a comment

Choose a reason for hiding this comment

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

I really like this logic!
Left some comments, mostly flow and readability related.

@tommaso-moro tommaso-moro merged commit 0b80f68 into github:main Aug 11, 2025
10 checks passed
nickytonline pushed a commit to nickytonline/github-mcp-http that referenced this pull request Oct 4, 2025
* make resolveGitReference function more robust, add comments to follow the logic

* add  tests for new functionality

* lint fix 1

* fix linting by using inverted if instead of empty if block

* remove unused var

* refactor

* remove comment

* small fix

* add ref == "" case in switch statement, use originalRef instead of ref in case definitions and some of the logic
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 not getting Permissions even they were given (for Organization)

2 participants