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:
parent
9d04671ee3
commit
099a2d702e
1 changed files with 1280 additions and 1408 deletions
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue