|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + ghErrors "github.com/github/github-mcp-server/pkg/errors" |
| 11 | + "github.com/github/github-mcp-server/pkg/translations" |
| 12 | + "github.com/google/go-github/v74/github" |
| 13 | + "github.com/mark3labs/mcp-go/mcp" |
| 14 | + "github.com/mark3labs/mcp-go/server" |
| 15 | +) |
| 16 | + |
| 17 | +// ReleaseRead creates a tool for reading GitHub releases in a repository. |
| 18 | +// Supports multiple methods: list, get_latest, and get_by_tag. |
| 19 | +func ReleaseRead(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 20 | + return mcp.NewTool("release_read", |
| 21 | + mcp.WithDescription(t("TOOL_RELEASE_READ_DESCRIPTION", `Read operations for GitHub releases in a repository. |
| 22 | +
|
| 23 | +Available methods: |
| 24 | +- list: List all releases in a repository. |
| 25 | +- get_latest: Get the latest release in a repository. |
| 26 | +- get_by_tag: Get a specific release by its tag name. |
| 27 | +`)), |
| 28 | + mcp.WithToolAnnotation(mcp.ToolAnnotation{ |
| 29 | + Title: t("TOOL_RELEASE_READ_USER_TITLE", "Read operations for releases"), |
| 30 | + ReadOnlyHint: ToBoolPtr(true), |
| 31 | + }), |
| 32 | + mcp.WithString("method", |
| 33 | + mcp.Required(), |
| 34 | + mcp.Enum("list", "get_latest", "get_by_tag"), |
| 35 | + mcp.Description("The read operation to perform on releases."), |
| 36 | + ), |
| 37 | + mcp.WithString("owner", |
| 38 | + mcp.Required(), |
| 39 | + mcp.Description("Repository owner"), |
| 40 | + ), |
| 41 | + mcp.WithString("repo", |
| 42 | + mcp.Required(), |
| 43 | + mcp.Description("Repository name"), |
| 44 | + ), |
| 45 | + mcp.WithString("tag", |
| 46 | + mcp.Description("Tag name (required for get_by_tag method)"), |
| 47 | + ), |
| 48 | + WithPagination(), |
| 49 | + ), |
| 50 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 51 | + method, err := RequiredParam[string](request, "method") |
| 52 | + if err != nil { |
| 53 | + return mcp.NewToolResultError(err.Error()), nil |
| 54 | + } |
| 55 | + |
| 56 | + owner, err := RequiredParam[string](request, "owner") |
| 57 | + if err != nil { |
| 58 | + return mcp.NewToolResultError(err.Error()), nil |
| 59 | + } |
| 60 | + repo, err := RequiredParam[string](request, "repo") |
| 61 | + if err != nil { |
| 62 | + return mcp.NewToolResultError(err.Error()), nil |
| 63 | + } |
| 64 | + |
| 65 | + client, err := getClient(ctx) |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("failed to get GitHub client: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + switch method { |
| 71 | + case "list": |
| 72 | + return ListReleasesMethod(ctx, client, owner, repo, request) |
| 73 | + case "get_latest": |
| 74 | + return GetLatestReleaseMethod(ctx, client, owner, repo) |
| 75 | + case "get_by_tag": |
| 76 | + return GetReleaseByTagMethod(ctx, client, owner, repo, request) |
| 77 | + default: |
| 78 | + return mcp.NewToolResultError(fmt.Sprintf("unknown method: %s", method)), nil |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// ListReleasesMethod handles the "list" method for ReleaseRead |
| 84 | +func ListReleasesMethod(ctx context.Context, client *github.Client, owner, repo string, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 85 | + pagination, err := OptionalPaginationParams(request) |
| 86 | + if err != nil { |
| 87 | + return mcp.NewToolResultError(err.Error()), nil |
| 88 | + } |
| 89 | + |
| 90 | + opts := &github.ListOptions{ |
| 91 | + Page: pagination.Page, |
| 92 | + PerPage: pagination.PerPage, |
| 93 | + } |
| 94 | + |
| 95 | + releases, resp, err := client.Repositories.ListReleases(ctx, owner, repo, opts) |
| 96 | + if err != nil { |
| 97 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, |
| 98 | + "failed to list releases", |
| 99 | + resp, |
| 100 | + err, |
| 101 | + ), nil |
| 102 | + } |
| 103 | + defer func() { _ = resp.Body.Close() }() |
| 104 | + |
| 105 | + if resp.StatusCode != http.StatusOK { |
| 106 | + body, err := io.ReadAll(resp.Body) |
| 107 | + if err != nil { |
| 108 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 109 | + } |
| 110 | + return mcp.NewToolResultError(fmt.Sprintf("failed to list releases: %s", string(body))), nil |
| 111 | + } |
| 112 | + |
| 113 | + r, err := json.Marshal(releases) |
| 114 | + if err != nil { |
| 115 | + return nil, fmt.Errorf("failed to marshal response: %w", err) |
| 116 | + } |
| 117 | + |
| 118 | + return mcp.NewToolResultText(string(r)), nil |
| 119 | +} |
| 120 | + |
| 121 | +// GetLatestReleaseMethod handles the "get_latest" method for ReleaseRead |
| 122 | +func GetLatestReleaseMethod(ctx context.Context, client *github.Client, owner, repo string) (*mcp.CallToolResult, error) { |
| 123 | + release, resp, err := client.Repositories.GetLatestRelease(ctx, owner, repo) |
| 124 | + if err != nil { |
| 125 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, |
| 126 | + "failed to get latest release", |
| 127 | + resp, |
| 128 | + err, |
| 129 | + ), nil |
| 130 | + } |
| 131 | + defer func() { _ = resp.Body.Close() }() |
| 132 | + |
| 133 | + if resp.StatusCode != http.StatusOK { |
| 134 | + body, err := io.ReadAll(resp.Body) |
| 135 | + if err != nil { |
| 136 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 137 | + } |
| 138 | + return mcp.NewToolResultError(fmt.Sprintf("failed to get latest release: %s", string(body))), nil |
| 139 | + } |
| 140 | + |
| 141 | + r, err := json.Marshal(release) |
| 142 | + if err != nil { |
| 143 | + return nil, fmt.Errorf("failed to marshal response: %w", err) |
| 144 | + } |
| 145 | + |
| 146 | + return mcp.NewToolResultText(string(r)), nil |
| 147 | +} |
| 148 | + |
| 149 | +// GetReleaseByTagMethod handles the "get_by_tag" method for ReleaseRead |
| 150 | +func GetReleaseByTagMethod(ctx context.Context, client *github.Client, owner, repo string, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 151 | + tag, err := RequiredParam[string](request, "tag") |
| 152 | + if err != nil { |
| 153 | + return mcp.NewToolResultError(err.Error()), nil |
| 154 | + } |
| 155 | + |
| 156 | + release, resp, err := client.Repositories.GetReleaseByTag(ctx, owner, repo, tag) |
| 157 | + if err != nil { |
| 158 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, |
| 159 | + fmt.Sprintf("failed to get release by tag: %s", tag), |
| 160 | + resp, |
| 161 | + err, |
| 162 | + ), nil |
| 163 | + } |
| 164 | + defer func() { _ = resp.Body.Close() }() |
| 165 | + |
| 166 | + if resp.StatusCode != http.StatusOK { |
| 167 | + body, err := io.ReadAll(resp.Body) |
| 168 | + if err != nil { |
| 169 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 170 | + } |
| 171 | + return mcp.NewToolResultError(fmt.Sprintf("failed to get release by tag: %s", string(body))), nil |
| 172 | + } |
| 173 | + |
| 174 | + r, err := json.Marshal(release) |
| 175 | + if err != nil { |
| 176 | + return nil, fmt.Errorf("failed to marshal response: %w", err) |
| 177 | + } |
| 178 | + |
| 179 | + return mcp.NewToolResultText(string(r)), nil |
| 180 | +} |
0 commit comments