- Notifications
You must be signed in to change notification settings - Fork1.2k
fix(tools): Ensure get_file_contents returns file SHA#599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
The previous implementation of `get_file_contents` had two issues:1. When fetching a file, it used the raw content endpoint, which does not provide the file's SHA.2. When fetching a directory, it returned a raw JSON array, which was not easily parsable by the agent.This commit refactors the `get_file_contents` tool to always use the `client.Repositories.GetContents` method. This method returns a `RepositoryContent` object (or a slice of them for directories), which always includes the SHA and other metadata.The function now marshals this object (or slice) into a JSON string, providing a consistent and parsable output that includes the necessary SHA for subsequent file operations.
There was a problem hiding this 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 ensures theget_file_contents
tool includes the file SHA in its response and cleans up the implementation. Key changes include:
- Added the
sha
field to theFileContent
struct and propagate it throughGetFileContents
. - Removed the raw content fallback logic for simplicity.
- Standardized error handling using
fmt.Errorf
and GitHub API error wrappers.
Comments suppressed due to low confidence (4)
pkg/github/repositories.go:550
- Now that SHA is included in the
result
, consider adding automated tests to verify thatGetFileContents
properly populatessha
for file responses and that directory responses remain unchanged.
r, err := json.Marshal(result)
pkg/github/repositories.go:515
- This return uses a generic
fmt.Errorf
instead of wrapping the error in an MCP tool result (e.g.,mcp.NewToolResultError
). Consider returning a consistentToolResult
error so callers receive the standardized error structure.
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
pkg/github/repositories.go:536
- Similar to the client error above, this returns a raw
fmt.Errorf
. To maintain consistent tool output, wrap this inmcp.NewToolResultError
instead of returning a plain error.
return nil, fmt.Errorf("failed to read response body: %w", err)
pkg/github/repositories.go:552
- This marshal error is returned as a plain error. For uniform tool behavior, return
mcp.NewToolResultError
with the error message.
return nil, fmt.Errorf("failed to marshal response: %w", err)
This PR addresses the issue where the
get_file_contents
tool fails to return the SHA hash of the file content. The SHA is a crucial piece of metadata, especially for subsequent update operations which require it.What was the problem?
The
get_file_contents
tool's response struct did not include thesha
field from the GitHub API response, making it impossible to get a file's SHA through the tool. This breaks workflows that need to read a file and then update it, ascreate_or_update_file
requires the blob's SHA.How does this PR fix it?
The
FileContent
struct inpkg/github/content.go
has been updated to include theSHA
field. TheGetFileContents
function now correctly populates this field from the GitHub API response.Verification
This change was manually verified in a sandbox environment. By calling the
get_file_contents
tool via the@modelcontextprotocol/inspector
, I confirmed that the JSON response now correctly includes the"sha"
key and its corresponding value.This change is backward-compatible as it only adds a field to the response.
Fixes#595