Add registry validators (#350)
Fixes #96 ## Motivation and Context See #96 ## How Has This Been Tested? Has some unit and integration tests, still needs proper testing of the different registry types ## Breaking Changes Yes, new package uploads will need extra verification ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update
This commit is contained in:
parent
6ea3828e3c
commit
694a4d8879
11 changed files with 1859 additions and 728 deletions
167
internal/validators/registries/mcpb.go
Normal file
167
internal/validators/registries/mcpb.go
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
package registries
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
)
|
||||
|
||||
func ValidateMCPB(ctx context.Context, pkg model.Package, _ string) error {
|
||||
err := validateMCPBUrl(pkg.Identifier)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
inferredBaseURL, err := inferMCPBRegistryBaseURL(pkg.Identifier)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if pkg.RegistryBaseURL == "" {
|
||||
pkg.RegistryBaseURL = inferredBaseURL
|
||||
} else if pkg.RegistryBaseURL != inferredBaseURL {
|
||||
return fmt.Errorf("MCPB package '%s' has inconsistent registry base URL: %s (expected: %s)",
|
||||
pkg.Identifier, pkg.RegistryBaseURL, inferredBaseURL)
|
||||
}
|
||||
|
||||
// Parse the URL to validate format
|
||||
url, err := url.Parse(pkg.Identifier)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid MCPB package URL: %w", err)
|
||||
}
|
||||
if url.Scheme != "https" {
|
||||
return fmt.Errorf("invalid MCPB package URL, must use HTTPS: %s", pkg.Identifier)
|
||||
}
|
||||
|
||||
// Check that the URL contains 'mcp' somewhere (case-insensitive)
|
||||
if !strings.Contains(strings.ToLower(pkg.Identifier), "mcp") {
|
||||
return fmt.Errorf("MCPB package URL must contain 'mcp': %s", pkg.Identifier)
|
||||
}
|
||||
|
||||
// Verify the file exists and is publicly accessible
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, pkg.Identifier, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "MCP-Registry-Validator/1.0")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to verify MCPB package accessibility: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("MCPB package '%s' is not publicly accessible (status: %d)", pkg.Identifier, resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateMCPBUrl(fullURL string) error {
|
||||
parsedURL, err := url.Parse(fullURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid MCPB package URL: %w", err)
|
||||
}
|
||||
|
||||
host := strings.ToLower(parsedURL.Host)
|
||||
allowedHosts := []string{
|
||||
"github.com",
|
||||
"www.github.com",
|
||||
"gitlab.com",
|
||||
"www.gitlab.com",
|
||||
}
|
||||
|
||||
isAllowed := false
|
||||
for _, allowed := range allowedHosts {
|
||||
if host == allowed {
|
||||
isAllowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !isAllowed {
|
||||
return fmt.Errorf("MCPB packages must be hosted on allowlisted providers (GitHub or GitLab). Host '%s' is not allowed", host)
|
||||
}
|
||||
|
||||
// Validate URL path is a proper release URL with strict structure validation
|
||||
path := parsedURL.Path
|
||||
switch host {
|
||||
case "github.com", "www.github.com":
|
||||
// GitHub release URLs must match: /owner/repo/releases/download/tag/filename
|
||||
if !isValidGitHubReleaseURL(path) {
|
||||
return fmt.Errorf("GitHub MCPB packages must be release assets following the pattern '/owner/repo/releases/download/tag/filename'")
|
||||
}
|
||||
case "gitlab.com", "www.gitlab.com":
|
||||
// GitLab release URLs must match specific patterns
|
||||
if !isValidGitLabReleaseURL(path) {
|
||||
return fmt.Errorf("GitLab MCPB packages must be release assets following patterns '/owner/repo/-/releases/tag/downloads/filename' or '/owner/repo/-/package_files/id/download'")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// isValidGitHubReleaseURL validates that a path follows the GitHub release asset pattern
|
||||
// Pattern: /owner/repo/releases/download/tag/filename
|
||||
func isValidGitHubReleaseURL(path string) bool {
|
||||
// GitHub release URL pattern: /owner/repo/releases/download/tag/filename
|
||||
// - owner: username or organization (1-39 chars, alphanumeric + hyphens, no consecutive hyphens)
|
||||
// - repo: repository name (similar rules to owner)
|
||||
// - tag: release tag (can contain various characters but not empty)
|
||||
// - filename: asset filename (not empty)
|
||||
pattern := `^/([a-zA-Z0-9]([a-zA-Z0-9\-]{0,37}[a-zA-Z0-9])?)/([a-zA-Z0-9._\-]+)/releases/download/([^/]+)/([^/]+)$`
|
||||
matched, _ := regexp.MatchString(pattern, path)
|
||||
return matched
|
||||
}
|
||||
|
||||
// isValidGitLabReleaseURL validates that a path follows GitLab release asset patterns
|
||||
func isValidGitLabReleaseURL(path string) bool {
|
||||
// GitLab release URL patterns:
|
||||
// 1. /owner/repo/-/releases/tag/downloads/filename
|
||||
// 2. /owner/repo/-/package_files/id/download
|
||||
// 3. /group/subgroup/repo/-/releases/tag/downloads/filename (nested groups)
|
||||
|
||||
// The key insight is that GitLab URLs have "/-/" as a delimiter that separates the
|
||||
// project path from the GitLab-specific routes. Everything before "/-/" is the project path.
|
||||
|
||||
// Pattern 1: Release downloads with /-/releases/tag/downloads/filename
|
||||
releasePattern := `^/([a-zA-Z0-9._\-]+(?:/[a-zA-Z0-9._\-]+)*)/-/releases/([^/]+)/downloads/([^/]+)$`
|
||||
if matched, _ := regexp.MatchString(releasePattern, path); matched {
|
||||
return true
|
||||
}
|
||||
|
||||
// Pattern 2: Package files with /-/package_files/id/download
|
||||
packagePattern := `^/([a-zA-Z0-9._\-]+(?:/[a-zA-Z0-9._\-]+)*)/-/package_files/([0-9]+)/download$`
|
||||
if matched, _ := regexp.MatchString(packagePattern, path); matched {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// inferMCPBRegistryBaseURL infers the registry base URL from an MCPB identifier
|
||||
func inferMCPBRegistryBaseURL(identifier string) (string, error) {
|
||||
parsedURL, err := url.Parse(identifier)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
host := strings.ToLower(parsedURL.Host)
|
||||
switch host {
|
||||
case "github.com", "www.github.com":
|
||||
return model.RegistryURLGitHub, nil
|
||||
case "gitlab.com", "www.gitlab.com":
|
||||
return model.RegistryURLGitLab, nil
|
||||
default:
|
||||
return "", fmt.Errorf("invalid host for MCPB package: %s, expected github or gitlab", host)
|
||||
}
|
||||
}
|
||||
81
internal/validators/registries/mcpb_test.go
Normal file
81
internal/validators/registries/mcpb_test.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package registries_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/validators/registries"
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateMCPB(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
packageName string
|
||||
serverName string
|
||||
expectError bool
|
||||
errorMessage string
|
||||
}{
|
||||
{
|
||||
name: "valid MCPB package should pass",
|
||||
packageName: "https://github.com/domdomegg/airtable-mcp-server/releases/download/v1.7.2/airtable-mcp-server.mcpb",
|
||||
serverName: "io.github.domdomegg/airtable-mcp-server",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "valid MCPB package should pass",
|
||||
packageName: "https://github.com/microsoft/playwright-mcp/releases/download/v0.0.36/playwright-mcp-extension-v0.0.36.zip",
|
||||
serverName: "com.microsoft/playwright-mcp",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "valid MCPB package with .mcpb extension should fail accessibility check",
|
||||
packageName: "https://github.com/example/server/releases/download/v1.0.0/server.mcpb",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "not publicly accessible",
|
||||
},
|
||||
{
|
||||
name: "invalid URL without mcp anywhere should fail",
|
||||
packageName: "https://github.com/example/server/releases/download/v1.0.0/server.tar.gz",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "URL must contain 'mcp'",
|
||||
},
|
||||
{
|
||||
name: "invalid URL format should fail",
|
||||
packageName: "not://a valid url for mcpb!",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "invalid MCPB package URL",
|
||||
},
|
||||
{
|
||||
name: "non-existent file should fail accessibility check",
|
||||
packageName: "https://github.com/nonexistent/repo/releases/download/v1.0.0/mcp-server.tar.gz",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "not publicly accessible",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
pkg := model.Package{
|
||||
RegistryType: model.RegistryTypeMCPB,
|
||||
Identifier: tt.packageName,
|
||||
}
|
||||
|
||||
err := registries.ValidateMCPB(ctx, pkg, tt.serverName)
|
||||
|
||||
if tt.expectError {
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tt.errorMessage)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
66
internal/validators/registries/npm.go
Normal file
66
internal/validators/registries/npm.go
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package registries
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
)
|
||||
|
||||
// NPMPackageResponse represents the structure returned by the NPM registry API
|
||||
type NPMPackageResponse struct {
|
||||
MCPName string `json:"mcpName"`
|
||||
}
|
||||
|
||||
// ValidateNPM validates that an NPM package contains the correct MCP server name
|
||||
func ValidateNPM(ctx context.Context, pkg model.Package, serverName string) error {
|
||||
// Set default registry base URL if empty
|
||||
if pkg.RegistryBaseURL == "" {
|
||||
pkg.RegistryBaseURL = model.RegistryURLNPM
|
||||
}
|
||||
|
||||
// Validate that the registry base URL matches NPM exactly
|
||||
if pkg.RegistryBaseURL != model.RegistryURLNPM {
|
||||
return fmt.Errorf("registry type and base URL do not match: '%s' is not valid for registry type '%s'. Expected: %s",
|
||||
pkg.RegistryBaseURL, model.RegistryTypeNPM, model.RegistryURLNPM)
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
url := pkg.RegistryBaseURL + "/" + pkg.Identifier + "/" + pkg.Version
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "MCP-Registry-Validator/1.0")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch package metadata from NPM: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("NPM package '%s' not found (status: %d)", pkg.Identifier, resp.StatusCode)
|
||||
}
|
||||
|
||||
var npmResp NPMPackageResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&npmResp); err != nil {
|
||||
return fmt.Errorf("failed to parse NPM package metadata: %w", err)
|
||||
}
|
||||
|
||||
if npmResp.MCPName == "" {
|
||||
return fmt.Errorf("NPM package '%s' is missing required 'mcpName' field. Add this to your package.json: \"mcpName\": \"%s\"", pkg.Identifier, serverName)
|
||||
}
|
||||
|
||||
if npmResp.MCPName != serverName {
|
||||
return fmt.Errorf("NPM package ownership validation failed. Expected mcpName '%s', got '%s'", serverName, npmResp.MCPName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
90
internal/validators/registries/npm_test.go
Normal file
90
internal/validators/registries/npm_test.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
package registries_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/validators/registries"
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateNPM_RealPackages(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
packageName string
|
||||
version string
|
||||
serverName string
|
||||
expectError bool
|
||||
errorMessage string
|
||||
}{
|
||||
{
|
||||
name: "non-existent package should fail",
|
||||
packageName: generateRandomPackageName(),
|
||||
version: "1.0.0",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "not found",
|
||||
},
|
||||
{
|
||||
name: "real package without mcpName should fail",
|
||||
packageName: "express", // Popular package without mcpName field
|
||||
version: "4.18.2",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "missing required 'mcpName' field",
|
||||
},
|
||||
{
|
||||
name: "real package without mcpName should fail",
|
||||
packageName: "lodash", // Another popular package
|
||||
version: "4.17.21",
|
||||
serverName: "com.example/completely-different-name",
|
||||
expectError: true,
|
||||
errorMessage: "missing required 'mcpName' field",
|
||||
},
|
||||
{
|
||||
name: "real package without mcpName should fail",
|
||||
packageName: "airtable-mcp-server",
|
||||
version: "1.5.0",
|
||||
serverName: "io.github.domdomegg/airtable-mcp-server",
|
||||
expectError: true,
|
||||
errorMessage: "missing required 'mcpName' field",
|
||||
},
|
||||
{
|
||||
name: "real package with incorrect mcpName should fail",
|
||||
packageName: "airtable-mcp-server",
|
||||
version: "1.7.2",
|
||||
serverName: "io.github.not-domdomegg/airtable-mcp-server",
|
||||
expectError: true,
|
||||
errorMessage: "Expected mcpName 'io.github.not-domdomegg/airtable-mcp-server', got 'io.github.domdomegg/airtable-mcp-server'",
|
||||
},
|
||||
{
|
||||
name: "real package with correct mcpName should pass",
|
||||
packageName: "airtable-mcp-server",
|
||||
version: "1.7.2",
|
||||
serverName: "io.github.domdomegg/airtable-mcp-server",
|
||||
expectError: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
pkg := model.Package{
|
||||
RegistryType: model.RegistryTypeNPM,
|
||||
Identifier: tt.packageName,
|
||||
Version: tt.version,
|
||||
}
|
||||
|
||||
err := registries.ValidateNPM(ctx, pkg, tt.serverName)
|
||||
|
||||
if tt.expectError {
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tt.errorMessage)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
67
internal/validators/registries/nuget.go
Normal file
67
internal/validators/registries/nuget.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package registries
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
)
|
||||
|
||||
// ValidateNuGet validates that a NuGet package contains the correct MCP server name
|
||||
func ValidateNuGet(ctx context.Context, pkg model.Package, serverName string) error {
|
||||
// Set default registry base URL if empty
|
||||
if pkg.RegistryBaseURL == "" {
|
||||
pkg.RegistryBaseURL = model.RegistryURLNuGet
|
||||
}
|
||||
|
||||
// Validate that the registry base URL matches NuGet exactly
|
||||
if pkg.RegistryBaseURL != model.RegistryURLNuGet {
|
||||
return fmt.Errorf("registry type and base URL do not match: '%s' is not valid for registry type '%s'. Expected: %s",
|
||||
pkg.RegistryBaseURL, model.RegistryTypeNuGet, model.RegistryURLNuGet)
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
lowerID := strings.ToLower(pkg.Identifier)
|
||||
lowerVersion := strings.ToLower(pkg.Version)
|
||||
if lowerVersion == "" {
|
||||
return fmt.Errorf("NuGet package validation requires a specific version, but none was provided")
|
||||
}
|
||||
|
||||
// Try to get README from the package
|
||||
readmeURL := fmt.Sprintf("%s/v3-flatcontainer/%s/%s/readme", pkg.RegistryBaseURL, lowerID, lowerVersion)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, readmeURL, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "MCP-Registry-Validator/1.0")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch README from NuGet: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
// Check README content
|
||||
readmeBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read README content: %w", err)
|
||||
}
|
||||
|
||||
readmeContent := string(readmeBytes)
|
||||
|
||||
// Check for mcp-name: format (more specific)
|
||||
mcpNamePattern := "mcp-name: " + serverName
|
||||
if strings.Contains(readmeContent, mcpNamePattern) {
|
||||
return nil // Found as mcp-name: format
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("NuGet package '%s' ownership validation failed. The server name '%s' must appear as 'mcp-name: %s' in the package README. Add it to your package README", pkg.Identifier, serverName, serverName)
|
||||
}
|
||||
90
internal/validators/registries/nuget_test.go
Normal file
90
internal/validators/registries/nuget_test.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
package registries_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/validators/registries"
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateNuGet_RealPackages(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
packageName string
|
||||
version string
|
||||
serverName string
|
||||
expectError bool
|
||||
errorMessage string
|
||||
}{
|
||||
{
|
||||
name: "non-existent package should fail",
|
||||
packageName: generateRandomNuGetPackageName(),
|
||||
version: "1.0.0",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "ownership validation failed",
|
||||
},
|
||||
{
|
||||
name: "real package without version should fail",
|
||||
packageName: "Newtonsoft.Json",
|
||||
version: "", // No version provided
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "requires a specific version",
|
||||
},
|
||||
{
|
||||
name: "real package with non-existent version should fail",
|
||||
packageName: "Newtonsoft.Json",
|
||||
version: "999.999.999", // Version that doesn't exist
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "ownership validation failed",
|
||||
},
|
||||
{
|
||||
name: "real package without server name in README should fail",
|
||||
packageName: "Newtonsoft.Json",
|
||||
version: "13.0.3", // Popular version
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "ownership validation failed",
|
||||
},
|
||||
{
|
||||
name: "real package without server name in README should fail",
|
||||
packageName: "TimeMcpServer",
|
||||
version: "1.0.0",
|
||||
serverName: "io.github.domdomegg/time-mcp-server",
|
||||
expectError: true,
|
||||
errorMessage: "ownership validation failed",
|
||||
},
|
||||
{
|
||||
name: "real package with server name in README should pass",
|
||||
packageName: "TimeMcpServer",
|
||||
version: "1.0.2",
|
||||
serverName: "io.github.domdomegg/time-mcp-server",
|
||||
expectError: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
pkg := model.Package{
|
||||
RegistryType: model.RegistryTypeNuGet,
|
||||
Identifier: tt.packageName,
|
||||
Version: tt.version,
|
||||
}
|
||||
|
||||
err := registries.ValidateNuGet(ctx, pkg, tt.serverName)
|
||||
|
||||
if tt.expectError {
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tt.errorMessage)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
260
internal/validators/registries/oci.go
Normal file
260
internal/validators/registries/oci.go
Normal file
|
|
@ -0,0 +1,260 @@
|
|||
package registries
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
)
|
||||
|
||||
const (
|
||||
dockerIoAPIBaseURL = "https://registry-1.docker.io"
|
||||
)
|
||||
|
||||
// OCIAuthResponse represents the Docker Hub authentication response
|
||||
type OCIAuthResponse struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// OCIManifest represents an OCI image manifest
|
||||
type OCIManifest struct {
|
||||
Manifests []struct {
|
||||
Digest string `json:"digest"`
|
||||
} `json:"manifests,omitempty"`
|
||||
Config struct {
|
||||
Digest string `json:"digest"`
|
||||
} `json:"config,omitempty"`
|
||||
}
|
||||
|
||||
// OCIImageConfig represents an OCI image configuration
|
||||
type OCIImageConfig struct {
|
||||
Config struct {
|
||||
Labels map[string]string `json:"Labels"`
|
||||
} `json:"config"`
|
||||
}
|
||||
|
||||
// ValidateOCI validates that an OCI image contains the correct MCP server name annotation
|
||||
func ValidateOCI(ctx context.Context, pkg model.Package, serverName string) error {
|
||||
// Set default registry base URL if empty
|
||||
if pkg.RegistryBaseURL == "" {
|
||||
pkg.RegistryBaseURL = model.RegistryURLDocker
|
||||
}
|
||||
|
||||
// Validate that the registry base URL matches OCI/Docker exactly
|
||||
if pkg.RegistryBaseURL != model.RegistryURLDocker {
|
||||
return fmt.Errorf("registry type and base URL do not match: '%s' is not valid for registry type '%s'. Expected: %s",
|
||||
pkg.RegistryBaseURL, model.RegistryTypeOCI, model.RegistryURLDocker)
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
// Parse image reference (namespace/repo or repo)
|
||||
namespace, repo, err := parseImageReference(pkg.Identifier)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid OCI image reference: %w", err)
|
||||
}
|
||||
|
||||
apiBaseURL := pkg.RegistryBaseURL
|
||||
if pkg.RegistryBaseURL == model.RegistryURLDocker {
|
||||
// docker.io is an exceptional registry that was created before standardisation, so needs a custom API base url
|
||||
// https://github.com/containers/image/blob/5e4845dddd57598eb7afeaa6e0f4c76531bd3c91/docker/docker_client.go#L225-L229
|
||||
apiBaseURL = dockerIoAPIBaseURL
|
||||
}
|
||||
|
||||
tag := pkg.Version
|
||||
manifestURL := fmt.Sprintf("%s/v2/%s/%s/manifests/%s", apiBaseURL, namespace, repo, tag)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, manifestURL, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create manifest request: %w", err)
|
||||
}
|
||||
|
||||
// Get auth token for docker.io
|
||||
// We only support auth for docker.io, other registries must allow unauthed requests
|
||||
if apiBaseURL == dockerIoAPIBaseURL {
|
||||
token, err := getDockerIoAuthToken(ctx, client, namespace, repo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to authenticate with Docker registry: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/vnd.docker.distribution.manifest.v2+json,application/vnd.oci.image.manifest.v1+json")
|
||||
req.Header.Set("User-Agent", "MCP-Registry-Validator/1.0")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch OCI manifest: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusUnauthorized {
|
||||
return fmt.Errorf("OCI image '%s/%s:%s' not found (status: %d)", namespace, repo, tag, resp.StatusCode)
|
||||
}
|
||||
if resp.StatusCode == http.StatusTooManyRequests {
|
||||
// Rate limited, skip validation for now
|
||||
log.Printf("Warning: Rate limited when accessing OCI image '%s/%s:%s'. Skipping validation.", namespace, repo, tag)
|
||||
return nil
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("failed to fetch OCI manifest (status: %d)", resp.StatusCode)
|
||||
}
|
||||
|
||||
var manifest OCIManifest
|
||||
if err := json.NewDecoder(resp.Body).Decode(&manifest); err != nil {
|
||||
return fmt.Errorf("failed to parse OCI manifest: %w", err)
|
||||
}
|
||||
|
||||
// Handle multi-arch images by using first manifest
|
||||
var configDigest string
|
||||
if len(manifest.Manifests) > 0 {
|
||||
// This is a multi-arch image, get the specific manifest
|
||||
specificManifest, err := getSpecificManifest(ctx, client, apiBaseURL, namespace, repo, manifest.Manifests[0].Digest)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get specific manifest: %w", err)
|
||||
}
|
||||
configDigest = specificManifest.Config.Digest
|
||||
} else {
|
||||
configDigest = manifest.Config.Digest
|
||||
}
|
||||
|
||||
if configDigest == "" {
|
||||
return fmt.Errorf("unable to determine image config digest for '%s/%s:%s'", namespace, repo, tag)
|
||||
}
|
||||
|
||||
// Get image config (contains labels)
|
||||
config, err := getImageConfig(ctx, client, apiBaseURL, namespace, repo, configDigest)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get image config: %w", err)
|
||||
}
|
||||
|
||||
mcpName, exists := config.Config.Labels["io.modelcontextprotocol.server.name"]
|
||||
if !exists {
|
||||
return fmt.Errorf("OCI image '%s/%s:%s' is missing required annotation. Add this to your Dockerfile: LABEL io.modelcontextprotocol.server.name=\"%s\"", namespace, repo, tag, serverName)
|
||||
}
|
||||
|
||||
if mcpName != serverName {
|
||||
return fmt.Errorf("OCI image ownership validation failed. Expected annotation 'io.modelcontextprotocol.server.name' = '%s', got '%s'", serverName, mcpName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseImageReference(identifier string) (string, string, error) {
|
||||
parts := strings.Split(identifier, "/")
|
||||
switch len(parts) {
|
||||
case 2:
|
||||
return parts[0], parts[1], nil
|
||||
case 1:
|
||||
return "library", parts[0], nil
|
||||
default:
|
||||
return "", "", fmt.Errorf("invalid image reference: %s", identifier)
|
||||
}
|
||||
}
|
||||
|
||||
// getDockerIoAuthToken retrieves an authentication token from Docker Hub
|
||||
func getDockerIoAuthToken(ctx context.Context, client *http.Client, namespace, repo string) (string, error) {
|
||||
authURL := fmt.Sprintf("https://auth.docker.io/token?service=registry.docker.io&scope=repository:%s/%s:pull", namespace, repo)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, authURL, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create auth request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to request auth token: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("auth request failed with status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var authResp OCIAuthResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&authResp); err != nil {
|
||||
return "", fmt.Errorf("failed to parse auth response: %w", err)
|
||||
}
|
||||
|
||||
return authResp.Token, nil
|
||||
}
|
||||
|
||||
// getSpecificManifest retrieves a specific manifest for multi-arch images
|
||||
func getSpecificManifest(ctx context.Context, client *http.Client, apiBaseURL, namespace, repo, digest string) (*OCIManifest, error) {
|
||||
manifestURL := fmt.Sprintf("%s/v2/%s/%s/manifests/%s", apiBaseURL, namespace, repo, digest)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, manifestURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create specific manifest request: %w", err)
|
||||
}
|
||||
|
||||
// Get auth token for docker.io
|
||||
if apiBaseURL == dockerIoAPIBaseURL {
|
||||
token, err := getDockerIoAuthToken(ctx, client, namespace, repo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to authenticate with Docker registry: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/vnd.oci.image.manifest.v1+json")
|
||||
req.Header.Set("User-Agent", "MCP-Registry-Validator/1.0")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch specific manifest: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("specific manifest not found (status: %d)", resp.StatusCode)
|
||||
}
|
||||
|
||||
var manifest OCIManifest
|
||||
if err := json.NewDecoder(resp.Body).Decode(&manifest); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse specific manifest: %w", err)
|
||||
}
|
||||
|
||||
return &manifest, nil
|
||||
}
|
||||
|
||||
// getImageConfig retrieves the image configuration containing labels
|
||||
func getImageConfig(ctx context.Context, client *http.Client, apiBaseURL, namespace, repo, configDigest string) (*OCIImageConfig, error) {
|
||||
configURL := fmt.Sprintf("%s/v2/%s/%s/blobs/%s", apiBaseURL, namespace, repo, configDigest)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, configURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create config request: %w", err)
|
||||
}
|
||||
|
||||
// Get auth token for docker.io
|
||||
if apiBaseURL == dockerIoAPIBaseURL {
|
||||
token, err := getDockerIoAuthToken(ctx, client, namespace, repo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to authenticate with Docker registry: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/vnd.docker.distribution.manifest.v2+json")
|
||||
req.Header.Set("User-Agent", "MCP-Registry-Validator/1.0")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch image config: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("image config not found (status: %d)", resp.StatusCode)
|
||||
}
|
||||
|
||||
var config OCIImageConfig
|
||||
if err := json.NewDecoder(resp.Body).Decode(&config); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse image config: %w", err)
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
84
internal/validators/registries/oci_test.go
Normal file
84
internal/validators/registries/oci_test.go
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
package registries_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/validators/registries"
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateOCI_RealPackages(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
packageName string
|
||||
version string
|
||||
serverName string
|
||||
expectError bool
|
||||
errorMessage string
|
||||
}{
|
||||
{
|
||||
name: "non-existent image should fail",
|
||||
packageName: generateRandomImageName(),
|
||||
version: "latest",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "not found",
|
||||
},
|
||||
{
|
||||
name: "real image without MCP annotation should fail",
|
||||
packageName: "nginx", // Popular image without MCP annotation
|
||||
version: "latest",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "missing required annotation",
|
||||
},
|
||||
{
|
||||
name: "real image with specific tag without MCP annotation should fail",
|
||||
packageName: "redis",
|
||||
version: "7-alpine", // Specific tag
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "missing required annotation",
|
||||
},
|
||||
{
|
||||
name: "namespaced image without MCP annotation should fail",
|
||||
packageName: "hello-world", // Simple image for testing
|
||||
version: "latest",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "missing required annotation",
|
||||
},
|
||||
{
|
||||
name: "real image with correct MCP annotation should pass",
|
||||
packageName: "domdomegg/airtable-mcp-server",
|
||||
version: "1.7.2",
|
||||
serverName: "io.github.domdomegg/airtable-mcp-server", // This should match the annotation
|
||||
expectError: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Skip("Skipping OCI registry tests because we keep hitting DockerHub rate limits")
|
||||
|
||||
pkg := model.Package{
|
||||
RegistryType: model.RegistryTypeOCI,
|
||||
Identifier: tt.packageName,
|
||||
Version: tt.version,
|
||||
}
|
||||
|
||||
err := registries.ValidateOCI(ctx, pkg, tt.serverName)
|
||||
|
||||
if tt.expectError {
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tt.errorMessage)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
70
internal/validators/registries/pypi.go
Normal file
70
internal/validators/registries/pypi.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package registries
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
)
|
||||
|
||||
// PyPIPackageResponse represents the structure returned by the PyPI JSON API
|
||||
type PyPIPackageResponse struct {
|
||||
Info struct {
|
||||
Description string `json:"description"`
|
||||
} `json:"info"`
|
||||
}
|
||||
|
||||
// ValidatePyPI validates that a PyPI package contains the correct MCP server name
|
||||
func ValidatePyPI(ctx context.Context, pkg model.Package, serverName string) error {
|
||||
// Set default registry base URL if empty
|
||||
if pkg.RegistryBaseURL == "" {
|
||||
pkg.RegistryBaseURL = model.RegistryURLPyPI
|
||||
}
|
||||
|
||||
// Validate that the registry base URL matches PyPI exactly
|
||||
if pkg.RegistryBaseURL != model.RegistryURLPyPI {
|
||||
return fmt.Errorf("registry type and base URL do not match: '%s' is not valid for registry type '%s'. Expected: %s",
|
||||
pkg.RegistryBaseURL, model.RegistryTypePyPI, model.RegistryURLPyPI)
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
url := fmt.Sprintf("%s/pypi/%s/json", pkg.RegistryBaseURL, pkg.Identifier)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "MCP-Registry-Validator/1.0")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch package metadata from PyPI: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("PyPI package '%s' not found (status: %d)", pkg.Identifier, resp.StatusCode)
|
||||
}
|
||||
|
||||
var pypiResp PyPIPackageResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&pypiResp); err != nil {
|
||||
return fmt.Errorf("failed to parse PyPI package metadata: %w", err)
|
||||
}
|
||||
|
||||
// Check description (README) content
|
||||
description := pypiResp.Info.Description
|
||||
|
||||
// Check for mcp-name: format (more specific)
|
||||
mcpNamePattern := "mcp-name: " + serverName
|
||||
if strings.Contains(description, mcpNamePattern) {
|
||||
return nil // Found as mcp-name: format
|
||||
}
|
||||
|
||||
return fmt.Errorf("PyPI package '%s' ownership validation failed. The server name '%s' must appear as 'mcp-name: %s' in the package README", pkg.Identifier, serverName, serverName)
|
||||
}
|
||||
74
internal/validators/registries/pypi_test.go
Normal file
74
internal/validators/registries/pypi_test.go
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
package registries_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/validators/registries"
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidatePyPI_RealPackages(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
packageName string
|
||||
version string
|
||||
serverName string
|
||||
expectError bool
|
||||
errorMessage string
|
||||
}{
|
||||
{
|
||||
name: "non-existent package should fail",
|
||||
packageName: generateRandomPackageName(),
|
||||
version: "1.0.0",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "not found",
|
||||
},
|
||||
{
|
||||
name: "real package without MCP server name should fail",
|
||||
packageName: "requests", // Popular package without MCP server name in keywords/description/URLs
|
||||
version: "2.31.0",
|
||||
serverName: "com.example/test",
|
||||
expectError: true,
|
||||
errorMessage: "ownership validation failed",
|
||||
},
|
||||
{
|
||||
name: "real package with different server name should fail",
|
||||
packageName: "numpy", // Another popular package
|
||||
version: "1.25.2",
|
||||
serverName: "com.example/completely-different-name",
|
||||
expectError: true,
|
||||
errorMessage: "ownership validation failed", // Will fail because numpy doesn't have this server name
|
||||
},
|
||||
{
|
||||
name: "real package with server name in README should pass",
|
||||
packageName: "time-mcp-pypi",
|
||||
version: "1.0.0",
|
||||
serverName: "io.github.domdomegg/time-mcp-pypi",
|
||||
expectError: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
pkg := model.Package{
|
||||
RegistryType: model.RegistryTypePyPI,
|
||||
Identifier: tt.packageName,
|
||||
Version: tt.version,
|
||||
}
|
||||
|
||||
err := registries.ValidatePyPI(ctx, pkg, tt.serverName)
|
||||
|
||||
if tt.expectError {
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tt.errorMessage)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
32
internal/validators/registries/testutils_test.go
Normal file
32
internal/validators/registries/testutils_test.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package registries_test
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func generateRandomPackageName() string {
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
// Fallback to a static name if crypto/rand fails
|
||||
return "nonexistent-pkg-fallback"
|
||||
}
|
||||
return fmt.Sprintf("nonexistent-pkg-%s", hex.EncodeToString(bytes))
|
||||
}
|
||||
|
||||
func generateRandomNuGetPackageName() string {
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "NonExistent.Package.Fallback"
|
||||
}
|
||||
return fmt.Sprintf("NonExistent.Package.%s", hex.EncodeToString(bytes)[:16])
|
||||
}
|
||||
|
||||
func generateRandomImageName() string {
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "nonexistent-image-fallback"
|
||||
}
|
||||
return fmt.Sprintf("nonexistent-image-%s", hex.EncodeToString(bytes)[:16])
|
||||
}
|
||||
Loading…
Reference in a new issue