|
| 1 | +package scopes |
| 2 | + |
| 3 | +import "github.com/github/github-mcp-server/pkg/inventory" |
| 4 | + |
| 5 | +// ToolScopeMap maps tool names to their scope requirements. |
| 6 | +type ToolScopeMap map[string]*ToolScopeInfo |
| 7 | + |
| 8 | +// ToolScopeInfo contains scope information for a single tool. |
| 9 | +type ToolScopeInfo struct { |
| 10 | + // RequiredScopes contains the scopes that are directly required by this tool. |
| 11 | + RequiredScopes []string |
| 12 | + |
| 13 | + // AcceptedScopes contains all scopes that satisfy the requirements (including parent scopes). |
| 14 | + AcceptedScopes []string |
| 15 | +} |
| 16 | + |
| 17 | +// globalToolScopeMap is populated from inventory when SetToolScopeMapFromInventory is called |
| 18 | +var globalToolScopeMap ToolScopeMap |
| 19 | + |
| 20 | +// SetToolScopeMapFromInventory builds and stores a tool scope map from an inventory. |
| 21 | +// This should be called after building the inventory to make scopes available for middleware. |
| 22 | +func SetToolScopeMapFromInventory(inv *inventory.Inventory) { |
| 23 | + globalToolScopeMap = GetToolScopeMapFromInventory(inv) |
| 24 | +} |
| 25 | + |
| 26 | +// SetGlobalToolScopeMap sets the global tool scope map directly. |
| 27 | +// This is useful for testing when you don't have a full inventory. |
| 28 | +func SetGlobalToolScopeMap(m ToolScopeMap) { |
| 29 | + globalToolScopeMap = m |
| 30 | +} |
| 31 | + |
| 32 | +// GetToolScopeMap returns the global tool scope map. |
| 33 | +// Returns an empty map if SetToolScopeMapFromInventory hasn't been called yet. |
| 34 | +func GetToolScopeMap() (ToolScopeMap, error) { |
| 35 | + if globalToolScopeMap == nil { |
| 36 | + return make(ToolScopeMap), nil |
| 37 | + } |
| 38 | + return globalToolScopeMap, nil |
| 39 | +} |
| 40 | + |
| 41 | +// GetToolScopeInfo returns scope information for a specific tool from the global scope map. |
| 42 | +func GetToolScopeInfo(toolName string) (*ToolScopeInfo, error) { |
| 43 | + m, err := GetToolScopeMap() |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + return m[toolName], nil |
| 48 | +} |
| 49 | + |
| 50 | +// GetToolScopeMapFromInventory builds a tool scope map from an inventory. |
| 51 | +// This extracts scope information from ServerTool.RequiredScopes and ServerTool.AcceptedScopes. |
| 52 | +func GetToolScopeMapFromInventory(inv *inventory.Inventory) ToolScopeMap { |
| 53 | + result := make(ToolScopeMap) |
| 54 | + |
| 55 | + // Get all tools from the inventory (both enabled and disabled) |
| 56 | + // We need all tools for scope checking purposes |
| 57 | + allTools := inv.AllTools() |
| 58 | + for i := range allTools { |
| 59 | + tool := &allTools[i] |
| 60 | + if len(tool.RequiredScopes) > 0 || len(tool.AcceptedScopes) > 0 { |
| 61 | + result[tool.Tool.Name] = &ToolScopeInfo{ |
| 62 | + RequiredScopes: tool.RequiredScopes, |
| 63 | + AcceptedScopes: tool.AcceptedScopes, |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return result |
| 69 | +} |
| 70 | + |
| 71 | +// HasAcceptedScope checks if any of the provided user scopes satisfy the tool's requirements. |
| 72 | +func (t *ToolScopeInfo) HasAcceptedScope(userScopes ...string) bool { |
| 73 | + if t == nil || len(t.AcceptedScopes) == 0 { |
| 74 | + return true // No scopes required |
| 75 | + } |
| 76 | + |
| 77 | + userScopeSet := make(map[string]bool) |
| 78 | + for _, scope := range userScopes { |
| 79 | + userScopeSet[scope] = true |
| 80 | + } |
| 81 | + |
| 82 | + for _, scope := range t.AcceptedScopes { |
| 83 | + if userScopeSet[scope] { |
| 84 | + return true |
| 85 | + } |
| 86 | + } |
| 87 | + return false |
| 88 | +} |
| 89 | + |
| 90 | +// MissingScopes returns the required scopes that are not present in the user's scopes. |
| 91 | +func (t *ToolScopeInfo) MissingScopes(userScopes ...string) []string { |
| 92 | + if t == nil || len(t.RequiredScopes) == 0 { |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + // Create a set of user scopes for O(1) lookup |
| 97 | + userScopeSet := make(map[string]bool, len(userScopes)) |
| 98 | + for _, s := range userScopes { |
| 99 | + userScopeSet[s] = true |
| 100 | + } |
| 101 | + |
| 102 | + // Check if any accepted scope is present |
| 103 | + hasAccepted := false |
| 104 | + for _, scope := range t.AcceptedScopes { |
| 105 | + if userScopeSet[scope] { |
| 106 | + hasAccepted = true |
| 107 | + break |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if hasAccepted { |
| 112 | + return nil // User has sufficient scopes |
| 113 | + } |
| 114 | + |
| 115 | + // Return required scopes as the minimum needed |
| 116 | + missing := make([]string, len(t.RequiredScopes)) |
| 117 | + copy(missing, t.RequiredScopes) |
| 118 | + return missing |
| 119 | +} |
| 120 | + |
| 121 | +// GetRequiredScopesSlice returns the required scopes as a slice of strings. |
| 122 | +func (t *ToolScopeInfo) GetRequiredScopesSlice() []string { |
| 123 | + if t == nil { |
| 124 | + return nil |
| 125 | + } |
| 126 | + scopes := make([]string, len(t.RequiredScopes)) |
| 127 | + copy(scopes, t.RequiredScopes) |
| 128 | + return scopes |
| 129 | +} |
0 commit comments