本项目是一个模型上下文协议(MCP)服务器实现,专为以只读模式查询 PostgreSQL 数据库而设计。它能与 Claude Desktop 及其他 MCP 客户端协同工作,为用户提供安全、便捷的数据库查询服务。
# 克隆仓库
git clone https://github.com/RathodDarshil/mcp-postgres-query-server.git
cd mcp-postgres-query-server
# 安装依赖项
npm install
# 构建项目
npm run build
您可以配置 Claude Desktop 以自动启动并连接到 MCP 服务器:
claude_desktop_config.json
的 mcpServers
部分添加 postgres-query-server:{
"mcpServers": {
"postgres-query": {
"command": "node",
"args": [
"/path/to/your/mcp-postgres-query-server/dist/index.js",
"postgresql://username:password@hostname:port/database"
]
}
}
}
/path/to/your/
以下是带有 postgres-query 的配置文件完整示例:
{
"mcpServers": {
"postgres-query": {
"command": "node",
"args": [
"/Users/darshilrathod/mcp-servers/mcp-postgres-query-server/dist/index.js",
"postgresql://user:password@localhost:5432/mydatabase"
]
}
}
}
要更新 Claude Desktop 配置:
npm run build
进行重建对配置好的 PostgreSQL 数据库执行只读 SQL 查询。
query
(字符串):要执行的 SQL SELECT 查询rows
:结果集行columns
:列信息rowCount
:行数type
:查询类型{
"procedure": "query-postgres",
"params": {
"query": "SELECT * FROM users LIMIT 10"
}
}
{
"rows": [
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
],
"columns": ["id", "name"],
"rowCount": 2,
"type": "SELECT"
}
在 config.js
文件中,配置 PostgreSQL 连接详细信息:
module.exports = {
host: 'localhost',
port: 5432,
user: 'yourdbuser',
password: 'yourdbpassword',
database: 'yourdatabasename'
};
在 index.js
文件中,定义处理查询的逻辑:
const { Pool } = require('pg');
const pool = new Pool(require('./config'));
async function queryPostgres(query) {
try {
const result = await pool.query(query);
return {
rows: Array.from(result.rows),
columns: result.columns,
rowCount: result.rowCount,
type: result.command
};
} catch (error) {
throw new Error(`Query failed: ${error.message}`);
}
}
module.exports = { queryPostgres };
此项目实现了以下功能的模型上下文协议(MCP)服务器:
文档未提及相关信息,暂不展示。