@@ -507,6 +507,24 @@ func GetFileContents(getClient GetClientFn, getRawClient raw.GetRawClientFn, t t
507
507
// If the path is (most likely) not to be a directory, we will
508
508
// first try to get the raw content from the GitHub raw content API.
509
509
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
510
528
511
529
rawClient , err := getRawClient (ctx )
512
530
if err != nil {
@@ -548,18 +566,28 @@ func GetFileContents(getClient GetClientFn, getRawClient raw.GetRawClientFn, t t
548
566
}
549
567
550
568
if strings .HasPrefix (contentType , "application" ) || strings .HasPrefix (contentType , "text" ) {
551
- return mcp . NewToolResultResource ( "successfully downloaded text file" , mcp.TextResourceContents {
569
+ result := mcp.TextResourceContents {
552
570
URI : resourceURI ,
553
571
Text : string (body ),
554
572
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
556
579
}
557
580
558
- return mcp . NewToolResultResource ( "successfully downloaded binary file" , mcp.BlobResourceContents {
581
+ result := mcp.BlobResourceContents {
559
582
URI : resourceURI ,
560
583
Blob : base64 .StdEncoding .EncodeToString (body ),
561
584
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
563
591
564
592
}
565
593
}
0 commit comments