PostgreSQL MCP Server with Authentication

PostgreSQL MCP Server with Authentication

Provides authenticated access to PostgreSQL databases for Claude AI, enabling users to browse database tables, discover schemas, and execute custom SQL queries through natural language interaction.

Category
访问服务器

README

PostgreSQL MCP Server with Authentication

A Model Context Protocol (MCP) server that provides authenticated access to PostgreSQL databases for Claude AI. Supports Claude Web with GitHub OAuth 2.0 authentication and can be deployed on EC2 with RDS PostgreSQL.

Features

  • 🔐 GitHub OAuth 2.0 Authentication - Secure authentication for Claude Web
  • 🗄️ PostgreSQL Integration - Full database access with schema discovery
  • 🛠️ SQL Query Tool - Execute custom SQL queries
  • 📊 Resource Discovery - Browse database tables and schemas
  • ☁️ Production Ready - Supports EC2 deployment with RDS PostgreSQL
  • 🚀 Easy Setup - Docker-based PostgreSQL for development

Quick Start

1. Prerequisites

  • Node.js 18+
  • PostgreSQL database (local Docker or AWS RDS)
  • GitHub OAuth App (for Claude Web authentication)
  • EC2 instance (for production deployment)

2. Installation

git clone <your-repo-url>
cd mcp-server
npm install

3. Configuration

Copy the example environment file and configure:

cp .env.example .env

Edit .env with your settings:

# Database Configuration (RDS PostgreSQL)
DB_HOST=your-rds-endpoint.region.rds.amazonaws.com
DB_PORT=5432
DB_NAME=your_database_name
DB_USER=your_db_username
DB_PASSWORD=your_db_password

# Server Configuration (EC2)
SERVER_PORT=3000
SERVER_HOST=0.0.0.0
PUBLIC_URL=http://44.200.16.187:3000

# Authentication
JWT_SECRET=your-jwt-secret-at-least-32-characters-long

# GitHub OAuth (for Claude Web)
GITHUB_CLIENT_ID=your_github_oauth_app_client_id
GITHUB_CLIENT_SECRET=your_github_oauth_app_client_secret
GITHUB_REDIRECT_URI=http://44.200.16.187:3000/auth/callback

# Environment
NODE_ENV=production

4. Development Setup (Optional)

For local development with Docker:

# Start PostgreSQL with sample data
docker-compose up -d

# Start development server
npm run dev

5. Production Deployment

On EC2:

# Build the application
npm run build

# Start with PM2 (recommended)
npm run pm2:start

# Or start directly
npm start

6. Usage

Configure Claude Web:

  • Server URL: http://44.200.16.187:3000
  • Client ID: your_github_client_id

Architecture

Files Structure

  • src/index.ts - Main MCP server (stdio transport for Claude Desktop)
  • src/unified-server.ts - Authenticated MCP server (HTTP transport for Claude Web)
  • src/auth.ts - JWT and API key authentication middleware
  • src/config.ts - Configuration management with Zod validation
  • src/simple-mcp.ts - Minimal MCP server for testing
  • docker-compose.yml - PostgreSQL database setup
  • init.sql - Sample database schema and data

Database Schema

The server includes sample data with three tables:

  • users - User accounts (id, name, email, created_at)
  • products - Product catalog (id, name, price, description, category)
  • orders - Order history (id, user_id, product_id, quantity, total_amount)

Available Tools

  • query - Execute SQL queries against the database
  • describe_table - Get detailed table schema information

Available Resources

  • postgres://table/{table_name} - Access to individual database tables

GitHub OAuth Setup

1. Create GitHub OAuth App

  1. Go to GitHub Developer Settings

  2. Click "New OAuth App"

  3. Fill in the details:

    • Application name: MCP PostgreSQL Server
    • Homepage URL: https://your-ngrok-url.ngrok-free.app
    • Authorization callback URL: https://your-ngrok-url.ngrok-free.app/auth/callback
  4. Copy the Client ID and Client Secret to your .env file

2. Update URLs for ngrok

When using ngrok, update both:

  • Your .env file with the ngrok URL
  • Your GitHub OAuth app settings with the new callback URL

Development

Available Scripts

  • npm run dev - Start MCP server (stdio mode)
  • npm run dev:unified - Start authenticated MCP server (HTTP mode)
  • npm run dev:simple - Start minimal MCP server for testing
  • npm run build - Build TypeScript to JavaScript
  • npm run lint - Run ESLint
  • npm run typecheck - Run TypeScript type checking

Testing

Try these prompts in Claude:

What tables are available in the database?
Show me some sample data from the users table.
Which user has placed the most orders?
What's the total revenue from all orders?
Run this SQL: SELECT name, email FROM users WHERE created_at > '2020-01-01'

Security

  • JWT tokens with configurable secrets
  • GitHub OAuth 2.0 with PKCE support
  • Parameterized SQL queries to prevent injection
  • CORS protection configured for Claude domains
  • Environment-based configuration (no secrets in code)

Troubleshooting

Common Issues

  1. "No tools, resources, or prompts" - Make sure the server sends notifications/tools/list_changed after initialization
  2. OAuth errors - Verify GitHub OAuth app callback URLs match your ngrok URL
  3. Database connection fails - Check that PostgreSQL container is running with docker-compose ps
  4. 404 errors - Ensure ngrok is forwarding to the correct port (3000)

Debug Mode

Enable verbose logging by checking the terminal output when running the server. All MCP requests and OAuth flows are logged.

License

MIT

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

Support

  • Check the MCP documentation
  • Review the troubleshooting section above
  • Open an issue for bugs or feature requests

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选