Init world. Basic registry API server

Work in progress .
This commit adds the core registry service implementation with methods to:
- Retrieve all registry entries
- List entries with cursor-based pagination
- Fetch specific server details by ID
This commit is contained in:
Avinash Sridhar 2025-04-22 17:09:44 -04:00
commit 4fb5bc477e
No known key found for this signature in database
3 changed files with 1702 additions and 2 deletions

View file

@ -0,0 +1,15 @@
// Package v0 contains API handlers for version 0 of the API
package v0
import (
"encoding/json"
"net/http"
)
// HealthHandler returns a handler for health check endpoint
func HealthHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
}

View file

@ -0,0 +1,27 @@
// Package v0 contains API handlers for version 0 of the API
package v0
import (
"encoding/json"
"net/http"
"github.com/modelcontextprotocol/registry/internal/config"
)
// PingHandler returns a handler for the ping endpoint that returns build version
func PingHandler(cfg *config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
response := map[string]string{
"status": "ok",
"version": cfg.Version,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
}

View file

@ -0,0 +1,125 @@
// Package v0 contains API handlers for version 0 of the API
package v0
import (
"encoding/json"
"net/http"
"strconv"
"github.com/google/uuid"
"github.com/modelcontextprotocol/registry/internal/model"
"github.com/modelcontextprotocol/registry/internal/service"
)
// Response is a paginated API response
type PaginatedResponse struct {
Data []model.Entry `json:"servers"`
Metadata Metadata `json:"metadata,omitempty"`
}
// Metadata contains pagination metadata
type Metadata struct {
NextCursor string `json:"next_cursor,omitempty"`
}
// ServersHandler returns a handler for listing registry items
func ServersHandler(registry service.RegistryService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Parse cursor and limit from query parameters
cursor := r.URL.Query().Get("cursor")
if cursor != "" {
_, err := uuid.Parse(cursor)
if err != nil {
http.Error(w, "Invalid cursor parameter", http.StatusBadRequest)
return
}
}
limitStr := r.URL.Query().Get("limit")
// Default limit if not specified
limit := 30
// Try to parse limit from query param
if limitStr != "" {
parsedLimit, err := strconv.Atoi(limitStr)
if err != nil {
http.Error(w, "Invalid limit parameter", http.StatusBadRequest)
return
}
// Check if limit is within reasonable bounds
if parsedLimit <= 0 {
http.Error(w, "Limit must be greater than 0", http.StatusBadRequest)
return
}
if parsedLimit > 100 {
// Cap maximum limit to prevent excessive queries
limit = 100
} else {
limit = parsedLimit
}
}
// Use the GetAll method to get paginated results
registries, nextCursor, err := registry.List(cursor, limit)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create paginated response
response := PaginatedResponse{
Data: registries,
}
// Add metadata if there's a next cursor
if nextCursor != "" {
response.Metadata = Metadata{
NextCursor: nextCursor,
}
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
}
// ServersDetailHandler returns a handler for getting details of a specific server by ID
func ServersDetailHandler(registry service.RegistryService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Extract the server ID from the URL path
id := r.PathValue("id")
// Validate that the ID is a valid UUID
_, err := uuid.Parse(id)
if err != nil {
http.Error(w, "Invalid server ID format", http.StatusBadRequest)
return
}
// Get the server details from the registry service
serverDetail, err := registry.GetByID(id)
if err != nil {
if err.Error() == "record not found" {
http.Error(w, "Server not found", http.StatusNotFound)
return
}
http.Error(w, "Error retrieving server details", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(serverDetail)
}
}