|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/github/github-mcp-server/pkg/github" |
| 12 | + "github.com/github/github-mcp-server/pkg/inventory" |
| 13 | + "github.com/github/github-mcp-server/pkg/translations" |
| 14 | +) |
| 15 | + |
| 16 | +// generateInsidersFeaturesDocs refreshes the auto-generated section of |
| 17 | +// docs/insiders-features.md with the tools and schemas affected by each |
| 18 | +// Insiders feature flag. |
| 19 | +func generateInsidersFeaturesDocs(docsPath string) error { |
| 20 | + body := generateFlaggedToolsDoc(github.InsidersFeatureFlags, "_No Insiders-only tool changes._") |
| 21 | + return rewriteAutomatedSection(docsPath, "START AUTOMATED INSIDERS TOOLS", "END AUTOMATED INSIDERS TOOLS", body) |
| 22 | +} |
| 23 | + |
| 24 | +// generateFeatureFlagsDocs refreshes the auto-generated section of |
| 25 | +// docs/feature-flags.md with the tools and schemas affected by each |
| 26 | +// user-controllable feature flag. |
| 27 | +func generateFeatureFlagsDocs(docsPath string) error { |
| 28 | + body := generateFlaggedToolsDoc(github.AllowedFeatureFlags, "_No user-controllable feature flags affect tool registration._") |
| 29 | + return rewriteAutomatedSection(docsPath, "START AUTOMATED FEATURE FLAG TOOLS", "END AUTOMATED FEATURE FLAG TOOLS", body) |
| 30 | +} |
| 31 | + |
| 32 | +// generateFlaggedToolsDoc renders, for each flag in the input set, the tools |
| 33 | +// whose registration or definition differs from the default user experience. |
| 34 | +// Each affected tool is printed with its full schema using the same writer |
| 35 | +// used by the README so the output style stays consistent. |
| 36 | +func generateFlaggedToolsDoc(flags []string, emptyMessage string) string { |
| 37 | + t, _ := translations.TranslationHelper() |
| 38 | + defaultTools := indexToolsByName(buildInventoryWithFlags(t, nil).ToolsForRegistration(context.Background())) |
| 39 | + |
| 40 | + var buf strings.Builder |
| 41 | + hasAny := false |
| 42 | + |
| 43 | + for _, flag := range flags { |
| 44 | + affected := flaggedToolDiff(t, flag, defaultTools) |
| 45 | + if len(affected) == 0 { |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + if hasAny { |
| 50 | + buf.WriteString("\n\n") |
| 51 | + } |
| 52 | + hasAny = true |
| 53 | + |
| 54 | + fmt.Fprintf(&buf, "### `%s`\n\n", flag) |
| 55 | + for i, tool := range affected { |
| 56 | + writeToolDoc(&buf, tool) |
| 57 | + if i < len(affected)-1 { |
| 58 | + buf.WriteString("\n\n") |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if !hasAny { |
| 64 | + return emptyMessage |
| 65 | + } |
| 66 | + // Leading/trailing newlines around the body produce blank lines between |
| 67 | + // our content and the surrounding marker comments, so the trailing comment |
| 68 | + // doesn't get absorbed into the final list item by markdown renderers. |
| 69 | + return "\n" + strings.TrimSuffix(buf.String(), "\n") + "\n" |
| 70 | +} |
| 71 | + |
| 72 | +// flaggedToolDiff returns the tools whose definition (input schema or meta) |
| 73 | +// differs from the default-flagged inventory when only the given flag is on, |
| 74 | +// plus tools that exist only in the flag-on inventory. Results are sorted by |
| 75 | +// tool name. |
| 76 | +func flaggedToolDiff(t translations.TranslationHelperFunc, flag string, defaultTools map[string]inventory.ServerTool) []inventory.ServerTool { |
| 77 | + flagTools := buildInventoryWithFlags(t, map[string]bool{flag: true}).ToolsForRegistration(context.Background()) |
| 78 | + |
| 79 | + out := make([]inventory.ServerTool, 0) |
| 80 | + seen := make(map[string]struct{}, len(flagTools)) |
| 81 | + |
| 82 | + for _, tool := range flagTools { |
| 83 | + if _, ok := seen[tool.Tool.Name]; ok { |
| 84 | + continue |
| 85 | + } |
| 86 | + seen[tool.Tool.Name] = struct{}{} |
| 87 | + |
| 88 | + baseline, hadBaseline := defaultTools[tool.Tool.Name] |
| 89 | + if hadBaseline && reflect.DeepEqual(tool.Tool.InputSchema, baseline.Tool.InputSchema) && reflect.DeepEqual(tool.Tool.Meta, baseline.Tool.Meta) { |
| 90 | + continue |
| 91 | + } |
| 92 | + out = append(out, tool) |
| 93 | + } |
| 94 | + |
| 95 | + sort.Slice(out, func(i, j int) bool { return out[i].Tool.Name < out[j].Tool.Name }) |
| 96 | + return out |
| 97 | +} |
| 98 | + |
| 99 | +// buildInventoryWithFlags constructs an inventory whose feature checker treats |
| 100 | +// the given flags as enabled and every other flag as disabled. Passing nil |
| 101 | +// produces the default-flagged inventory. |
| 102 | +func buildInventoryWithFlags(t translations.TranslationHelperFunc, enabled map[string]bool) *inventory.Inventory { |
| 103 | + checker := func(_ context.Context, flag string) (bool, error) { |
| 104 | + return enabled[flag], nil |
| 105 | + } |
| 106 | + inv, _ := github.NewInventory(t). |
| 107 | + WithToolsets([]string{"all"}). |
| 108 | + WithFeatureChecker(checker). |
| 109 | + Build() |
| 110 | + return inv |
| 111 | +} |
| 112 | + |
| 113 | +// indexToolsByName returns a map keyed by tool name. When duplicates exist |
| 114 | +// (e.g. flag-gated dual registrations), the first occurrence wins, mirroring |
| 115 | +// AvailableTools' deterministic sort order. |
| 116 | +func indexToolsByName(tools []inventory.ServerTool) map[string]inventory.ServerTool { |
| 117 | + out := make(map[string]inventory.ServerTool, len(tools)) |
| 118 | + for _, tool := range tools { |
| 119 | + if _, ok := out[tool.Tool.Name]; ok { |
| 120 | + continue |
| 121 | + } |
| 122 | + out[tool.Tool.Name] = tool |
| 123 | + } |
| 124 | + return out |
| 125 | +} |
| 126 | + |
| 127 | +// rewriteAutomatedSection reads a markdown file, replaces the content between |
| 128 | +// the named markers with body, and writes it back. |
| 129 | +func rewriteAutomatedSection(path, startMarker, endMarker, body string) error { |
| 130 | + content, err := os.ReadFile(path) //#nosec G304 |
| 131 | + if err != nil { |
| 132 | + return fmt.Errorf("failed to read docs file: %w", err) |
| 133 | + } |
| 134 | + updated, err := replaceSection(string(content), startMarker, endMarker, body) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + return os.WriteFile(path, []byte(updated), 0600) //#nosec G306 |
| 139 | +} |
0 commit comments