|
| 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 | +funcgetIssue(client*github.Client) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 16 | +returnmcp.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 | +iferr!=nil { |
| 38 | +returnnil,fmt.Errorf("failed to get issue: %w",err) |
| 39 | +} |
| 40 | +deferfunc() {_=resp.Body.Close() }() |
| 41 | + |
| 42 | +ifresp.StatusCode!=200 { |
| 43 | +body,err:=io.ReadAll(resp.Body) |
| 44 | +iferr!=nil { |
| 45 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 46 | +} |
| 47 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to get issue: %s",string(body))),nil |
| 48 | +} |
| 49 | + |
| 50 | +r,err:=json.Marshal(issue) |
| 51 | +iferr!=nil { |
| 52 | +returnnil,fmt.Errorf("failed to marshal issue: %w",err) |
| 53 | +} |
| 54 | + |
| 55 | +returnmcp.NewToolResultText(string(r)),nil |
| 56 | +} |
| 57 | +} |
| 58 | + |
| 59 | +// addIssueComment creates a tool to add a comment to an issue. |
| 60 | +funcaddIssueComment(client*github.Client) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 61 | +returnmcp.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 | +iferr!=nil { |
| 92 | +returnnil,fmt.Errorf("failed to create comment: %w",err) |
| 93 | +} |
| 94 | +deferfunc() {_=resp.Body.Close() }() |
| 95 | + |
| 96 | +ifresp.StatusCode!=201 { |
| 97 | +body,err:=io.ReadAll(resp.Body) |
| 98 | +iferr!=nil { |
| 99 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 100 | +} |
| 101 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to create comment: %s",string(body))),nil |
| 102 | +} |
| 103 | + |
| 104 | +r,err:=json.Marshal(createdComment) |
| 105 | +iferr!=nil { |
| 106 | +returnnil,fmt.Errorf("failed to marshal response: %w",err) |
| 107 | +} |
| 108 | + |
| 109 | +returnmcp.NewToolResultText(string(r)),nil |
| 110 | +} |
| 111 | +} |
| 112 | + |
| 113 | +// searchIssues creates a tool to search for issues and pull requests. |
| 114 | +funcsearchIssues(client*github.Client) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 115 | +returnmcp.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 | +ifs,ok:=request.Params.Arguments["sort"].(string);ok { |
| 138 | +sort=s |
| 139 | +} |
| 140 | +order:="" |
| 141 | +ifo,ok:=request.Params.Arguments["order"].(string);ok { |
| 142 | +order=o |
| 143 | +} |
| 144 | +perPage:=30 |
| 145 | +ifpp,ok:=request.Params.Arguments["per_page"].(float64);ok { |
| 146 | +perPage=int(pp) |
| 147 | +} |
| 148 | +page:=1 |
| 149 | +ifp,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 | +iferr!=nil { |
| 164 | +returnnil,fmt.Errorf("failed to search issues: %w",err) |
| 165 | +} |
| 166 | +deferfunc() {_=resp.Body.Close() }() |
| 167 | + |
| 168 | +ifresp.StatusCode!=200 { |
| 169 | +body,err:=io.ReadAll(resp.Body) |
| 170 | +iferr!=nil { |
| 171 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 172 | +} |
| 173 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to search issues: %s",string(body))),nil |
| 174 | +} |
| 175 | + |
| 176 | +r,err:=json.Marshal(result) |
| 177 | +iferr!=nil { |
| 178 | +returnnil,fmt.Errorf("failed to marshal response: %w",err) |
| 179 | +} |
| 180 | + |
| 181 | +returnmcp.NewToolResultText(string(r)),nil |
| 182 | +} |
| 183 | +} |