|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "image" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "regexp" |
| 13 | + "strings" |
| 14 | + |
| 15 | + _ "image/jpeg" |
| 16 | + _ "image/png" |
| 17 | + |
| 18 | + "github.com/docker/mcp-registry/internal/licenses" |
| 19 | + "github.com/docker/mcp-registry/pkg/github" |
| 20 | + "github.com/docker/mcp-registry/pkg/servers" |
| 21 | + "gopkg.in/yaml.v3" |
| 22 | +) |
| 23 | + |
| 24 | +func main() { |
| 25 | + name := flag.String("name", "", "Name of the mcp server, name is guessed if not provided") |
| 26 | + flag.Parse() |
| 27 | + |
| 28 | + if err := run(*name); err != nil { |
| 29 | + log.Fatal(err) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func run(name string) error { |
| 34 | + if err := isNameValid(name); err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + if err := isDirectoryValid(name); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + if err := areSecretsValid(name); err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + if err := IsLicenseValid(name); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + if err := isIconValid(name); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +// check if the name is a valid |
| 57 | +func isNameValid(name string) error { |
| 58 | + // check if name has only letters, numbers, and hyphens |
| 59 | + if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(name) { |
| 60 | + return fmt.Errorf("name is not valid. It must be a lowercase string with only letters, numbers, and hyphens") |
| 61 | + } |
| 62 | + |
| 63 | + fmt.Println("✅ Name is valid") |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// check if the directory is valid |
| 68 | +// servers/<NAME>/server.yaml exists |
| 69 | +func isDirectoryValid(name string) error { |
| 70 | + _, err := os.Stat(filepath.Join("servers", name, "server.yaml")) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + server, err := readServerYaml(name) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + // check if the server.yaml file has a valid name |
| 80 | + if server.Name != name { |
| 81 | + return fmt.Errorf("server.yaml file has a invalid name. It must be %s", name) |
| 82 | + } |
| 83 | + |
| 84 | + fmt.Println("✅ Directory is valid") |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// check if the secrets are valid |
| 89 | +// secrets must be prefixed with the name of the server |
| 90 | +func areSecretsValid(name string) error { |
| 91 | + // read the server.yaml file |
| 92 | + server, err := readServerYaml(name) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + // check if the server.yaml file has a valid secrets |
| 98 | + if len(server.Config.Secrets) > 0 { |
| 99 | + for _, secret := range server.Config.Secrets { |
| 100 | + if !strings.HasPrefix(secret.Name, name+".") { |
| 101 | + return fmt.Errorf("secret %s is not valid. It must be prefixed with the name of the server", secret.Name) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + fmt.Println("✅ Secrets are valid") |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// check if the license is valid |
| 111 | +// the license must be valid |
| 112 | +func IsLicenseValid(name string) error { |
| 113 | + ctx := context.Background() |
| 114 | + client := github.New() |
| 115 | + server, err := readServerYaml(name) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + repository, err := client.GetProjectRepository(ctx, server.Source.Project) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + if !licenses.IsValid(repository.License) { |
| 125 | + return fmt.Errorf("project %s is licensed under %s which may be incompatible with some tools", server.Source.Project, repository.License.GetName()) |
| 126 | + } |
| 127 | + fmt.Println("✅ License is valid") |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func isIconValid(name string) error { |
| 133 | + server, err := readServerYaml(name) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + if server.Image == "" { |
| 139 | + return fmt.Errorf("image is not valid. It must be a valid image") |
| 140 | + } |
| 141 | + // fetch the image and check the size |
| 142 | + resp, err := http.Get(server.About.Icon) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + defer resp.Body.Close() |
| 147 | + if resp.StatusCode != 200 { |
| 148 | + return fmt.Errorf("image is not valid. It must be a valid image") |
| 149 | + } |
| 150 | + if resp.ContentLength > 2*1024*1024 { |
| 151 | + return fmt.Errorf("image is too large. It must be less than 2MB") |
| 152 | + } |
| 153 | + img, format, err := image.DecodeConfig(resp.Body) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + if format != "png" { |
| 158 | + return fmt.Errorf("image is not a png. It must be a png") |
| 159 | + } |
| 160 | + |
| 161 | + if img.Width > 512 || img.Height > 512 { |
| 162 | + return fmt.Errorf("image is too large. It must be less than 512x512") |
| 163 | + } |
| 164 | + |
| 165 | + fmt.Println("✅ Icon is valid") |
| 166 | + return nil |
| 167 | +} |
| 168 | + |
| 169 | +func readServerYaml(name string) (servers.Server, error) { |
| 170 | + serverYaml, err := os.ReadFile(filepath.Join("servers", name, "server.yaml")) |
| 171 | + if err != nil { |
| 172 | + return servers.Server{}, err |
| 173 | + } |
| 174 | + var server servers.Server |
| 175 | + err = yaml.Unmarshal(serverYaml, &server) |
| 176 | + if err != nil { |
| 177 | + return servers.Server{}, err |
| 178 | + } |
| 179 | + return server, nil |
| 180 | +} |
0 commit comments