|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "time" |
| 10 | + |
| 11 | + mjrToken "github.com/major-technology/cli/clients/token" |
| 12 | + "github.com/major-technology/cli/singletons" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var checkReadonlyCmd = &cobra.Command{ |
| 17 | + Use: "check-readonly [tool-name]", |
| 18 | + Short: "Check if an MCP tool is read-only", |
| 19 | + Hidden: true, |
| 20 | + Args: cobra.ExactArgs(1), |
| 21 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 22 | + return runCheckReadonly(args[0]) |
| 23 | + }, |
| 24 | +} |
| 25 | + |
| 26 | +type toolMetadataItem struct { |
| 27 | + Name string `json:"name"` |
| 28 | + Description string `json:"description"` |
| 29 | + ReadOnly bool `json:"readOnly"` |
| 30 | + ResourceType string `json:"resourceType"` |
| 31 | +} |
| 32 | + |
| 33 | +type toolCache struct { |
| 34 | + Tools []toolMetadataItem `json:"tools"` |
| 35 | + FetchedAt time.Time `json:"fetchedAt"` |
| 36 | +} |
| 37 | + |
| 38 | +func runCheckReadonly(toolName string) error { |
| 39 | + tools, err := getCachedToolMetadata() |
| 40 | + if err != nil { |
| 41 | + // If we can't get metadata, don't block — let Claude Code prompt the user |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + for _, t := range tools { |
| 46 | + if t.Name == toolName && t.ReadOnly { |
| 47 | + result := map[string]any{ |
| 48 | + "hookSpecificOutput": map[string]any{ |
| 49 | + "hookEventName": "PreToolUse", |
| 50 | + "permissionDecision": "allow", |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + return json.NewEncoder(os.Stdout).Encode(result) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Not read-only or not found — exit silently, Claude Code will prompt the user |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func getCachedToolMetadata() ([]toolMetadataItem, error) { |
| 63 | + homeDir, err := os.UserHomeDir() |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + cachePath := filepath.Join(homeDir, ".major", "cache", "tool-metadata.json") |
| 69 | + |
| 70 | + // Try to read cache |
| 71 | + data, err := os.ReadFile(cachePath) |
| 72 | + if err == nil { |
| 73 | + var cache toolCache |
| 74 | + |
| 75 | + if json.Unmarshal(data, &cache) == nil { |
| 76 | + if time.Since(cache.FetchedAt) < 24*time.Hour { |
| 77 | + return cache.Tools, nil |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Fetch fresh metadata from resource API |
| 83 | + cfg := singletons.GetConfig() |
| 84 | + if cfg == nil || cfg.ResourceAPIURL == "" { |
| 85 | + return nil, fmt.Errorf("config not loaded") |
| 86 | + } |
| 87 | + |
| 88 | + url := cfg.ResourceAPIURL + "/cli/v1/mcp/tools" |
| 89 | + |
| 90 | + // Get auth headers |
| 91 | + token, err := mjrToken.GetToken() |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + orgID, _, err := mjrToken.GetDefaultOrg() |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + req, err := http.NewRequest("GET", url, nil) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + req.Header.Set("Authorization", "Bearer "+token) |
| 107 | + req.Header.Set("x-major-org-id", orgID) |
| 108 | + |
| 109 | + client := &http.Client{Timeout: 10 * time.Second} |
| 110 | + resp, err := client.Do(req) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + defer resp.Body.Close() |
| 115 | + |
| 116 | + if resp.StatusCode != http.StatusOK { |
| 117 | + return nil, fmt.Errorf("tools endpoint returned %d", resp.StatusCode) |
| 118 | + } |
| 119 | + |
| 120 | + var toolsResp struct { |
| 121 | + Tools []toolMetadataItem `json:"tools"` |
| 122 | + } |
| 123 | + |
| 124 | + if err := json.NewDecoder(resp.Body).Decode(&toolsResp); err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + |
| 128 | + // Cache it |
| 129 | + cache := toolCache{ |
| 130 | + Tools: toolsResp.Tools, |
| 131 | + FetchedAt: time.Now(), |
| 132 | + } |
| 133 | + |
| 134 | + cacheData, _ := json.Marshal(cache) |
| 135 | + os.MkdirAll(filepath.Dir(cachePath), 0755) |
| 136 | + os.WriteFile(cachePath, cacheData, 0644) |
| 137 | + |
| 138 | + return toolsResp.Tools, nil |
| 139 | +} |
0 commit comments