@@ -454,8 +454,14 @@ func GetFileContents(getClient GetClientFn, t translations.TranslationHelperFunc
454
454
if err != nil {
455
455
return nil , fmt .Errorf ("failed to get GitHub client: %w" , err )
456
456
}
457
- opts := & github.RepositoryContentGetOptions {Ref : branch }
458
- fileContent , dirContent , resp , err := client .Repositories .GetContents (ctx , owner , repo , path , opts )
457
+
458
+ // Use the go-github client to get file/directory contents
459
+ opts := & github.RepositoryContentGetOptions {}
460
+ if branch != "" {
461
+ opts .Ref = branch
462
+ }
463
+
464
+ fileContent , directoryContent , resp , err := client .Repositories .GetContents (ctx , owner , repo , path , opts )
459
465
if err != nil {
460
466
return nil , fmt .Errorf ("failed to get file contents: %w" , err )
461
467
}
@@ -469,19 +475,23 @@ func GetFileContents(getClient GetClientFn, t translations.TranslationHelperFunc
469
475
return mcp .NewToolResultError (fmt .Sprintf ("failed to get file contents: %s" , string (body ))), nil
470
476
}
471
477
472
- var result interface {}
473
478
if fileContent != nil {
474
- result = fileContent
479
+ // It's a file - return the raw decoded content
480
+ content , err := fileContent .GetContent ()
481
+ if err != nil {
482
+ return nil , fmt .Errorf ("failed to decode file content: %w" , err )
483
+ }
484
+ return mcp .NewToolResultText (content ), nil
485
+ } else if directoryContent != nil {
486
+ // It's a directory - return JSON array of contents
487
+ r , err := json .Marshal (directoryContent )
488
+ if err != nil {
489
+ return nil , fmt .Errorf ("failed to marshal directory contents: %w" , err )
490
+ }
491
+ return mcp .NewToolResultText (string (r )), nil
475
492
} else {
476
- result = dirContent
493
+ return mcp . NewToolResultError ( "unexpected response: neither file nor directory content returned" ), nil
477
494
}
478
-
479
- r , err := json .Marshal (result )
480
- if err != nil {
481
- return nil , fmt .Errorf ("failed to marshal response: %w" , err )
482
- }
483
-
484
- return mcp .NewToolResultText (string (r )), nil
485
495
}
486
496
}
487
497
0 commit comments