Deduplicate server JSON structs between publisher and registry (#318)

<!-- Provide a brief summary of your changes -->

## Motivation and Context
<!-- Why is this change needed? What problem does it solve? -->

The following PR eliminates duplicate struct definitions by migrating
the publisher tool to use canonical types from pkg/model, creating a
single source of truth across the codebase. The publisher tool
previously had its own copies of structs that were duplicated, ie.
ServerJSON, Package, Repository, VersionDetail, EnvironmentVariable,
RuntimeArgument and a few others which was not optimal


**Changes in the PR:**
- Refactored the structs to single canonical types located at pkg/model
- Refactor the implementation to use/build canonical structures directly
  - Remove all duplicate definitions (5 structs eliminated)
- Publisher now uses model.ServerJSON, model.Package, model.Repository,
etc.
- Renamed some of the structs to better reflect their use case, i.e.
ServerJSON instead of ServerDetail
- Fixes an issue where the Remote property was missing from the previous
publisher copy of the Server

**Benefits:**
  - Single source of truth - Schema changes in one place
  - Type safety - Compiler catches mismatches
  - Enhanced validation - Field constraints and bson tags
  - Zero breaking changes - Same CLI interface and JSON output
  - Future-proof - New canonical fields automatically available

Supersedes: #217 and #240

## 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 <radoslav@stacklok.com>
This commit is contained in:
Radoslav Dimitrov 2025-08-28 16:58:15 +03:00 • committed by GitHub
commit 099a2d702e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 1280 additions and 1408 deletions

View file

@ -1,9 +1,14 @@
package validators
import (
"encoding/json"
"fmt"
"net/url"
"slices"
"strings"
"github.com/modelcontextprotocol/registry/internal/model"
apiv1 "github.com/modelcontextprotocol/registry/pkg/api/v1"
"github.com/modelcontextprotocol/registry/pkg/model"
)
// ServerValidator validates server details
@ -12,7 +17,7 @@ type ServerValidator struct {
}
// Validate checks if the server details are valid
func (v *ServerValidator) Validate(obj *model.ServerDetail) error {
func (v *ServerValidator) Validate(obj *model.ServerJSON) error {
if err := v.RepositoryValidator.Validate(&obj.Repository); err != nil {
return err
}
@ -104,7 +109,7 @@ func NewObjectValidator() *ObjectValidator {
}
}
func (ov *ObjectValidator) Validate(obj *model.ServerDetail) error {
func (ov *ObjectValidator) Validate(obj *model.ServerJSON) error {
if err := ov.ServerValidator.Validate(obj); err != nil {
return err
}
@ -122,3 +127,163 @@ func (ov *ObjectValidator) Validate(obj *model.ServerDetail) error {
}
return nil
}
// ValidatePublisherExtensions validates that publisher extensions are within size limits
func ValidatePublisherExtensions(req apiv1.PublishRequest) error {
const maxExtensionSize = 4 * 1024 // 4KB limit
// Check size limit for x-publisher extension
if req.XPublisher != nil {
extensionsJSON, err := json.Marshal(req.XPublisher)
if err != nil {
return fmt.Errorf("failed to marshal x-publisher extension: %w", err)
}
if len(extensionsJSON) > maxExtensionSize {
return fmt.Errorf("x-publisher extension exceeds 4KB limit (%d bytes)", len(extensionsJSON))
}
}
return nil
}
// ValidatePublishRequestExtensions validates that only allowed extension fields are present
func ValidatePublishRequestExtensions(requestData []byte) error {
// Parse the raw JSON to check for unknown fields
var rawRequest map[string]interface{}
if err := json.Unmarshal(requestData, &rawRequest); err != nil {
return fmt.Errorf("failed to parse request JSON: %w", err)
}
// Define allowed top-level fields
allowedFields := map[string]bool{
"server": true,
"x-publisher": true,
}
// Check for any disallowed fields
var invalidFields []string
for field := range rawRequest {
if !allowedFields[field] {
invalidFields = append(invalidFields, field)
}
}
if len(invalidFields) > 0 {
return fmt.Errorf("invalid extension fields: %v. Only 'server' and 'x-publisher' fields are allowed", invalidFields)
}
return nil
}
// ExtractPublisherExtensions extracts publisher extensions from a apiv1.PublishRequest
func ExtractPublisherExtensions(req apiv1.PublishRequest) map[string]interface{} {
publisherExtensions := make(map[string]interface{})
if req.XPublisher != nil {
// Copy fields directly, avoiding double nesting
for k, v := range req.XPublisher {
publisherExtensions[k] = v
}
}
return publisherExtensions
}
// ParseServerName extracts the server name from a model.ServerJSON for validation purposes
func ParseServerName(serverDetail model.ServerJSON) (string, error) {
name := serverDetail.Name
if name == "" {
return "", fmt.Errorf("server name is required and must be a string")
}
// Validate format: dns-namespace/name
if !strings.Contains(name, "/") {
return "", fmt.Errorf("server name must be in format 'dns-namespace/name' (e.g., 'com.example.api/server')")
}
parts := strings.SplitN(name, "/", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", fmt.Errorf("server name must be in format 'dns-namespace/name' with non-empty namespace and name parts")
}
return name, nil
}
// ValidateRemoteNamespaceMatch validates that remote URLs match the reverse-DNS namespace
func ValidateRemoteNamespaceMatch(serverDetail model.ServerJSON) error {
namespace := serverDetail.Name
for _, remote := range serverDetail.Remotes {
if err := validateRemoteURLMatchesNamespace(remote.URL, namespace); err != nil {
return fmt.Errorf("remote URL %s does not match namespace %s: %w", remote.URL, namespace, err)
}
}
return nil
}
// validateRemoteURLMatchesNamespace checks if a remote URL's hostname matches the publisher domain from the namespace
func validateRemoteURLMatchesNamespace(remoteURL, namespace string) error {
// Parse the URL to extract the hostname
parsedURL, err := url.Parse(remoteURL)
if err != nil {
return fmt.Errorf("invalid URL format: %w", err)
}
hostname := parsedURL.Hostname()
if hostname == "" {
return fmt.Errorf("URL must have a valid hostname")
}
// Skip validation for localhost and local development URLs
if hostname == "localhost" || strings.HasSuffix(hostname, ".localhost") || hostname == "127.0.0.1" {
return nil
}
// Extract publisher domain from reverse-DNS namespace
publisherDomain := extractPublisherDomainFromNamespace(namespace)
if publisherDomain == "" {
return fmt.Errorf("invalid namespace format: cannot extract domain from %s", namespace)
}
// Check if the remote URL hostname matches the publisher domain or is a subdomain
if !isValidHostForDomain(hostname, publisherDomain) {
return fmt.Errorf("remote URL host %s does not match publisher domain %s", hostname, publisherDomain)
}
return nil
}
// extractPublisherDomainFromNamespace converts reverse-DNS namespace to normal domain format
// e.g., "com.example" -> "example.com"
func extractPublisherDomainFromNamespace(namespace string) string {
// Extract the namespace part before the first slash
namespacePart := namespace
if slashIdx := strings.Index(namespace, "/"); slashIdx != -1 {
namespacePart = namespace[:slashIdx]
}
// Split into parts and reverse them to get normal domain format
parts := strings.Split(namespacePart, ".")
if len(parts) < 2 {
return ""
}
// Reverse the parts to convert from reverse-DNS to normal domain
slices.Reverse(parts)
return strings.Join(parts, ".")
}
// isValidHostForDomain checks if a hostname is the domain or a subdomain of the publisher domain
func isValidHostForDomain(hostname, publisherDomain string) bool {
// Exact match
if hostname == publisherDomain {
return true
}
// Subdomain match - hostname should end with "." + publisherDomain
if strings.HasSuffix(hostname, "."+publisherDomain) {
return true
}
return false
}

View file

@ -1,11 +1,15 @@
package validators_test
import (
"encoding/json"
"testing"
"time"
"github.com/modelcontextprotocol/registry/internal/model"
"github.com/modelcontextprotocol/registry/internal/validators"
apiv1 "github.com/modelcontextprotocol/registry/pkg/api/v1"
"github.com/modelcontextprotocol/registry/pkg/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestObjectValidator_Validate(t *testing.T) {
@ -13,12 +17,12 @@ func TestObjectValidator_Validate(t *testing.T) {
tests := []struct {
name string
serverDetail model.ServerDetail
serverDetail model.ServerJSON
expectedError string
}{
{
name: "valid server detail with all fields",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -46,7 +50,7 @@ func TestObjectValidator_Validate(t *testing.T) {
},
{
name: "server with invalid repository source",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -61,7 +65,7 @@ func TestObjectValidator_Validate(t *testing.T) {
},
{
name: "server with invalid GitHub URL format",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -76,7 +80,7 @@ func TestObjectValidator_Validate(t *testing.T) {
},
{
name: "server with invalid GitLab URL format",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -91,7 +95,7 @@ func TestObjectValidator_Validate(t *testing.T) {
},
{
name: "package with spaces in name",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -113,7 +117,7 @@ func TestObjectValidator_Validate(t *testing.T) {
},
{
name: "multiple packages with one invalid",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -140,7 +144,7 @@ func TestObjectValidator_Validate(t *testing.T) {
},
{
name: "remote with invalid URL",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -160,7 +164,7 @@ func TestObjectValidator_Validate(t *testing.T) {
},
{
name: "remote with missing scheme",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -180,7 +184,7 @@ func TestObjectValidator_Validate(t *testing.T) {
},
{
name: "multiple remotes with one invalid",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -203,7 +207,7 @@ func TestObjectValidator_Validate(t *testing.T) {
},
{
name: "server detail with nil packages and remotes",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -220,7 +224,7 @@ func TestObjectValidator_Validate(t *testing.T) {
},
{
name: "server detail with empty packages and remotes slices",
serverDetail: model.ServerDetail{
serverDetail: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
@ -250,3 +254,433 @@ func TestObjectValidator_Validate(t *testing.T) {
})
}
}
func TestExtractPublisherExtensions(t *testing.T) {
tests := []struct {
name string
request apiv1.PublishRequest
expected map[string]interface{}
}{
{
name: "nil publisher extensions",
request: apiv1.PublishRequest{
Server: model.ServerJSON{Name: "test"},
},
expected: map[string]interface{}{},
},
{
name: "empty publisher extensions",
request: apiv1.PublishRequest{
Server: model.ServerJSON{Name: "test"},
XPublisher: map[string]interface{}{},
},
expected: map[string]interface{}{},
},
{
name: "simple publisher extensions",
request: apiv1.PublishRequest{
Server: model.ServerJSON{Name: "test"},
XPublisher: map[string]interface{}{
"build_info": map[string]interface{}{
"version": "1.2.3",
"commit": "abc123",
},
"publisher_metadata": "test-publisher",
},
},
expected: map[string]interface{}{
"build_info": map[string]interface{}{
"version": "1.2.3",
"commit": "abc123",
},
"publisher_metadata": "test-publisher",
},
},
{
name: "nested publisher extensions",
request: apiv1.PublishRequest{
Server: model.ServerJSON{Name: "test"},
XPublisher: map[string]interface{}{
"ci": map[string]interface{}{
"pipeline": map[string]interface{}{
"id": "12345",
"branch": "main",
},
"artifacts": []string{"binary", "docs"},
},
},
},
expected: map[string]interface{}{
"ci": map[string]interface{}{
"pipeline": map[string]interface{}{
"id": "12345",
"branch": "main",
},
"artifacts": []string{"binary", "docs"},
},
},
},
{
name: "nil publisher extensions (should be ignored)",
request: apiv1.PublishRequest{
Server: model.ServerJSON{Name: "test"},
XPublisher: nil,
},
expected: map[string]interface{}{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := validators.ExtractPublisherExtensions(tt.request)
assert.Equal(t, tt.expected, result)
})
}
}
func TestExtractPublisherExtensions_DoesNotDoubleNest(t *testing.T) {
// This test specifically verifies the bug we fixed - no double nesting
request := apiv1.PublishRequest{
Server: model.ServerJSON{Name: "test"},
XPublisher: map[string]interface{}{
"tool": "publisher-cli",
"version": "1.0.0",
},
}
result := validators.ExtractPublisherExtensions(request)
// Verify we get the data directly, not wrapped in another "x-publisher" key
assert.Equal(t, "publisher-cli", result["tool"])
assert.Equal(t, "1.0.0", result["version"])
// Verify we don't have double nesting
assert.NotContains(t, result, "x-publisher")
}
func TestServerResponse_JSONSerialization(t *testing.T) {
// Test that ServerResponse properly serializes to extension wrapper format
publishedTime := time.Date(2023, 12, 1, 10, 30, 0, 0, time.UTC)
response := apiv1.ServerRecord{
Server: model.ServerJSON{
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
URL: "https://github.com/test/server",
Source: "github",
ID: "test/server",
},
VersionDetail: model.VersionDetail{
Version: "1.0.0",
},
},
XIOModelContextProtocolRegistry: apiv1.RegistryExtensions{
ID: "registry-id-123",
PublishedAt: publishedTime,
IsLatest: true,
ReleaseDate: publishedTime.Format(time.RFC3339),
},
XPublisher: map[string]interface{}{
"build_tool": "ci-pipeline",
"commit": "abc123def",
},
}
// Serialize to JSON
jsonData, err := json.Marshal(response)
require.NoError(t, err)
// Parse back to verify structure
var parsed map[string]interface{}
err = json.Unmarshal(jsonData, &parsed)
require.NoError(t, err)
// Verify three-layer structure exists
assert.Contains(t, parsed, "server")
assert.Contains(t, parsed, "x-io.modelcontextprotocol.registry")
assert.Contains(t, parsed, "x-publisher")
// Verify server data
serverData := parsed["server"].(map[string]interface{})
assert.Equal(t, "test-server", serverData["name"])
// Verify registry metadata
registryData := parsed["x-io.modelcontextprotocol.registry"].(map[string]interface{})
assert.Equal(t, "registry-id-123", registryData["id"])
// Verify publisher extensions
publisherData := parsed["x-publisher"].(map[string]interface{})
assert.Equal(t, "ci-pipeline", publisherData["build_tool"])
assert.Equal(t, "abc123def", publisherData["commit"])
}
func TestPublishRequest_WithPublisherExtensions(t *testing.T) {
// Test that PublishRequest properly deserializes publisher extensions
jsonData := `{
"server": {
"name": "test-server",
"description": "Test server",
"version_detail": {
"version": "1.0.0"
}
},
"x-publisher": {
"tool": "publisher-cli",
"metadata": {
"build_date": "2023-12-01",
"commit": "abc123"
}
}
}`
var request apiv1.PublishRequest
err := json.Unmarshal([]byte(jsonData), &request)
require.NoError(t, err)
// Verify server data
assert.Equal(t, "test-server", request.Server.Name)
assert.Equal(t, "Test server", request.Server.Description)
// Verify publisher extensions
require.NotNil(t, request.XPublisher)
assert.Equal(t, "publisher-cli", request.XPublisher["tool"])
metadata := request.XPublisher["metadata"].(map[string]interface{})
assert.Equal(t, "2023-12-01", metadata["build_date"])
assert.Equal(t, "abc123", metadata["commit"])
}
func TestServerResponse_EmptyExtensions(t *testing.T) {
// Test behavior with empty/nil extensions
response := apiv1.ServerRecord{
Server: model.ServerJSON{
Name: "minimal-server",
Description: "Minimal test",
},
XIOModelContextProtocolRegistry: apiv1.RegistryExtensions{
ID: "min-id",
},
XPublisher: nil,
}
jsonData, err := json.Marshal(response)
require.NoError(t, err)
var parsed map[string]interface{}
err = json.Unmarshal(jsonData, &parsed)
require.NoError(t, err)
// Should still have the structure, even with nil publisher extensions
assert.Contains(t, parsed, "server")
assert.Contains(t, parsed, "x-io.modelcontextprotocol.registry")
// x-publisher should be null/nil in JSON when empty
publisherValue, exists := parsed["x-publisher"]
if exists {
assert.Nil(t, publisherValue)
}
}
func TestValidateRemoteNamespaceMatch(t *testing.T) {
tests := []struct {
name string
serverDetail model.ServerJSON
expectError bool
errorMsg string
}{
{
name: "valid match - example.com domain",
serverDetail: model.ServerJSON{
Name: "com.example/test-server",
Remotes: []model.Remote{
{URL: "https://example.com/mcp"},
},
},
expectError: false,
},
{
name: "valid match - subdomain mcp.example.com",
serverDetail: model.ServerJSON{
Name: "com.example/test-server",
Remotes: []model.Remote{
{URL: "https://mcp.example.com/endpoint"},
},
},
expectError: false,
},
{
name: "valid match - api subdomain",
serverDetail: model.ServerJSON{
Name: "com.example/api-server",
Remotes: []model.Remote{
{URL: "https://api.example.com/mcp"},
},
},
expectError: false,
},
{
name: "invalid - wrong domain",
serverDetail: model.ServerJSON{
Name: "com.example/test-server",
Remotes: []model.Remote{
{URL: "https://google.com/mcp"},
},
},
expectError: true,
errorMsg: "remote URL host google.com does not match publisher domain example.com",
},
{
name: "invalid - different domain entirely",
serverDetail: model.ServerJSON{
Name: "com.microsoft/server",
Remotes: []model.Remote{
{URL: "https://api.github.com/endpoint"},
},
},
expectError: true,
errorMsg: "remote URL host api.github.com does not match publisher domain microsoft.com",
},
{
name: "localhost URLs allowed with any namespace",
serverDetail: model.ServerJSON{
Name: "com.example/test-server",
Remotes: []model.Remote{
{URL: "http://localhost:3000/sse"},
},
},
expectError: false,
},
{
name: "invalid URL format",
serverDetail: model.ServerJSON{
Name: "com.example/test",
Remotes: []model.Remote{
{URL: "not-a-valid-url"},
},
},
expectError: true,
errorMsg: "URL must have a valid hostname",
},
{
name: "empty remotes array",
serverDetail: model.ServerJSON{
Name: "com.example/test",
Remotes: []model.Remote{},
},
expectError: false,
},
{
name: "multiple valid remotes - different subdomains",
serverDetail: model.ServerJSON{
Name: "com.example/server",
Remotes: []model.Remote{
{URL: "https://api.example.com/sse"},
{URL: "https://mcp.example.com/websocket"},
},
},
expectError: false,
},
{
name: "one valid, one invalid remote",
serverDetail: model.ServerJSON{
Name: "com.example/server",
Remotes: []model.Remote{
{URL: "https://example.com/sse"},
{URL: "https://google.com/websocket"},
},
},
expectError: true,
errorMsg: "remote URL host google.com does not match publisher domain example.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validators.ValidateRemoteNamespaceMatch(tt.serverDetail)
if tt.expectError {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errorMsg)
} else {
assert.NoError(t, err)
}
})
}
}
func TestParseServerName(t *testing.T) {
tests := []struct {
name string
serverDetail model.ServerJSON
expectError bool
errorMsg string
}{
{
name: "valid namespace/name format",
serverDetail: model.ServerJSON{
Name: "com.example.api/server",
},
expectError: false,
},
{
name: "valid complex namespace",
serverDetail: model.ServerJSON{
Name: "com.github.microsoft.azure/webapp-server",
},
expectError: false,
},
{
name: "empty server name",
serverDetail: model.ServerJSON{
Name: "",
},
expectError: true,
errorMsg: "server name is required",
},
{
name: "missing slash separator",
serverDetail: model.ServerJSON{
Name: "com.example.server",
},
expectError: true,
errorMsg: "server name must be in format 'dns-namespace/name'",
},
{
name: "empty namespace part",
serverDetail: model.ServerJSON{
Name: "/server-name",
},
expectError: true,
errorMsg: "non-empty namespace and name parts",
},
{
name: "empty name part",
serverDetail: model.ServerJSON{
Name: "com.example/",
},
expectError: true,
errorMsg: "non-empty namespace and name parts",
},
{
name: "multiple slashes - uses first as separator",
serverDetail: model.ServerJSON{
Name: "com.example/server/path",
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := validators.ParseServerName(tt.serverDetail)
if tt.expectError {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errorMsg)
} else {
assert.NoError(t, err)
}
})
}
}