Allow for vendor extensions on servers (#298)
<!-- Provide a brief summary of your changes -->
This PR serves as a placeholder and brings together all of the changes
related to supporting vendor extensions passed through the API. The idea
is to have a single place to track and review the work as it comes
together (even if it’s still a bit of a work in progress).
**The changes in this PR cover:**
* Updates on the data model
* Updates on both databases (in-memory and postgresql), added a new
table to PSQL for tracking the extensions leaving the server table only
for the clean server.json properties
* Updates the seed.json file (thus the big line change)
* Adds a seed-migrate tool to help migrating an existing seed file to
the new format
* Updates the seed import mechanism to support this
* Updates the existing tests - unit, integration and validation tests
* Adds the publish handler to the OpenAPI spec (it was missing)
* Fixes linting errors
* Updates on the API side on (publish, list, get) to support the new
format of `ServerResponse` -
```
{
"server": {},
"x-io.modelcontextprotocol.registry": {}, /*not available for publish since this gets populated by the registry*/
"x-publisher": {}
}
```
## Motivation and Context
<!-- Why is this change needed? What problem does it solve? -->
Motivated by the discussions in:
https://github.com/modelcontextprotocol/registry/discussions/284
## How Has This Been Tested?
<!-- Have you tested this in a real application? Which scenarios were
tested? -->
Locally and by running all of the tests
## Breaking Changes
<!-- Will users need to update their code or configurations? -->
Yes, it changes both the API and the format of the seed file
## 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)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
---
Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
Co-authored-by: adam jones <domdomegg+git@gmail.com>
This commit is contained in:
parent
ebaf4c9c99
commit
8d7e471fd3
1 changed files with 19980 additions and 15589 deletions
|
|
@ -3,7 +3,6 @@ package database
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
|
@ -16,21 +15,32 @@ import (
|
|||
|
||||
// MemoryDB is an in-memory implementation of the Database interface
|
||||
type MemoryDB struct {
|
||||
entries map[string]*model.ServerDetail
|
||||
entries map[string]*model.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.Server) *MemoryDB {
|
||||
// Convert Server entries to ServerDetail entries
|
||||
serverDetails := make(map[string]*model.ServerDetail)
|
||||
for k, v := range e {
|
||||
serverDetails[k] = &model.ServerDetail{
|
||||
Server: *v,
|
||||
func NewMemoryDB(e map[string]*model.ServerDetail) *MemoryDB {
|
||||
// Convert ServerDetail entries to ServerRecord entries
|
||||
serverRecords := make(map[string]*model.ServerRecord)
|
||||
for registryID, serverDetail := range e {
|
||||
// Create registry metadata
|
||||
now := time.Now()
|
||||
record := &model.ServerRecord{
|
||||
ServerJSON: *serverDetail,
|
||||
RegistryMetadata: model.RegistryMetadata{
|
||||
ID: registryID,
|
||||
PublishedAt: now,
|
||||
UpdatedAt: now,
|
||||
IsLatest: true,
|
||||
ReleaseDate: now.Format(time.RFC3339),
|
||||
},
|
||||
PublisherExtensions: make(map[string]interface{}),
|
||||
}
|
||||
serverRecords[registryID] = record
|
||||
}
|
||||
return &MemoryDB{
|
||||
entries: serverDetails,
|
||||
entries: serverRecords,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -82,15 +92,13 @@ func compareSemanticVersions(version1, version2 string) int {
|
|||
return 0
|
||||
}
|
||||
|
||||
// List retrieves all MCPRegistry entries with optional filtering and pagination
|
||||
//
|
||||
//gocognit:ignore
|
||||
// List retrieves ServerRecord entries with optional filtering and pagination
|
||||
func (db *MemoryDB) List(
|
||||
ctx context.Context,
|
||||
filter map[string]any,
|
||||
cursor string,
|
||||
limit int,
|
||||
) ([]*model.Server, string, error) {
|
||||
) ([]*model.ServerRecord, string, error) {
|
||||
if ctx.Err() != nil {
|
||||
return nil, "", ctx.Err()
|
||||
}
|
||||
|
|
@ -101,16 +109,18 @@ func (db *MemoryDB) List(
|
|||
|
||||
db.mu.RLock()
|
||||
defer db.mu.RUnlock()
|
||||
|
||||
|
||||
// Convert all entries to a slice for pagination
|
||||
var allEntries []*model.Server
|
||||
// Convert all entries to a slice for pagination, filter by is_latest
|
||||
var allEntries []*model.ServerRecord
|
||||
for _, entry := range db.entries {
|
||||
serverCopy := entry.Server
|
||||
allEntries = append(allEntries, &serverCopy)
|
||||
if entry.RegistryMetadata.IsLatest {
|
||||
allEntries = append(allEntries, entry)
|
||||
}
|
||||
}
|
||||
|
||||
// Simple filtering implementation
|
||||
var filteredEntries []*model.Server
|
||||
var filteredEntries []*model.ServerRecord
|
||||
for _, entry := range allEntries {
|
||||
include := true
|
||||
|
||||
|
|
@ -118,22 +128,17 @@ func (db *MemoryDB) List(
|
|||
for key, value := range filter {
|
||||
switch key {
|
||||
case "name":
|
||||
if entry.Name != value.(string) {
|
||||
include = false
|
||||
}
|
||||
case "repoUrl":
|
||||
if entry.Repository.URL != value.(string) {
|
||||
include = false
|
||||
}
|
||||
case "serverDetail.id":
|
||||
if entry.ID != value.(string) {
|
||||
if entry.ServerJSON.Name != value.(string) {
|
||||
include = false
|
||||
}
|
||||
case "version":
|
||||
if entry.VersionDetail.Version != value.(string) {
|
||||
if entry.ServerJSON.VersionDetail.Version != value.(string) {
|
||||
include = false
|
||||
}
|
||||
case "status":
|
||||
if string(entry.ServerJSON.Status) != value.(string) {
|
||||
include = false
|
||||
}
|
||||
// Add more filter options as needed
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -142,43 +147,43 @@ func (db *MemoryDB) List(
|
|||
}
|
||||
}
|
||||
|
||||
// Find starting point for cursor-based pagination
|
||||
// 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
|
||||
})
|
||||
|
||||
// Find starting point for cursor-based pagination using registry metadata ID
|
||||
startIdx := 0
|
||||
if cursor != "" {
|
||||
for i, entry := range filteredEntries {
|
||||
if entry.ID == cursor {
|
||||
if entry.RegistryMetadata.ID == cursor {
|
||||
startIdx = i + 1 // Start after the cursor
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort filteredEntries by ID for consistent pagination
|
||||
sort.Slice(filteredEntries, func(i, j int) bool {
|
||||
return filteredEntries[i].ID < filteredEntries[j].ID
|
||||
})
|
||||
|
||||
// Apply pagination
|
||||
endIdx := min(startIdx+limit, len(filteredEntries))
|
||||
|
||||
var result []*model.Server
|
||||
var result []*model.ServerRecord
|
||||
if startIdx < len(filteredEntries) {
|
||||
result = filteredEntries[startIdx:endIdx]
|
||||
} else {
|
||||
result = []*model.Server{}
|
||||
result = []*model.ServerRecord{}
|
||||
}
|
||||
|
||||
// Determine next cursor
|
||||
// Determine next cursor using registry metadata ID
|
||||
nextCursor := ""
|
||||
if endIdx < len(filteredEntries) {
|
||||
nextCursor = filteredEntries[endIdx-1].ID
|
||||
nextCursor = filteredEntries[endIdx-1].RegistryMetadata.ID
|
||||
}
|
||||
|
||||
return result, nextCursor, nil
|
||||
}
|
||||
|
||||
// GetByID retrieves a single ServerDetail by its ID
|
||||
func (db *MemoryDB) GetByID(ctx context.Context, id string) (*model.ServerDetail, error) {
|
||||
// GetByID retrieves a single ServerRecord by its registry metadata ID
|
||||
func (db *MemoryDB) GetByID(ctx context.Context, id string) (*model.ServerRecord, error) {
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
|
|
@ -186,66 +191,84 @@ func (db *MemoryDB) GetByID(ctx context.Context, id string) (*model.ServerDetail
|
|||
db.mu.RLock()
|
||||
defer db.mu.RUnlock()
|
||||
|
||||
// Find entry by registry metadata ID
|
||||
if entry, exists := db.entries[id]; exists {
|
||||
// Return a copy of the ServerDetail
|
||||
serverDetailCopy := *entry
|
||||
return &serverDetailCopy, nil
|
||||
// Return a copy of the ServerRecord
|
||||
entryCopy := *entry
|
||||
return &entryCopy, nil
|
||||
}
|
||||
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
// Publish adds a new ServerDetail to the database
|
||||
func (db *MemoryDB) Publish(ctx context.Context, serverDetail *model.ServerDetail) error {
|
||||
// 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{}) (*model.ServerRecord, error) {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
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")
|
||||
}
|
||||
|
||||
db.mu.Lock()
|
||||
defer db.mu.Unlock()
|
||||
|
||||
// check for name
|
||||
if serverDetail.Name == "" {
|
||||
return ErrInvalidInput
|
||||
}
|
||||
|
||||
// check that the name and the version are unique
|
||||
// Also check version ordering - don't allow publishing older versions after newer ones
|
||||
var latestVersion string
|
||||
// Check for existing entry with same name and compare versions
|
||||
var existingRecord *model.ServerRecord
|
||||
for _, entry := range db.entries {
|
||||
if entry.Name == serverDetail.Name {
|
||||
if entry.VersionDetail.Version == serverDetail.VersionDetail.Version {
|
||||
return ErrAlreadyExists
|
||||
}
|
||||
|
||||
// Track the latest version for this package name
|
||||
if latestVersion == "" || compareSemanticVersions(entry.VersionDetail.Version, latestVersion) > 0 {
|
||||
latestVersion = entry.VersionDetail.Version
|
||||
}
|
||||
if entry.RegistryMetadata.IsLatest && entry.ServerJSON.Name == name {
|
||||
existingRecord = entry
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// If we found existing versions, check if the new version is older than the latest
|
||||
if latestVersion != "" && compareSemanticVersions(serverDetail.VersionDetail.Version, latestVersion) < 0 {
|
||||
return ErrInvalidVersion
|
||||
// Version comparison
|
||||
if existingRecord != nil {
|
||||
existingVersion := existingRecord.ServerJSON.VersionDetail.Version
|
||||
if compareSemanticVersions(version, existingVersion) <= 0 {
|
||||
return nil, fmt.Errorf("version must be greater than existing version %s", existingVersion)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate repository URL
|
||||
if serverDetail.Repository.URL == "" {
|
||||
return ErrInvalidInput
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
|
||||
// Always generate a new UUID for the ID
|
||||
serverDetail.ID = uuid.New().String()
|
||||
serverDetail.VersionDetail.IsLatest = true // Assume the new version is the latest
|
||||
// Only set ReleaseDate if it's not already provided
|
||||
if serverDetail.VersionDetail.ReleaseDate == "" {
|
||||
serverDetail.VersionDetail.ReleaseDate = time.Now().Format(time.RFC3339)
|
||||
// Create new registry metadata
|
||||
now := time.Now()
|
||||
registryMetadata := model.RegistryMetadata{
|
||||
ID: uuid.New().String(),
|
||||
PublishedAt: now,
|
||||
UpdatedAt: now,
|
||||
IsLatest: true,
|
||||
ReleaseDate: now.Format(time.RFC3339),
|
||||
}
|
||||
// Store a copy of the entire ServerDetail
|
||||
serverDetailCopy := *serverDetail
|
||||
db.entries[serverDetail.ID] = &serverDetailCopy
|
||||
|
||||
return nil
|
||||
// Create server record
|
||||
record := &model.ServerRecord{
|
||||
ServerJSON: serverDetail,
|
||||
RegistryMetadata: registryMetadata,
|
||||
PublisherExtensions: publisherExtensions,
|
||||
}
|
||||
|
||||
// Mark existing record as not latest
|
||||
if existingRecord != nil {
|
||||
existingRecord.RegistryMetadata.IsLatest = false
|
||||
}
|
||||
|
||||
// Store the record using registry metadata ID
|
||||
db.entries[registryMetadata.ID] = record
|
||||
|
||||
return record, nil
|
||||
}
|
||||
|
||||
// ImportSeed imports initial data from a seed file into memory database
|
||||
|
|
@ -253,39 +276,25 @@ func (db *MemoryDB) ImportSeed(ctx context.Context, seedFilePath string) error {
|
|||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
// Read the seed file
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read seed file: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("Importing %d servers into memory database", len(seedData))
|
||||
|
||||
// Lock for concurrent access
|
||||
db.mu.Lock()
|
||||
defer db.mu.Unlock()
|
||||
|
||||
for i, server := range seedData {
|
||||
if server.ID == "" || server.Name == "" {
|
||||
log.Printf("Skipping server %d: ID or Name is empty", i+1)
|
||||
continue
|
||||
}
|
||||
|
||||
// Set default version information if missing
|
||||
if server.VersionDetail.Version == "" {
|
||||
server.VersionDetail.Version = "0.0.1-seed"
|
||||
server.VersionDetail.ReleaseDate = time.Now().Format(time.RFC3339)
|
||||
server.VersionDetail.IsLatest = true
|
||||
}
|
||||
|
||||
// Store a copy of the server detail
|
||||
serverDetailCopy := server
|
||||
db.entries[server.ID] = &serverDetailCopy
|
||||
|
||||
log.Printf("[%d/%d] Imported server: %s", i+1, len(seedData), server.Name)
|
||||
// Import each server
|
||||
for _, record := range seedData {
|
||||
// Use the registry metadata ID as the map key
|
||||
db.entries[record.RegistryMetadata.ID] = record
|
||||
}
|
||||
|
||||
|
||||
log.Println("Memory database import completed successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -302,4 +311,4 @@ func (db *MemoryDB) Connection() *ConnectionInfo {
|
|||
IsConnected: true, // Memory DB is always connected
|
||||
Raw: db.entries,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue