@@ -507,6 +507,24 @@ func GetFileContents(getClient GetClientFn, getRawClient raw.GetRawClientFn, t t
507507 // If the path is (most likely) not to be a directory, we will
508508 // first try to get the raw content from the GitHub raw content API.
509509 if path != "" && ! strings .HasSuffix (path , "/" ) {
510+ // First, get file info from Contents API to retrieve SHA
511+ var fileSHA string
512+ opts := & github.RepositoryContentGetOptions {Ref : ref }
513+ fileContent , _ , respContents , err := client .Repositories .GetContents (ctx , owner , repo , path , opts )
514+ if respContents != nil {
515+ defer func () { _ = respContents .Body .Close () }()
516+ }
517+ if err != nil {
518+ return ghErrors .NewGitHubAPIErrorResponse (ctx ,
519+ "failed to get file SHA" ,
520+ respContents ,
521+ err ,
522+ ), nil
523+ }
524+ if fileContent == nil || fileContent .SHA == nil {
525+ return mcp .NewToolResultError ("file content SHA is nil" ), nil
526+ }
527+ fileSHA = * fileContent .SHA
510528
511529 rawClient , err := getRawClient (ctx )
512530 if err != nil {
@@ -548,18 +566,28 @@ func GetFileContents(getClient GetClientFn, getRawClient raw.GetRawClientFn, t t
548566 }
549567
550568 if strings .HasPrefix (contentType , "application" ) || strings .HasPrefix (contentType , "text" ) {
551- return mcp . NewToolResultResource ( "successfully downloaded text file" , mcp.TextResourceContents {
569+ result := mcp.TextResourceContents {
552570 URI : resourceURI ,
553571 Text : string (body ),
554572 MIMEType : contentType ,
555- }), nil
573+ }
574+ // Include SHA in the result metadata
575+ if fileSHA != "" {
576+ return mcp .NewToolResultResource (fmt .Sprintf ("successfully downloaded text file (SHA: %s)" , fileSHA ), result ), nil
577+ }
578+ return mcp .NewToolResultResource ("successfully downloaded text file" , result ), nil
556579 }
557580
558- return mcp . NewToolResultResource ( "successfully downloaded binary file" , mcp.BlobResourceContents {
581+ result := mcp.BlobResourceContents {
559582 URI : resourceURI ,
560583 Blob : base64 .StdEncoding .EncodeToString (body ),
561584 MIMEType : contentType ,
562- }), nil
585+ }
586+ // Include SHA in the result metadata
587+ if fileSHA != "" {
588+ return mcp .NewToolResultResource (fmt .Sprintf ("successfully downloaded binary file (SHA: %s)" , fileSHA ), result ), nil
589+ }
590+ return mcp .NewToolResultResource ("successfully downloaded binary file" , result ), nil
563591
564592 }
565593 }
0 commit comments