Skip to content

Commit 2a2bbfe

Browse files
authored
Use constants instead of hardcoded strings (#330)
<!-- Provide a brief summary of your changes --> ## Motivation and Context <!-- Why is this change needed? What problem does it solve? --> The following PR updates some of the hardcoded references to public constants. ## How Has This Been Tested? <!-- Have you tested this in a real application? Which scenarios were tested? --> Locally ## Breaking Changes <!-- Will users need to update their code or configurations? --> No ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [ ] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [ ] My code follows the repository's style guidelines - [ ] New and existing tests pass locally - [ ] I have added appropriate error handling - [ ] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions --> Signed-off-by: Radoslav Dimitrov <[email protected]>
1 parent e0aecad commit 2a2bbfe

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

cmd/publisher/commands/init.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ import (
1414
"github.com/modelcontextprotocol/registry/pkg/model"
1515
)
1616

17-
const (
18-
RegistryTypeNPM = "npm"
19-
RegistryTypePyPi = "pypi"
20-
RegistryTypeOci = "oci"
21-
)
2217

2318
func InitCommand() error {
2419
// Check if server.json already exists
@@ -193,29 +188,29 @@ func detectRepoURL() string {
193188
func detectPackageType() string {
194189
// Check for package.json
195190
if _, err := os.Stat("package.json"); err == nil {
196-
return RegistryTypeNPM
191+
return model.RegistryTypeNPM
197192
}
198193

199194
// Check for pyproject.toml or setup.py
200195
if _, err := os.Stat("pyproject.toml"); err == nil {
201-
return RegistryTypePyPi
196+
return model.RegistryTypePyPI
202197
}
203198
if _, err := os.Stat("setup.py"); err == nil {
204-
return RegistryTypePyPi
199+
return model.RegistryTypePyPI
205200
}
206201

207202
// Check for Dockerfile
208203
if _, err := os.Stat("Dockerfile"); err == nil {
209-
return RegistryTypeOci
204+
return model.RegistryTypeOCI
210205
}
211206

212207
// Default to npm as most common
213-
return RegistryTypeNPM
208+
return model.RegistryTypeNPM
214209
}
215210

216211
func detectPackageIdentifier(serverName string, packageType string) string {
217212
switch packageType {
218-
case RegistryTypeNPM:
213+
case model.RegistryTypeNPM:
219214
// Try to get from package.json
220215
if data, err := os.ReadFile("package.json"); err == nil {
221216
var pkg map[string]any
@@ -235,7 +230,7 @@ func detectPackageIdentifier(serverName string, packageType string) string {
235230
}
236231
return "@your-org/your-package"
237232

238-
case "pypi":
233+
case model.RegistryTypePyPI:
239234
// Try to get from pyproject.toml or setup.py
240235
if data, err := os.ReadFile("pyproject.toml"); err == nil {
241236
// Simple extraction - could be improved with proper TOML parser
@@ -254,7 +249,7 @@ func detectPackageIdentifier(serverName string, packageType string) string {
254249
}
255250
return "your-package"
256251

257-
case "docker":
252+
case model.RegistryTypeOCI:
258253
// Use a sensible default
259254
if strings.Contains(serverName, "/") {
260255
parts := strings.Split(serverName, "/")
@@ -275,15 +270,15 @@ func createServerJSON(
275270
// Determine registry type and base URL
276271
var registryType, registryBaseURL string
277272
switch packageType {
278-
case RegistryTypeNPM:
279-
registryType = RegistryTypeNPM
280-
registryBaseURL = "https://registry.npmjs.org"
281-
case RegistryTypePyPi:
282-
registryType = RegistryTypePyPi
283-
registryBaseURL = "https://pypi.org"
284-
case RegistryTypeOci:
285-
registryType = RegistryTypeOci
286-
registryBaseURL = "https://docker.io"
273+
case model.RegistryTypeNPM:
274+
registryType = model.RegistryTypeNPM
275+
registryBaseURL = model.RegistryURLNPM
276+
case model.RegistryTypePyPI:
277+
registryType = model.RegistryTypePyPI
278+
registryBaseURL = model.RegistryURLPyPI
279+
case model.RegistryTypeOCI:
280+
registryType = model.RegistryTypeOCI
281+
registryBaseURL = model.RegistryURLDocker
287282
case "url":
288283
registryType = "url"
289284
registryBaseURL = ""

internal/validators/validators.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func validatePackage(pkg *model.Package) error {
168168
registryType := strings.ToLower(pkg.RegistryType)
169169

170170
// For direct download packages (mcpb or direct URLs)
171-
if registryType == "mcpb" ||
171+
if registryType == model.RegistryTypeMCPB ||
172172
strings.HasPrefix(pkg.Identifier, "http://") || strings.HasPrefix(pkg.Identifier, "https://") {
173173
parsedURL, err := url.Parse(pkg.Identifier)
174174
if err != nil {
@@ -178,7 +178,7 @@ func validatePackage(pkg *model.Package) error {
178178
host := strings.ToLower(parsedURL.Host)
179179

180180
// For MCPB packages, validate they're from allowed hosts
181-
if registryType == "mcpb" {
181+
if registryType == model.RegistryTypeMCPB {
182182
return validateMCPBPackage(host)
183183
}
184184

pkg/model/constants.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package model
2+
3+
// Registry Types - supported package registry types
4+
const (
5+
RegistryTypeNPM = "npm"
6+
RegistryTypePyPI = "pypi"
7+
RegistryTypeOCI = "oci"
8+
RegistryTypeNuGet = "nuget"
9+
RegistryTypeMCPB = "mcpb"
10+
)
11+
12+
// Registry Base URLs - supported package registry base URLs
13+
const (
14+
RegistryURLNPM = "https://registry.npmjs.org"
15+
RegistryURLPyPI = "https://pypi.org"
16+
RegistryURLDocker = "https://docker.io"
17+
RegistryURLNuGet = "https://api.nuget.org"
18+
RegistryURLGitHub = "https://github.com"
19+
RegistryURLGitLab = "https://gitlab.com"
20+
)
21+
22+
// Transport Types - supported remote transport protocols
23+
const (
24+
TransportTypeStreamable = "streamable"
25+
TransportTypeSSE = "sse"
26+
)
27+
28+
// Runtime Hints - supported package runtime hints
29+
const (
30+
RuntimeHintNPX = "npx"
31+
RuntimeHintUVX = "uvx"
32+
RuntimeHintDocker = "docker"
33+
RuntimeHintDNX = "dnx"
34+
)

0 commit comments

Comments
 (0)