<!-- 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>
218 lines
No EOL
5.9 KiB
Go
218 lines
No EOL
5.9 KiB
Go
package database_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/modelcontextprotocol/registry/internal/database"
|
|
"github.com/modelcontextprotocol/registry/internal/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{
|
|
{
|
|
Server: model.ServerDetail{
|
|
Name: "test-server-1",
|
|
Description: "Test server 1",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/test/repo1",
|
|
Source: "github",
|
|
ID: "123",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
},
|
|
XIOModelContextProtocolRegistry: map[string]interface{}{
|
|
"id": "test-id-1",
|
|
"published_at": "2023-01-01T00:00:00Z",
|
|
"is_latest": true,
|
|
},
|
|
},
|
|
}
|
|
|
|
// Write seed data to temp file
|
|
data, err := json.Marshal(seedData)
|
|
assert.NoError(t, err)
|
|
|
|
err = func() error {
|
|
f, err := os.Create(tempFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
_, err = f.Write(data)
|
|
return err
|
|
}()
|
|
assert.NoError(t, err)
|
|
defer os.Remove(tempFile)
|
|
|
|
// Test reading the file
|
|
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)
|
|
}
|
|
|
|
func TestReadSeedFile_DirectHTTPURL(t *testing.T) {
|
|
// Create a test HTTP server that serves seed JSON directly in extension wrapper format
|
|
seedData := []model.ServerResponse{
|
|
{
|
|
Server: model.ServerDetail{
|
|
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,
|
|
},
|
|
},
|
|
}
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(seedData); err != nil {
|
|
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
|
|
// Test reading from HTTP URL ending in .json
|
|
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)
|
|
}
|
|
|
|
func TestReadSeedFile_RegistryURL(t *testing.T) {
|
|
// Create mock registry responses
|
|
server1 := model.ServerResponse{
|
|
Server: model.ServerDetail{
|
|
Name: "Test Server 1",
|
|
Description: "First test server",
|
|
},
|
|
XIOModelContextProtocolRegistry: map[string]interface{}{
|
|
"id": "server-1",
|
|
"published_at": "2023-01-01T00:00:00Z",
|
|
"is_latest": true,
|
|
},
|
|
}
|
|
server2 := model.ServerResponse{
|
|
Server: model.ServerDetail{
|
|
Name: "Test Server 2",
|
|
Description: "Second test server",
|
|
},
|
|
XIOModelContextProtocolRegistry: map[string]interface{}{
|
|
"id": "server-2",
|
|
"published_at": "2023-01-01T00:00:00Z",
|
|
"is_latest": true,
|
|
},
|
|
}
|
|
|
|
serverDetail1 := model.ServerDetail{
|
|
Name: "Test Server 1",
|
|
Description: "First test server",
|
|
Packages: []model.Package{
|
|
{
|
|
RegistryName: "npm",
|
|
Name: "test-package-1",
|
|
Version: "1.0.0",
|
|
},
|
|
},
|
|
}
|
|
serverDetail2 := model.ServerDetail{
|
|
Name: "Test Server 2",
|
|
Description: "Second test server",
|
|
Packages: []model.Package{
|
|
{
|
|
RegistryName: "npm",
|
|
Name: "test-package-2",
|
|
Version: "2.0.0",
|
|
},
|
|
},
|
|
}
|
|
|
|
// Create a test HTTP server that simulates the registry API
|
|
mux := http.NewServeMux()
|
|
|
|
// Handle /v0/servers endpoint (paginated)
|
|
mux.HandleFunc("/v0/servers", func(w http.ResponseWriter, r *http.Request) {
|
|
cursor := r.URL.Query().Get("cursor")
|
|
|
|
type Metadata struct {
|
|
NextCursor string `json:"next_cursor,omitempty"`
|
|
Count int `json:"count,omitempty"`
|
|
}
|
|
|
|
type PaginatedResponse struct {
|
|
Servers []model.ServerResponse `json:"servers"`
|
|
Metadata *Metadata `json:"metadata,omitempty"`
|
|
}
|
|
|
|
var response PaginatedResponse
|
|
switch cursor {
|
|
case "":
|
|
// First page
|
|
response = PaginatedResponse{
|
|
Servers: []model.ServerResponse{server1},
|
|
Metadata: &Metadata{
|
|
NextCursor: "next-cursor-1",
|
|
Count: 1,
|
|
},
|
|
}
|
|
case "next-cursor-1":
|
|
// Second page
|
|
response = PaginatedResponse{
|
|
Servers: []model.ServerResponse{server2},
|
|
Metadata: &Metadata{
|
|
Count: 1,
|
|
// No NextCursor means end of pagination
|
|
},
|
|
}
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError)
|
|
}
|
|
})
|
|
|
|
// Handle individual server detail endpoints
|
|
mux.HandleFunc("/v0/servers/server-1", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(serverDetail1); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
})
|
|
|
|
mux.HandleFunc("/v0/servers/server-2", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(serverDetail2); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
})
|
|
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
// Test reading from registry root URL (this should trigger pagination)
|
|
result, err := database.ReadSeedFile(context.Background(), server.URL+"/v0/servers")
|
|
assert.NoError(t, err)
|
|
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)
|
|
|
|
// Verify metadata was extracted
|
|
assert.Equal(t, "server-1", result[0].RegistryMetadata.ID)
|
|
assert.Equal(t, "server-2", result[1].RegistryMetadata.ID)
|
|
} |