Refactor database implementation to use PostgreSQL instead of MongoDB (#289)

Fixes #228

## Motivation and Context

See #19

## How Has This Been Tested?

- Run locally
- Run in docker compose
- Run infra in minikube
- Unit and integration tests all passing :)

## Breaking Changes

Yes: users will not be able to access any data they used to have in
custom mongodb. We are accepting this breaking change, given the
registry is in experimental development status.

## 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

## 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

I have not implemented connection pooling support for simplicity, and
because cloudnative-pg already provides built-in connection pooling with
pgBouncer so if we want it later we can switch to this endpoint easily.
This commit is contained in:
adam jones 2025-08-22 01:40:28 +01:00 • committed by GitHub
commit ebaf4c9c99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 846 additions and 564 deletions

View file

@ -6,8 +6,9 @@ MCP_REGISTRY_VERSION=dev
MCP_REGISTRY_LOG_LEVEL=info
# Database configuration
MCP_REGISTRY_DATABASE_TYPE=mongodb
MCP_REGISTRY_DATABASE_URL=mongodb://username:password@localhost:27017
# Supported types: postgresql, memory
MCP_REGISTRY_DATABASE_TYPE=postgresql
MCP_REGISTRY_DATABASE_URL=postgres://username:password@localhost:5432/mcp-registry
MCP_REGISTRY_DATABASE_NAME=mcp-registry
MCP_REGISTRY_COLLECTION_NAME=servers_v2

View file

@ -57,7 +57,7 @@ check: lint validate test-all ## Run all checks (lint, validate, unit tests)
dev-compose: ## Start development environment with Docker Compose (builds image automatically)
docker compose up --build
dev-local: ## Run registry locally (requires MongoDB)
dev-local: ## Run registry locally
go run ./cmd/registry
# Cleanup

View file

@ -24,7 +24,7 @@ The MCP Registry service provides a centralized repository for MCP server entrie
- Health check endpoint for service monitoring
- Support for various environment configurations
- Graceful shutdown handling
- MongoDB and in-memory database support
- PostgreSQL and in-memory database support
- Comprehensive API documentation
- Pagination support for listing registry entries
- Seed data export/import composability with HTTP support
@ -35,7 +35,7 @@ The MCP Registry service provides a centralized repository for MCP server entrie
### Prerequisites
- Go 1.24.x (required - check with `go version`)
- MongoDB
- PostgreSQL
- Docker (optional, but recommended for development)
For development:
@ -46,14 +46,13 @@ For development:
## Running
The easiest way to get the registry running is uses docker compose. This will setup the MCP Registry service, import the seed data and run MongoDB in a local Docker environment.
The easiest way to get the registry running is uses docker compose. This will setup the MCP Registry service, import the seed data and run PostgreSQL in a local Docker environment.
```bash
# Run the registry and MongoDB with docker compose
make dev-compose
```
This will start the MCP Registry service and MongoDB with Docker, running at [`localhost:8080`](http://localhost:8080).
This will start the MCP Registry service running at [`localhost:8080`](http://localhost:8080).
## Building
@ -63,15 +62,15 @@ If you prefer to run the service locally without Docker, you can build and run i
# Build a registry executable
make build
```
This will create the `registry` binary in the current directory. You'll need to have MongoDB running locally or with Docker.
This will create the `registry` binary in the current directory.
To run the service locally:
```bash
# Run registry locally (requires MongoDB)
# Run registry locally
make dev-local
```
By default, the service will run on [`localhost:8080`](http://localhost:8080).
By default, the service will run on [`localhost:8080`](http://localhost:8080). You'll need to use the in-memory database or have PostgreSQL running.
To build the CLI tool for publishing MCP servers to the registry:
@ -95,11 +94,12 @@ Key development commands:
```bash
# Development
make dev-compose # Start development environment with Docker Compose
make dev-local # Run registry locally (requires MongoDB)
make dev-local # Run registry locally
# Build targets
make build # Build the registry application
make publisher # Build the publisher tool
make migrate # Build the database migration tool
# Testing
make test-unit # Run unit tests with coverage report
@ -156,7 +156,7 @@ This will prevent commits that fail linting or have formatting issues.
│ ├── api/ # HTTP server and request handlers (routing)
│ ├── auth/ # GitHub OAuth integration
│ ├── config/ # Configuration management
│ ├── database/ # Data persistence abstraction (MongoDB and in-memory)
│ ├── database/ # Data persistence abstraction
│ ├── model/ # Data models and domain structures
│ └── service/ # Business logic implementation
├── pkg/ # Public libraries
@ -175,9 +175,9 @@ This will prevent commits that fail linting or have formatting issues.
5. JSON responses returned to clients
### Key Interfaces
- **Database Interface** (`internal/database/database.go`) - Abstracts data persistence with MongoDB and memory implementations
- **Database Interface** (`internal/database/database.go`) - Abstracts data persistence with PostgreSQL and in-memory implementations
- **RegistryService** (`internal/service/service.go`) - Business logic abstraction over database
- **Auth Service** (`internal/auth/auth.go`) - GitHub OAuth token validation
- **Auth Service** (`internal/auth/jwt.go`) - Registry token creation and validation
### Authentication Flow
Publishing requires GitHub OAuth validation:

View file

@ -48,38 +48,37 @@ func main() {
case config.DatabaseTypeMemory:
db = database.NewMemoryDB(map[string]*model.Server{})
registryService = service.NewRegistryServiceWithDB(db)
case config.DatabaseTypeMongoDB:
// Use MongoDB for real registry service in production/other environments
// Create a context with timeout for MongoDB connection
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 MongoDB
db, err = database.NewMongoDB(ctx, cfg.DatabaseURL, cfg.DatabaseName, cfg.CollectionName)
// Connect to PostgreSQL
db, err = database.NewPostgreSQL(ctx, cfg.DatabaseURL)
if err != nil {
log.Printf("Failed to connect to MongoDB: %v", err)
log.Printf("Failed to connect to PostgreSQL: %v", err)
return
}
// Create registry service with MongoDB
// Create registry service with PostgreSQL
registryService = service.NewRegistryServiceWithDB(db)
log.Printf("MongoDB database name: %s", cfg.DatabaseName)
log.Printf("MongoDB collection name: %s", cfg.CollectionName)
log.Printf("PostgreSQL database URL: %s", cfg.DatabaseURL)
// Store the MongoDB instance for later cleanup
// Store the PostgreSQL instance for later cleanup
defer func() {
if err := db.Close(); err != nil {
log.Printf("Error closing MongoDB connection: %v", err)
log.Printf("Error closing PostgreSQL connection: %v", err)
} else {
log.Println("MongoDB connection closed successfully")
log.Println("PostgreSQL connection closed successfully")
}
}()
default:
log.Printf("Invalid database type: %s; supported types: %s, %s", cfg.DatabaseType, config.DatabaseTypeMemory, config.DatabaseTypeMongoDB)
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 (works for both memory and MongoDB)
// 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)

View file

@ -3,4 +3,5 @@ config:
mcp-registry:provider: local
mcp-registry:githubClientId: Iv23licy3GSiM9Km5jtd
mcp-registry:githubClientSecret: 0e8db54879b02c29adef51795586f3c510a9341d
mcp-registry:jwtPrivateKey: bb2c6b424005acd5df47a9e2c87f446def86dd740c888ea3efb825b23f7ef47c
encryptionsalt: v1:ijIHaqhbXVA=:v1:7voX1Kv+Bunz33iN:fyVHMOhlGIymzJ+ILgUBy3ExTwUUnA==

View file

@ -11,7 +11,7 @@ Pre-requisites:
- Access to a Kubernetes cluster via kubeconfig. You can run a cluster locally with [minikube](https://minikube.sigs.k8s.io/docs/start/).
1. Ensure your kubeconfig is configured at the cluster you want to use. For minikube, run `minikube start && minikube tunnel`.
2. Run `make local-up` to deploy the stack. Run this again if the first attempt fails.
2. Run `make local-up` to deploy the stack.
3. Access the repository via the ingress load balancer. You can find its external IP with `kubectl get svc ingress-nginx-controller -n ingress-nginx`. Then run `curl -H "Host: local.registry.modelcontextprotocol.io" -k https://<EXTERNAL-IP>/v0/ping` to check that the service is up.
#### To change config
@ -103,7 +103,7 @@ Pre-requisites:
│ ├── cert_manager.go # SSL certificate management
│ ├── deploy.go # Deployment orchestration
│ ├── ingress.go # Ingress controller setup
│ ├── mongodb.go # MongoDB deployment
│ ├── postgres.go # PostgreSQL database deployment
│ └── registry.go # MCP Registry deployment
└── providers/ # Kubernetes cluster providers
├── types.go # Provider interface definitions
@ -122,7 +122,7 @@ Pre-requisites:
5. `k8s.DeployAll()` orchestrates complete deployment:
- Certificate manager for SSL/TLS
- Ingress controller for external access
- MongoDB for data persistence
- Database for data persistence
- MCP Registry application
## Configuration
@ -152,5 +152,5 @@ kubectl get svc -n ingress-nginx
```bash
kubectl logs -l app=mcp-registry
kubectl logs -l app=mongodb
kubectl logs -l app=postgres
```

View file

@ -69,7 +69,7 @@ func SetupCertManager(ctx *pulumi.Context, cluster *providers.ProviderInfo) erro
},
},
},
}, pulumi.Provider(cluster.Provider), pulumi.DependsOn([]pulumi.Resource{certManager}))
}, pulumi.Provider(cluster.Provider), pulumi.DependsOnInputs(certManager.Ready))
if err != nil {
return err
}

View file

@ -16,19 +16,19 @@ func DeployAll(ctx *pulumi.Context, cluster *providers.ProviderInfo, environment
}
// Setup ingress controller
err = SetupIngressController(ctx, cluster, environment)
ingressNginx, err := SetupIngressController(ctx, cluster, environment)
if err != nil {
return nil, err
}
// Deploy MongoDB
err = DeployMongoDB(ctx, cluster, environment)
// Deploy PostgreSQL databases
pgCluster, err := DeployPostgresDatabases(ctx, cluster, environment)
if err != nil {
return nil, err
}
// Deploy MCP Registry
service, err = DeployMCPRegistry(ctx, cluster, environment)
service, err = DeployMCPRegistry(ctx, cluster, environment, ingressNginx, pgCluster)
if err != nil {
return nil, err
}

View file

@ -13,7 +13,7 @@ import (
)
// SetupIngressController sets up the NGINX Ingress Controller
func SetupIngressController(ctx *pulumi.Context, cluster *providers.ProviderInfo, environment string) error {
func SetupIngressController(ctx *pulumi.Context, cluster *providers.ProviderInfo, environment string) (*helm.Chart, error) {
conf := config.New(ctx, "mcp-registry")
provider := conf.Get("provider")
if provider == "" {
@ -27,7 +27,7 @@ func SetupIngressController(ctx *pulumi.Context, cluster *providers.ProviderInfo
},
}, pulumi.Provider(cluster.Provider))
if err != nil {
return err
return nil, err
}
// Install NGINX Ingress Controller
@ -60,7 +60,7 @@ func SetupIngressController(ctx *pulumi.Context, cluster *providers.ProviderInfo
},
}, pulumi.Provider(cluster.Provider))
if err != nil {
return err
return nil, err
}
// Extract ingress IPs from the Helm chart's controller service
@ -90,5 +90,5 @@ func SetupIngressController(ctx *pulumi.Context, cluster *providers.ProviderInfo
})
ctx.Export("ingressIps", ingressIps)
return nil
return ingressNginx, nil
}

View file

@ -1,141 +0,0 @@
package k8s
import (
"fmt"
"github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/apps/v1"
corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/core/v1"
metav1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/meta/v1"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/modelcontextprotocol/registry/deploy/infra/pkg/providers"
)
// DeployMongoDB deploys MongoDB to the Kubernetes cluster
func DeployMongoDB(ctx *pulumi.Context, cluster *providers.ProviderInfo, environment string) error {
// Create PersistentVolumeClaim for MongoDB data
_, err := corev1.NewPersistentVolumeClaim(ctx, "mongodb-pvc", &corev1.PersistentVolumeClaimArgs{
Metadata: &metav1.ObjectMetaArgs{
Name: pulumi.String("mongodb-pvc"),
Namespace: pulumi.String("default"),
Labels: pulumi.StringMap{
"app": pulumi.String("mongodb"),
"environment": pulumi.String(environment),
},
},
Spec: &corev1.PersistentVolumeClaimSpecArgs{
AccessModes: pulumi.StringArray{
pulumi.String("ReadWriteOnce"),
},
Resources: &corev1.VolumeResourceRequirementsArgs{
Requests: pulumi.StringMap{
"storage": pulumi.String("10Gi"),
},
},
},
}, pulumi.Provider(cluster.Provider))
if err != nil {
return err
}
// Create MongoDB Deployment
_, err = v1.NewDeployment(ctx, "mongodb", &v1.DeploymentArgs{
Metadata: &metav1.ObjectMetaArgs{
Name: pulumi.String("mongodb"),
Namespace: pulumi.String("default"),
Labels: pulumi.StringMap{
"app": pulumi.String("mongodb"),
"environment": pulumi.String(environment),
},
},
Spec: &v1.DeploymentSpecArgs{
Selector: &metav1.LabelSelectorArgs{
MatchLabels: pulumi.StringMap{
"app": pulumi.String("mongodb"),
},
},
Template: &corev1.PodTemplateSpecArgs{
Metadata: &metav1.ObjectMetaArgs{
Labels: pulumi.StringMap{
"app": pulumi.String("mongodb"),
},
},
Spec: &corev1.PodSpecArgs{
Containers: corev1.ContainerArray{
&corev1.ContainerArgs{
Name: pulumi.String("mongodb"),
Image: pulumi.String("mongo:7.0"),
Ports: corev1.ContainerPortArray{
&corev1.ContainerPortArgs{
ContainerPort: pulumi.Int(27017),
Name: pulumi.String("mongodb"),
},
},
VolumeMounts: corev1.VolumeMountArray{
&corev1.VolumeMountArgs{
Name: pulumi.String("mongodb-data"),
MountPath: pulumi.String("/data/db"),
},
},
LivenessProbe: &corev1.ProbeArgs{
TcpSocket: &corev1.TCPSocketActionArgs{
Port: pulumi.Int(27017),
},
InitialDelaySeconds: pulumi.Int(30),
TimeoutSeconds: pulumi.Int(5),
},
ReadinessProbe: &corev1.ProbeArgs{
TcpSocket: &corev1.TCPSocketActionArgs{
Port: pulumi.Int(27017),
},
InitialDelaySeconds: pulumi.Int(5),
TimeoutSeconds: pulumi.Int(1),
},
},
},
Volumes: corev1.VolumeArray{
&corev1.VolumeArgs{
Name: pulumi.String("mongodb-data"),
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSourceArgs{
ClaimName: pulumi.String("mongodb-pvc"),
},
},
},
},
},
},
}, pulumi.Provider(cluster.Provider))
if err != nil {
return err
}
// Create MongoDB Service
_, err = corev1.NewService(ctx, "mongodb", &corev1.ServiceArgs{
Metadata: &metav1.ObjectMetaArgs{
Name: pulumi.String("mongodb"),
Namespace: pulumi.String("default"),
Labels: pulumi.StringMap{
"app": pulumi.String("mongodb"),
"environment": pulumi.String(environment),
},
},
Spec: &corev1.ServiceSpecArgs{
Selector: pulumi.StringMap{
"app": pulumi.String("mongodb"),
},
Ports: corev1.ServicePortArray{
&corev1.ServicePortArgs{
Port: pulumi.Int(27017),
TargetPort: pulumi.Int(27017),
Name: pulumi.String("mongodb"),
},
},
Type: pulumi.String("ClusterIP"),
},
}, pulumi.Provider(cluster.Provider))
if err != nil {
return fmt.Errorf("failed to create MongoDB service: %w", err)
}
return nil
}

View file

@ -0,0 +1,70 @@
package k8s
import (
"github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/apiextensions"
corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/core/v1"
"github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3"
metav1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/meta/v1"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/modelcontextprotocol/registry/deploy/infra/pkg/providers"
)
// DeployPostgresDatabases deploys the CloudNative PostgreSQL operator and PostgreSQL cluster
func DeployPostgresDatabases(ctx *pulumi.Context, cluster *providers.ProviderInfo, environment string) (*apiextensions.CustomResource, error) {
// Create cnpg-system namespace
cnpgNamespace, err := corev1.NewNamespace(ctx, "cnpg-system", &corev1.NamespaceArgs{
Metadata: &metav1.ObjectMetaArgs{
Name: pulumi.String("cnpg-system"),
},
}, pulumi.Provider(cluster.Provider))
if err != nil {
return nil, err
}
// Install cloudnative-pg Helm chart
cloudNativePG, err := helm.NewChart(ctx, "cloudnative-pg", helm.ChartArgs{
Chart: pulumi.String("cloudnative-pg"),
Version: pulumi.String("v0.26.0"),
FetchArgs: helm.FetchArgs{
Repo: pulumi.String("https://cloudnative-pg.github.io/charts"),
},
Namespace: cnpgNamespace.Metadata.Name().Elem(),
Values: pulumi.Map{
"webhooks": pulumi.Map{
"replicaCount": pulumi.Int(1),
},
},
}, pulumi.Provider(cluster.Provider))
if err != nil {
return nil, err
}
// Create PostgreSQL cluster with proper timeout handling
// Note: This may fail on first run until CloudNativePG operator is fully ready
pgCluster, err := apiextensions.NewCustomResource(ctx, "registry-pg", &apiextensions.CustomResourceArgs{
ApiVersion: pulumi.String("postgresql.cnpg.io/v1"),
Kind: pulumi.String("Cluster"),
Metadata: &metav1.ObjectMetaArgs{
Name: pulumi.String("registry-pg"),
Namespace: pulumi.String("default"),
Labels: pulumi.StringMap{
"app": pulumi.String("registry-pg"),
"environment": pulumi.String(environment),
},
},
OtherFields: map[string]any{
"spec": map[string]any{
"instances": 1,
"storage": map[string]any{
"size": "50Gi",
},
},
},
}, pulumi.Provider(cluster.Provider), pulumi.DependsOnInputs(cloudNativePG.Ready))
if err != nil {
return nil, err
}
return pgCluster, nil
}

View file

@ -4,8 +4,10 @@ import (
"os/exec"
"strings"
"github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/apiextensions"
v1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/apps/v1"
corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/core/v1"
"github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3"
metav1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/meta/v1"
networkingv1 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/networking/v1"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
@ -26,7 +28,7 @@ func getGitCommitHash() string {
}
// DeployMCPRegistry deploys the MCP Registry to the Kubernetes cluster
func DeployMCPRegistry(ctx *pulumi.Context, cluster *providers.ProviderInfo, environment string) (*corev1.Service, error) {
func DeployMCPRegistry(ctx *pulumi.Context, cluster *providers.ProviderInfo, environment string, ingressNginx *helm.Chart, pgCluster *apiextensions.CustomResource) (*corev1.Service, error) {
conf := config.New(ctx, "mcp-registry")
githubClientId := conf.Require("githubClientId")
@ -91,8 +93,18 @@ func DeployMCPRegistry(ctx *pulumi.Context, cluster *providers.ProviderInfo, env
},
Env: corev1.EnvVarArray{
&corev1.EnvVarArgs{
Name: pulumi.String("MCP_REGISTRY_DATABASE_URL"),
Value: pulumi.String("mongodb://mongodb.default.svc.cluster.local:27017"),
Name: pulumi.String("MCP_REGISTRY_DATABASE_URL"),
ValueFrom: &corev1.EnvVarSourceArgs{
SecretKeyRef: &corev1.SecretKeySelectorArgs{
Name: pgCluster.Metadata.Name().ApplyT(func(name *string) string {
if name == nil {
return "registry-pg-app"
}
return *name + "-app"
}).(pulumi.StringOutput),
Key: pulumi.String("uri"),
},
},
},
&corev1.EnvVarArgs{
Name: pulumi.String("MCP_REGISTRY_GITHUB_CLIENT_ID"),
@ -227,7 +239,7 @@ func DeployMCPRegistry(ctx *pulumi.Context, cluster *providers.ProviderInfo, env
return rules
}).(networkingv1.IngressRuleArrayOutput),
},
}, pulumi.Provider(cluster.Provider))
}, pulumi.Provider(cluster.Provider), pulumi.DependsOnInputs(ingressNginx.Ready))
if err != nil {
return nil, err
}

View file

@ -5,26 +5,35 @@ services:
build:
dockerfile: Dockerfile
depends_on:
- mongodb
postgres:
condition: service_healthy
# See .env.example for more documentation
environment:
- MCP_REGISTRY_DATABASE_URL=${MCP_REGISTRY_DATABASE_URL:-mongodb://mongodb:27017}
- MCP_REGISTRY_DATABASE_URL=${MCP_REGISTRY_DATABASE_URL:-postgres://mcpregistry:mcpregistry@postgres:5432/mcp-registry}
- MCP_REGISTRY_ENVIRONMENT=${MCP_REGISTRY_ENVIRONMENT:-test}
- MCP_REGISTRY_GITHUB_CLIENT_ID=${MCP_REGISTRY_GITHUB_CLIENT_ID:-Iv23licy3GSiM9Km5jtd}
- MCP_REGISTRY_GITHUB_CLIENT_SECRET=${MCP_REGISTRY_GITHUB_CLIENT_SECRET:-0e8db54879b02c29adef51795586f3c510a9341d}
- MCP_REGISTRY_JWT_PRIVATE_KEY=${MCP_REGISTRY_JWT_PRIVATE_KEY:-8103179d8ef955f6d3de6d6217224a909ec4060529dfeb1d4ca5a994537658cd}
- MCP_REGISTRY_ENABLE_ANONYMOUS_AUTH=${MCP_REGISTRY_ENABLE_ANONYMOUS_AUTH:-true}
- MCP_REGISTRY_SEED_FROM=${MCP_REGISTRY_SEED_FROM:-data/seed.json}
ports:
- 8080:8080
restart: "unless-stopped"
mongodb:
image: mongo
container_name: mongodb
postgres:
image: postgres:16-alpine
container_name: postgres
environment:
- PUID=1000
- PGID=1000
POSTGRES_DB: mcp-registry
POSTGRES_USER: mcpregistry
POSTGRES_PASSWORD: mcpregistry
PGDATA: /var/lib/postgresql/data/pgdata
volumes:
- './.db:/data/db'
- './.db:/var/lib/postgresql/data'
healthcheck:
test: ["CMD-SHELL", "pg_isready -U mcpregistry -d mcp-registry"]
interval: 1s
retries: 30
ports:
- 27017:27017
- "5432:5432"
restart: "unless-stopped"

View file

@ -14,7 +14,7 @@ graph TB
subgraph "MCP Registry"
API[REST API<br/>Go]
DB[(MongoDB or PostgreSQL)]
DB[(PostgreSQL)]
CDN[CDN Cache]
end
@ -61,7 +61,7 @@ The main application server implemented in Go, providing:
- GitHub OAuth integration (extensible to other providers)
- DNS verification system (optional for custom namespaces)
### Database (MongoDB or PostgreSQL)
### Database (PostgreSQL)
Primary data store for:
- Versioned server metadata (server.json contents)

12
go.mod
View file

@ -7,10 +7,10 @@ require (
github.com/danielgtaylor/huma/v2 v2.34.1
github.com/golang-jwt/jwt/v5 v5.3.0
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.7.5
github.com/prometheus/client_golang v1.23.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/stretchr/testify v1.10.0
go.mongodb.org/mongo-driver v1.17.3
go.opentelemetry.io/contrib/instrumentation/runtime v0.62.0
go.opentelemetry.io/otel v1.37.0
go.opentelemetry.io/otel/exporters/prometheus v0.59.1
@ -25,10 +25,9 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
@ -36,14 +35,9 @@ require (
github.com/prometheus/otlptranslator v0.0.0-20250717125610-8549f4ab4f8f // indirect
github.com/prometheus/procfs v0.17.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel/trace v1.37.0 // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/text v0.28.0 // indirect
google.golang.org/protobuf v1.36.6 // indirect

51
go.sum
View file

@ -6,6 +6,7 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/danielgtaylor/huma/v2 v2.34.1 h1:EmOJAbzEGfy0wAq/QMQ1YKfEMBEfE94xdBRLPBP0gwQ=
github.com/danielgtaylor/huma/v2 v2.34.1/go.mod h1:ynwJgLk8iGVgoaipi5tgwIQ5yoFNmiu+QdhU7CEEmhk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
@ -15,14 +16,20 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc h1:GN2Lv3MGO7AS6PrRoT6yV5+wkrOpcszoIsO4+4ds248=
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc/go.mod h1:+JKpmjMGhpgPL+rXZ5nsZieVzvarn86asRlBg4uNGnk=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.7.5 h1:JHGfMnQY+IEtGM63d+NGMjoRpysB2JBwDr5fsngwmJs=
github.com/jackc/pgx/v5 v5.7.5/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@ -31,8 +38,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -51,21 +56,13 @@ github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.17.3 h1:TQyXhnsWfWtgAhMtOgtYHMTkZIfBTpMTsMnd9ZBeHxQ=
go.mongodb.org/mongo-driver v1.17.3/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/contrib/instrumentation/runtime v0.62.0 h1:ZIt0ya9/y4WyRIzfLC8hQRRsWg0J9M9GyaGtIMiElZI=
@ -84,41 +81,19 @@ go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mx
go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -7,16 +7,16 @@ import (
type DatabaseType string
const (
DatabaseTypeMongoDB DatabaseType = "mongodb"
DatabaseTypeMemory DatabaseType = "memory"
DatabaseTypePostgreSQL DatabaseType = "postgresql"
DatabaseTypeMemory DatabaseType = "memory"
)
// Config holds the application configuration
// See .env.example for more documentation
type Config struct {
ServerAddress string `env:"SERVER_ADDRESS" envDefault:":8080"`
DatabaseType DatabaseType `env:"DATABASE_TYPE" envDefault:"mongodb"`
DatabaseURL string `env:"DATABASE_URL" envDefault:"mongodb://localhost:27017"`
DatabaseType DatabaseType `env:"DATABASE_TYPE" envDefault:"postgresql"`
DatabaseURL string `env:"DATABASE_URL" envDefault:"postgres://localhost:5432/mcp-registry?sslmode=disable"`
DatabaseName string `env:"DATABASE_NAME" envDefault:"mcp-registry"`
CollectionName string `env:"COLLECTION_NAME" envDefault:"servers_v2"`
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`

View file

@ -36,8 +36,8 @@ type ConnectionType string
const (
// ConnectionTypeMemory represents an in-memory database connection
ConnectionTypeMemory ConnectionType = "memory"
// ConnectionTypeMongoDB represents a MongoDB database connection
ConnectionTypeMongoDB ConnectionType = "mongodb"
// ConnectionTypePostgreSQL represents a PostgreSQL database connection
ConnectionTypePostgreSQL ConnectionType = "postgresql"
)
// ConnectionInfo provides information about the database connection
@ -47,7 +47,7 @@ type ConnectionInfo struct {
// IsConnected indicates whether the database is currently connected
IsConnected bool
// Raw provides access to the underlying connection object, which will vary by implementation
// For MongoDB, this will be *mongo.Client
// For PostgreSQL, this will be *pgx.Conn
// For MemoryDB, this will be map[string]*model.MCPRegistry
Raw any
}

View file

@ -0,0 +1,192 @@
package database
import (
"context"
"embed"
"errors"
"fmt"
"log"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/jackc/pgx/v5"
)
//go:embed migrations/*.sql
var migrationFiles embed.FS
// Migration represents a database migration
type Migration struct {
Version int
Name string
SQL string
}
// Migrator handles database migrations
type Migrator struct {
conn *pgx.Conn
}
// NewMigrator creates a new migrator instance
func NewMigrator(conn *pgx.Conn) *Migrator {
return &Migrator{conn: conn}
}
// ensureMigrationsTable creates the migrations tracking table if it doesn't exist
func (m *Migrator) ensureMigrationsTable(ctx context.Context) error {
query := `
CREATE TABLE IF NOT EXISTS schema_migrations (
version INTEGER PRIMARY KEY,
name VARCHAR(255) NOT NULL,
applied_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
)
`
_, err := m.conn.Exec(ctx, query)
return err
}
// getAppliedMigrations returns a map of already applied migration versions
func (m *Migrator) getAppliedMigrations(ctx context.Context) (map[int]bool, error) {
query := "SELECT version FROM schema_migrations ORDER BY version"
rows, err := m.conn.Query(ctx, query)
if err != nil {
return nil, fmt.Errorf("failed to query applied migrations: %w", err)
}
defer rows.Close()
applied := make(map[int]bool)
for rows.Next() {
var version int
if err := rows.Scan(&version); err != nil {
return nil, fmt.Errorf("failed to scan migration version: %w", err)
}
applied[version] = true
}
return applied, rows.Err()
}
// loadMigrations loads all migration files from the embedded filesystem
func (m *Migrator) loadMigrations() ([]Migration, error) {
entries, err := migrationFiles.ReadDir("migrations")
if err != nil {
return nil, fmt.Errorf("failed to read migrations directory: %w", err)
}
var migrations []Migration
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
continue
}
// Parse version from filename (e.g., "001_initial_schema.sql" -> version 1)
name := entry.Name()
parts := strings.SplitN(name, "_", 2)
if len(parts) != 2 {
log.Printf("Skipping migration file with invalid name format: %s", name)
continue
}
version, err := strconv.Atoi(parts[0])
if err != nil {
log.Printf("Skipping migration file with invalid version: %s", name)
continue
}
// Read the migration SQL
content, err := migrationFiles.ReadFile(filepath.Join("migrations", name))
if err != nil {
return nil, fmt.Errorf("failed to read migration file %s: %w", name, err)
}
migrations = append(migrations, Migration{
Version: version,
Name: strings.TrimSuffix(name, ".sql"),
SQL: string(content),
})
}
// Sort migrations by version
sort.Slice(migrations, func(i, j int) bool {
return migrations[i].Version < migrations[j].Version
})
return migrations, nil
}
// Migrate runs all pending migrations
func (m *Migrator) Migrate(ctx context.Context) error {
// Ensure the migrations table exists
if err := m.ensureMigrationsTable(ctx); err != nil {
return fmt.Errorf("failed to create migrations table: %w", err)
}
// Get applied migrations
applied, err := m.getAppliedMigrations(ctx)
if err != nil {
return fmt.Errorf("failed to get applied migrations: %w", err)
}
// Load all migration files
migrations, err := m.loadMigrations()
if err != nil {
return fmt.Errorf("failed to load migrations: %w", err)
}
// Find pending migrations
var pending []Migration
for _, migration := range migrations {
if !applied[migration.Version] {
pending = append(pending, migration)
}
}
if len(pending) == 0 {
log.Println("No pending migrations")
return nil
}
log.Printf("Applying %d pending migrations", len(pending))
// Apply each pending migration in a transaction
for _, migration := range pending {
if err := m.applyMigration(ctx, migration); err != nil {
return fmt.Errorf("failed to apply migration %d (%s): %w", migration.Version, migration.Name, err)
}
log.Printf("Applied migration %d: %s", migration.Version, migration.Name)
}
log.Println("All migrations applied successfully")
return nil
}
// applyMigration applies a single migration in a transaction
func (m *Migrator) applyMigration(ctx context.Context, migration Migration) error {
tx, err := m.conn.Begin(ctx)
if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err)
}
defer func() {
if err := tx.Rollback(ctx); err != nil && !errors.Is(err, pgx.ErrTxClosed) {
log.Printf("Failed to rollback migration transaction: %v", err)
}
}()
// Execute the migration SQL
_, err = tx.Exec(ctx, migration.SQL)
if err != nil {
return fmt.Errorf("failed to execute migration SQL: %w", err)
}
// Record the migration as applied
_, err = tx.Exec(ctx,
"INSERT INTO schema_migrations (version, name) VALUES ($1, $2)",
migration.Version, migration.Name)
if err != nil {
return fmt.Errorf("failed to record migration: %w", err)
}
return tx.Commit(ctx)
}

View file

@ -0,0 +1,58 @@
-- PostgreSQL schema for MCP Registry
-- This schema uses a hybrid approach with relational tables for core entities
-- and JSONB columns for complex nested data structures
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pg_trgm";
-- Servers table - core server information
CREATE TABLE servers (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
status VARCHAR(50) DEFAULT 'active',
-- Repository information stored as JSONB for flexibility
repository JSONB,
-- Version details
version VARCHAR(255) NOT NULL,
release_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
is_latest BOOLEAN DEFAULT true,
-- Packages and remotes stored as JSONB arrays
packages JSONB DEFAULT '[]'::jsonb,
remotes JSONB DEFAULT '[]'::jsonb,
-- Metadata
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Indexes for performance
CREATE UNIQUE INDEX idx_servers_id ON servers(id);
CREATE INDEX idx_servers_name ON servers(name);
CREATE UNIQUE INDEX idx_servers_name_version ON servers(name, version);
CREATE INDEX idx_servers_latest ON servers(is_latest) WHERE is_latest = true;
CREATE INDEX idx_servers_status ON servers(status);
-- GIN indexes for JSONB fields
CREATE INDEX idx_servers_repository_gin ON servers USING GIN(repository);
CREATE INDEX idx_servers_packages_gin ON servers USING GIN(packages);
CREATE INDEX idx_servers_remotes_gin ON servers USING GIN(remotes);
-- Function to update the updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
-- Trigger to automatically update updated_at
CREATE TRIGGER update_servers_updated_at
BEFORE UPDATE ON servers
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

Some files were not shown because too many files have changed in this diff Show more