<!-- 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>
134 lines
3.7 KiB
Go
134 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/modelcontextprotocol/registry/internal/api"
|
|
"github.com/modelcontextprotocol/registry/internal/config"
|
|
"github.com/modelcontextprotocol/registry/internal/database"
|
|
"github.com/modelcontextprotocol/registry/internal/model"
|
|
"github.com/modelcontextprotocol/registry/internal/service"
|
|
"github.com/modelcontextprotocol/registry/internal/telemetry"
|
|
)
|
|
|
|
func main() {
|
|
// Parse command line flags
|
|
showVersion := flag.Bool("version", false, "Display version information")
|
|
flag.Parse()
|
|
|
|
// Show version information if requested
|
|
if *showVersion {
|
|
log.Printf("MCP Registry v%s\n", Version)
|
|
log.Printf("Git commit: %s\n", GitCommit)
|
|
log.Printf("Build time: %s\n", BuildTime)
|
|
return
|
|
}
|
|
|
|
log.Printf("Starting MCP Registry Application v%s (commit: %s)", Version, GitCommit)
|
|
|
|
var (
|
|
registryService service.RegistryService
|
|
db database.Database
|
|
err error
|
|
)
|
|
|
|
// Initialize configuration
|
|
cfg := config.NewConfig()
|
|
|
|
// Initialize services based on environment
|
|
switch cfg.DatabaseType {
|
|
case config.DatabaseTypeMemory:
|
|
db = database.NewMemoryDB(map[string]*model.ServerDetail{})
|
|
registryService = service.NewRegistryServiceWithDB(db)
|
|
case config.DatabaseTypePostgreSQL:
|
|
// Use PostgreSQL for real registry service
|
|
// Create a context with timeout for PostgreSQL connection
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
// Connect to PostgreSQL
|
|
db, err = database.NewPostgreSQL(ctx, cfg.DatabaseURL)
|
|
if err != nil {
|
|
log.Printf("Failed to connect to PostgreSQL: %v", err)
|
|
return
|
|
}
|
|
|
|
// Create registry service with PostgreSQL
|
|
registryService = service.NewRegistryServiceWithDB(db)
|
|
log.Printf("PostgreSQL database URL: %s", cfg.DatabaseURL)
|
|
|
|
// Store the PostgreSQL instance for later cleanup
|
|
defer func() {
|
|
if err := db.Close(); err != nil {
|
|
log.Printf("Error closing PostgreSQL connection: %v", err)
|
|
} else {
|
|
log.Println("PostgreSQL connection closed successfully")
|
|
}
|
|
}()
|
|
default:
|
|
log.Printf("Invalid database type: %s; supported types: %s, %s", cfg.DatabaseType, config.DatabaseTypeMemory, config.DatabaseTypePostgreSQL)
|
|
return
|
|
}
|
|
|
|
// Import seed data if seed source is provided
|
|
if cfg.SeedFrom != "" {
|
|
log.Printf("Importing data from %s...", cfg.SeedFrom)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
|
defer cancel()
|
|
|
|
if err := db.ImportSeed(ctx, cfg.SeedFrom); err != nil {
|
|
log.Printf("Failed to import seed data: %v", err)
|
|
} else {
|
|
log.Println("Data import completed successfully")
|
|
}
|
|
}
|
|
|
|
shutdownTelemetry, metrics, err := telemetry.InitMetrics(cfg.Version)
|
|
if err != nil {
|
|
log.Printf("Failed to initialize metrics: %v", err)
|
|
return
|
|
}
|
|
|
|
defer func() {
|
|
if err := shutdownTelemetry(context.Background()); err != nil {
|
|
log.Printf("Failed to shutdown telemetry: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Initialize HTTP server
|
|
server := api.NewServer(cfg, registryService, metrics)
|
|
|
|
// Start server in a goroutine so it doesn't block signal handling
|
|
go func() {
|
|
if err := server.Start(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
log.Printf("Failed to start server: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
|
|
// Wait for interrupt signal to gracefully shutdown the server
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
<-quit
|
|
log.Println("Shutting down server...")
|
|
|
|
// Create context with timeout for shutdown
|
|
sctx, scancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer scancel()
|
|
|
|
// Gracefully shutdown the server
|
|
if err := server.Shutdown(sctx); err != nil {
|
|
log.Printf("Server forced to shutdown: %v", err)
|
|
}
|
|
|
|
log.Println("Server exiting")
|
|
}
|