Skip to main content
Glama
RathodDarshil

PostgreSQL Query MCP Server

MCP Postgres 查询服务器

模型上下文协议 (MCP) 服务器实现,用于以只读模式查询 PostgreSQL 数据库,旨在与 Claude Desktop 和其他 MCP 客户端配合使用。

概述

该项目实现了模型上下文协议(MCP)服务器,它提供:

  1. PostgreSQL 数据库的安全只读接口

  2. 通过 MCP 协议与 Claude Desktop 集成

  3. SQL 查询验证以确保仅执行 SELECT 查询

  4. 查询超时保护(10秒)

Related MCP server: PostgreSQL MCP Server

先决条件

  • Node.js(v14 或更高版本)

  • npm(Node.js 附带)

  • PostgreSQL 数据库(通过命令行提供连接详细信息)

安装

# Clone the repository
git clone https://github.com/RathodDarshil/mcp-postgres-query-server.git
cd mcp-postgres-query-server

# Install dependencies
npm install

# Build the project
npm run build

连接到 Claude Desktop

您可以配置 Claude Desktop 自动启动并连接到 MCP 服务器:

  1. 访问 Claude Desktop 配置文件:

    • 打开 Claude 桌面

    • 前往“设置”>“开发者”>“编辑配置”

    • 这将在您的默认文本编辑器中打开配置文件

  2. 将 postgres-query-server 添加到claude_desktop_config.jsonmcpServers部分:

{
    "mcpServers": {
        "postgres-query": {
            "command": "node",
            "args": [
                "/path/to/your/mcp-postgres-query-server/dist/index.js",
                "postgresql://username:password@hostname:port/database"
            ]
        }
    }
}
  1. /path/to/your/替换为项目目录的实际路径。

  2. 将 PostgreSQL 连接字符串替换为您的实际数据库凭据。

  3. 保存文件并重启 Claude Desktop。MCP 服务器现在应该出现在“设置”中的 MCP 服务器选择下拉菜单中。

示例配置

以下是 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 配置:

  1. 打开 Claude 桌面

  2. 前往“设置”>“开发者”>“编辑配置”

  3. 对配置文件进行更改

  4. 保存文件

  5. 重新启动 Claude Desktop 以使更改生效

  6. 如果您已更新 MCP 服务器代码,请确保在重新启动之前使用npm run build重新构建它

特征

  • 只读数据库访问:出于安全考虑,只允许执行 SELECT 查询

  • 查询验证:防止潜在的有害 SQL 操作

  • 超时保护:运行时间超过 10 秒的查询将自动终止

  • MCP 协议支持:完整实现模型上下文协议

  • JSON 响应格式:查询结果以结构化的 JSON 格式返回

API

工具

查询-postgres

对配置的 PostgreSQL 数据库执行只读 SQL 查询。

参数:

  • query (字符串):要执行的 SQL SELECT 查询

回复:

  • JSON 对象包含:

    • rows :结果集行

    • rowCount :返回的行数

    • fields :列元数据

例子:

query-postgres: SELECT * FROM users LIMIT 5

发展

主服务器实现位于src/index.ts中。关键组件:

  • PostgreSQL 连接池设置

  • 查询验证逻辑

  • MCP 服务器配置

  • 工具和资源定义

要修改服务器的行为,您可以:

  • 编辑isReadOnlyQuery()中的查询验证逻辑

  • 向 MCP 服务器添加额外的工具或资源

  • 修改查询超时时长(目前为10秒)

安全注意事项

  • 服务器验证所有查询以确保它们是只读的

  • 使用 SSL 连接数据库

  • 查询超时可防止资源耗尽

  • 不允许进行任何写入操作

  • 数据库凭证直接通过命令行参数传递,而不是存储在文件中

执照

国际学习中心

贡献

欢迎贡献代码!欢迎提交 Pull 请求。

Available Tools

1 tool
query-postgresD
ParametersJSON Schema
NameRequiredDescriptionDefault
queryYes

TDQS

D1/5.0
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Tool has no description.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness1/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Tool has no description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Tool has no description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Tool has no description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose1/5

Does the description clearly state what the tool does and how it differs from similar tools?

Tool has no description.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Tool has no description.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections. Dates show when Glama detected each change.

  1. 1 tool update
    • First observedquery-postgres

TDQS

D1.7/5.0
Disambiguation5/5

With only one tool, there is no possibility of ambiguity or overlap between tools. The tool's purpose is singular and clear, eliminating any risk of misselection.

Naming Consistency5/5

A single tool inherently has perfect naming consistency, as there are no other tools to compare it against. The name 'query-postgres' follows a clear verb-noun pattern without any conflicting conventions.

Tool Count2/5

A single tool is too few for a PostgreSQL server's apparent scope, which typically involves multiple operations like querying, inserting, updating, and deleting data. This minimal set severely limits functionality and agent workflows.

Completeness1/5

The tool surface is severely incomplete for a PostgreSQL domain, covering only querying. Obvious gaps include create, update, delete, schema management, and transaction handling, which will cause frequent agent failures in database interactions.

Maintenance

ActivityInactive
ResponsivenessNo issues

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Connectors

Related MCP Servers

  • -
    license
    Not graded
    quality
    A
    maintenance
    A Model Context Protocol server that provides read-only access to PostgreSQL databases. This server enables LLMs to inspect database schemas and execute read-only queries.
    100,745
    90,042
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A Model Context Protocol server that provides read-only access to PostgreSQL databases, enabling LLMs to inspect database schemas and execute read-only queries.
    100,745
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A Model Context Protocol server that provides read-only access to PostgreSQL databases, enabling LLMs to inspect database schemas and execute read-only queries.
    100,745
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A Model Context Protocol server providing read-only access to PostgreSQL databases, enabling LLMs to inspect database schemas and execute read-only SQL queries.
    100,745
    MIT

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/RathodDarshil/mcp-postgres-query-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server