feat: implement publish endpoint and related database functionality
This commit is contained in:
parent
b09275fa67
commit
582d93a103
9 changed files with 417 additions and 30 deletions
64
internal/api/handlers/v0/publish.go
Normal file
64
internal/api/handlers/v0/publish.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// Package v0 contains API handlers for version 0 of the API
|
||||
package v0
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/model"
|
||||
"github.com/modelcontextprotocol/registry/internal/service"
|
||||
)
|
||||
|
||||
// PublishHandler handles requests to publish new server details to the registry
|
||||
func PublishHandler(registry service.RegistryService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Only allow POST method
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Read the request body
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "Error reading request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
// Parse request body into ServerDetail struct
|
||||
var serverDetail model.ServerDetail
|
||||
err = json.Unmarshal(body, &serverDetail)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid request payload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if serverDetail.Name == "" {
|
||||
http.Error(w, "Name is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// version is required
|
||||
if serverDetail.VersionDetail.Version == "" {
|
||||
http.Error(w, "Version is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Call the publish method on the registry service
|
||||
err = registry.Publish(&serverDetail)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to publish server details: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"message": "Server publication successful",
|
||||
"id": serverDetail.ID,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@ func RegisterV0Routes(mux *http.ServeMux, cfg *config.Config, registry service.R
|
|||
mux.HandleFunc("/v0/servers", v0.ServersHandler(registry))
|
||||
mux.HandleFunc("/v0/servers/{id}", v0.ServersDetailHandler(registry))
|
||||
mux.HandleFunc("/v0/ping", v0.PingHandler(cfg))
|
||||
mux.HandleFunc("/v0/publish", v0.PublishHandler(registry))
|
||||
|
||||
// Register Swagger UI routes
|
||||
mux.HandleFunc("/v0/swagger/", v0.SwaggerHandler())
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ type Database interface {
|
|||
List(ctx context.Context, filter map[string]interface{}, cursor string, limit int) ([]*model.Entry, string, error)
|
||||
// GetByID retrieves a single ServerDetail by it's ID
|
||||
GetByID(ctx context.Context, id string) (*model.ServerDetail, error)
|
||||
// Publish adds a new ServerDetail to the database
|
||||
Publish(ctx context.Context, serverDetail *model.ServerDetail) error
|
||||
// Close closes the database connection
|
||||
Close() error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ func (db *MemoryDB) List(ctx context.Context, filter map[string]interface{}, cur
|
|||
include = false
|
||||
}
|
||||
case "version":
|
||||
if entry.Version != value.(string) {
|
||||
if entry.VersionDetail.Version != value.(string) {
|
||||
include = false
|
||||
}
|
||||
// Add more filter options as needed
|
||||
|
|
@ -125,17 +125,54 @@ func (db *MemoryDB) GetByID(ctx context.Context, id string) (*model.ServerDetail
|
|||
|
||||
if entry, exists := db.entries[id]; exists {
|
||||
return &model.ServerDetail{
|
||||
ID: entry.ID,
|
||||
Name: entry.Name,
|
||||
Description: entry.Description,
|
||||
Version: entry.Version,
|
||||
Repository: entry.Repository,
|
||||
ID: entry.ID,
|
||||
Name: entry.Name,
|
||||
Description: entry.Description,
|
||||
VersionDetail: entry.VersionDetail,
|
||||
Repository: entry.Repository,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
// Publish adds a new ServerDetail to the database
|
||||
func (db *MemoryDB) Publish(ctx context.Context, serverDetail *model.ServerDetail) error {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
db.mu.Lock()
|
||||
defer db.mu.Unlock()
|
||||
|
||||
// check for name
|
||||
if serverDetail.Name == "" {
|
||||
return ErrInvalidInput
|
||||
}
|
||||
|
||||
// check that the name and the version are unique
|
||||
|
||||
for _, entry := range db.entries {
|
||||
if entry.Name == serverDetail.Name && entry.VersionDetail.Version == serverDetail.VersionDetail.Version {
|
||||
return ErrAlreadyExists
|
||||
}
|
||||
}
|
||||
|
||||
if serverDetail.Repository.URL == "" {
|
||||
return ErrInvalidInput
|
||||
}
|
||||
|
||||
db.entries[serverDetail.ID] = &model.Entry{
|
||||
ID: serverDetail.ID,
|
||||
Name: serverDetail.Name,
|
||||
Description: serverDetail.Description,
|
||||
VersionDetail: serverDetail.VersionDetail,
|
||||
Repository: serverDetail.Repository,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the database connection
|
||||
// For an in-memory database, this is a no-op
|
||||
func (db *MemoryDB) Close() error {
|
||||
|
|
|
|||
|
|
@ -40,13 +40,17 @@ func NewMongoDB(ctx context.Context, connectionURI, databaseName, collectionName
|
|||
// Create indexes for better query performance
|
||||
models := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "name", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
Keys: bson.D{{Key: "name", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "id", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
// add an index for the combination of name and version
|
||||
{
|
||||
Keys: bson.D{{Key: "name", Value: 1}, {Key: "versiondetail.version", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
|
||||
_, err = collection.Indexes().CreateMany(ctx, models)
|
||||
|
|
@ -73,7 +77,9 @@ func (db *MongoDB) List(ctx context.Context, filter map[string]interface{}, curs
|
|||
}
|
||||
|
||||
// Convert Go map to MongoDB filter
|
||||
mongoFilter := bson.M{}
|
||||
mongoFilter := bson.M{
|
||||
"versiondetail.islatest": true,
|
||||
}
|
||||
// Map common filter keys to MongoDB document paths
|
||||
for k, v := range filter {
|
||||
// Handle nested fields with dot notation
|
||||
|
|
@ -164,6 +170,53 @@ func (db *MongoDB) GetByID(ctx context.Context, id string) (*model.ServerDetail,
|
|||
return &entry, nil
|
||||
}
|
||||
|
||||
// Publish adds a new ServerDetail to the database
|
||||
func (db *MongoDB) Publish(ctx context.Context, serverDetail *model.ServerDetail) error {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
// find a server detail with the same name and check that the current version is greater than the existing one
|
||||
filter := bson.M{
|
||||
"name": serverDetail.Name,
|
||||
"versiondetail.islatest": true,
|
||||
}
|
||||
|
||||
var existingEntry model.ServerDetail
|
||||
err := db.collection.FindOne(ctx, filter).Decode(&existingEntry)
|
||||
if err != nil && err != mongo.ErrNoDocuments {
|
||||
return fmt.Errorf("error checking existing entry: %w", err)
|
||||
}
|
||||
|
||||
// check that the current version is greater than the existing one
|
||||
if serverDetail.VersionDetail.Version <= existingEntry.VersionDetail.Version {
|
||||
return fmt.Errorf("version must be greater than existing version")
|
||||
}
|
||||
|
||||
// update the existing entry to not be the latest version
|
||||
if existingEntry.ID != "" {
|
||||
_, err = db.collection.UpdateOne(ctx, bson.M{"id": existingEntry.ID}, bson.M{"$set": bson.M{"versiondetail.islatest": false}})
|
||||
if err != nil {
|
||||
return fmt.Errorf("error updating existing entry: %w", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
serverDetail.ID = uuid.New().String()
|
||||
serverDetail.VersionDetail.IsLatest = true
|
||||
serverDetail.VersionDetail.ReleaseDate = time.Now().Format(time.RFC3339)
|
||||
|
||||
// Insert the entry into the database
|
||||
_, err = db.collection.InsertOne(ctx, serverDetail)
|
||||
if err != nil {
|
||||
if mongo.IsDuplicateKeyError(err) {
|
||||
return ErrAlreadyExists
|
||||
}
|
||||
return fmt.Errorf("error inserting entry: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the database connection
|
||||
func (db *MongoDB) Close() error {
|
||||
return db.client.Disconnect(context.Background())
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
package model
|
||||
|
||||
type Entry struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Repository Repository `json:"repository,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Repository Repository `json:"repository,omitempty"`
|
||||
VersionDetail VersionDetail `json:"version_detail,omitempty"`
|
||||
}
|
||||
|
||||
type Repository struct {
|
||||
|
|
@ -14,16 +14,21 @@ type Repository struct {
|
|||
Branch string `json:"branch,omitempty"`
|
||||
Commit string `json:"commit,omitempty"`
|
||||
}
|
||||
type VersionDetail struct {
|
||||
Version string `json:"version,omitempty"`
|
||||
ReleaseDate string `json:"release_date,omitempty"` //RFC 3339 date format
|
||||
IsLatest bool `json:"is_latest,omitempty"`
|
||||
}
|
||||
|
||||
type ServerDetail struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Repository Repository `json:"repository,omitempty"`
|
||||
RegistryCanonical string `json:"registry_canonical,omitempty"`
|
||||
Registries []Registries `json:"registries,omitempty"`
|
||||
Remotes []Remotes `json:"remotes,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
VersionDetail VersionDetail `json:"version_detail,omitempty"`
|
||||
Repository Repository `json:"repository,omitempty"`
|
||||
RegistryCanonical string `json:"registry_canonical,omitempty"`
|
||||
Registries []Registries `json:"registries,omitempty"`
|
||||
Remotes []Remotes `json:"remotes,omitempty"`
|
||||
}
|
||||
|
||||
type Registries struct {
|
||||
|
|
|
|||
|
|
@ -28,7 +28,11 @@ func NewFakeRegistryService() RegistryService {
|
|||
Branch: "main",
|
||||
Commit: "abc123def456",
|
||||
},
|
||||
Version: "1.0.0",
|
||||
VersionDetail: model.VersionDetail{
|
||||
Version: "1.0.0",
|
||||
ReleaseDate: time.Now().Format(time.RFC3339),
|
||||
IsLatest: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: uuid.New().String(),
|
||||
|
|
@ -40,7 +44,11 @@ func NewFakeRegistryService() RegistryService {
|
|||
Branch: "develop",
|
||||
Commit: "def456ghi789",
|
||||
},
|
||||
Version: "2.1.0",
|
||||
VersionDetail: model.VersionDetail{
|
||||
Version: "0.9.0",
|
||||
ReleaseDate: time.Now().Format(time.RFC3339),
|
||||
IsLatest: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: uuid.New().String(),
|
||||
|
|
@ -52,7 +60,11 @@ func NewFakeRegistryService() RegistryService {
|
|||
Branch: "feature/mcp-server",
|
||||
Commit: "789jkl012mno",
|
||||
},
|
||||
Version: "0.9.5",
|
||||
VersionDetail: model.VersionDetail{
|
||||
Version: "0.9.5",
|
||||
ReleaseDate: time.Now().Format(time.RFC3339),
|
||||
IsLatest: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -103,6 +115,16 @@ func (s *fakeRegistryService) GetByID(id string) (*model.ServerDetail, error) {
|
|||
return serverDetail, nil
|
||||
}
|
||||
|
||||
// Publish adds a new server detail to the in-memory database
|
||||
func (s *fakeRegistryService) Publish(serverDetail *model.ServerDetail) error {
|
||||
// Create a timeout context for the database operation
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Use the database's Publish method to add the server detail
|
||||
return s.db.Publish(ctx, serverDetail)
|
||||
}
|
||||
|
||||
// Close closes the in-memory database connection
|
||||
func (s *fakeRegistryService) Close() error {
|
||||
return s.db.Close()
|
||||
|
|
|
|||
|
|
@ -81,3 +81,21 @@ func (s *registryServiceImpl) GetByID(id string) (*model.ServerDetail, error) {
|
|||
|
||||
return serverDetail, nil
|
||||
}
|
||||
|
||||
// Publish adds a new server detail to the registry
|
||||
func (s *registryServiceImpl) Publish(serverDetail *model.ServerDetail) error {
|
||||
// Create a timeout context for the database operation
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if serverDetail == nil {
|
||||
return database.ErrInvalidInput
|
||||
}
|
||||
|
||||
err := s.db.Publish(ctx, serverDetail)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,4 +6,5 @@ import "github.com/modelcontextprotocol/registry/internal/model"
|
|||
type RegistryService interface {
|
||||
List(cursor string, limit int) ([]model.Entry, string, error)
|
||||
GetByID(id string) (*model.ServerDetail, error)
|
||||
Publish(serverDetail *model.ServerDetail) error
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue