AgenticMCP
A Model Context Protocol (MCP) server that provides secure, role-based access to PostgreSQL databases for AI agents.
README
AgenticMCP - PostgreSQL MCP Server
A Model Context Protocol (MCP) server that provides secure, role-based access to PostgreSQL databases for AI agents.
Features
- PostgreSQL Integration: Connect to any PostgreSQL database
- Role-Based Access Control (RBAC): Fine-grained permissions at table, column, and row levels
- Safe Query Building: All queries use parameterized statements to prevent SQL injection
- Docker Support: Easy deployment with Docker and Docker Compose
- CI/CD Ready: GitHub Actions workflows included
- Multiple MCP Tools: Comprehensive tools for database operations
Available MCP Tools
| Tool | Description | Permission Required |
|---|---|---|
list_tables |
List all accessible tables | Any role |
describe_table |
Get table schema | Read access |
select |
Query data with filtering, sorting, pagination | Read access |
insert |
Insert new rows | Write access |
update |
Update existing rows | Write access |
delete |
Delete rows | Write access |
query |
Execute raw SQL SELECT | Admin only |
get_role_info |
Get current role and permissions | Any role |
reload_permissions |
Reload permissions configuration | Any role |
Quick Start
1. Using Docker Compose (Recommended)
# Clone the repository
git clone https://github.com/YOUR_USERNAME/AgenticMCP.git
cd AgenticMCP
# Start PostgreSQL and the MCP server
docker compose -f docker/docker-compose.yml up -d
# Check logs
docker compose -f docker/docker-compose.yml logs -f
This will start:
- PostgreSQL on port 5432
- Sample database with test data
- MCP server instances for different roles
2. Local Installation
# Create virtual environment
python -m venv .venv
.venv\Scripts\activate # Windows
# source .venv/bin/activate # Linux/Mac
# Install with dependencies
pip install -e ".[dev]"
# Set environment variables
export MCP_DB_HOST=localhost
export MCP_DB_PORT=5432
export MCP_DB_NAME=app_db
export MCP_DB_USER=app_user
export MCP_DB_PASSWORD=your_password
export MCP_ROLE=reader
# Run the server
agenticmcp
3. Using Docker Image
# Pull the image
docker pull ghcr.io/YOUR_USERNAME/agenticmcp:latest
# Run the server
docker run -i --rm \
-e MCP_DB_HOST=host.docker.internal \
-e MCP_DB_PORT=5432 \
-e MCP_DB_NAME=app_db \
-e MCP_DB_USER=app_user \
-e MCP_DB_PASSWORD=your_password \
-e MCP_ROLE=reader \
-v $(pwd)/config:/app/config:ro \
ghcr.io/YOUR_USERNAME/agenticmcp:latest
Configuration
Environment Variables
| Variable | Description | Default |
|---|---|---|
MCP_DB_HOST |
PostgreSQL host | localhost |
MCP_DB_PORT |
PostgreSQL port | 5432 |
MCP_DB_NAME |
Database name | postgres |
MCP_DB_USER |
Database user | postgres |
MCP_DB_PASSWORD |
Database password | (empty) |
MCP_ROLE |
Role for access control | reader |
MCP_USER_ID |
User ID for row-level security | (optional) |
MCP_TENANT_ID |
Tenant ID for multi-tenant | (optional) |
MCP_PERMISSIONS_FILE |
Path to permissions.yaml | config/permissions.yaml |
MCP_MAX_QUERY_ROWS |
Maximum rows per query | 1000 |
MCP_QUERY_TIMEOUT |
Query timeout in seconds | 30 |
Permissions Configuration
Edit config/permissions.yaml to define roles and access:
version: "1.0"
default_role: "reader"
roles:
admin:
description: "Full administrative access"
tables: ["*"]
operations: ["*"]
reader:
description: "Read-only access"
tables: ["users", "products"]
operations: ["read"]
columns:
users: ["id", "name"] # Exclude sensitive columns
writer:
description: "Read and write access"
tables: ["users", "orders"]
operations: ["read", "write"]
row_filters:
orders: "user_id = {user_id}" # Row-level security
tables:
users:
primary_key: "id"
columns:
- name: id
type: "integer"
- name: email
type: "text"
sensitive: true
visible_to: ["admin"]
Client Configuration
Claude Desktop
Add to your Claude Desktop config (claude_desktop_config.json):
{
"mcpServers": {
"agenticmcp-postgres": {
"command": "docker",
"args": [
"run", "-i", "--rm",
"-e", "MCP_DB_HOST=host.docker.internal",
"-e", "MCP_DB_PORT=5432",
"-e", "MCP_DB_NAME=app_db",
"-e", "MCP_DB_USER=app_user",
"-e", "MCP_DB_PASSWORD=your_password",
"-e", "MCP_ROLE=reader",
"ghcr.io/YOUR_USERNAME/agenticmcp:latest"
]
}
}
}
See examples/claude_desktop_config.json for more examples.
MCP Inspector
# Start the server
agenticmcp
# In another terminal, run inspector
npx @modelcontextprotocol/inspector
Development
Setup
# Install with dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run with coverage
pytest --cov=agenticmcp
# Format code
black src/
# Lint
ruff check src/
# Type check
mypy src/
Database Initialization
The docker/init.sql file creates sample tables for testing:
users- User accountsproducts- Product catalogorders- Orders with statusorder_items- Order line itemsanalytics- Analytics metrics
CI/CD
GitHub Actions
The project includes two workflows:
CI Workflow (.github/workflows/ci.yml):
- Runs on push and pull requests
- Executes linting, type checking, and tests
- Builds Docker image
Release Workflow (.github/workflows/release.yml):
- Triggers on version tags (
v*.*.*) - Builds and pushes Docker image to GHCR
- Creates GitHub release
Manual Docker Build
# Build the image
docker build -f docker/Dockerfile -t agenticmcp:test .
# Run the container
docker run -i --rm \
-e MCP_DB_HOST=host.docker.internal \
-e MCP_DB_NAME=app_db \
-e MCP_ROLE=admin \
agenticmcp:test
Security
- SQL Injection Prevention: All queries use parameterized statements
- Row-Level Security: Support for WHERE clause injection based on user context
- Column-Level Filtering: Sensitive columns can be hidden from specific roles
- Admin-Only Raw Queries: Raw SQL execution restricted to admin role
- Connection Pooling: Efficient database connection management
Project Structure
agenticmcp/
├── src/agenticmcp/
│ ├── __init__.py
│ ├── server.py # MCP server implementation
│ ├── database.py # Database connection and queries
│ ├── permissions.py # Access control system
│ ├── config.py # Configuration management
│ └── tools/ # MCP tool implementations
├── config/
│ └── permissions.yaml # Role and table permissions
├── docker/
│ ├── Dockerfile
│ ├── docker-compose.yml
│ └── init.sql # Sample database schema
├── .github/workflows/
│ ├── ci.yml # Continuous Integration
│ └── release.yml # Release automation
├── examples/
│ ├── claude_desktop_config.json
│ └── inspector_config.json
└── tests/
├── test_server.py
├── test_database.py
└── test_permissions.py
License
MIT
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
Support
For issues and questions, please use the GitHub issue tracker.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。