MCP Postgres 查询服务器
模型上下文协议 (MCP) 服务器实现,用于以只读模式查询 PostgreSQL 数据库,旨在与 Claude Desktop 和其他 MCP 客户端配合使用。
概述
该项目实现了模型上下文协议(MCP)服务器,它提供:
PostgreSQL 数据库的安全只读接口
通过 MCP 协议与 Claude Desktop 集成
SQL 查询验证以确保仅执行 SELECT 查询
查询超时保护(10秒)
先决条件
Node.js(v14 或更高版本)
npm(Node.js 附带)
PostgreSQL 数据库(通过命令行提供连接详细信息)
安装
连接到 Claude Desktop
您可以配置 Claude Desktop 自动启动并连接到 MCP 服务器:
访问 Claude Desktop 配置文件:
打开 Claude 桌面
前往“设置”>“开发者”>“编辑配置”
这将在您的默认文本编辑器中打开配置文件
将 postgres-query-server 添加到
claude_desktop_config.json
的mcpServers
部分:
将
/path/to/your/
替换为项目目录的实际路径。将 PostgreSQL 连接字符串替换为您的实际数据库凭据。
保存文件并重启 Claude Desktop。MCP 服务器现在应该出现在“设置”中的 MCP 服务器选择下拉菜单中。
示例配置
以下是 postgres-query 配置文件的完整示例:
更新配置
要更新您的 Claude Desktop 配置:
打开 Claude 桌面
前往“设置”>“开发者”>“编辑配置”
对配置文件进行更改
保存文件
重新启动 Claude Desktop 以使更改生效
如果您已更新 MCP 服务器代码,请确保在重新启动之前使用
npm run build
重新构建它
特征
只读数据库访问:出于安全考虑,只允许执行 SELECT 查询
查询验证:防止潜在的有害 SQL 操作
超时保护:运行时间超过 10 秒的查询将自动终止
MCP 协议支持:完整实现模型上下文协议
JSON 响应格式:查询结果以结构化的 JSON 格式返回
API
工具
查询-postgres
对配置的 PostgreSQL 数据库执行只读 SQL 查询。
参数:
query
(字符串):要执行的 SQL SELECT 查询
回复:
JSON 对象包含:
rows
:结果集行rowCount
:返回的行数fields
:列元数据
例子:
发展
主服务器实现位于src/index.ts
中。关键组件:
PostgreSQL 连接池设置
查询验证逻辑
MCP 服务器配置
工具和资源定义
要修改服务器的行为,您可以:
编辑
isReadOnlyQuery()
中的查询验证逻辑向 MCP 服务器添加额外的工具或资源
修改查询超时时长(目前为10秒)
安全注意事项
服务器验证所有查询以确保它们是只读的
使用 SSL 连接数据库
查询超时可防止资源耗尽
不允许进行任何写入操作
数据库凭证直接通过命令行参数传递,而不是存储在文件中
执照
国际学习中心
贡献
欢迎贡献代码!欢迎提交 Pull 请求。
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
Tools
安全的模型上下文协议服务器允许 Claude 对 PostgreSQL 数据库执行只读 SQL 查询,从而实现与数据库数据的自然语言交互。
Related MCP Servers
- -securityAlicense-qualityA Model Context Protocol server that enables Claude to execute SQL queries on Snowflake databases with automatic connection lifecycle management.Last updated -43MIT License
- -securityFlicense-qualityA Model Context Protocol server that provides read-only access to PostgreSQL databases, enabling LLMs to inspect database schemas and execute read-only queries.Last updated -21,552
- -securityFlicense-qualityA Model Context Protocol server that allows Large Language Models like Claude to execute SQL queries, explore database schemas, and maintain persistent connections to SQL Server databases.Last updated -
- AsecurityAlicenseAqualityA Model Context Protocol server that enables Claude to access and interact with Oracle databases through natural language queries.Last updated -3MIT License