- Notifications
You must be signed in to change notification settings - Fork962
Experiment: Make a working version of completions#451
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
Draft
SamMorrowDrums wants to merge1 commit intomainChoose a base branch fromcompletions-test
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
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
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
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
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,160 @@ | ||
package github | ||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"github.com/google/go-github/v69/github" | ||
"github.com/sammorrowdrums/mcp-go/mcp" | ||
) | ||
// RepositoryResourceCompletionHandler returns a CompletionHandlerFunc for repository resource completions. | ||
func RepositoryResourceCompletionHandler(getClient GetClientFn) func(ctx context.Context, req mcp.CompleteRequest) (*mcp.CompleteResult, error) { | ||
return func(ctx context.Context, req mcp.CompleteRequest) (*mcp.CompleteResult, error) { | ||
ref, ok := req.Params.Ref.(map[string]any) | ||
if !ok || ref["type"] != "ref/resource" { | ||
return nil, nil // Not a resource completion | ||
} | ||
uri, _ := ref["uri"].(string) | ||
argName := req.Params.Argument.Name | ||
argValue := req.Params.Argument.Value | ||
client, err := getClient(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var values []string | ||
switch argName { | ||
case "owner": | ||
user, _, err := client.Users.Get(ctx, "") | ||
if err == nil && user.GetLogin() != "" { | ||
values = append(values, user.GetLogin()) | ||
} | ||
orgs, _, _ := client.Organizations.List(ctx, "", nil) | ||
for _, org := range orgs { | ||
values = append(values, org.GetLogin()) | ||
} | ||
case "repo": | ||
// print the whole mcp complete request for debugging | ||
fmt.Printf("MCP Complete Request: %+v\n", req) | ||
fmt.Printf("URI: %s\n", uri) | ||
owner := getArgFromURI(uri, "owner") | ||
if owner != "" { | ||
repos, _, err := client.Search.Repositories(ctx, fmt.Sprintf("user:%s", owner), &github.SearchOptions{ListOptions: github.ListOptions{PerPage: 100}}) | ||
if err != nil || repos == nil { | ||
break | ||
} | ||
for _, repo := range repos.Repositories { | ||
if argValue == "" || strings.Contains(repo.GetName(), argValue) { | ||
values = append(values, repo.GetName()) | ||
} | ||
} | ||
} | ||
case "branch": | ||
owner := getArgFromURI(uri, "owner") | ||
repo := getArgFromURI(uri, "repo") | ||
if owner != "" && repo != "" { | ||
branches, _, _ := client.Repositories.ListBranches(ctx, owner, repo, nil) | ||
for _, branch := range branches { | ||
if argValue == "" || strings.Contains(branch.GetName(), argValue) { | ||
values = append(values, branch.GetName()) | ||
} | ||
} | ||
} | ||
case "sha": | ||
owner := getArgFromURI(uri, "owner") | ||
repo := getArgFromURI(uri, "repo") | ||
if owner != "" && repo != "" { | ||
commits, _, _ := client.Repositories.ListCommits(ctx, owner, repo, nil) | ||
for _, commit := range commits { | ||
sha := commit.GetSHA() | ||
if argValue == "" || strings.HasPrefix(sha, argValue) { | ||
values = append(values, sha) | ||
} | ||
} | ||
} | ||
case "tag": | ||
owner := getArgFromURI(uri, "owner") | ||
repo := getArgFromURI(uri, "repo") | ||
if owner != "" && repo != "" { | ||
tags, _, _ := client.Repositories.ListTags(ctx, owner, repo, nil) | ||
for _, tag := range tags { | ||
if argValue == "" || strings.Contains(tag.GetName(), argValue) { | ||
values = append(values, tag.GetName()) | ||
} | ||
} | ||
} | ||
case "prNumber": | ||
owner := getArgFromURI(uri, "owner") | ||
repo := getArgFromURI(uri, "repo") | ||
if owner != "" && repo != "" { | ||
prs, _, _ := client.PullRequests.List(ctx, owner, repo, nil) | ||
for _, pr := range prs { | ||
num := fmt.Sprintf("%d", pr.GetNumber()) | ||
if argValue == "" || strings.HasPrefix(num, argValue) { | ||
values = append(values, num) | ||
} | ||
} | ||
} | ||
case "path": | ||
owner := getArgFromURI(uri, "owner") | ||
repo := getArgFromURI(uri, "repo") | ||
refVal := getArgFromURI(uri, "branch") | ||
if refVal == "" { | ||
refVal = getArgFromURI(uri, "sha") | ||
} | ||
if refVal == "" { | ||
refVal = getArgFromURI(uri, "tag") | ||
} | ||
if refVal == "" { | ||
refVal = "main" | ||
} | ||
if owner != "" && repo != "" { | ||
contents, dirContents, _, _ := client.Repositories.GetContents(ctx, owner, repo, "", &github.RepositoryContentGetOptions{Ref: refVal}) | ||
if dirContents != nil { | ||
for _, entry := range dirContents { | ||
if argValue == "" || strings.HasPrefix(entry.GetName(), argValue) { | ||
values = append(values, entry.GetName()) | ||
} | ||
} | ||
} else if contents != nil { | ||
if argValue == "" || strings.HasPrefix(contents.GetName(), argValue) { | ||
values = append(values, contents.GetName()) | ||
} | ||
} | ||
} | ||
} | ||
if len(values) > 100 { | ||
values = values[:100] | ||
} | ||
return &mcp.CompleteResult{ | ||
Completion: struct { | ||
Values []string `json:"values"` | ||
Total int `json:"total,omitempty"` | ||
HasMore bool `json:"hasMore,omitempty"` | ||
}{ | ||
Values: values, | ||
Total: len(values), | ||
HasMore: false, | ||
}, | ||
}, nil | ||
} | ||
} | ||
func getArgFromURI(uri, name string) string { | ||
trimmed := strings.TrimPrefix(uri, "repo://") | ||
parts := strings.Split(trimmed, "/") | ||
if name == "owner" && len(parts) > 0 && parts[0] != "" { | ||
return parts[0] | ||
} | ||
if name == "repo" && len(parts) > 1 && parts[1] != "" { | ||
return parts[1] | ||
} | ||
return "" | ||
} |
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,31 @@ | ||
package github | ||
import ( | ||
"context" | ||
"testing" | ||
"github.com/google/go-github/v69/github" | ||
"github.com/sammorrowdrums/mcp-go/mcp" | ||
"github.com/stretchr/testify/require" | ||
) | ||
// Add more fake methods as needed for testing | ||
func TestRepositoryResourceCompletionHandler_Owner(t *testing.T) { | ||
// Stub getClient to return a fake client with a user and orgs | ||
getClient := func(ctx context.Context) (*github.Client, error) { | ||
client := github.NewClient(nil) | ||
// You can use github's testing helpers or mock the methods as needed | ||
return client, nil | ||
} | ||
handler := RepositoryResourceCompletionHandler(getClient) | ||
request := mcp.CompleteRequest{} | ||
request.Params.Ref = map[string]any{"type": "ref/resource", "uri": "repo://"} | ||
request.Params.Argument.Name = "owner" | ||
request.Params.Argument.Value = "" | ||
result, err := handler(context.Background(), request) | ||
require.NoError(t, err) | ||
// In a real test, assert on result.Completion.Values | ||
_ = result | ||
} |
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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.