A Model Context Protocol (MCP) server implementation in Go that provides a flexible and secure way to manage and query data models through a RESTful API.
- 🔐 Secure model management with JWT authentication
- 📝 Dynamic model registration with JSON schema validation
- 🔍 Flexible query system for searching across models
- 🗄️ PostgreSQL integration with automatic migrations
- 🔒 Role-based access control (admin vs public endpoints)
- 🚀 RESTful API design
- Go 1.16 or higher
- PostgreSQL 12 or higher
- Make (optional, for using Makefile commands)
- Clone the repository:
git clone https://github.com/Nysonn/postgres-server.git
cd postgres-server- Install dependencies:
go mod download- Create a
.envfile in the project root:
DATABASE_URL=postgresql://user:password@localhost:5432/your_database
JWT_SECRET=your_jwt_secret_key
SERVER_ADDRESS=:8080 # Optional, defaults to :8080- Create a PostgreSQL database:
CREATE DATABASE your_database;- The server will automatically run migrations to create necessary tables.
go run cmd/server/main.goThe server will:
- Load environment variables
- Connect to the database
- Run any pending migrations
- Start the HTTP server
POST /admin/models/register
Authorization: Bearer <your_jwt_token>
Content-Type: application/json
{
"name": "items",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"category": {"type": "string"},
"price_ugx": {"type": "number"},
"available": {"type": "boolean"}
}
}
}GET /admin/models/list
Authorization: Bearer <your_jwt_token>GET /admin/models/get?name=<model_name>
Authorization: Bearer <your_jwt_token>DELETE /admin/models/delete?name=<model_name>
Authorization: Bearer <your_jwt_token>POST /query
Content-Type: application/json
{
"model": "items",
"queryText": "search term",
"fields": ["name", "category"],
"maxResults": 10
}Use the included token generator:
go run cmd/token/main.goThis will output a JWT token that can be used for admin operations.
.
├── cmd/
│ ├── server/ # Main server application
│ └── token/ # JWT token generator
├── internal/
│ ├── config/ # Configuration management
│ ├── db/ # Database connection and utilities
│ ├── handler/ # HTTP request handlers
│ └── middleware/ # HTTP middleware (auth, etc.)
├── migrations/ # Database migrations
├── .env # Environment variables (not in git)
├── .gitignore
├── go.mod
├── go.sum
└── README.md
-
Create new migration files in the
migrationsdirectory:XXXX_description.up.sqlfor applying changesXXXX_description.down.sqlfor reverting changes
-
The server will automatically run new migrations on startup.
go test ./...- JWT tokens should be kept secure
- Database credentials should be properly managed
- The server should be run behind a reverse proxy in production
- Regular security audits are recommended
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request