|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + |
| 9 | + "github.com/google/go-github/v69/github" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/mark3labs/mcp-go/server" |
| 12 | +) |
| 13 | + |
| 14 | +// getIssue creates a tool to get details of a specific issue in a GitHub repository. |
| 15 | +func getIssue(client *github.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 16 | + return mcp.NewTool("get_issue", |
| 17 | + mcp.WithDescription("Get details of a specific issue in a GitHub repository."), |
| 18 | + mcp.WithString("owner", |
| 19 | + mcp.Required(), |
| 20 | + mcp.Description("The owner of the repository."), |
| 21 | + ), |
| 22 | + mcp.WithString("repo", |
| 23 | + mcp.Required(), |
| 24 | + mcp.Description("The name of the repository."), |
| 25 | + ), |
| 26 | + mcp.WithNumber("issue_number", |
| 27 | + mcp.Required(), |
| 28 | + mcp.Description("The number of the issue."), |
| 29 | + ), |
| 30 | + ), |
| 31 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 32 | + owner := request.Params.Arguments["owner"].(string) |
| 33 | + repo := request.Params.Arguments["repo"].(string) |
| 34 | + issueNumber := int(request.Params.Arguments["issue_number"].(float64)) |
| 35 | + |
| 36 | + issue, resp, err := client.Issues.Get(ctx, owner, repo, issueNumber) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("failed to get issue: %w", err) |
| 39 | + } |
| 40 | + defer func() { _ = resp.Body.Close() }() |
| 41 | + |
| 42 | + if resp.StatusCode != 200 { |
| 43 | + body, err := io.ReadAll(resp.Body) |
| 44 | + if err != nil { |
| 45 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 46 | + } |
| 47 | + return mcp.NewToolResultError(fmt.Sprintf("failed to get issue: %s", string(body))), nil |
| 48 | + } |
| 49 | + |
| 50 | + r, err := json.Marshal(issue) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("failed to marshal issue: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + return mcp.NewToolResultText(string(r)), nil |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// addIssueComment creates a tool to add a comment to an issue. |
| 60 | +func addIssueComment(client *github.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 61 | + return mcp.NewTool("add_issue_comment", |
| 62 | + mcp.WithDescription("Add a comment to an issue"), |
| 63 | + mcp.WithString("owner", |
| 64 | + mcp.Required(), |
| 65 | + mcp.Description("Repository owner"), |
| 66 | + ), |
| 67 | + mcp.WithString("repo", |
| 68 | + mcp.Required(), |
| 69 | + mcp.Description("Repository name"), |
| 70 | + ), |
| 71 | + mcp.WithNumber("issue_number", |
| 72 | + mcp.Required(), |
| 73 | + mcp.Description("Issue number to comment on"), |
| 74 | + ), |
| 75 | + mcp.WithString("body", |
| 76 | + mcp.Required(), |
| 77 | + mcp.Description("Comment text"), |
| 78 | + ), |
| 79 | + ), |
| 80 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 81 | + owner := request.Params.Arguments["owner"].(string) |
| 82 | + repo := request.Params.Arguments["repo"].(string) |
| 83 | + issueNumber := int(request.Params.Arguments["issue_number"].(float64)) |
| 84 | + body := request.Params.Arguments["body"].(string) |
| 85 | + |
| 86 | + comment := &github.IssueComment{ |
| 87 | + Body: github.Ptr(body), |
| 88 | + } |
| 89 | + |
| 90 | + createdComment, resp, err := client.Issues.CreateComment(ctx, owner, repo, issueNumber, comment) |
| 91 | + if err != nil { |
| 92 | + return nil, fmt.Errorf("failed to create comment: %w", err) |
| 93 | + } |
| 94 | + defer func() { _ = resp.Body.Close() }() |
| 95 | + |
| 96 | + if resp.StatusCode != 201 { |
| 97 | + body, err := io.ReadAll(resp.Body) |
| 98 | + if err != nil { |
| 99 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 100 | + } |
| 101 | + return mcp.NewToolResultError(fmt.Sprintf("failed to create comment: %s", string(body))), nil |
| 102 | + } |
| 103 | + |
| 104 | + r, err := json.Marshal(createdComment) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("failed to marshal response: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + return mcp.NewToolResultText(string(r)), nil |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// searchIssues creates a tool to search for issues and pull requests. |
| 114 | +func searchIssues(client *github.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 115 | + return mcp.NewTool("search_issues", |
| 116 | + mcp.WithDescription("Search for issues and pull requests"), |
| 117 | + mcp.WithString("q", |
| 118 | + mcp.Required(), |
| 119 | + mcp.Description("Search query using GitHub issues search syntax"), |
| 120 | + ), |
| 121 | + mcp.WithString("sort", |
| 122 | + mcp.Description("Sort field (comments, reactions, created, etc.)"), |
| 123 | + ), |
| 124 | + mcp.WithString("order", |
| 125 | + mcp.Description("Sort order ('asc' or 'desc')"), |
| 126 | + ), |
| 127 | + mcp.WithNumber("per_page", |
| 128 | + mcp.Description("Results per page (max 100)"), |
| 129 | + ), |
| 130 | + mcp.WithNumber("page", |
| 131 | + mcp.Description("Page number"), |
| 132 | + ), |
| 133 | + ), |
| 134 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 135 | + query := request.Params.Arguments["q"].(string) |
| 136 | + sort := "" |
| 137 | + if s, ok := request.Params.Arguments["sort"].(string); ok { |
| 138 | + sort = s |
| 139 | + } |
| 140 | + order := "" |
| 141 | + if o, ok := request.Params.Arguments["order"].(string); ok { |
| 142 | + order = o |
| 143 | + } |
| 144 | + perPage := 30 |
| 145 | + if pp, ok := request.Params.Arguments["per_page"].(float64); ok { |
| 146 | + perPage = int(pp) |
| 147 | + } |
| 148 | + page := 1 |
| 149 | + if p, ok := request.Params.Arguments["page"].(float64); ok { |
| 150 | + page = int(p) |
| 151 | + } |
| 152 | + |
| 153 | + opts := &github.SearchOptions{ |
| 154 | + Sort: sort, |
| 155 | + Order: order, |
| 156 | + ListOptions: github.ListOptions{ |
| 157 | + PerPage: perPage, |
| 158 | + Page: page, |
| 159 | + }, |
| 160 | + } |
| 161 | + |
| 162 | + result, resp, err := client.Search.Issues(ctx, query, opts) |
| 163 | + if err != nil { |
| 164 | + return nil, fmt.Errorf("failed to search issues: %w", err) |
| 165 | + } |
| 166 | + defer func() { _ = resp.Body.Close() }() |
| 167 | + |
| 168 | + if resp.StatusCode != 200 { |
| 169 | + body, err := io.ReadAll(resp.Body) |
| 170 | + if err != nil { |
| 171 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 172 | + } |
| 173 | + return mcp.NewToolResultError(fmt.Sprintf("failed to search issues: %s", string(body))), nil |
| 174 | + } |
| 175 | + |
| 176 | + r, err := json.Marshal(result) |
| 177 | + if err != nil { |
| 178 | + return nil, fmt.Errorf("failed to marshal response: %w", err) |
| 179 | + } |
| 180 | + |
| 181 | + return mcp.NewToolResultText(string(r)), nil |
| 182 | + } |
| 183 | +} |
0 commit comments