Skip to content

Commit 6b22cf0

Browse files
yshnggsridharavinashCopilot
authored
feat(config): specify to use in-memory database (#74)
<!-- Provide a brief summary of your changes --> Add support for specifying database type (MongoDB or in-memory database). ## Motivation and Context <!-- Why is this change needed? What problem does it solve? --> The README states that MCP Registry supports both MongoDB and in-memory databases, but does not provide the capability to specify which database type to use. https://github.com/modelcontextprotocol/registry/blob/5d9171bdccf579398273806a7067cda29e88363c/README.md?plain=1#L19 This PR addresses this limitation by adding explicit database type configuration through the `DATABASE_TYPE` environment variable, allowing users to choose between `mongodb` and `memory` database types in a standardized way. ## How Has This Been Tested? <!-- Have you tested this in a real application? Which scenarios were tested? --> Yes ```console $ export MCP_REGISTRY_DATABASE_TYPE="memory" $ ./scripts/test_endpoints.sh Testing health endpoint: http://localhost:8080/v0/health Status Code: 200 Response: { "status": "ok", "github_client_id": "" } Health check successful ------------------------------------- Testing servers endpoint: http://localhost:8080/v0/servers Status Code: 200 Response Summary: Total registries: 0 servers Names: Pagination Metadata: {} servers Details: { "servers": [], "metadata": {} } servers request successful ------------------------------------- Testing ping endpoint: http://localhost:8080/v0/ping Status Code: 200 Response: { "status": "ok", "version": "dev" } Ping successful ------------------------------------- All tests passed successfully! ``` ## Breaking Changes <!-- Will users need to update their code or configurations? --> No ## 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) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [x] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [x] I have added appropriate error handling - [x] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions --> Close #63 --------- Co-authored-by: Avinash Sridhar <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent d16328e commit 6b22cf0

File tree

3 files changed

+59
-39
lines changed

3 files changed

+59
-39
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ The service can be configured using environment variables:
289289
| Variable | Description | Default |
290290
|----------|-------------|---------|
291291
| `MCP_REGISTRY_APP_VERSION` | Application version | `dev` |
292+
| `MCP_REGISTRY_DATABASE_TYPE` | Database type | `mongodb` |
292293
| `MCP_REGISTRY_COLLECTION_NAME` | MongoDB collection name | `servers_v2` |
293294
| `MCP_REGISTRY_DATABASE_NAME` | MongoDB database name | `mcp-registry` |
294295
| `MCP_REGISTRY_DATABASE_URL` | MongoDB connection string | `mongodb://localhost:27017` |

cmd/registry/main.go

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/modelcontextprotocol/registry/internal/auth"
1616
"github.com/modelcontextprotocol/registry/internal/config"
1717
"github.com/modelcontextprotocol/registry/internal/database"
18+
"github.com/modelcontextprotocol/registry/internal/model"
1819
"github.com/modelcontextprotocol/registry/internal/service"
1920
)
2021

@@ -39,38 +40,48 @@ func main() {
3940
// Initialize services based on environment
4041
var registryService service.RegistryService
4142

42-
// Use MongoDB for real registry service in production/other environments
43-
// Create a context with timeout for MongoDB connection
44-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
45-
defer cancel()
46-
47-
// Connect to MongoDB
48-
mongoDB, err := database.NewMongoDB(ctx, cfg.DatabaseURL, cfg.DatabaseName, cfg.CollectionName)
49-
if err != nil {
50-
log.Printf("Failed to connect to MongoDB: %v", err)
51-
return
52-
}
53-
54-
// Create registry service with MongoDB
55-
registryService = service.NewRegistryServiceWithDB(mongoDB)
56-
log.Printf("MongoDB database name: %s", cfg.DatabaseName)
57-
log.Printf("MongoDB collection name: %s", cfg.CollectionName)
58-
59-
// Store the MongoDB instance for later cleanup
60-
defer func() {
61-
if err := mongoDB.Close(); err != nil {
62-
log.Printf("Error closing MongoDB connection: %v", err)
63-
} else {
64-
log.Println("MongoDB connection closed successfully")
43+
switch cfg.DatabaseType {
44+
case config.DatabaseTypeMemory:
45+
memoryDB := database.NewMemoryDB(map[string]*model.Server{})
46+
registryService = service.NewRegistryServiceWithDB(memoryDB)
47+
case config.DatabaseTypeMongoDB:
48+
// Use MongoDB for real registry service in production/other environments
49+
// Create a context with timeout for MongoDB connection
50+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
51+
defer cancel()
52+
53+
// Connect to MongoDB
54+
mongoDB, err := database.NewMongoDB(ctx, cfg.DatabaseURL, cfg.DatabaseName, cfg.CollectionName)
55+
if err != nil {
56+
log.Printf("Failed to connect to MongoDB: %v", err)
57+
return
6558
}
66-
}()
6759

68-
if cfg.SeedImport {
69-
log.Println("Importing data...")
70-
if err := database.ImportSeedFile(mongoDB, cfg.SeedFilePath); err != nil {
71-
log.Printf("Failed to import seed file: %v", err)
60+
// Create registry service with MongoDB
61+
registryService = service.NewRegistryServiceWithDB(mongoDB)
62+
log.Printf("MongoDB database name: %s", cfg.DatabaseName)
63+
log.Printf("MongoDB collection name: %s", cfg.CollectionName)
64+
65+
// Store the MongoDB instance for later cleanup
66+
defer func() {
67+
if err := mongoDB.Close(); err != nil {
68+
log.Printf("Error closing MongoDB connection: %v", err)
69+
} else {
70+
log.Println("MongoDB connection closed successfully")
71+
}
72+
}()
73+
74+
if cfg.SeedImport {
75+
log.Println("Importing data...")
76+
if err := database.ImportSeedFile(mongoDB, cfg.SeedFilePath); err != nil {
77+
log.Printf("Failed to import seed file: %v", err)
78+
} else {
79+
log.Println("Data import completed successfully")
80+
}
7281
}
73-
log.Println("Data import completed successfully")
82+
default:
83+
log.Printf("Invalid database type: %s; supported types: %s, %s", cfg.DatabaseType, config.DatabaseTypeMemory, config.DatabaseTypeMongoDB)
84+
return
7485
}
7586

7687
// Initialize authentication services

internal/config/config.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ import (
44
env "github.com/caarlos0/env/v11"
55
)
66

7+
type DatabaseType string
8+
9+
const (
10+
DatabaseTypeMongoDB DatabaseType = "mongodb"
11+
DatabaseTypeMemory DatabaseType = "memory"
12+
)
13+
714
// Config holds the application configuration
815
type Config struct {
9-
ServerAddress string `env:"SERVER_ADDRESS" envDefault:":8080"`
10-
DatabaseURL string `env:"DATABASE_URL" envDefault:"mongodb://localhost:27017"`
11-
DatabaseName string `env:"DATABASE_NAME" envDefault:"mcp-registry"`
12-
CollectionName string `env:"COLLECTION_NAME" envDefault:"servers_v2"`
13-
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
14-
SeedFilePath string `env:"SEED_FILE_PATH" envDefault:"data/seed.json"`
15-
SeedImport bool `env:"SEED_IMPORT" envDefault:"true"`
16-
Version string `env:"VERSION" envDefault:"dev"`
17-
GithubClientID string `env:"GITHUB_CLIENT_ID" envDefault:""`
18-
GithubClientSecret string `env:"GITHUB_CLIENT_SECRET" envDefault:""`
16+
ServerAddress string `env:"SERVER_ADDRESS" envDefault:":8080"`
17+
DatabaseType DatabaseType `env:"DATABASE_TYPE" envDefault:"mongodb"`
18+
DatabaseURL string `env:"DATABASE_URL" envDefault:"mongodb://localhost:27017"`
19+
DatabaseName string `env:"DATABASE_NAME" envDefault:"mcp-registry"`
20+
CollectionName string `env:"COLLECTION_NAME" envDefault:"servers_v2"`
21+
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
22+
SeedFilePath string `env:"SEED_FILE_PATH" envDefault:"data/seed.json"`
23+
SeedImport bool `env:"SEED_IMPORT" envDefault:"true"`
24+
Version string `env:"VERSION" envDefault:"dev"`
25+
GithubClientID string `env:"GITHUB_CLIENT_ID" envDefault:""`
26+
GithubClientSecret string `env:"GITHUB_CLIENT_SECRET" envDefault:""`
1927
}
2028

2129
// NewConfig creates a new configuration with default values

0 commit comments

Comments
 (0)