diff --git a/README.md b/README.md index c7243033b..bcd9f85c8 100644 --- a/README.md +++ b/README.md @@ -726,12 +726,12 @@ The following sets of tools are available: - **issue_read** - Get issue details - `issue_number`: The number of the issue (number, required) - `method`: The read operation to perform on a single issue. -Options are: -1. get - Get details of a specific issue. -2. get_comments - Get issue comments. -3. get_sub_issues - Get sub-issues of the issue. -4. get_labels - Get labels assigned to the issue. - (string, required) + Options are: + 1. get - Get details of a specific issue. + 2. get_comments - Get issue comments. + 3. get_sub_issues - Get sub-issues of the issue. + 4. get_labels - Get labels assigned to the issue. + (string, required) - `owner`: The owner of the repository (string, required) - `page`: Page number for pagination (min 1) (number, optional) - `perPage`: Results per page for pagination (min 1, max 100) (number, optional) @@ -744,10 +744,10 @@ Options are: - `issue_number`: Issue number to update (number, optional) - `labels`: Labels to apply to this issue (string[], optional) - `method`: Write operation to perform on a single issue. -Options are: -- 'create' - creates a new issue. -- 'update' - updates an existing issue. - (string, required) + Options are: + - 'create' - creates a new issue. + - 'update' - updates an existing issue. + (string, required) - `milestone`: Milestone number (number, optional) - `owner`: Repository owner (string, required) - `repo`: Repository name (string, required) @@ -784,11 +784,11 @@ Options are: - `before_id`: The ID of the sub-issue to be prioritized before (either after_id OR before_id should be specified) (number, optional) - `issue_number`: The number of the parent issue (number, required) - `method`: The action to perform on a single sub-issue -Options are: -- 'add' - add a sub-issue to a parent issue in a GitHub repository. -- 'remove' - remove a sub-issue from a parent issue in a GitHub repository. -- 'reprioritize' - change the order of sub-issues within a parent issue in a GitHub repository. Use either 'after_id' or 'before_id' to specify the new position. - (string, required) + Options are: + - 'add' - add a sub-issue to a parent issue in a GitHub repository. + - 'remove' - remove a sub-issue from a parent issue in a GitHub repository. + - 'reprioritize' - change the order of sub-issues within a parent issue in a GitHub repository. Use either 'after_id' or 'before_id' to specify the new position. + (string, required) - `owner`: Repository owner (string, required) - `replace_parent`: When true, replaces the sub-issue's current parent issue. Use with 'add' method only. (boolean, optional) - `repo`: Repository name (string, required) @@ -986,15 +986,15 @@ Options are: - **pull_request_read** - Get details for a single pull request - `method`: Action to specify what pull request data needs to be retrieved from GitHub. -Possible options: - 1. get - Get details of a specific pull request. - 2. get_diff - Get the diff of a pull request. - 3. get_status - Get status of a head commit in a pull request. This reflects status of builds and checks. - 4. get_files - Get the list of files changed in a pull request. Use with pagination parameters to control the number of results returned. - 5. get_review_comments - Get the review comments on a pull request. They are comments made on a portion of the unified diff during a pull request review. Use with pagination parameters to control the number of results returned. - 6. get_reviews - Get the reviews on a pull request. When asked for review comments, use get_review_comments method. - 7. get_comments - Get comments on a pull request. Use this if user doesn't specifically want review comments. Use with pagination parameters to control the number of results returned. - (string, required) + Possible options: + 1. get - Get details of a specific pull request. + 2. get_diff - Get the diff of a pull request. + 3. get_status - Get status of a head commit in a pull request. This reflects status of builds and checks. + 4. get_files - Get the list of files changed in a pull request. Use with pagination parameters to control the number of results returned. + 5. get_review_comments - Get the review comments on a pull request. They are comments made on a portion of the unified diff during a pull request review. Use with pagination parameters to control the number of results returned. + 6. get_reviews - Get the reviews on a pull request. When asked for review comments, use get_review_comments method. + 7. get_comments - Get comments on a pull request. Use this if user doesn't specifically want review comments. Use with pagination parameters to control the number of results returned. + (string, required) - `owner`: Repository owner (string, required) - `page`: Page number for pagination (min 1) (number, optional) - `perPage`: Results per page for pagination (min 1, max 100) (number, optional) diff --git a/cmd/github-mcp-server/generate_docs.go b/cmd/github-mcp-server/generate_docs.go index 87c9c0514..61459d7f0 100644 --- a/cmd/github-mcp-server/generate_docs.go +++ b/cmd/github-mcp-server/generate_docs.go @@ -269,6 +269,9 @@ func generateToolDoc(tool mcp.Tool) string { description = prop.Description + // Indent any continuation lines in the description to maintain markdown formatting + description = indentMultilineDescription(description, " ") + paramLine := fmt.Sprintf(" - `%s`: %s (%s, %s)", propName, description, typeStr, requiredStr) lines = append(lines, paramLine) } @@ -288,6 +291,19 @@ func contains(slice []string, item string) bool { return false } +// indentMultilineDescription adds the specified indent to all lines after the first line. +// This ensures that multi-line descriptions maintain proper markdown list formatting. +func indentMultilineDescription(description, indent string) string { + lines := strings.Split(description, "\n") + if len(lines) <= 1 { + return description + } + for i := 1; i < len(lines); i++ { + lines[i] = indent + lines[i] + } + return strings.Join(lines, "\n") +} + func replaceSection(content, startMarker, endMarker, newContent string) string { startPattern := fmt.Sprintf(``, regexp.QuoteMeta(startMarker)) endPattern := fmt.Sprintf(``, regexp.QuoteMeta(endMarker)) diff --git a/docs/remote-server.md b/docs/remote-server.md index 1030911ef..e06d41a75 100644 --- a/docs/remote-server.md +++ b/docs/remote-server.md @@ -19,7 +19,7 @@ Below is a table of available toolsets for the remote GitHub MCP Server. Each to | Name | Description | API URL | 1-Click Install (VS Code) | Read-only Link | 1-Click Read-only Install (VS Code) | |----------------|--------------------------------------------------|-------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| Default | ["Default" toolset](../README.md#default-toolset) | https://api.githubcopilot.com/mcp/ | [Install](https://insiders.vscode.dev/redirect/mcp/install?name=github&config=%7B%22type%22%3A%20%22http%22%2C%22url%22%3A%20%22https%3A%2F%2Fapi.githubcopilot.com%2Fmcp%2F%22%7D) | [read-only](https://api.githubcopilot.com/mcp/readonly) | [Install read-only](https://insiders.vscode.dev/redirect/mcp/install?name=github&config=%7B%22type%22%3A%20%22http%22%2C%22url%22%3A%20%22https%3A%2F%2Fapi.githubcopilot.com%2Fmcp%2Freadonly%22%7D) | +| all | All available GitHub MCP tools | https://api.githubcopilot.com/mcp/ | [Install](https://insiders.vscode.dev/redirect/mcp/install?name=github&config=%7B%22type%22%3A%20%22http%22%2C%22url%22%3A%20%22https%3A%2F%2Fapi.githubcopilot.com%2Fmcp%2F%22%7D) | [read-only](https://api.githubcopilot.com/mcp/readonly) | [Install read-only](https://insiders.vscode.dev/redirect/mcp/install?name=github&config=%7B%22type%22%3A%20%22http%22%2C%22url%22%3A%20%22https%3A%2F%2Fapi.githubcopilot.com%2Fmcp%2Freadonly%22%7D) | | Actions | GitHub Actions workflows and CI/CD operations | https://api.githubcopilot.com/mcp/x/actions | [Install](https://insiders.vscode.dev/redirect/mcp/install?name=gh-actions&config=%7B%22type%22%3A%20%22http%22%2C%22url%22%3A%20%22https%3A%2F%2Fapi.githubcopilot.com%2Fmcp%2Fx%2Factions%22%7D) | [read-only](https://api.githubcopilot.com/mcp/x/actions/readonly) | [Install read-only](https://insiders.vscode.dev/redirect/mcp/install?name=gh-actions&config=%7B%22type%22%3A%20%22http%22%2C%22url%22%3A%20%22https%3A%2F%2Fapi.githubcopilot.com%2Fmcp%2Fx%2Factions%2Freadonly%22%7D) | | Code Security | Code security related tools, such as GitHub Code Scanning | https://api.githubcopilot.com/mcp/x/code_security | [Install](https://insiders.vscode.dev/redirect/mcp/install?name=gh-code_security&config=%7B%22type%22%3A%20%22http%22%2C%22url%22%3A%20%22https%3A%2F%2Fapi.githubcopilot.com%2Fmcp%2Fx%2Fcode_security%22%7D) | [read-only](https://api.githubcopilot.com/mcp/x/code_security/readonly) | [Install read-only](https://insiders.vscode.dev/redirect/mcp/install?name=gh-code_security&config=%7B%22type%22%3A%20%22http%22%2C%22url%22%3A%20%22https%3A%2F%2Fapi.githubcopilot.com%2Fmcp%2Fx%2Fcode_security%2Freadonly%22%7D) | | Dependabot | Dependabot tools | https://api.githubcopilot.com/mcp/x/dependabot | [Install](https://insiders.vscode.dev/redirect/mcp/install?name=gh-dependabot&config=%7B%22type%22%3A%20%22http%22%2C%22url%22%3A%20%22https%3A%2F%2Fapi.githubcopilot.com%2Fmcp%2Fx%2Fdependabot%22%7D) | [read-only](https://api.githubcopilot.com/mcp/x/dependabot/readonly) | [Install read-only](https://insiders.vscode.dev/redirect/mcp/install?name=gh-dependabot&config=%7B%22type%22%3A%20%22http%22%2C%22url%22%3A%20%22https%3A%2F%2Fapi.githubcopilot.com%2Fmcp%2Fx%2Fdependabot%2Freadonly%22%7D) | diff --git a/internal/ghmcp/server.go b/internal/ghmcp/server.go index f4e67f264..41f9016a2 100644 --- a/internal/ghmcp/server.go +++ b/internal/ghmcp/server.go @@ -100,9 +100,10 @@ func NewMCPServer(cfg MCPServerConfig) (*mcp.Server, error) { enabledToolsets := cfg.EnabledToolsets - // If dynamic toolsets are enabled, remove "all" from the enabled toolsets + // If dynamic toolsets are enabled, remove "all" and "default" from the enabled toolsets if cfg.DynamicToolsets { enabledToolsets = github.RemoveToolset(enabledToolsets, github.ToolsetMetadataAll.ID) + enabledToolsets = github.RemoveToolset(enabledToolsets, github.ToolsetMetadataDefault.ID) } // Clean up the passed toolsets @@ -176,8 +177,8 @@ func NewMCPServer(cfg MCPServerConfig) (*mcp.Server, error) { // Register specific tools if configured if len(cfg.EnabledTools) > 0 { - // Clean and validate tool names enabledTools := github.CleanTools(cfg.EnabledTools) + enabledTools, _ = tsg.ResolveToolAliases(enabledTools) // Register the specified tools (additive to any toolsets already enabled) err = tsg.RegisterSpecificTools(ghServer, enabledTools, cfg.ReadOnly) diff --git a/pkg/github/deprecated_tool_aliases.go b/pkg/github/deprecated_tool_aliases.go new file mode 100644 index 000000000..4abdca14d --- /dev/null +++ b/pkg/github/deprecated_tool_aliases.go @@ -0,0 +1,14 @@ +// deprecated_tool_aliases.go +package github + +// DeprecatedToolAliases maps old tool names to their new canonical names. +// When tools are renamed, add an entry here to maintain backward compatibility. +// Users referencing the old name will receive the new tool with a deprecation warning. +// +// Example: +// +// "get_issue": "issue_read", +// "create_pr": "pull_request_create", +var DeprecatedToolAliases = map[string]string{ + // Add entries as tools are renamed +} diff --git a/pkg/github/issues.go b/pkg/github/issues.go index 46111a4d6..ec83e4efa 100644 --- a/pkg/github/issues.go +++ b/pkg/github/issues.go @@ -1381,11 +1381,14 @@ func ListIssues(getGQLClient GetGQLClientFn, t translations.TranslationHelperFun return utils.NewToolResultError(err.Error()), nil, nil } - // If the state has a value, cast into an array of strings + // Normalize and filter by state + state = strings.ToUpper(state) var states []githubv4.IssueState - if state != "" { - states = append(states, githubv4.IssueState(state)) - } else { + + switch state { + case "OPEN", "CLOSED": + states = []githubv4.IssueState{githubv4.IssueState(state)} + default: states = []githubv4.IssueState{githubv4.IssueStateOpen, githubv4.IssueStateClosed} } @@ -1405,13 +1408,21 @@ func ListIssues(getGQLClient GetGQLClientFn, t translations.TranslationHelperFun return utils.NewToolResultError(err.Error()), nil, nil } - // These variables are required for the GraphQL query to be set by default - // If orderBy is empty, default to CREATED_AT - if orderBy == "" { + // Normalize and validate orderBy + orderBy = strings.ToUpper(orderBy) + switch orderBy { + case "CREATED_AT", "UPDATED_AT", "COMMENTS": + // Valid, keep as is + default: orderBy = "CREATED_AT" } - // If direction is empty, default to DESC - if direction == "" { + + // Normalize and validate direction + direction = strings.ToUpper(direction) + switch direction { + case "ASC", "DESC": + // Valid, keep as is + default: direction = "DESC" } diff --git a/pkg/github/issues_test.go b/pkg/github/issues_test.go index 48901ccdc..c4454624b 100644 --- a/pkg/github/issues_test.go +++ b/pkg/github/issues_test.go @@ -1183,6 +1183,16 @@ func Test_ListIssues(t *testing.T) { expectError: false, expectedCount: 2, }, + { + name: "filter by open state - lc", + reqParams: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "state": "open", + }, + expectError: false, + expectedCount: 2, + }, { name: "filter by closed state", reqParams: map[string]interface{}{ @@ -1229,6 +1239,9 @@ func Test_ListIssues(t *testing.T) { case "filter by open state": matcher := githubv4mock.NewQueryMatcher(qBasicNoLabels, varsOpenOnly, mockResponseOpenOnly) httpClient = githubv4mock.NewMockedHTTPClient(matcher) + case "filter by open state - lc": + matcher := githubv4mock.NewQueryMatcher(qBasicNoLabels, varsOpenOnly, mockResponseOpenOnly) + httpClient = githubv4mock.NewMockedHTTPClient(matcher) case "filter by closed state": matcher := githubv4mock.NewQueryMatcher(qBasicNoLabels, varsClosedOnly, mockResponseClosedOnly) httpClient = githubv4mock.NewMockedHTTPClient(matcher) diff --git a/pkg/github/repositories.go b/pkg/github/repositories.go index dbf24e8e3..ff81484f2 100644 --- a/pkg/github/repositories.go +++ b/pkg/github/repositories.go @@ -402,6 +402,8 @@ func CreateOrUpdateFile(getClient GetClientFn, t translations.TranslationHelperF if err != nil { return nil, nil, fmt.Errorf("failed to get GitHub client: %w", err) } + + path = strings.TrimPrefix(path, "/") fileContent, resp, err := client.Repositories.CreateFile(ctx, owner, repo, path, opts) if err != nil { return ghErrors.NewGitHubAPIErrorResponse(ctx, diff --git a/pkg/github/tools.go b/pkg/github/tools.go index d37af98b8..f21a9ae5b 100644 --- a/pkg/github/tools.go +++ b/pkg/github/tools.go @@ -378,6 +378,8 @@ func DefaultToolsetGroup(readOnly bool, getClient GetClientFn, getGQLClient GetG tsg.AddToolset(stargazers) tsg.AddToolset(labels) + tsg.AddDeprecatedToolAliases(DeprecatedToolAliases) + return tsg } diff --git a/pkg/toolsets/toolsets.go b/pkg/toolsets/toolsets.go index d4964ee5f..d96b5fb50 100644 --- a/pkg/toolsets/toolsets.go +++ b/pkg/toolsets/toolsets.go @@ -192,16 +192,24 @@ func (t *Toolset) AddReadTools(tools ...ServerTool) *Toolset { } type ToolsetGroup struct { - Toolsets map[string]*Toolset - everythingOn bool - readOnly bool + Toolsets map[string]*Toolset + deprecatedAliases map[string]string + everythingOn bool + readOnly bool } func NewToolsetGroup(readOnly bool) *ToolsetGroup { return &ToolsetGroup{ - Toolsets: make(map[string]*Toolset), - everythingOn: false, - readOnly: readOnly, + Toolsets: make(map[string]*Toolset), + deprecatedAliases: make(map[string]string), + everythingOn: false, + readOnly: readOnly, + } +} + +func (tg *ToolsetGroup) AddDeprecatedToolAliases(aliases map[string]string) { + for oldName, newName := range aliases { + tg.deprecatedAliases[oldName] = newName } } @@ -307,6 +315,26 @@ func NewToolDoesNotExistError(name string) *ToolDoesNotExistError { return &ToolDoesNotExistError{Name: name} } +// ResolveToolAliases resolves deprecated tool aliases to their canonical names. +// It logs a warning to stderr for each deprecated alias that is resolved. +// Returns: +// - resolved: tool names with aliases replaced by canonical names +// - aliasesUsed: map of oldName → newName for each alias that was resolved +func (tg *ToolsetGroup) ResolveToolAliases(toolNames []string) (resolved []string, aliasesUsed map[string]string) { + resolved = make([]string, 0, len(toolNames)) + aliasesUsed = make(map[string]string) + for _, toolName := range toolNames { + if canonicalName, isAlias := tg.deprecatedAliases[toolName]; isAlias { + fmt.Fprintf(os.Stderr, "Warning: tool %q is deprecated, use %q instead\n", toolName, canonicalName) + aliasesUsed[toolName] = canonicalName + resolved = append(resolved, canonicalName) + } else { + resolved = append(resolved, toolName) + } + } + return resolved, aliasesUsed +} + // FindToolByName searches all toolsets (enabled or disabled) for a tool by name. // Returns the tool, its parent toolset name, and an error if not found. func (tg *ToolsetGroup) FindToolByName(toolName string) (*ServerTool, string, error) { diff --git a/pkg/toolsets/toolsets_test.go b/pkg/toolsets/toolsets_test.go index 3f4581f34..6362aad0e 100644 --- a/pkg/toolsets/toolsets_test.go +++ b/pkg/toolsets/toolsets_test.go @@ -3,8 +3,23 @@ package toolsets import ( "errors" "testing" + + "github.com/modelcontextprotocol/go-sdk/mcp" ) +// mockTool creates a minimal ServerTool for testing +func mockTool(name string, readOnly bool) ServerTool { + return ServerTool{ + Tool: mcp.Tool{ + Name: name, + Annotations: &mcp.ToolAnnotations{ + ReadOnlyHint: readOnly, + }, + }, + RegisterFunc: func(_ *mcp.Server) {}, + } +} + func TestNewToolsetGroupIsEmptyWithoutEverythingOn(t *testing.T) { tsg := NewToolsetGroup(false) if len(tsg.Toolsets) != 0 { @@ -262,3 +277,119 @@ func TestToolsetGroup_GetToolset(t *testing.T) { t.Errorf("expected error to be ToolsetDoesNotExistError, got %v", err) } } + +func TestAddDeprecatedToolAliases(t *testing.T) { + tsg := NewToolsetGroup(false) + + // Test adding aliases + tsg.AddDeprecatedToolAliases(map[string]string{ + "old_name": "new_name", + "get_issue": "issue_read", + "create_pr": "pull_request_create", + }) + + if len(tsg.deprecatedAliases) != 3 { + t.Errorf("expected 3 aliases, got %d", len(tsg.deprecatedAliases)) + } + if tsg.deprecatedAliases["old_name"] != "new_name" { + t.Errorf("expected alias 'old_name' -> 'new_name', got '%s'", tsg.deprecatedAliases["old_name"]) + } + if tsg.deprecatedAliases["get_issue"] != "issue_read" { + t.Errorf("expected alias 'get_issue' -> 'issue_read'") + } + if tsg.deprecatedAliases["create_pr"] != "pull_request_create" { + t.Errorf("expected alias 'create_pr' -> 'pull_request_create'") + } +} + +func TestResolveToolAliases(t *testing.T) { + tsg := NewToolsetGroup(false) + tsg.AddDeprecatedToolAliases(map[string]string{ + "get_issue": "issue_read", + "create_pr": "pull_request_create", + }) + + // Test resolving a mix of aliases and canonical names + input := []string{"get_issue", "some_tool", "create_pr"} + resolved, aliasesUsed := tsg.ResolveToolAliases(input) + + // Verify resolved names + if len(resolved) != 3 { + t.Fatalf("expected 3 resolved names, got %d", len(resolved)) + } + if resolved[0] != "issue_read" { + t.Errorf("expected 'issue_read', got '%s'", resolved[0]) + } + if resolved[1] != "some_tool" { + t.Errorf("expected 'some_tool' (unchanged), got '%s'", resolved[1]) + } + if resolved[2] != "pull_request_create" { + t.Errorf("expected 'pull_request_create', got '%s'", resolved[2]) + } + + // Verify aliasesUsed map + if len(aliasesUsed) != 2 { + t.Fatalf("expected 2 aliases used, got %d", len(aliasesUsed)) + } + if aliasesUsed["get_issue"] != "issue_read" { + t.Errorf("expected aliasesUsed['get_issue'] = 'issue_read', got '%s'", aliasesUsed["get_issue"]) + } + if aliasesUsed["create_pr"] != "pull_request_create" { + t.Errorf("expected aliasesUsed['create_pr'] = 'pull_request_create', got '%s'", aliasesUsed["create_pr"]) + } +} + +func TestFindToolByName(t *testing.T) { + tsg := NewToolsetGroup(false) + + // Create a toolset with a tool + toolset := NewToolset("test-toolset", "Test toolset") + toolset.readTools = append(toolset.readTools, mockTool("issue_read", true)) + tsg.AddToolset(toolset) + + // Find by canonical name + tool, toolsetName, err := tsg.FindToolByName("issue_read") + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if tool.Tool.Name != "issue_read" { + t.Errorf("expected tool name 'issue_read', got '%s'", tool.Tool.Name) + } + if toolsetName != "test-toolset" { + t.Errorf("expected toolset name 'test-toolset', got '%s'", toolsetName) + } + + // FindToolByName does NOT resolve aliases - it expects canonical names + _, _, err = tsg.FindToolByName("get_issue") + if err == nil { + t.Error("expected error when using alias directly with FindToolByName") + } +} + +func TestRegisterSpecificTools(t *testing.T) { + tsg := NewToolsetGroup(false) + + // Create a toolset with both read and write tools + toolset := NewToolset("test-toolset", "Test toolset") + toolset.readTools = append(toolset.readTools, mockTool("issue_read", true)) + toolset.writeTools = append(toolset.writeTools, mockTool("issue_write", false)) + tsg.AddToolset(toolset) + + // Test registering with canonical names + err := tsg.RegisterSpecificTools(nil, []string{"issue_read"}, false) + if err != nil { + t.Errorf("expected no error registering tool, got %v", err) + } + + // Test registering write tool in read-only mode (should skip but not error) + err = tsg.RegisterSpecificTools(nil, []string{"issue_write"}, true) + if err != nil { + t.Errorf("expected no error when skipping write tool in read-only mode, got %v", err) + } + + // Test registering non-existent tool (should error) + err = tsg.RegisterSpecificTools(nil, []string{"nonexistent"}, false) + if err == nil { + t.Error("expected error for non-existent tool") + } +} diff --git a/script/get-me b/script/get-me index 46339ae53..954f57cec 100755 --- a/script/get-me +++ b/script/get-me @@ -1,3 +1,17 @@ #!/bin/bash -echo '{"jsonrpc":"2.0","id":3,"params":{"name":"get_me"},"method":"tools/call"}' | go run cmd/github-mcp-server/main.go stdio | jq . +# MCP requires initialize -> notifications/initialized -> tools/call +output=$( + ( + echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"get-me-script","version":"1.0.0"}}}' + echo '{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}' + echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"get_me","arguments":{}}}' + sleep 1 + ) | go run cmd/github-mcp-server/main.go stdio 2>/dev/null | tail -1 +) + +if command -v jq &> /dev/null; then + echo "$output" | jq '.result.content[0].text | fromjson' +else + echo "$output" +fi diff --git a/script/licenses b/script/licenses index 4200316b9..214efa435 100755 --- a/script/licenses +++ b/script/licenses @@ -1,21 +1,163 @@ #!/bin/bash +# +# Generate license files for all platform/arch combinations. +# This script handles architecture-specific dependency differences by: +# 1. Generating separate license reports per GOOS/GOARCH combination +# 2. Grouping identical reports together (comma-separated arch names) +# 3. Creating an index at the top of each platform file +# 4. Copying all license files to third-party/ +# +# Note: third-party/ is a union of all license files across all architectures. +# This means that license files for dependencies present in only some architectures +# may still appear in third-party/. This is intentional and ensures compliance. +# +# Note: we ignore warnings because we want the command to succeed, however the output should be checked +# for any new warnings, and potentially we may need to add license information. +# +# Normally these warnings are packages containing non go code, which may or may not require explicit attribution, +# depending on the license. + +set -e go install github.com/google/go-licenses@latest +# actions/setup-go does not setup the installed toolchain to be preferred over the system install, +# which causes go-licenses to raise "Package ... does not have module info" errors in CI. +# For more information, https://github.com/google/go-licenses/issues/244#issuecomment-1885098633 +if [ "$CI" = "true" ]; then + export GOROOT=$(go env GOROOT) + export PATH=${GOROOT}/bin:$PATH +fi + rm -rf third-party mkdir -p third-party export TEMPDIR="$(mktemp -d)" trap "rm -fr ${TEMPDIR}" EXIT -for goos in linux darwin windows ; do - # Note: we ignore warnings because we want the command to succeed, however the output should be checked - # for any new warnings, and potentially we may need to add license information. - # - # Normally these warnings are packages containing non go code, which may or may not require explicit attribution, - # depending on the license. - GOOS="${goos}" GOFLAGS=-mod=mod go-licenses save ./... --save_path="${TEMPDIR}/${goos}" --force || echo "Ignore warnings" - GOOS="${goos}" GOFLAGS=-mod=mod go-licenses report ./... --template .github/licenses.tmpl > third-party-licenses.${goos}.md || echo "Ignore warnings" - cp -fR "${TEMPDIR}/${goos}"/* third-party/ +# Cross-platform hash function (works on both Linux and macOS) +compute_hash() { + if command -v md5sum >/dev/null 2>&1; then + md5sum | cut -d' ' -f1 + elif command -v md5 >/dev/null 2>&1; then + md5 -q + else + # Fallback to cksum if neither is available + cksum | cut -d' ' -f1 + fi +} + +# Function to get architectures for a given OS +get_archs() { + case "$1" in + linux) echo "386 amd64 arm64" ;; + darwin) echo "amd64 arm64" ;; + windows) echo "386 amd64 arm64" ;; + esac +} + +# Generate reports for each platform/arch combination +for goos in darwin linux windows; do + echo "Processing ${goos}..." + + archs=$(get_archs "$goos") + + for goarch in $archs; do + echo " Generating for ${goos}/${goarch}..." + + # Generate the license report for this arch + report_file="${TEMPDIR}/${goos}_${goarch}_report.md" + GOOS="${goos}" GOARCH="${goarch}" GOFLAGS=-mod=mod go-licenses report ./... --template .github/licenses.tmpl > "${report_file}" 2>/dev/null || echo " (warnings ignored for ${goos}/${goarch})" + + # Save licenses to temp directory + GOOS="${goos}" GOARCH="${goarch}" GOFLAGS=-mod=mod go-licenses save ./... --save_path="${TEMPDIR}/${goos}_${goarch}" --force 2>/dev/null || echo " (warnings ignored for ${goos}/${goarch})" + + # Copy to third-party (accumulate all - union of all architectures for compliance) + if [ -d "${TEMPDIR}/${goos}_${goarch}" ]; then + cp -fR "${TEMPDIR}/${goos}_${goarch}"/* third-party/ 2>/dev/null || true + fi + + # Extract just the package list (skip header), sort it, and hash it + # Use LC_ALL=C for consistent sorting across different systems + packages_file="${TEMPDIR}/${goos}_${goarch}_packages.txt" + if [ -s "${report_file}" ] && grep -qE '^ - \[' "${report_file}" 2>/dev/null; then + grep -E '^ - \[' "${report_file}" | LC_ALL=C sort > "${packages_file}" + hash=$(cat "${packages_file}" | compute_hash) + else + echo "(FAILED TO GENERATE LICENSE REPORT FOR ${goos}/${goarch})" > "${packages_file}" + hash="FAILED_${goos}_${goarch}" + fi + + # Store hash for grouping + echo "${hash}" > "${TEMPDIR}/${goos}_${goarch}_hash.txt" + done + + # Group architectures with identical reports (deterministic order) + # Create groups file: hash -> comma-separated archs + groups_file="${TEMPDIR}/${goos}_groups.txt" + rm -f "${groups_file}" + + # Process architectures in order to build groups + for goarch in $archs; do + hash=$(cat "${TEMPDIR}/${goos}_${goarch}_hash.txt") + # Check if we've seen this hash before + if grep -q "^${hash}:" "${groups_file}" 2>/dev/null; then + # Append to existing group + existing=$(grep "^${hash}:" "${groups_file}" | cut -d: -f2) + sed -i.bak "s/^${hash}:.*/${hash}:${existing}, ${goarch}/" "${groups_file}" + rm -f "${groups_file}.bak" + else + # New group + echo "${hash}:${goarch}" >> "${groups_file}" + fi + done + + # Generate the combined report for this platform + output_file="third-party-licenses.${goos}.md" + + cat > "${output_file}" << 'EOF' +# GitHub MCP Server dependencies + +The following open source dependencies are used to build the [github/github-mcp-server][] GitHub Model Context Protocol Server. + +## Table of Contents + +EOF + + # Build table of contents (sorted for determinism) + # Use LC_ALL=C for consistent sorting across different systems + LC_ALL=C sort "${groups_file}" | while IFS=: read -r hash group_archs; do + # Create anchor-friendly name + anchor=$(echo "${group_archs}" | tr ', ' '-' | tr -s '-') + echo "- [${group_archs}](#${anchor})" >> "${output_file}" + done + + echo "" >> "${output_file}" + echo "---" >> "${output_file}" + echo "" >> "${output_file}" + + # Add each unique report section (sorted for determinism) + # Use LC_ALL=C for consistent sorting across different systems + LC_ALL=C sort "${groups_file}" | while IFS=: read -r hash group_archs; do + # Get the packages from the first arch in this group + first_arch=$(echo "${group_archs}" | cut -d',' -f1 | tr -d ' ') + packages=$(cat "${TEMPDIR}/${goos}_${first_arch}_packages.txt") + + cat >> "${output_file}" << EOF +## ${group_archs} + +The following packages are included for the ${group_archs} architectures. + +${packages} + +EOF + done + + # Add footer + echo "[github/github-mcp-server]: https://github.com/github/github-mcp-server" >> "${output_file}" + + echo "Generated ${output_file}" done +echo "Done! License files generated." + diff --git a/script/licenses-check b/script/licenses-check index 67b567d02..430c8170b 100755 --- a/script/licenses-check +++ b/script/licenses-check @@ -1,21 +1,34 @@ #!/bin/bash +# +# Check that license files are up to date. +# This script regenerates the license files and compares them with the committed versions. +# If there are differences, it exits with an error. -go install github.com/google/go-licenses@latest - -for goos in linux darwin windows ; do - # Note: we ignore warnings because we want the command to succeed, however the output should be checked - # for any new warnings, and potentially we may need to add license information. - # - # Normally these warnings are packages containing non go code, which may or may not require explicit attribution, - # depending on the license. - GOOS="${goos}" GOFLAGS=-mod=mod go-licenses report ./... --template .github/licenses.tmpl > third-party-licenses.${goos}.copy.md || echo "Ignore warnings" - if ! diff -s third-party-licenses.${goos}.copy.md third-party-licenses.${goos}.md; then - printf "License check failed.\n\nPlease update the license file by running \`.script/licenses\` and committing the output." - rm -f third-party-licenses.${goos}.copy.md - exit 1 - fi - rm -f third-party-licenses.${goos}.copy.md +set -e + +# Store original files for comparison +TEMPDIR="$(mktemp -d)" +trap "rm -fr ${TEMPDIR}" EXIT + +# Save original license markdown files +for goos in darwin linux windows; do + cp "third-party-licenses.${goos}.md" "${TEMPDIR}/" done +# Save the state of third-party directory +cp -r third-party "${TEMPDIR}/third-party.orig" + +# Regenerate using the same script +./script/licenses + +# Check for any differences in workspace +if ! git diff --exit-code --quiet third-party-licenses.*.md third-party/; then + echo "License files are out of date:" + git diff third-party-licenses.*.md third-party/ + echo "" + printf "\nLicense check failed.\n\nPlease update the license files by running \`./script/licenses\` and committing the output.\n" + exit 1 +fi +echo "License check passed for all platforms." diff --git a/third-party-licenses.darwin.md b/third-party-licenses.darwin.md index 32cdb5b6d..ef8816689 100644 --- a/third-party-licenses.darwin.md +++ b/third-party-licenses.darwin.md @@ -2,10 +2,15 @@ The following open source dependencies are used to build the [github/github-mcp-server][] GitHub Model Context Protocol Server. -## Go Packages +## Table of Contents -Some packages may only be included on certain architectures or operating systems. +- [amd64, arm64](#amd64-arm64) +--- + +## amd64, arm64 + +The following packages are included for the amd64, arm64 architectures. - [github.com/aymerick/douceur](https://pkg.go.dev/github.com/aymerick/douceur) ([MIT](https://github.com/aymerick/douceur/blob/v0.2.0/LICENSE)) - [github.com/fsnotify/fsnotify](https://pkg.go.dev/github.com/fsnotify/fsnotify) ([BSD-3-Clause](https://github.com/fsnotify/fsnotify/blob/v1.9.0/LICENSE)) diff --git a/third-party-licenses.linux.md b/third-party-licenses.linux.md index 32cdb5b6d..851a70594 100644 --- a/third-party-licenses.linux.md +++ b/third-party-licenses.linux.md @@ -2,10 +2,15 @@ The following open source dependencies are used to build the [github/github-mcp-server][] GitHub Model Context Protocol Server. -## Go Packages +## Table of Contents -Some packages may only be included on certain architectures or operating systems. +- [386, amd64, arm64](#386-amd64-arm64) +--- + +## 386, amd64, arm64 + +The following packages are included for the 386, amd64, arm64 architectures. - [github.com/aymerick/douceur](https://pkg.go.dev/github.com/aymerick/douceur) ([MIT](https://github.com/aymerick/douceur/blob/v0.2.0/LICENSE)) - [github.com/fsnotify/fsnotify](https://pkg.go.dev/github.com/fsnotify/fsnotify) ([BSD-3-Clause](https://github.com/fsnotify/fsnotify/blob/v1.9.0/LICENSE)) diff --git a/third-party-licenses.windows.md b/third-party-licenses.windows.md index c7e00fb13..f4f8ee42c 100644 --- a/third-party-licenses.windows.md +++ b/third-party-licenses.windows.md @@ -2,10 +2,15 @@ The following open source dependencies are used to build the [github/github-mcp-server][] GitHub Model Context Protocol Server. -## Go Packages +## Table of Contents -Some packages may only be included on certain architectures or operating systems. +- [386, amd64, arm64](#386-amd64-arm64) +--- + +## 386, amd64, arm64 + +The following packages are included for the 386, amd64, arm64 architectures. - [github.com/aymerick/douceur](https://pkg.go.dev/github.com/aymerick/douceur) ([MIT](https://github.com/aymerick/douceur/blob/v0.2.0/LICENSE)) - [github.com/fsnotify/fsnotify](https://pkg.go.dev/github.com/fsnotify/fsnotify) ([BSD-3-Clause](https://github.com/fsnotify/fsnotify/blob/v1.9.0/LICENSE))