From ebfca5494312cafcecd3084c9b00f4cdf534b0c6 Mon Sep 17 00:00:00 2001 From: yshngg Date: Mon, 26 May 2025 10:13:55 +0800 Subject: [PATCH 1/6] feat(config): specify to use in-memory database --- cmd/registry/main.go | 68 ++++++++++++++++++++++----------------- internal/config/config.go | 28 ++++++++++------ 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/cmd/registry/main.go b/cmd/registry/main.go index 7042b515d..c1e3c22cf 100644 --- a/cmd/registry/main.go +++ b/cmd/registry/main.go @@ -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" ) @@ -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 diff --git a/internal/config/config.go b/internal/config/config.go index cbb5637c7..950c3f581 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 From 0de1c984b9725a8f0fb4641749d30fa048c0d88e Mon Sep 17 00:00:00 2001 From: yshngg Date: Mon, 26 May 2025 11:08:08 +0800 Subject: [PATCH 2/6] docs(README.md): update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5cd446a93..9ac3cdb6b 100644 --- a/README.md +++ b/README.md @@ -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` | From 9bc6b7298ae0405189ae714c5f8a29f101072447 Mon Sep 17 00:00:00 2001 From: yshngg Date: Wed, 4 Jun 2025 10:09:14 +0800 Subject: [PATCH 3/6] fix(cmd/registry/main): correct logging logic for seed import --- cmd/registry/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/registry/main.go b/cmd/registry/main.go index c1e3c22cf..5a030d268 100644 --- a/cmd/registry/main.go +++ b/cmd/registry/main.go @@ -75,8 +75,9 @@ func main() { log.Println("Importing data...") if err := database.ImportSeedFile(mongoDB, cfg.SeedFilePath); err != nil { log.Printf("Failed to import seed file: %v", err) + } else { + log.Println("Data import completed successfully") } - log.Println("Data import completed successfully") } default: log.Printf("Invalid database type: %s", cfg.DatabaseType) From 43702f4aa2cf8d0c6fa0480f2a2a751ee0ba2b20 Mon Sep 17 00:00:00 2001 From: Avinash Sridhar Date: Wed, 4 Jun 2025 09:14:42 -0400 Subject: [PATCH 4/6] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- cmd/registry/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/registry/main.go b/cmd/registry/main.go index 5a030d268..cb776f56b 100644 --- a/cmd/registry/main.go +++ b/cmd/registry/main.go @@ -80,7 +80,7 @@ func main() { } } default: - log.Printf("Invalid database type: %s", cfg.DatabaseType) + log.Printf("Invalid database type: %s; supported types: %s, %s", cfg.DatabaseType, config.DatabaseTypeMemory, config.DatabaseTypeMongoDB) return } From 8748904e12c885e4d2a7992429b7f610314e53ab Mon Sep 17 00:00:00 2001 From: Avinash Sridhar Date: Wed, 4 Jun 2025 09:14:49 -0400 Subject: [PATCH 5/6] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- cmd/registry/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/registry/main.go b/cmd/registry/main.go index cb776f56b..2763c647a 100644 --- a/cmd/registry/main.go +++ b/cmd/registry/main.go @@ -81,7 +81,7 @@ func main() { } default: log.Printf("Invalid database type: %s; supported types: %s, %s", cfg.DatabaseType, config.DatabaseTypeMemory, config.DatabaseTypeMongoDB) - return + os.Exit(1) } // Initialize authentication services From ba569ce409b5f0a1b40c6cfa9bea36a852717285 Mon Sep 17 00:00:00 2001 From: Avinash Sridhar Date: Wed, 4 Jun 2025 09:17:51 -0400 Subject: [PATCH 6/6] revert to pass linter --- cmd/registry/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/registry/main.go b/cmd/registry/main.go index 2763c647a..cb776f56b 100644 --- a/cmd/registry/main.go +++ b/cmd/registry/main.go @@ -81,7 +81,7 @@ func main() { } default: log.Printf("Invalid database type: %s; supported types: %s, %s", cfg.DatabaseType, config.DatabaseTypeMemory, config.DatabaseTypeMongoDB) - os.Exit(1) + return } // Initialize authentication services