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
5 changed files with 1280 additions and 1408 deletions

View file

@ -4,7 +4,8 @@ import (
"context"
"errors"
"github.com/modelcontextprotocol/registry/internal/model"
apiv1 "github.com/modelcontextprotocol/registry/pkg/api/v1"
"github.com/modelcontextprotocol/registry/pkg/model"
)
// Common database errors
@ -20,16 +21,16 @@ var (
// Database defines the interface for database operations with extension wrapper architecture
type Database interface {
// List retrieves all ServerRecord entries with optional filtering
List(ctx context.Context, filter map[string]any, cursor string, limit int) ([]*model.ServerRecord, string, error)
List(ctx context.Context, filter map[string]any, cursor string, limit int) ([]*apiv1.ServerRecord, string, error)
// GetByID retrieves a single ServerRecord by its ID
GetByID(ctx context.Context, id string) (*model.ServerRecord, error)
GetByID(ctx context.Context, id string) (*apiv1.ServerRecord, error)
// Publish adds a new server to the database with separated server.json and extensions
// The registryMetadata contains metadata determined by the service layer (e.g., is_latest, timestamps)
Publish(ctx context.Context, serverDetail model.ServerDetail, publisherExtensions map[string]interface{}, registryMetadata model.RegistryMetadata) (*model.ServerRecord, error)
Publish(ctx context.Context, serverDetail model.ServerJSON, publisherExtensions map[string]interface{}, registryMetadata apiv1.RegistryExtensions) (*apiv1.ServerRecord, error)
// UpdateLatestFlag updates the is_latest flag for a specific server record
UpdateLatestFlag(ctx context.Context, id string, isLatest bool) error
// UpdateServer updates an existing server record with new server details
UpdateServer(ctx context.Context, id string, serverDetail model.ServerDetail, publisherExtensions map[string]interface{}) (*model.ServerRecord, error)
UpdateServer(ctx context.Context, id string, serverDetail model.ServerJSON, publisherExtensions map[string]interface{}) (*apiv1.ServerRecord, error)
// ImportSeed imports initial data from a seed file
ImportSeed(ctx context.Context, seedFilePath string) error
// Close closes the database connection

View file

@ -8,17 +8,16 @@ import (
"net/http"
"os"
"strings"
"time"
"github.com/modelcontextprotocol/registry/internal/model"
apiv1 "github.com/modelcontextprotocol/registry/pkg/api/v1"
)
// ReadSeedFile reads seed data from various sources:
// 1. Local file paths (*.json files) - expects extension wrapper format
// 2. Direct HTTP URLs to seed.json files - expects extension wrapper format
// 2. Direct HTTP URLs to seed.json files - expects extension wrapper format
// 3. Registry root URLs (automatically appends /v0/servers and paginates)
// Only the extension wrapper format is supported (array of ServerResponse objects)
func ReadSeedFile(ctx context.Context, path string) ([]*model.ServerRecord, error) {
func ReadSeedFile(ctx context.Context, path string) ([]*apiv1.ServerRecord, error) {
var data []byte
var err error
@ -40,17 +39,17 @@ func ReadSeedFile(ctx context.Context, path string) ([]*model.ServerRecord, erro
}
// Parse extension wrapper format (only supported format)
var serverResponses []model.ServerResponse
var serverResponses []apiv1.ServerRecord
if err := json.Unmarshal(data, &serverResponses); err != nil {
return nil, fmt.Errorf("failed to parse seed data as extension wrapper format: %w", err)
}
if len(serverResponses) == 0 {
return []*model.ServerRecord{}, nil
return []*apiv1.ServerRecord{}, nil
}
// Convert ServerResponse to ServerRecord
var records []*model.ServerRecord
var records []*apiv1.ServerRecord
for _, response := range serverResponses {
record := convertServerResponseToRecord(response)
records = append(records, record)
@ -78,8 +77,8 @@ func fetchFromHTTP(ctx context.Context, url string) ([]byte, error) {
return io.ReadAll(resp.Body)
}
func fetchFromRegistryAPI(ctx context.Context, baseURL string) ([]*model.ServerRecord, error) {
var allRecords []*model.ServerRecord
func fetchFromRegistryAPI(ctx context.Context, baseURL string) ([]*apiv1.ServerRecord, error) {
var allRecords []*apiv1.ServerRecord
cursor := ""
for {
@ -98,7 +97,7 @@ func fetchFromRegistryAPI(ctx context.Context, baseURL string) ([]*model.ServerR
}
var response struct {
Servers []model.ServerResponse `json:"servers"`
Servers []apiv1.ServerRecord `json:"servers"`
Metadata *struct {
NextCursor string `json:"next_cursor,omitempty"`
} `json:"metadata,omitempty"`
@ -124,55 +123,20 @@ func fetchFromRegistryAPI(ctx context.Context, baseURL string) ([]*model.ServerR
return allRecords, nil
}
func convertServerResponseToRecord(response model.ServerResponse) *model.ServerRecord {
// Extract registry metadata from the extension
registryExt := response.XIOModelContextProtocolRegistry
// Parse timestamps
publishedAt, _ := time.Parse(time.RFC3339, getStringFromInterface(registryExt, "published_at"))
updatedAt, _ := time.Parse(time.RFC3339, getStringFromInterface(registryExt, "updated_at"))
registryMetadata := model.RegistryMetadata{
ID: getStringFromInterface(registryExt, "id"),
IsLatest: getBoolFromInterface(registryExt, "is_latest"),
PublishedAt: publishedAt,
UpdatedAt: updatedAt,
ReleaseDate: getStringFromInterface(registryExt, "release_date"),
}
func convertServerResponseToRecord(response apiv1.ServerRecord) *apiv1.ServerRecord {
// The registry extensions are already properly typed, so we can use them directly
registryMetadata := response.XIOModelContextProtocolRegistry
// Publisher extensions
publisherExtensions := make(map[string]interface{})
if response.XPublisher != nil {
if publisherMap, ok := response.XPublisher.(map[string]interface{}); ok {
publisherExtensions = publisherMap
}
publisherExtensions := response.XPublisher
if publisherExtensions == nil {
publisherExtensions = make(map[string]interface{})
}
return &model.ServerRecord{
ServerJSON: response.Server,
RegistryMetadata: registryMetadata,
PublisherExtensions: publisherExtensions,
return &apiv1.ServerRecord{
Server: response.Server,
XIOModelContextProtocolRegistry: registryMetadata,
XPublisher: publisherExtensions,
}
}
func getStringFromInterface(data interface{}, key string) string {
if dataMap, ok := data.(map[string]interface{}); ok {
if value, exists := dataMap[key]; exists {
if strValue, ok := value.(string); ok {
return strValue
}
}
}
return ""
}
func getBoolFromInterface(data interface{}, key string) bool {
if dataMap, ok := data.(map[string]interface{}); ok {
if value, exists := dataMap[key]; exists {
if boolValue, ok := value.(bool); ok {
return boolValue
}
}
}
return false
}

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)
}

View file

@ -7,32 +7,33 @@ import (
"sync"
"time"
"github.com/modelcontextprotocol/registry/internal/model"
apiv1 "github.com/modelcontextprotocol/registry/pkg/api/v1"
"github.com/modelcontextprotocol/registry/pkg/model"
)
// MemoryDB is an in-memory implementation of the Database interface
type MemoryDB struct {
entries map[string]*model.ServerRecord // maps registry metadata ID to ServerRecord
entries map[string]*apiv1.ServerRecord // maps registry metadata ID to ServerRecord
mu sync.RWMutex
}
// NewMemoryDB creates a new instance of the in-memory database
func NewMemoryDB(e map[string]*model.ServerDetail) *MemoryDB {
// Convert ServerDetail entries to ServerRecord entries
serverRecords := make(map[string]*model.ServerRecord)
func NewMemoryDB(e map[string]*model.ServerJSON) *MemoryDB {
// Convert ServerJSON entries to ServerRecord entries
serverRecords := make(map[string]*apiv1.ServerRecord)
for registryID, serverDetail := range e {
// Create registry metadata
now := time.Now()
record := &model.ServerRecord{
ServerJSON: *serverDetail,
RegistryMetadata: model.RegistryMetadata{
record := &apiv1.ServerRecord{
Server: *serverDetail,
XIOModelContextProtocolRegistry: apiv1.RegistryExtensions{
ID: registryID,
PublishedAt: now,
UpdatedAt: now,
IsLatest: true,
ReleaseDate: now.Format(time.RFC3339),
},
PublisherExtensions: make(map[string]interface{}),
XPublisher: make(map[string]interface{}),
}
serverRecords[registryID] = record
}
@ -41,14 +42,13 @@ func NewMemoryDB(e map[string]*model.ServerDetail) *MemoryDB {
}
}
//nolint:cyclop // Complexity from filtering logic is acceptable for memory implementation
func (db *MemoryDB) List(
ctx context.Context,
filter map[string]any,
cursor string,
limit int,
) ([]*model.ServerRecord, string, error) {
) ([]*apiv1.ServerRecord, string, error) {
if ctx.Err() != nil {
return nil, "", ctx.Err()
}
@ -59,18 +59,17 @@ func (db *MemoryDB) List(
db.mu.RLock()
defer db.mu.RUnlock()
// Convert all entries to a slice for pagination, filter by is_latest
var allEntries []*model.ServerRecord
var allEntries []*apiv1.ServerRecord
for _, entry := range db.entries {
if entry.RegistryMetadata.IsLatest {
if entry.XIOModelContextProtocolRegistry.IsLatest {
allEntries = append(allEntries, entry)
}
}
// Simple filtering implementation
var filteredEntries []*model.ServerRecord
var filteredEntries []*apiv1.ServerRecord
for _, entry := range allEntries {
include := true
@ -78,21 +77,21 @@ func (db *MemoryDB) List(
for key, value := range filter {
switch key {
case "name":
if entry.ServerJSON.Name != value.(string) {
if entry.Server.Name != value.(string) {
include = false
}
case "version":
if entry.ServerJSON.VersionDetail.Version != value.(string) {
if entry.Server.VersionDetail.Version != value.(string) {
include = false
}
case "status":
if string(entry.ServerJSON.Status) != value.(string) {
if string(entry.Server.Status) != value.(string) {
include = false
}
case "remote_url":
found := false
remoteURL := value.(string)
for _, remote := range entry.ServerJSON.Remotes {
for _, remote := range entry.Server.Remotes {
if remote.URL == remoteURL {
found = true
break
@ -111,14 +110,14 @@ func (db *MemoryDB) List(
// Sort filteredEntries by registry metadata ID for consistent pagination
sort.Slice(filteredEntries, func(i, j int) bool {
return filteredEntries[i].RegistryMetadata.ID < filteredEntries[j].RegistryMetadata.ID
return filteredEntries[i].XIOModelContextProtocolRegistry.ID < filteredEntries[j].XIOModelContextProtocolRegistry.ID
})
// Find starting point for cursor-based pagination using registry metadata ID
startIdx := 0
if cursor != "" {
for i, entry := range filteredEntries {
if entry.RegistryMetadata.ID == cursor {
if entry.XIOModelContextProtocolRegistry.ID == cursor {
startIdx = i + 1 // Start after the cursor
break
}
@ -128,24 +127,24 @@ func (db *MemoryDB) List(
// Apply pagination
endIdx := min(startIdx+limit, len(filteredEntries))
var result []*model.ServerRecord
var result []*apiv1.ServerRecord
if startIdx < len(filteredEntries) {
result = filteredEntries[startIdx:endIdx]
} else {
result = []*model.ServerRecord{}
result = []*apiv1.ServerRecord{}
}
// Determine next cursor using registry metadata ID
nextCursor := ""
if endIdx < len(filteredEntries) {
nextCursor = filteredEntries[endIdx-1].RegistryMetadata.ID
nextCursor = filteredEntries[endIdx-1].XIOModelContextProtocolRegistry.ID
}
return result, nextCursor, nil
}
// GetByID retrieves a single ServerRecord by its registry metadata ID
func (db *MemoryDB) GetByID(ctx context.Context, id string) (*model.ServerRecord, error) {
func (db *MemoryDB) GetByID(ctx context.Context, id string) (*apiv1.ServerRecord, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
@ -164,17 +163,17 @@ func (db *MemoryDB) GetByID(ctx context.Context, id string) (*model.ServerRecord
}
// Publish adds a new server to the database with separated server.json and extensions
func (db *MemoryDB) Publish(ctx context.Context, serverDetail model.ServerDetail, publisherExtensions map[string]interface{}, registryMetadata model.RegistryMetadata) (*model.ServerRecord, error) {
func (db *MemoryDB) Publish(ctx context.Context, serverDetail model.ServerJSON, publisherExtensions map[string]interface{}, registryMetadata apiv1.RegistryExtensions) (*apiv1.ServerRecord, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
// Extract name and version for validation
name := serverDetail.Name
if name == "" {
return nil, fmt.Errorf("name is required in server JSON")
}
version := serverDetail.VersionDetail.Version
if version == "" {
return nil, fmt.Errorf("version is required in version_detail")
@ -189,10 +188,10 @@ func (db *MemoryDB) Publish(ctx context.Context, serverDetail model.ServerDetail
}
// Create server record
record := &model.ServerRecord{
ServerJSON: serverDetail,
RegistryMetadata: registryMetadata,
PublisherExtensions: publisherExtensions,
record := &apiv1.ServerRecord{
Server: serverDetail,
XIOModelContextProtocolRegistry: registryMetadata,
XPublisher: publisherExtensions,
}
// Store the record using registry metadata ID
@ -206,7 +205,7 @@ func (db *MemoryDB) ImportSeed(ctx context.Context, seedFilePath string) error {
if ctx.Err() != nil {
return ctx.Err()
}
// This will need to be updated to work with the new ServerRecord format
// Read seed data using the shared ReadSeedFile function
seedData, err := ReadSeedFile(ctx, seedFilePath)
@ -221,9 +220,8 @@ func (db *MemoryDB) ImportSeed(ctx context.Context, seedFilePath string) error {
// Import each server
for _, record := range seedData {
// Use the registry metadata ID as the map key
db.entries[record.RegistryMetadata.ID] = record
db.entries[record.XIOModelContextProtocolRegistry.ID] = record
}
return nil
}
@ -238,8 +236,8 @@ func (db *MemoryDB) UpdateLatestFlag(ctx context.Context, id string, isLatest bo
defer db.mu.Unlock()
if entry, exists := db.entries[id]; exists {
entry.RegistryMetadata.IsLatest = isLatest
entry.RegistryMetadata.UpdatedAt = time.Now()
entry.XIOModelContextProtocolRegistry.IsLatest = isLatest
entry.XIOModelContextProtocolRegistry.UpdatedAt = time.Now()
return nil
}
@ -247,7 +245,7 @@ func (db *MemoryDB) UpdateLatestFlag(ctx context.Context, id string, isLatest bo
}
// UpdateServer updates an existing server record with new server details
func (db *MemoryDB) UpdateServer(ctx context.Context, id string, serverDetail model.ServerDetail, publisherExtensions map[string]interface{}) (*model.ServerRecord, error) {
func (db *MemoryDB) UpdateServer(ctx context.Context, id string, serverDetail model.ServerJSON, publisherExtensions map[string]interface{}) (*apiv1.ServerRecord, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
@ -261,9 +259,9 @@ func (db *MemoryDB) UpdateServer(ctx context.Context, id string, serverDetail mo
}
// Update the server details
entry.ServerJSON = serverDetail
entry.PublisherExtensions = publisherExtensions
entry.RegistryMetadata.UpdatedAt = time.Now()
entry.Server = serverDetail
entry.XPublisher = publisherExtensions
entry.XIOModelContextProtocolRegistry.UpdatedAt = time.Now()
// Return the updated record
return entry, nil
@ -282,4 +280,4 @@ func (db *MemoryDB) Connection() *ConnectionInfo {
IsConnected: true, // Memory DB is always connected
Raw: db.entries,
}
}
}

View file

@ -10,7 +10,8 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/modelcontextprotocol/registry/internal/model"
apiv1 "github.com/modelcontextprotocol/registry/pkg/api/v1"
"github.com/modelcontextprotocol/registry/pkg/model"
)
// PostgreSQL is an implementation of the Database interface using PostgreSQL
@ -47,7 +48,7 @@ func (db *PostgreSQL) List(
filter map[string]any,
cursor string,
limit int,
) ([]*model.ServerRecord, string, error) {
) ([]*apiv1.ServerRecord, string, error) {
if limit <= 0 {
limit = 10
}
@ -112,26 +113,26 @@ func (db *PostgreSQL) List(
}
defer rows.Close()
var results []*model.ServerRecord
var results []*apiv1.ServerRecord
for rows.Next() {
var record model.ServerRecord
var record apiv1.ServerRecord
var repositoryJSON, packagesJSON, remotesJSON, publisherExtensionsJSON []byte
var publishedAt, updatedAt, releaseDate time.Time
err := rows.Scan(
// Server fields
&record.ServerJSON.Name,
&record.ServerJSON.Description,
&record.ServerJSON.Status,
&record.Server.Name,
&record.Server.Description,
&record.Server.Status,
&repositoryJSON,
&record.ServerJSON.VersionDetail.Version,
&record.Server.VersionDetail.Version,
&packagesJSON,
&remotesJSON,
// Registry metadata fields
&record.RegistryMetadata.ID,
&record.XIOModelContextProtocolRegistry.ID,
&publishedAt,
&updatedAt,
&record.RegistryMetadata.IsLatest,
&record.XIOModelContextProtocolRegistry.IsLatest,
&releaseDate,
&publisherExtensionsJSON,
)
@ -145,9 +146,9 @@ func (db *PostgreSQL) List(
}
// Set registry metadata timestamps
record.RegistryMetadata.PublishedAt = publishedAt
record.RegistryMetadata.UpdatedAt = updatedAt
record.RegistryMetadata.ReleaseDate = releaseDate.Format(time.RFC3339)
record.XIOModelContextProtocolRegistry.PublishedAt = publishedAt
record.XIOModelContextProtocolRegistry.UpdatedAt = updatedAt
record.XIOModelContextProtocolRegistry.ReleaseDate = releaseDate.Format(time.RFC3339)
results = append(results, &record)
}
@ -159,45 +160,45 @@ func (db *PostgreSQL) List(
// Determine next cursor using registry metadata ID
nextCursor := ""
if len(results) > 0 && len(results) >= limit {
nextCursor = results[len(results)-1].RegistryMetadata.ID
nextCursor = results[len(results)-1].XIOModelContextProtocolRegistry.ID
}
return results, nextCursor, nil
}
// parseJSONFields parses JSON fields for a server record
func parseJSONFields(record *model.ServerRecord, repositoryJSON, packagesJSON, remotesJSON, publisherExtensionsJSON []byte) error {
func parseJSONFields(record *apiv1.ServerRecord, repositoryJSON, packagesJSON, remotesJSON, publisherExtensionsJSON []byte) error {
if len(repositoryJSON) > 0 {
if err := json.Unmarshal(repositoryJSON, &record.ServerJSON.Repository); err != nil {
if err := json.Unmarshal(repositoryJSON, &record.Server.Repository); err != nil {
return fmt.Errorf("failed to unmarshal repository: %w", err)
}
}
if len(packagesJSON) > 0 {
if err := json.Unmarshal(packagesJSON, &record.ServerJSON.Packages); err != nil {
if err := json.Unmarshal(packagesJSON, &record.Server.Packages); err != nil {
return fmt.Errorf("failed to unmarshal packages: %w", err)
}
}
if len(remotesJSON) > 0 {
if err := json.Unmarshal(remotesJSON, &record.ServerJSON.Remotes); err != nil {
if err := json.Unmarshal(remotesJSON, &record.Server.Remotes); err != nil {
return fmt.Errorf("failed to unmarshal remotes: %w", err)
}
}
if len(publisherExtensionsJSON) > 0 {
if err := json.Unmarshal(publisherExtensionsJSON, &record.PublisherExtensions); err != nil {
if err := json.Unmarshal(publisherExtensionsJSON, &record.XPublisher); err != nil {
return fmt.Errorf("failed to unmarshal publisher extensions: %w", err)
}
} else {
record.PublisherExtensions = make(map[string]interface{})
record.XPublisher = make(map[string]interface{})
}
return nil
}
// GetByID retrieves a single ServerRecord by its registry metadata ID
func (db *PostgreSQL) GetByID(ctx context.Context, id string) (*model.ServerRecord, error) {
func (db *PostgreSQL) GetByID(ctx context.Context, id string) (*apiv1.ServerRecord, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
@ -211,24 +212,24 @@ func (db *PostgreSQL) GetByID(ctx context.Context, id string) (*model.ServerReco
WHERE se.id = $1
`
var record model.ServerRecord
var record apiv1.ServerRecord
var repositoryJSON, packagesJSON, remotesJSON, publisherExtensionsJSON []byte
var publishedAt, updatedAt, releaseDate time.Time
err := db.conn.QueryRow(ctx, query, id).Scan(
// Server fields
&record.ServerJSON.Name,
&record.ServerJSON.Description,
&record.ServerJSON.Status,
&record.Server.Name,
&record.Server.Description,
&record.Server.Status,
&repositoryJSON,
&record.ServerJSON.VersionDetail.Version,
&record.Server.VersionDetail.Version,
&packagesJSON,
&remotesJSON,
// Registry metadata fields
&record.RegistryMetadata.ID,
&record.XIOModelContextProtocolRegistry.ID,
&publishedAt,
&updatedAt,
&record.RegistryMetadata.IsLatest,
&record.XIOModelContextProtocolRegistry.IsLatest,
&releaseDate,
&publisherExtensionsJSON,
)
@ -242,42 +243,42 @@ func (db *PostgreSQL) GetByID(ctx context.Context, id string) (*model.ServerReco
// Parse JSON fields
if len(repositoryJSON) > 0 {
if err := json.Unmarshal(repositoryJSON, &record.ServerJSON.Repository); err != nil {
if err := json.Unmarshal(repositoryJSON, &record.Server.Repository); err != nil {
return nil, fmt.Errorf("failed to unmarshal repository: %w", err)
}
}
if len(packagesJSON) > 0 {
if err := json.Unmarshal(packagesJSON, &record.ServerJSON.Packages); err != nil {
if err := json.Unmarshal(packagesJSON, &record.Server.Packages); err != nil {
return nil, fmt.Errorf("failed to unmarshal packages: %w", err)
}
}
if len(remotesJSON) > 0 {
if err := json.Unmarshal(remotesJSON, &record.ServerJSON.Remotes); err != nil {
if err := json.Unmarshal(remotesJSON, &record.Server.Remotes); err != nil {
return nil, fmt.Errorf("failed to unmarshal remotes: %w", err)
}
}
// Parse publisher extensions
if len(publisherExtensionsJSON) > 0 {
if err := json.Unmarshal(publisherExtensionsJSON, &record.PublisherExtensions); err != nil {
if err := json.Unmarshal(publisherExtensionsJSON, &record.XPublisher); err != nil {
return nil, fmt.Errorf("failed to unmarshal publisher extensions: %w", err)
}
} else {
record.PublisherExtensions = make(map[string]interface{})
record.XPublisher = make(map[string]interface{})
}
// Set registry metadata timestamps
record.RegistryMetadata.PublishedAt = publishedAt
record.RegistryMetadata.UpdatedAt = updatedAt
record.RegistryMetadata.ReleaseDate = releaseDate.Format(time.RFC3339)
record.XIOModelContextProtocolRegistry.PublishedAt = publishedAt
record.XIOModelContextProtocolRegistry.UpdatedAt = updatedAt
record.XIOModelContextProtocolRegistry.ReleaseDate = releaseDate.Format(time.RFC3339)
return &record, nil
}
// Publish adds a new server to the database with separated server.json and extensions
func (db *PostgreSQL) Publish(ctx context.Context, serverDetail model.ServerDetail, publisherExtensions map[string]interface{}, registryMetadata model.RegistryMetadata) (*model.ServerRecord, error) {
func (db *PostgreSQL) Publish(ctx context.Context, serverDetail model.ServerJSON, publisherExtensions map[string]interface{}, registryMetadata apiv1.RegistryExtensions) (*apiv1.ServerRecord, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
@ -292,7 +293,6 @@ func (db *PostgreSQL) Publish(ctx context.Context, serverDetail model.ServerDeta
}
}()
// Prepare JSON data for server table
repositoryJSON, err := json.Marshal(serverDetail.Repository)
if err != nil {
@ -317,7 +317,6 @@ func (db *PostgreSQL) Publish(ctx context.Context, serverDetail model.ServerDeta
// Use the same ID for both server and server_extensions records (1:1 relationship)
serverID := registryMetadata.ID
// Insert new server record
insertServerQuery := `
INSERT INTO servers (id, name, description, status, repository, version, packages, remotes)
@ -348,7 +347,7 @@ func (db *PostgreSQL) Publish(ctx context.Context, serverDetail model.ServerDeta
registryMetadata.PublishedAt,
registryMetadata.UpdatedAt,
registryMetadata.IsLatest,
registryMetadata.PublishedAt, // release_date
registryMetadata.PublishedAt, // release_date
publisherExtensionsJSON,
)
if err != nil {
@ -361,10 +360,10 @@ func (db *PostgreSQL) Publish(ctx context.Context, serverDetail model.ServerDeta
}
// Create and return the ServerRecord
record := &model.ServerRecord{
ServerJSON: serverDetail,
RegistryMetadata: registryMetadata,
PublisherExtensions: publisherExtensions,
record := &apiv1.ServerRecord{
Server: serverDetail,
XIOModelContextProtocolRegistry: registryMetadata,
XPublisher: publisherExtensions,
}
return record, nil
@ -392,11 +391,11 @@ func (db *PostgreSQL) ImportSeed(ctx context.Context, seedFilePath string) error
// Import each server
for _, record := range seedData {
// Convert record to the format expected by Publish
serverDetail := record.ServerJSON
publisherExtensions := record.PublisherExtensions
serverDetail := record.Server
publisherExtensions := record.XPublisher
// Use the existing Publish logic but with specific ID from seed data
if err := db.publishWithTransaction(ctx, tx, serverDetail, publisherExtensions, &record.RegistryMetadata); err != nil {
if err := db.publishWithTransaction(ctx, tx, serverDetail, publisherExtensions, &record.XIOModelContextProtocolRegistry); err != nil {
return fmt.Errorf("failed to import server %s: %w", serverDetail.Name, err)
}
}
@ -410,7 +409,7 @@ func (db *PostgreSQL) ImportSeed(ctx context.Context, seedFilePath string) error
}
// publishWithTransaction handles publishing within an existing transaction, optionally with predefined metadata
func (db *PostgreSQL) publishWithTransaction(ctx context.Context, tx pgx.Tx, serverDetail model.ServerDetail, publisherExtensions map[string]interface{}, existingMetadata *model.RegistryMetadata) error {
func (db *PostgreSQL) publishWithTransaction(ctx context.Context, tx pgx.Tx, serverDetail model.ServerJSON, publisherExtensions map[string]interface{}, existingMetadata *apiv1.RegistryExtensions) error {
// Use the same ID for both server and server_extensions (1:1 relationship)
var id string
if existingMetadata != nil && existingMetadata.ID != "" {
@ -518,7 +517,7 @@ func (db *PostgreSQL) UpdateLatestFlag(ctx context.Context, id string, isLatest
SET is_latest = $1, updated_at = $2
WHERE id = $3
`
result, err := db.conn.Exec(ctx, query, isLatest, time.Now(), id)
if err != nil {
return fmt.Errorf("failed to update latest flag: %w", err)
@ -532,7 +531,7 @@ func (db *PostgreSQL) UpdateLatestFlag(ctx context.Context, id string, isLatest
}
// UpdateServer updates an existing server record with new server details
func (db *PostgreSQL) UpdateServer(ctx context.Context, id string, serverDetail model.ServerDetail, publisherExtensions map[string]interface{}) (*model.ServerRecord, error) {
func (db *PostgreSQL) UpdateServer(ctx context.Context, id string, serverDetail model.ServerJSON, publisherExtensions map[string]interface{}) (*apiv1.ServerRecord, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
@ -641,4 +640,4 @@ func (db *PostgreSQL) Connection() *ConnectionInfo {
IsConnected: isConnected,
Raw: db.conn,
}
}
}