Changes on the server.json format and the API (#562)

This commit is contained in:
Radoslav Dimitrov 2025-09-29 23:30:35 +03:00 • committed by GitHub
commit b21c6f564d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -10,24 +10,24 @@ import (
"os"
"strings"
"github.com/modelcontextprotocol/registry/internal/database"
"github.com/modelcontextprotocol/registry/internal/service"
"github.com/modelcontextprotocol/registry/internal/validators"
apiv0 "github.com/modelcontextprotocol/registry/pkg/api/v0"
)
// Service handles importing seed data into the registry
type Service struct {
db database.Database
registry service.RegistryService
}
// NewService creates a new importer service
func NewService(db database.Database) *Service {
return &Service{db: db}
func NewService(registry service.RegistryService) *Service {
return &Service{registry: registry}
}
// ImportFromPath imports 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
// 1. Local file paths (*.json files) - expects ServerJSON array format
// 2. Direct HTTP URLs to seed.json files - expects ServerJSON array format
// 3. Registry root URLs (automatically appends /v0/servers and paginates)
func (s *Service) ImportFromPath(ctx context.Context, path string) error {
servers, err := readSeedFile(ctx, path)
@ -35,14 +35,29 @@ func (s *Service) ImportFromPath(ctx context.Context, path string) error {
return fmt.Errorf("failed to read seed data: %w", err)
}
// Import each server using CreateServer
// Import each server using registry service CreateServer
var successfullyCreated []string
var failedCreations []string
for _, server := range servers {
_, err := s.db.CreateServer(ctx, nil, server)
_, err := s.registry.CreateServer(ctx, server)
if err != nil {
return fmt.Errorf("failed to import server %s: %w", server.Name, err)
failedCreations = append(failedCreations, fmt.Sprintf("%s: %v", server.Name, err))
log.Printf("Failed to create server %s: %v", server.Name, err)
} else {
successfullyCreated = append(successfullyCreated, server.Name)
}
}
// Report import results after actual creation attempts
if len(failedCreations) > 0 {
log.Printf("Import completed with errors: %d servers created successfully, %d servers failed",
len(successfullyCreated), len(failedCreations))
log.Printf("Failed servers: %v", failedCreations)
return fmt.Errorf("failed to import %d servers", len(failedCreations))
}
log.Printf("Import completed successfully: all %d servers created", len(successfullyCreated))
return nil
}
@ -69,10 +84,10 @@ func readSeedFile(ctx context.Context, path string) ([]*apiv0.ServerJSON, error)
return nil, fmt.Errorf("failed to read seed data from %s: %w", path, err)
}
// Parse extension wrapper format (only supported format)
// Parse ServerJSON array format
var serverResponses []apiv0.ServerJSON
if err := json.Unmarshal(data, &serverResponses); err != nil {
return nil, fmt.Errorf("failed to parse seed data as extension wrapper format: %w", err)
return nil, fmt.Errorf("failed to parse seed data as ServerJSON array format: %w", err)
}
if len(serverResponses) == 0 {
@ -93,20 +108,19 @@ func readSeedFile(ctx context.Context, path string) ([]*apiv0.ServerJSON, error)
continue
}
// Convert valid ServerJSON to ServerRecord
record := convertServerResponseToRecord(response)
validRecords = append(validRecords, record)
// Add valid ServerJSON to records
validRecords = append(validRecords, &response)
}
// Print summary of validation results
if len(invalidServers) > 0 {
log.Printf("Import summary: %d valid servers imported, %d invalid servers skipped", len(validRecords), len(invalidServers))
log.Printf("Validation summary: %d servers passed validation, %d invalid servers skipped", len(validRecords), len(invalidServers))
log.Printf("Invalid servers: %v", invalidServers)
for _, failure := range validationFailures {
log.Printf(" - %s", failure)
}
} else {
log.Printf("Import summary: All %d servers imported successfully", len(validRecords))
log.Printf("Validation summary: All %d servers passed validation", len(validRecords))
}
return validRecords, nil
@ -151,9 +165,9 @@ func fetchFromRegistryAPI(ctx context.Context, baseURL string) ([]*apiv0.ServerJ
}
var response struct {
Servers []apiv0.ServerJSON `json:"servers"`
Servers []apiv0.ServerResponse `json:"servers"`
Metadata *struct {
NextCursor string `json:"next_cursor,omitempty"`
NextCursor string `json:"nextCursor,omitempty"`
} `json:"metadata,omitempty"`
}
@ -161,10 +175,9 @@ func fetchFromRegistryAPI(ctx context.Context, baseURL string) ([]*apiv0.ServerJ
return nil, fmt.Errorf("failed to parse registry API response: %w", err)
}
// Convert and add servers
// Extract ServerJSON from each ServerResponse
for _, serverResponse := range response.Servers {
record := convertServerResponseToRecord(serverResponse)
allRecords = append(allRecords, record)
allRecords = append(allRecords, &serverResponse.Server)
}
// Check if there's a next page
@ -177,8 +190,3 @@ func fetchFromRegistryAPI(ctx context.Context, baseURL string) ([]*apiv0.ServerJ
return allRecords, nil
}
func convertServerResponseToRecord(response apiv0.ServerJSON) *apiv0.ServerJSON {
// The response is already in the correct flattened format
// Just return a pointer to it
return &response
}