|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "mime" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/google/go-github/v69/github" |
| 11 | + "github.com/mark3labs/mcp-go/mcp" |
| 12 | + "github.com/mark3labs/mcp-go/server" |
| 13 | +) |
| 14 | + |
| 15 | +// getRepositoryContent defines the resource template and handler for the Repository Content API. |
| 16 | +func getRepositoryContent(client *github.Client) (mainTemplate mcp.ResourceTemplate, reftemplate mcp.ResourceTemplate, shaTemplate mcp.ResourceTemplate, tagTemplate mcp.ResourceTemplate, prTemplate mcp.ResourceTemplate, handler server.ResourceTemplateHandlerFunc) { |
| 17 | + |
| 18 | + return mcp.NewResourceTemplate( |
| 19 | + "repo://{owner}/{repo}/contents{/path*}", // Resource template |
| 20 | + "Repository Content", // Description |
| 21 | + ), mcp.NewResourceTemplate( |
| 22 | + "repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}", // Resource template |
| 23 | + "Repository Content for specific branch", // Description |
| 24 | + ), mcp.NewResourceTemplate( |
| 25 | + "repo://{owner}/{repo}/sha/{sha}/contents{/path*}", // Resource template |
| 26 | + "Repository Content for specific commit", // Description |
| 27 | + ), mcp.NewResourceTemplate( |
| 28 | + "repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}", // Resource template |
| 29 | + "Repository Content for specific tag", // Description |
| 30 | + ), mcp.NewResourceTemplate( |
| 31 | + "repo://{owner}/{repo}/refs/pull/{pr_number}/head/contents{/path*}", // Resource template |
| 32 | + "Repository Content for specific pull request", // Description |
| 33 | + ), func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { |
| 34 | + // Extract parameters from request.Params.URI |
| 35 | + |
| 36 | + owner := request.Params.Arguments["owner"].([]string)[0] |
| 37 | + repo := request.Params.Arguments["repo"].([]string)[0] |
| 38 | + // path should be a joined list of the path parts |
| 39 | + path := strings.Join(request.Params.Arguments["path"].([]string), "/") |
| 40 | + |
| 41 | + opts := &github.RepositoryContentGetOptions{} |
| 42 | + |
| 43 | + sha, ok := request.Params.Arguments["sha"].([]string) |
| 44 | + if ok { |
| 45 | + opts.Ref = sha[0] |
| 46 | + } |
| 47 | + |
| 48 | + branch, ok := request.Params.Arguments["branch"].([]string) |
| 49 | + if ok { |
| 50 | + opts.Ref = "refs/heads/" + branch[0] |
| 51 | + } |
| 52 | + |
| 53 | + tag, ok := request.Params.Arguments["tag"].([]string) |
| 54 | + if ok { |
| 55 | + opts.Ref = "refs/tags/" + tag[0] |
| 56 | + } |
| 57 | + prNumber, ok := request.Params.Arguments["pr_number"].([]string) |
| 58 | + if ok { |
| 59 | + opts.Ref = "refs/pull/" + prNumber[0] + "/head" |
| 60 | + } |
| 61 | + |
| 62 | + // Use the GitHub client to fetch repository content |
| 63 | + fileContent, directoryContent, _, err := client.Repositories.GetContents(ctx, owner, repo, path, opts) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + if directoryContent != nil { |
| 69 | + // Process the directory content and return it as resource contents |
| 70 | + var resources []mcp.ResourceContents |
| 71 | + for _, entry := range directoryContent { |
| 72 | + mimeType := "text/directory" |
| 73 | + if entry.GetType() == "file" { |
| 74 | + mimeType = mime.TypeByExtension(filepath.Ext(entry.GetName())) |
| 75 | + } |
| 76 | + resources = append(resources, mcp.TextResourceContents{ |
| 77 | + URI: entry.GetHTMLURL(), |
| 78 | + MIMEType: mimeType, |
| 79 | + Text: entry.GetName(), |
| 80 | + }) |
| 81 | + |
| 82 | + } |
| 83 | + return resources, nil |
| 84 | + |
| 85 | + } else if fileContent != nil { |
| 86 | + // Process the file content and return it as a binary resource |
| 87 | + |
| 88 | + if fileContent.Content != nil { |
| 89 | + decodedContent, err := fileContent.GetContent() |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + mimeType := mime.TypeByExtension(filepath.Ext(fileContent.GetName())) |
| 95 | + |
| 96 | + // Check if the file is text-based |
| 97 | + if strings.HasPrefix(mimeType, "text") { |
| 98 | + // Return as TextResourceContents |
| 99 | + return []mcp.ResourceContents{ |
| 100 | + mcp.TextResourceContents{ |
| 101 | + URI: request.Params.URI, |
| 102 | + MIMEType: mimeType, |
| 103 | + Text: decodedContent, |
| 104 | + }, |
| 105 | + }, nil |
| 106 | + } |
| 107 | + |
| 108 | + // Otherwise, return as BlobResourceContents |
| 109 | + return []mcp.ResourceContents{ |
| 110 | + mcp.BlobResourceContents{ |
| 111 | + URI: request.Params.URI, |
| 112 | + MIMEType: mimeType, |
| 113 | + Blob: base64.StdEncoding.EncodeToString([]byte(decodedContent)), // Encode content as Base64 |
| 114 | + }, |
| 115 | + }, nil |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return nil, nil |
| 120 | + } |
| 121 | +} |
0 commit comments