Skip to content

Add a tool to assign copilot to issues #405

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 65 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,67 @@ automation and interaction capabilities for developers and tools.

1. To run the server in a container, you will need to have [Docker](https://www.docker.com/) installed.
2. Once Docker is installed, you will also need to ensure Docker is running. The image is public; if you get errors on pull, you may have an expired token and need to `docker logout ghcr.io`.
3. Lastly you will need to [Create a GitHub Personal Access Token](https://github.com/settings/personal-access-tokens/new).
The MCP server can use many of the GitHub APIs, so enable the permissions that you feel comfortable granting your AI tools (to learn more about access tokens, please check out the [documentation](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)).
3. [Create a GitHub Personal Access Token](https://github.com/settings/personal-access-tokens/new).
Each tool requires specific permissions to function. See the [Required Token Permissions](#required-token-permissions) section below for details.

## Required Token Permissions

Each tool requires specific GitHub Personal Access Token permissions to function. Below are the required permissions for each tool category:

### Users
- **get_me**
- Required permissions:
- `read:user` - Read access to profile info

### Issues
- **get_issue**, **get_issue_comments**, **list_issues**
- Required permissions:
- `repo` - Full control of private repositories (for private repos)
- `public_repo` - Access public repositories (for public repos)

- **create_issue**, **add_issue_comment**, **update_issue**
- Required permissions:
- `repo` - Full control of private repositories (for private repos)
- `public_repo` - Access public repositories (for public repos)
- `write:discussion` - Write access to repository discussions (if using discussions)

### Pull Requests
- **get_pull_request**, **list_pull_requests**, **get_pull_request_files**, **get_pull_request_status**
- Required permissions:
- `repo` - Full control of private repositories (for private repos)
- `public_repo` - Access public repositories (for public repos)

- **merge_pull_request**, **update_pull_request_branch**, **create_pull_request**, **update_pull_request**
- Required permissions:
- `repo` - Full control of private repositories (for private repos)
- `public_repo` - Access public repositories (for public repos)
- `write:discussion` - Write access to repository discussions (if using discussions)

### Repositories
- **get_file_contents**, **search_repositories**, **list_commits**
- Required permissions:
- `repo` - Full control of private repositories (for private repos)
- `public_repo` - Access public repositories (for public repos)

- **create_or_update_file**, **push_files**, **create_repository**, **fork_repository**, **create_branch**
- Required permissions:
- `repo` - Full control of private repositories (for private repos)
- `public_repo` - Access public repositories (for public repos)
- `delete_repo` - Delete repositories (if needed)

### Search
- **search_code**, **search_users**
- Required permissions:
- No special permissions required for public data
- `repo` - Required for searching private repositories

### Code Scanning
- **get_code_scanning_alert**, **list_code_scanning_alerts**
- Required permissions:
- `security_events` - Read and write security events
- `repo` - Full control of private repositories (for private repos)

Note: For organization repositories, additional organization-specific permissions may be required.

## Installation

Expand Down Expand Up @@ -476,12 +535,6 @@ export GITHUB_MCP_TOOL_ADD_ISSUE_COMMENT_DESCRIPTION="an alternative description
- `branch`: Branch name (string, optional)
- `sha`: File SHA if updating (string, optional)

- **list_branches** - List branches in a GitHub repository
- `owner`: Repository owner (string, required)
- `repo`: Repository name (string, required)
- `page`: Page number (number, optional)
- `perPage`: Results per page (number, optional)

- **push_files** - Push multiple files in a single commit
- `owner`: Repository owner (string, required)
- `repo`: Repository name (string, required)
Expand Down Expand Up @@ -519,7 +572,7 @@ export GITHUB_MCP_TOOL_ADD_ISSUE_COMMENT_DESCRIPTION="an alternative description
- `branch`: New branch name (string, required)
- `sha`: SHA to create branch from (string, required)

- **list_commits** - Get a list of commits of a branch in a repository
- **list_commits** - Gets commits of a branch in a repository
- `owner`: Repository owner (string, required)
- `repo`: Repository name (string, required)
- `sha`: Branch name, tag, or commit SHA (string, optional)
Expand All @@ -534,6 +587,8 @@ export GITHUB_MCP_TOOL_ADD_ISSUE_COMMENT_DESCRIPTION="an alternative description
- `page`: Page number, for files in the commit (number, optional)
- `perPage`: Results per page, for files in the commit (number, optional)

### Search

- **search_code** - Search for code across GitHub repositories
- `query`: Search query (string, required)
- `sort`: Sort field (string, optional)
Expand Down Expand Up @@ -641,3 +696,4 @@ The exported Go API of this module should currently be considered unstable, and
## License

This project is licensed under the terms of the MIT open source license. Please refer to [MIT](./LICENSE) for the full terms.

68 changes: 68 additions & 0 deletions pkg/github/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,3 +734,71 @@ func parseISOTimestamp(timestamp string) (time.Time, error) {
// Return error with supported formats
return time.Time{}, fmt.Errorf("invalid ISO 8601 timestamp: %s (supported formats: YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DD)", timestamp)
}

// AssignCopilotToIssue creates a tool to assign Copilot to an issue in a GitHub repository.
func AssignCopilotToIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("assign_copilot_to_issue",
mcp.WithDescription(t("TOOL_ASSIGN_COPILOT_TO_ISSUE_DESCRIPTION", "Assign GitHub Copilot to an issue for automated assistance.")),
mcp.WithToolAnnotation(mcp.ToolAnnotation{
Title: t("TOOL_ASSIGN_COPILOT_TO_ISSUE_USER_TITLE", "Assign Copilot to issue"),
ReadOnlyHint: toBoolPtr(false),
}),
mcp.WithString("owner",
mcp.Required(),
mcp.Description("Repository owner"),
),
mcp.WithString("repo",
mcp.Required(),
mcp.Description("Repository name"),
),
mcp.WithNumber("issue_number",
mcp.Required(),
mcp.Description("Issue number to assign Copilot to"),
),
),
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
owner, err := requiredParam[string](request, "owner")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
repo, err := requiredParam[string](request, "repo")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
issueNumber, err := RequiredInt(request, "issue_number")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}

client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
}

assignees := []string{"github-copilot[bot]"}
issueRequest := &github.IssueRequest{
Assignees: &assignees,
}

updatedIssue, resp, err := client.Issues.Edit(ctx, owner, repo, issueNumber, issueRequest)
if err != nil {
return nil, fmt.Errorf("failed to assign copilot to issue: %w", err)
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
return mcp.NewToolResultError(fmt.Sprintf("failed to assign copilot to issue: %s", string(body))), nil
}

r, err := json.Marshal(updatedIssue)
if err != nil {
return nil, fmt.Errorf("failed to marshal response: %w", err)
}

return mcp.NewToolResultText(string(r)), nil
}
}
88 changes: 88 additions & 0 deletions pkg/github/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,3 +1130,91 @@ func Test_GetIssueComments(t *testing.T) {
})
}
}

func Test_AssignCopilotToIssue(t *testing.T) {
mockClient := github.NewClient(nil)
tool, _ := AssignCopilotToIssue(stubGetClientFn(mockClient), translations.NullTranslationHelper)

assert.Equal(t, "assign_copilot_to_issue", tool.Name)
assert.NotEmpty(t, tool.Description)
assert.Contains(t, tool.InputSchema.Properties, "owner")
assert.Contains(t, tool.InputSchema.Properties, "repo")
assert.Contains(t, tool.InputSchema.Properties, "issue_number")
assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "issue_number"})

mockIssue := &github.Issue{
Number: github.Ptr(42),
Title: github.Ptr("Test Issue"),
Assignees: []*github.User{{Login: github.Ptr("github-copilot[bot]")}},
}

tests := []struct {
name string
mockedClient *http.Client
requestArgs map[string]interface{}
expectError bool
expectedIssue *github.Issue
expectedErrMsg string
}{
{
name: "successful copilot assignment",
mockedClient: mock.NewMockedHTTPClient(
mock.WithRequestMatchHandler(
mock.PatchReposIssuesByOwnerByRepoByIssueNumber,
mockResponse(t, http.StatusOK, mockIssue),
),
),
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"issue_number": float64(42),
},
expectError: false,
expectedIssue: mockIssue,
},
{
name: "copilot assignment fails",
mockedClient: mock.NewMockedHTTPClient(
mock.WithRequestMatchHandler(
mock.PatchReposIssuesByOwnerByRepoByIssueNumber,
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnprocessableEntity)
_, _ = w.Write([]byte(`{"message": "Invalid request"}`))
}),
),
),
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"issue_number": float64(999),
},
expectError: true,
expectedErrMsg: "failed to assign copilot to issue",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
client := github.NewClient(tc.mockedClient)
_, handler := AssignCopilotToIssue(stubGetClientFn(client), translations.NullTranslationHelper)
request := createMCPRequest(tc.requestArgs)
result, err := handler(context.Background(), request)

if tc.expectError {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.expectedErrMsg)
return
}

require.NoError(t, err)
textContent := getTextResult(t, result)
var returnedIssue github.Issue
err = json.Unmarshal([]byte(textContent.Text), &returnedIssue)
require.NoError(t, err)
assert.Equal(t, *tc.expectedIssue.Number, *returnedIssue.Number)
if assert.NotEmpty(t, returnedIssue.Assignees) {
assert.Equal(t, "github-copilot[bot]", *returnedIssue.Assignees[0].Login)
}
})
}
}
1 change: 1 addition & 0 deletions pkg/github/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func InitToolsets(passedToolsets []string, readOnly bool, getClient GetClientFn,
toolsets.NewServerTool(CreateIssue(getClient, t)),
toolsets.NewServerTool(AddIssueComment(getClient, t)),
toolsets.NewServerTool(UpdateIssue(getClient, t)),
toolsets.NewServerTool(AssignCopilotToIssue(getClient, t)),
)
users := toolsets.NewToolset("users", "GitHub User related tools").
AddReadTools(
Expand Down