Skip to content

Always include SHA in get_file_contents responses #676

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

yonaka15
Copy link

Summary

This PR provides a simpler solution to #595 by always including SHA information in the get_file_contents tool responses without requiring any parameter changes.

Unlike #605 which adds an include_sha parameter, this approach transparently enhances all responses with SHA information while preserving the existing MCP server behavior.

Approach

The implementation adds a preliminary Contents API call to retrieve SHA information before fetching the actual content:

  1. Call Contents API to get file metadata (including SHA)
  2. Call Raw Content API to get the actual content (as before)
  3. Include SHA in both the resource URI and success message

This dual-API approach ensures:

  • Text files continue to return raw text content
  • Binary files continue to return base64-encoded content
  • SHA information is always available for create_or_update_file operations

Changes

  • Modified pkg/github/repositories.go to add Contents API call before Raw Content API
  • Updated tests in pkg/github/repositories_test.go to include Contents API mocks
  • No changes to tool schema or parameters - fully backward compatible

Benefits over #605

  1. No parameter required - SHA is always included automatically
  2. Simpler for users (AI) - No need to remember to set include_sha=true
  3. Better default behavior - Addresses the core issue directly
  4. Minimal performance impact - The additional API call is lightweight

Testing

Verified with both unit tests and MCP Inspector against real repositories:

  • Text files return raw content with SHA ✅
  • Binary files return base64 content with SHA ✅
  • Directories return JSON with SHA for each item ✅
  • Graceful fallback when Contents API fails ✅

Closes #595

…ub#595)

Enhance get_file_contents to include SHA information without changing
the existing MCP server response format.

Changes:
- Add Contents API call to retrieve SHA before fetching raw content
- Include SHA in resourceURI (repo://owner/repo/sha/{SHA}/contents/path)
- Add SHA to success messages
- Update tests to verify SHA inclusion
- Maintain original behavior: text files return raw text, binaries return base64

This preserves backward compatibility while providing SHA information
for better file versioning support.

Closes github#595
@Copilot Copilot AI review requested due to automatic review settings July 12, 2025 16:06
@yonaka15 yonaka15 requested a review from a team as a code owner July 12, 2025 16:06
Copy link
Contributor

@Copilot 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 enhances the get_file_contents tool to always include SHA information in responses by adding a preliminary Contents API call before fetching raw content. This provides a simpler, backward-compatible solution that automatically includes SHA data without requiring parameter changes.

Key changes:

  • Added Contents API call to retrieve file metadata (including SHA) before fetching raw content
  • Updated resource URI format to include SHA instead of branch reference
  • Enhanced success messages to include SHA information

Reviewed Changes

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

File Description
pkg/github/repositories.go Added Contents API call to retrieve SHA before raw content fetch and updated URI/message formatting
pkg/github/repositories_test.go Updated test mocks to include Contents API responses and adjusted expected URIs to use SHA format
Comments suppressed due to low confidence (1)

pkg/github/repositories.go:513

  • The variable name 'errContents' is inconsistent with Go naming conventions. It should be 'err' or follow the existing pattern used elsewhere in the function.
				fileContent, _, respContents, errContents := client.Repositories.GetContents(ctx, owner, repo, path, opts)

@yonaka15 yonaka15 changed the title fix: Always include SHA in get_file_contents responses (#595) fix: Always include SHA in get_file_contents responses Jul 12, 2025
Ensure response body is properly closed even when an error occurs by moving
the defer statement before the error check. This prevents potential resource
leaks when the Contents API returns an error with a non-nil response.

Changes:
- Move defer respContents.Body.Close() before error checking
- Rename errContents to err for consistency
- Add nil check for respContents before attempting to close body

This follows Go best practices for handling HTTP responses and prevents
potential goroutine/memory leaks.
@yonaka15 yonaka15 changed the title fix: Always include SHA in get_file_contents responses Always include SHA in get_file_contents responses Jul 12, 2025
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.

Tool get_file_contents is missing the required sha in its response
1 participant