Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ The service can be configured using environment variables:
| Variable | Description | Default |
|----------|-------------|---------|
| `MCP_REGISTRY_APP_VERSION` | Application version | `dev` |
| `MCP_REGISTRY_DATABASE_TYPE` | Database type | `mongodb` |
| `MCP_REGISTRY_COLLECTION_NAME` | MongoDB collection name | `servers_v2` |
| `MCP_REGISTRY_DATABASE_NAME` | MongoDB database name | `mcp-registry` |
| `MCP_REGISTRY_DATABASE_URL` | MongoDB connection string | `mongodb://localhost:27017` |
Expand Down
68 changes: 39 additions & 29 deletions cmd/registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/modelcontextprotocol/registry/internal/auth"
"github.com/modelcontextprotocol/registry/internal/config"
"github.com/modelcontextprotocol/registry/internal/database"
"github.com/modelcontextprotocol/registry/internal/model"
"github.com/modelcontextprotocol/registry/internal/service"
)

Expand All @@ -39,38 +40,47 @@ func main() {
// Initialize services based on environment
var registryService service.RegistryService

// Use MongoDB for real registry service in production/other environments
// Create a context with timeout for MongoDB connection
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// Connect to MongoDB
mongoDB, err := database.NewMongoDB(ctx, cfg.DatabaseURL, cfg.DatabaseName, cfg.CollectionName)
if err != nil {
log.Printf("Failed to connect to MongoDB: %v", err)
return
}

// Create registry service with MongoDB
registryService = service.NewRegistryServiceWithDB(mongoDB)
log.Printf("MongoDB database name: %s", cfg.DatabaseName)
log.Printf("MongoDB collection name: %s", cfg.CollectionName)

// Store the MongoDB instance for later cleanup
defer func() {
if err := mongoDB.Close(); err != nil {
log.Printf("Error closing MongoDB connection: %v", err)
} else {
log.Println("MongoDB connection closed successfully")
switch cfg.DatabaseType {
case config.DatabaseTypeMemory:
memoryDB := database.NewMemoryDB(map[string]*model.Server{})
registryService = service.NewRegistryServiceWithDB(memoryDB)
case config.DatabaseTypeMongoDB:
// Use MongoDB for real registry service in production/other environments
// Create a context with timeout for MongoDB connection
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// Connect to MongoDB
mongoDB, err := database.NewMongoDB(ctx, cfg.DatabaseURL, cfg.DatabaseName, cfg.CollectionName)
if err != nil {
log.Printf("Failed to connect to MongoDB: %v", err)
return
}
}()

if cfg.SeedImport {
log.Println("Importing data...")
if err := database.ImportSeedFile(mongoDB, cfg.SeedFilePath); err != nil {
log.Printf("Failed to import seed file: %v", err)
// Create registry service with MongoDB
registryService = service.NewRegistryServiceWithDB(mongoDB)
log.Printf("MongoDB database name: %s", cfg.DatabaseName)
log.Printf("MongoDB collection name: %s", cfg.CollectionName)

// Store the MongoDB instance for later cleanup
defer func() {
if err := mongoDB.Close(); err != nil {
log.Printf("Error closing MongoDB connection: %v", err)
} else {
log.Println("MongoDB connection closed successfully")
}
}()

if cfg.SeedImport {
log.Println("Importing data...")
if err := database.ImportSeedFile(mongoDB, cfg.SeedFilePath); err != nil {
log.Printf("Failed to import seed file: %v", err)
}
log.Println("Data import completed successfully")
}
log.Println("Data import completed successfully")
default:
log.Printf("Invalid database type: %s", cfg.DatabaseType)
return
}

// Initialize authentication services
Expand Down
28 changes: 18 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ import (
env "github.com/caarlos0/env/v11"
)

type DatabaseType string

const (
DatabaseTypeMongoDB DatabaseType = "mongodb"
DatabaseTypeMemory DatabaseType = "memory"
)

// Config holds the application configuration
type Config struct {
ServerAddress string `env:"SERVER_ADDRESS" envDefault:":8080"`
DatabaseURL string `env:"DATABASE_URL" envDefault:"mongodb://localhost:27017"`
DatabaseName string `env:"DATABASE_NAME" envDefault:"mcp-registry"`
CollectionName string `env:"COLLECTION_NAME" envDefault:"servers_v2"`
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
SeedFilePath string `env:"SEED_FILE_PATH" envDefault:"data/seed.json"`
SeedImport bool `env:"SEED_IMPORT" envDefault:"true"`
Version string `env:"VERSION" envDefault:"dev"`
GithubClientID string `env:"GITHUB_CLIENT_ID" envDefault:""`
GithubClientSecret string `env:"GITHUB_CLIENT_SECRET" envDefault:""`
ServerAddress string `env:"SERVER_ADDRESS" envDefault:":8080"`
DatabaseType DatabaseType `env:"DATABASE_TYPE" envDefault:"mongodb"`
DatabaseURL string `env:"DATABASE_URL" envDefault:"mongodb://localhost:27017"`
DatabaseName string `env:"DATABASE_NAME" envDefault:"mcp-registry"`
CollectionName string `env:"COLLECTION_NAME" envDefault:"servers_v2"`
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
SeedFilePath string `env:"SEED_FILE_PATH" envDefault:"data/seed.json"`
SeedImport bool `env:"SEED_IMPORT" envDefault:"true"`
Version string `env:"VERSION" envDefault:"dev"`
GithubClientID string `env:"GITHUB_CLIENT_ID" envDefault:""`
GithubClientSecret string `env:"GITHUB_CLIENT_SECRET" envDefault:""`
}

// NewConfig creates a new configuration with default values
Expand Down
Loading