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

View file

@ -7,18 +7,20 @@ import (
"net/http/httptest"
"os"
"testing"
"time"
"github.com/modelcontextprotocol/registry/internal/database"
"github.com/modelcontextprotocol/registry/internal/model"
apiv1 "github.com/modelcontextprotocol/registry/pkg/api/v1"
"github.com/modelcontextprotocol/registry/pkg/model"
"github.com/stretchr/testify/assert"
)
func TestReadSeedFile_LocalFile(t *testing.T) {
// Create a temporary seed file in extension wrapper format
tempFile := "/tmp/test_seed.json"
seedData := []model.ServerResponse{
seedData := []apiv1.ServerRecord{
{
Server: model.ServerDetail{
Server: model.ServerJSON{
Name: "test-server-1",
Description: "Test server 1",
Repository: model.Repository{
@ -30,10 +32,11 @@ func TestReadSeedFile_LocalFile(t *testing.T) {
Version: "1.0.0",
},
},
XIOModelContextProtocolRegistry: map[string]interface{}{
"id": "test-id-1",
"published_at": "2023-01-01T00:00:00Z",
"is_latest": true,
XIOModelContextProtocolRegistry: apiv1.RegistryExtensions{
ID: "test-id-1",
PublishedAt: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
IsLatest: true,
ReleaseDate: "2023-01-01T00:00:00Z",
},
},
}
@ -58,21 +61,22 @@ func TestReadSeedFile_LocalFile(t *testing.T) {
result, err := database.ReadSeedFile(context.Background(), tempFile)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, "test-server-1", result[0].ServerJSON.Name)
assert.Equal(t, "test-server-1", result[0].Server.Name)
}
func TestReadSeedFile_DirectHTTPURL(t *testing.T) {
// Create a test HTTP server that serves seed JSON directly in extension wrapper format
seedData := []model.ServerResponse{
seedData := []apiv1.ServerRecord{
{
Server: model.ServerDetail{
Server: model.ServerJSON{
Name: "test-server-1",
Description: "Test server 1",
},
XIOModelContextProtocolRegistry: map[string]interface{}{
"id": "test-id-1",
"published_at": "2023-01-01T00:00:00Z",
"is_latest": true,
XIOModelContextProtocolRegistry: apiv1.RegistryExtensions{
ID: "test-id-1",
PublishedAt: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
IsLatest: true,
ReleaseDate: "2023-01-01T00:00:00Z",
},
},
}
@ -89,13 +93,13 @@ func TestReadSeedFile_DirectHTTPURL(t *testing.T) {
result, err := database.ReadSeedFile(context.Background(), server.URL+"/seed.json")
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, "test-server-1", result[0].ServerJSON.Name)
assert.Equal(t, "test-server-1", result[0].Server.Name)
}
func TestReadSeedFile_RegistryURL(t *testing.T) {
// Create mock registry responses
server1 := model.ServerResponse{
Server: model.ServerDetail{
server1 := apiv1.ServerRecord{
Server: model.ServerJSON{
Name: "Test Server 1",
Description: "First test server",
Packages: []model.Package{
@ -107,14 +111,15 @@ func TestReadSeedFile_RegistryURL(t *testing.T) {
},
},
},
XIOModelContextProtocolRegistry: map[string]interface{}{
"id": "server-1",
"published_at": "2023-01-01T00:00:00Z",
"is_latest": true,
XIOModelContextProtocolRegistry: apiv1.RegistryExtensions{
ID: "server-1",
PublishedAt: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
IsLatest: true,
ReleaseDate: "2023-01-01T00:00:00Z",
},
}
server2 := model.ServerResponse{
Server: model.ServerDetail{
server2 := apiv1.ServerRecord{
Server: model.ServerJSON{
Name: "Test Server 2",
Description: "Second test server",
Packages: []model.Package{
@ -126,14 +131,15 @@ func TestReadSeedFile_RegistryURL(t *testing.T) {
},
},
},
XIOModelContextProtocolRegistry: map[string]interface{}{
"id": "server-2",
"published_at": "2023-01-01T00:00:00Z",
"is_latest": true,
XIOModelContextProtocolRegistry: apiv1.RegistryExtensions{
ID: "server-2",
PublishedAt: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
IsLatest: true,
ReleaseDate: "2023-01-01T00:00:00Z",
},
}
serverDetail1 := model.ServerDetail{
serverDetail1 := model.ServerJSON{
Name: "Test Server 1",
Description: "First test server",
Packages: []model.Package{
@ -145,7 +151,7 @@ func TestReadSeedFile_RegistryURL(t *testing.T) {
},
},
}
serverDetail2 := model.ServerDetail{
serverDetail2 := model.ServerJSON{
Name: "Test Server 2",
Description: "Second test server",
Packages: []model.Package{
@ -171,8 +177,8 @@ func TestReadSeedFile_RegistryURL(t *testing.T) {
}
type PaginatedResponse struct {
Servers []model.ServerResponse `json:"servers"`
Metadata *Metadata `json:"metadata,omitempty"`
Servers []apiv1.ServerRecord `json:"servers"`
Metadata *Metadata `json:"metadata,omitempty"`
}
var response PaginatedResponse
@ -180,7 +186,7 @@ func TestReadSeedFile_RegistryURL(t *testing.T) {
case "":
// First page
response = PaginatedResponse{
Servers: []model.ServerResponse{server1},
Servers: []apiv1.ServerRecord{server1},
Metadata: &Metadata{
NextCursor: "next-cursor-1",
Count: 1,
@ -189,7 +195,7 @@ func TestReadSeedFile_RegistryURL(t *testing.T) {
case "next-cursor-1":
// Second page
response = PaginatedResponse{
Servers: []model.ServerResponse{server2},
Servers: []apiv1.ServerRecord{server2},
Metadata: &Metadata{
Count: 1,
// No NextCursor means end of pagination
@ -198,7 +204,7 @@ func TestReadSeedFile_RegistryURL(t *testing.T) {
default:
// No more pages
response = PaginatedResponse{
Servers: []model.ServerResponse{},
Servers: []apiv1.ServerRecord{},
Metadata: &Metadata{},
}
}
@ -233,20 +239,20 @@ func TestReadSeedFile_RegistryURL(t *testing.T) {
assert.Len(t, result, 2)
// Verify the servers were imported correctly
assert.Equal(t, "Test Server 1", result[0].ServerJSON.Name)
assert.Equal(t, "Test Server 2", result[1].ServerJSON.Name)
assert.Equal(t, "Test Server 1", result[0].Server.Name)
assert.Equal(t, "Test Server 2", result[1].Server.Name)
// Verify packages were imported with new schema
assert.Len(t, result[0].ServerJSON.Packages, 1)
assert.Equal(t, "npm", result[0].ServerJSON.Packages[0].RegistryType)
assert.Equal(t, "https://registry.npmjs.org", result[0].ServerJSON.Packages[0].RegistryBaseURL)
assert.Equal(t, "test-package-1", result[0].ServerJSON.Packages[0].Identifier)
assert.Len(t, result[1].ServerJSON.Packages, 1)
assert.Equal(t, "npm", result[1].ServerJSON.Packages[0].RegistryType)
assert.Equal(t, "https://registry.npmjs.org", result[1].ServerJSON.Packages[0].RegistryBaseURL)
assert.Equal(t, "test-package-2", result[1].ServerJSON.Packages[0].Identifier)
assert.Len(t, result[0].Server.Packages, 1)
assert.Equal(t, "npm", result[0].Server.Packages[0].RegistryType)
assert.Equal(t, "https://registry.npmjs.org", result[0].Server.Packages[0].RegistryBaseURL)
assert.Equal(t, "test-package-1", result[0].Server.Packages[0].Identifier)
assert.Len(t, result[1].Server.Packages, 1)
assert.Equal(t, "npm", result[1].Server.Packages[0].RegistryType)
assert.Equal(t, "https://registry.npmjs.org", result[1].Server.Packages[0].RegistryBaseURL)
assert.Equal(t, "test-package-2", result[1].Server.Packages[0].Identifier)
// Verify metadata was extracted
assert.Equal(t, "server-1", result[0].RegistryMetadata.ID)
assert.Equal(t, "server-2", result[1].RegistryMetadata.ID)
}
assert.Equal(t, "server-1", result[0].XIOModelContextProtocolRegistry.ID)
assert.Equal(t, "server-2", result[1].XIOModelContextProtocolRegistry.ID)
}