Skip to content

Document add_sub_issue tool functionality #235

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
87 changes: 68 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,67 @@ automation and interaction capabilities for developers and tools.
## Prerequisites

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.
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)).
2. [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 @@ -190,6 +246,13 @@ export GITHUB_MCP_TOOL_ADD_ISSUE_COMMENT_DESCRIPTION="an alternative description
- `issue_number`: Issue number (number, required)
- `body`: Comment text (string, required)

- **add_sub_issue** - Add a sub-issue to an existing issue

- `owner`: Repository owner (string, required)
- `repo`: Repository name (string, required)
- `parent_issue_number`: Parent issue number (number, required)
- `child_issue_number`: Child issue number to add as sub-issue (number, required)

- **list_issues** - List and filter repository issues

- `owner`: Repository owner (string, required)
Expand Down Expand Up @@ -340,13 +403,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)
Expand Down Expand Up @@ -390,21 +446,14 @@ 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)
- `path`: Only commits containing this file path (string, optional)
- `page`: Page number (number, optional)
- `perPage`: Results per page (number, optional)

- **get_commit** - Get details for a commit from a repository
- `owner`: Repository owner (string, required)
- `repo`: Repository name (string, required)
- `sha`: Commit SHA, branch name, or tag name (string, required)
- `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
Expand Down Expand Up @@ -497,4 +546,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.
This project is licensed under the terms of the MIT open source license. Please refer to [MIT](./LICENSE) for the full terms.
192 changes: 192 additions & 0 deletions pkg/github/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ import (
"github.com/github/github-mcp-server/pkg/translations"
"github.com/google/go-github/v69/github"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/mock"
"github.com/mark3labs/mcp-go/server"
)

const (
PostReposSubIssuesByOwnerByRepoByParentIssueNumberByChildIssueNumber mock.EndpointPattern = "POST /repos/{owner}/{repo}/issues/{parent_issue_number}/sub-issues/{child_issue_number}"
GetReposSubIssuesByOwnerByRepoByIssueNumber mock.EndpointPattern = "GET /repos/{owner}/{repo}/issues/{issue_number}/sub-issues"
)

// GetIssue creates a tool to get details of a specific issue in a GitHub repository.
func GetIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("get_issue",
Expand Down Expand Up @@ -49,6 +55,8 @@ func GetIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (tool
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
}

// Get issue details
issue, resp, err := client.Issues.Get(ctx, owner, repo, issueNumber)
if err != nil {
return nil, fmt.Errorf("failed to get issue: %w", err)
Expand All @@ -63,6 +71,37 @@ func GetIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (tool
return mcp.NewToolResultError(fmt.Sprintf("failed to get issue: %s", string(body))), nil
}

// Get sub-issues
url := fmt.Sprintf("repos/%v/%v/issues/%d/sub-issues", owner, repo, issueNumber)
req, err := client.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

var subIssues []*github.Issue
resp, err = client.Do(ctx, req, &subIssues)
if err == nil && resp.StatusCode == http.StatusOK {
// Only include sub-issues if the request was successful
// Create a custom response struct that includes sub-issues
type IssueWithSubIssues struct {
*github.Issue
SubIssues []*github.Issue `json:"sub_issues,omitempty"`
}

issueWithSubs := &IssueWithSubIssues{
Issue: issue,
SubIssues: subIssues,
}

r, err := json.Marshal(issueWithSubs)
if err != nil {
return nil, fmt.Errorf("failed to marshal issue with sub-issues: %w", err)
}

return mcp.NewToolResultText(string(r)), nil
}

// If getting sub-issues failed, just return the main issue
r, err := json.Marshal(issue)
if err != nil {
return nil, fmt.Errorf("failed to marshal issue: %w", err)
Expand Down Expand Up @@ -683,6 +722,159 @@ func GetIssueComments(getClient GetClientFn, t translations.TranslationHelperFun
}
}

// AddSubIssue creates a tool to add a sub-issue to an existing issue.
func AddSubIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("add_sub_issue",
mcp.WithDescription(t("TOOL_ADD_SUB_ISSUE_DESCRIPTION", "Add a sub-issue to an existing issue")),
mcp.WithString("owner",
mcp.Required(),
mcp.Description("Repository owner"),
),
mcp.WithString("repo",
mcp.Required(),
mcp.Description("Repository name"),
),
mcp.WithNumber("parent_issue_number",
mcp.Required(),
mcp.Description("Parent issue number"),
),
mcp.WithNumber("child_issue_number",
mcp.Required(),
mcp.Description("Child issue number to add as sub-issue"),
),
),
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
}
parentIssueNumber, err := RequiredInt(request, "parent_issue_number")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
childIssueNumber, err := RequiredInt(request, "child_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)
}

// First verify both issues exist
_, resp, err := client.Issues.Get(ctx, owner, repo, parentIssueNumber)
if err != nil {
return nil, fmt.Errorf("failed to get parent issue: %w", err)
}
if resp.StatusCode != http.StatusOK {
return mcp.NewToolResultError("parent issue not found"), nil
}

_, resp, err = client.Issues.Get(ctx, owner, repo, childIssueNumber)
if err != nil {
return nil, fmt.Errorf("failed to get child issue: %w", err)
}
if resp.StatusCode != http.StatusOK {
return mcp.NewToolResultError("child issue not found"), nil
}

// Add sub-issue relationship
url := fmt.Sprintf("repos/%v/%v/issues/%d/sub-issues/%d", owner, repo, parentIssueNumber, childIssueNumber)
req, err := client.NewRequest("POST", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

resp, err = client.Do(ctx, req, nil)
if err != nil {
return nil, fmt.Errorf("failed to add sub-issue: %w", err)
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusCreated {
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 add sub-issue: %s", string(body))), nil
}

return mcp.NewToolResultText(fmt.Sprintf("Successfully added issue #%d as a sub-issue of #%d", childIssueNumber, parentIssueNumber)), nil
}
}

// GetSubIssues creates a tool to get sub-issues of a specific issue.
func GetSubIssues(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("get_sub_issues",
mcp.WithDescription(t("TOOL_GET_SUB_ISSUES_DESCRIPTION", "Get sub-issues of a specific issue")),
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("Parent issue number"),
),
),
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)
}

// Get sub-issues
url := fmt.Sprintf("repos/%v/%v/issues/%d/sub-issues", owner, repo, issueNumber)
req, err := client.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

var subIssues []*github.Issue
resp, err := client.Do(ctx, req, &subIssues)
if err != nil {
return nil, fmt.Errorf("failed to get sub-issues: %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 get sub-issues: %s", string(body))), nil
}

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

return mcp.NewToolResultText(string(r)), nil
}
}

// parseISOTimestamp parses an ISO 8601 timestamp string into a time.Time object.
// Returns the parsed time or an error if parsing fails.
// Example formats supported: "2023-01-15T14:30:00Z", "2023-01-15"
Expand Down
Loading