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
2 changed files with 4056 additions and 5608 deletions

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
}

View file

@ -7,10 +7,11 @@ import (
"net/http/httptest"
"os"
"testing"
"time"
"github.com/modelcontextprotocol/registry/internal/config"
"github.com/modelcontextprotocol/registry/internal/database"
"github.com/modelcontextprotocol/registry/internal/importer"
"github.com/modelcontextprotocol/registry/internal/service"
apiv0 "github.com/modelcontextprotocol/registry/pkg/api/v0"
"github.com/modelcontextprotocol/registry/pkg/model"
"github.com/stretchr/testify/assert"
@ -20,7 +21,7 @@ import (
func TestImportService_LocalFile(t *testing.T) {
// Create a temporary seed file
tempFile := "/tmp/test_import_seed.json"
seedData := []apiv0.ServerJSON{
seedData := []*apiv0.ServerJSON{
{
Name: "io.github.test/test-server-1",
Description: "Test server 1",
@ -30,15 +31,6 @@ func TestImportService_LocalFile(t *testing.T) {
ID: "123",
},
Version: "1.0.0",
Meta: &apiv0.ServerMeta{
Official: &apiv0.RegistryExtensions{
ServerID: "server-id-1",
VersionID: "test-id-1",
PublishedAt: time.Now(),
UpdatedAt: time.Now(),
IsLatest: true,
},
},
},
}
@ -49,149 +41,201 @@ func TestImportService_LocalFile(t *testing.T) {
require.NoError(t, err)
defer os.Remove(tempFile)
// Create registry service
testDB := database.NewTestDB(t)
registryService := service.NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
// Create importer service and test import
service := importer.NewService(testDB)
err = service.ImportFromPath(context.Background(), tempFile)
importerService := importer.NewService(registryService)
err = importerService.ImportFromPath(context.Background(), tempFile)
require.NoError(t, err)
// Verify the server was imported
servers, _, err := testDB.List(context.Background(), nil, nil, "", 10)
// Verify the server was imported using registry service
servers, _, err := registryService.ListServers(context.Background(), nil, "", 10)
require.NoError(t, err)
assert.Len(t, servers, 1)
assert.Equal(t, "io.github.test/test-server-1", servers[0].Name)
assert.Equal(t, "io.github.test/test-server-1", servers[0].Server.Name)
assert.Equal(t, "1.0.0", servers[0].Server.Version)
assert.Equal(t, "Test server 1", servers[0].Server.Description)
assert.NotNil(t, servers[0].Meta.Official)
assert.Equal(t, model.StatusActive, servers[0].Meta.Official.Status)
}
func TestImportService_HTTPFile(t *testing.T) {
// Create a test HTTP server
seedData := []apiv0.ServerJSON{
seedData := []*apiv0.ServerJSON{
{
Name: "io.github.test/http-test-server",
Description: "HTTP test server",
Repository: model.Repository{
URL: "https://github.com/test/repo1",
Source: "github",
ID: "123",
},
Version: "2.0.0",
Meta: &apiv0.ServerMeta{
Official: &apiv0.RegistryExtensions{
ServerID: "server-id-test",
VersionID: "test-id-2",
PublishedAt: time.Now(),
UpdatedAt: time.Now(),
IsLatest: true,
},
},
},
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(seedData)
}))
defer server.Close()
testDB := database.NewTestDB(t)
// Create importer service and test import
service := importer.NewService(testDB)
err := service.ImportFromPath(context.Background(), server.URL+"/seed.json")
require.NoError(t, err)
// Verify the server was imported
servers, _, err := testDB.List(context.Background(), nil, nil, "", 10)
require.NoError(t, err)
assert.Len(t, servers, 1)
assert.Equal(t, "io.github.test/http-test-server", servers[0].Name)
}
func TestImportService_RegistryAPI(t *testing.T) {
// Create test data
testServers := []apiv0.ServerJSON{
{
Name: "io.github.test/api-server-1",
Description: "API server 1",
Repository: model.Repository{
URL: "https://github.com/test/repo1",
Source: "github",
ID: "123",
},
Version: "1.0.0",
Meta: &apiv0.ServerMeta{
Official: &apiv0.RegistryExtensions{
ServerID: "server-id-test-1",
VersionID: "api-test-id-1",
PublishedAt: time.Now(),
UpdatedAt: time.Now(),
IsLatest: true,
},
},
},
{
Name: "io.github.test/api-server-2",
Description: "API server 2",
Repository: model.Repository{
URL: "https://github.com/test/repo2",
URL: "https://github.com/test/http-repo",
Source: "github",
ID: "456",
},
Version: "2.0.0",
Meta: &apiv0.ServerMeta{
Official: &apiv0.RegistryExtensions{
ServerID: "server-id-test-2",
VersionID: "api-test-id-2",
PublishedAt: time.Now(),
UpdatedAt: time.Now(),
IsLatest: true,
},
},
},
}
// Create test server that responds with paginated results
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cursor := r.URL.Query().Get("cursor")
jsonData, err := json.Marshal(seedData)
require.NoError(t, err)
var response struct {
Servers []apiv0.ServerJSON `json:"servers"`
Metadata *struct {
NextCursor string `json:"next_cursor,omitempty"`
} `json:"metadata,omitempty"`
// Create test HTTP server
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(jsonData)
}))
defer httpServer.Close()
// Create registry service
testDB := database.NewTestDB(t)
registryService := service.NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
// Create importer service and test import
importerService := importer.NewService(registryService)
err = importerService.ImportFromPath(context.Background(), httpServer.URL+"/seed.json")
require.NoError(t, err)
// Verify the server was imported
servers, _, err := registryService.ListServers(context.Background(), nil, "", 10)
require.NoError(t, err)
assert.Len(t, servers, 1)
assert.Equal(t, "io.github.test/http-test-server", servers[0].Server.Name)
assert.Equal(t, "2.0.0", servers[0].Server.Version)
assert.Equal(t, "HTTP test server", servers[0].Server.Description)
assert.NotNil(t, servers[0].Meta.Official)
}
func TestImportService_RegistryPagination(t *testing.T) {
ctx := context.Background()
// Create registry service with test data
testDB := database.NewTestDB(t)
registryService := service.NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
// Setup source registry with test data
sourceServers := []*apiv0.ServerJSON{
{
Name: "com.source/server-1",
Description: "Source server 1",
Version: "1.0.0",
},
{
Name: "com.source/server-2",
Description: "Source server 2",
Version: "1.0.0",
},
}
for _, server := range sourceServers {
_, err := registryService.CreateServer(ctx, server)
require.NoError(t, err)
}
// Create test HTTP server that serves the registry API
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
servers, _, _ := registryService.ListServers(ctx, nil, "", 10)
// Convert to response format
serverValues := make([]apiv0.ServerResponse, len(servers))
for i, server := range servers {
serverValues[i] = *server
}
switch cursor {
case "":
// First page
response.Servers = []apiv0.ServerJSON{testServers[0]}
response.Metadata = &struct {
NextCursor string `json:"next_cursor,omitempty"`
}{NextCursor: "page2"}
case "page2":
// Second page
response.Servers = []apiv0.ServerJSON{testServers[1]}
// No next cursor means end of results
response := apiv0.ServerListResponse{
Servers: serverValues,
Metadata: apiv0.Metadata{
Count: len(servers),
},
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(response)
}))
defer server.Close()
defer httpServer.Close()
testDB := database.NewTestDB(t)
// Create target registry for import
targetDB := database.NewTestDB(t)
targetRegistryService := service.NewRegistryService(targetDB, &config.Config{EnableRegistryValidation: false})
// Create importer service and test import
service := importer.NewService(testDB)
err := service.ImportFromPath(context.Background(), server.URL+"/v0/servers")
// Create importer service and test registry import
importerService := importer.NewService(targetRegistryService)
err := importerService.ImportFromPath(context.Background(), httpServer.URL+"/v0/servers")
require.NoError(t, err)
// Verify both servers were imported
servers, _, err := testDB.List(context.Background(), nil, nil, "", 10)
// Verify servers were imported
importedServers, _, err := targetRegistryService.ListServers(context.Background(), nil, "", 10)
require.NoError(t, err)
assert.Len(t, servers, 2)
assert.Len(t, importedServers, 2)
names := []string{servers[0].Name, servers[1].Name}
assert.Contains(t, names, "io.github.test/api-server-1")
assert.Contains(t, names, "io.github.test/api-server-2")
// Verify server details
serverNames := make([]string, len(importedServers))
for i, server := range importedServers {
serverNames[i] = server.Server.Name
}
assert.Contains(t, serverNames, "com.source/server-1")
assert.Contains(t, serverNames, "com.source/server-2")
}
func TestImportService_ErrorHandling(t *testing.T) {
// Create registry service
testDB := database.NewTestDB(t)
registryService := service.NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
importerService := importer.NewService(registryService)
tests := []struct {
name string
path string
expectError bool
errorMsg string
}{
{
name: "non-existent local file",
path: "/tmp/non-existent-file.json",
expectError: true,
errorMsg: "failed to read seed data",
},
{
name: "invalid JSON file",
path: "/tmp/invalid.json",
expectError: true,
errorMsg: "failed to read seed data",
},
{
name: "non-existent HTTP URL",
path: "http://non-existent-domain-12345.com/seed.json",
expectError: true,
errorMsg: "failed to read seed data",
},
}
// Create invalid JSON file for testing
invalidJSON := []byte("{invalid json}")
tempFile, err := os.CreateTemp("", "invalid-*.json")
require.NoError(t, err)
defer os.Remove(tempFile.Name())
err = os.WriteFile(tempFile.Name(), invalidJSON, 0600)
require.NoError(t, err)
// Update test case to use temp file
for i := range tests {
if tests[i].path == "/tmp/invalid.json" {
tests[i].path = tempFile.Name()
break
}
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := importerService.ImportFromPath(context.Background(), tt.path)
if tt.expectError {
assert.Error(t, err)
if tt.errorMsg != "" {
assert.Contains(t, err.Error(), tt.errorMsg)
}
} else {
assert.NoError(t, err)
}
})
}
}