- Notifications
You must be signed in to change notification settings - Fork944
feature: repo resource#16
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package github | ||
import ( | ||
"context" | ||
"encoding/base64" | ||
"mime" | ||
"path/filepath" | ||
"strings" | ||
"github.com/google/go-github/v69/github" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/mark3labs/mcp-go/server" | ||
) | ||
// getRepositoryContent defines the resource template and handler for the Repository Content API. | ||
func getRepositoryContent(client *github.Client) (mainTemplate mcp.ResourceTemplate, reftemplate mcp.ResourceTemplate, shaTemplate mcp.ResourceTemplate, tagTemplate mcp.ResourceTemplate, prTemplate mcp.ResourceTemplate, handler server.ResourceTemplateHandlerFunc) { | ||
return mcp.NewResourceTemplate( | ||
"repo://{owner}/{repo}/contents{/path*}", // Resource template | ||
"Repository Content", // Description | ||
), mcp.NewResourceTemplate( | ||
"repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}", // Resource template | ||
"Repository Content for specific branch", // Description | ||
), mcp.NewResourceTemplate( | ||
"repo://{owner}/{repo}/sha/{sha}/contents{/path*}", // Resource template | ||
"Repository Content for specific commit", // Description | ||
), mcp.NewResourceTemplate( | ||
"repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}", // Resource template | ||
"Repository Content for specific tag", // Description | ||
), mcp.NewResourceTemplate( | ||
"repo://{owner}/{repo}/refs/pull/{pr_number}/head/contents{/path*}", // Resource template | ||
"Repository Content for specific pull request", // Description | ||
), func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { | ||
// Extract parameters from request.Params.URI | ||
owner := request.Params.Arguments["owner"].([]string)[0] | ||
repo := request.Params.Arguments["repo"].([]string)[0] | ||
// path should be a joined list of the path parts | ||
path := strings.Join(request.Params.Arguments["path"].([]string), "/") | ||
opts := &github.RepositoryContentGetOptions{} | ||
sha, ok := request.Params.Arguments["sha"].([]string) | ||
if ok { | ||
opts.Ref = sha[0] | ||
} | ||
branch, ok := request.Params.Arguments["branch"].([]string) | ||
if ok { | ||
opts.Ref = "refs/heads/" + branch[0] | ||
} | ||
tag, ok := request.Params.Arguments["tag"].([]string) | ||
if ok { | ||
opts.Ref = "refs/tags/" + tag[0] | ||
} | ||
prNumber, ok := request.Params.Arguments["pr_number"].([]string) | ||
if ok { | ||
opts.Ref = "refs/pull/" + prNumber[0] + "/head" | ||
} | ||
// Use the GitHub client to fetch repository content | ||
fileContent, directoryContent, _, err := client.Repositories.GetContents(ctx, owner, repo, path, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if directoryContent != nil { | ||
// Process the directory content and return it as resource contents | ||
var resources []mcp.ResourceContents | ||
for _, entry := range directoryContent { | ||
mimeType := "text/directory" | ||
if entry.GetType() == "file" { | ||
mimeType = mime.TypeByExtension(filepath.Ext(entry.GetName())) | ||
} | ||
resources = append(resources, mcp.TextResourceContents{ | ||
URI: entry.GetHTMLURL(), | ||
MIMEType: mimeType, | ||
Text: entry.GetName(), | ||
}) | ||
} | ||
return resources, nil | ||
} else if fileContent != nil { | ||
// Process the file content and return it as a binary resource | ||
if fileContent.Content != nil { | ||
decodedContent, err := fileContent.GetContent() | ||
if err != nil { | ||
return nil, err | ||
} | ||
mimeType := mime.TypeByExtension(filepath.Ext(fileContent.GetName())) | ||
// Check if the file is text-based | ||
if strings.HasPrefix(mimeType, "text") { | ||
// Return as TextResourceContents | ||
return []mcp.ResourceContents{ | ||
mcp.TextResourceContents{ | ||
URI: request.Params.URI, | ||
MIMEType: mimeType, | ||
Text: decodedContent, | ||
}, | ||
}, nil | ||
} | ||
// Otherwise, return as BlobResourceContents | ||
return []mcp.ResourceContents{ | ||
mcp.BlobResourceContents{ | ||
URI: request.Params.URI, | ||
MIMEType: mimeType, | ||
Blob: base64.StdEncoding.EncodeToString([]byte(decodedContent)), // Encode content as Base64 | ||
}, | ||
}, nil | ||
} | ||
} | ||
return nil, nil | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/bin/bash | ||
echo '{"jsonrpc":"2.0","id":3,"params":{"name":"get_me"},"method":"tools/call"}' | go run cmd/github-mcp-server/main.go stdio | jq . |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.