MCP cover image
See in Github
2025-03-09

1

Github Watches

1

Github Forks

0

Github Stars

MCP Postgres Query Server

A Model Context Protocol (MCP) server implementation for querying a PostgreSQL database in read-only mode, designed to work with Claude Desktop and other MCP clients.

Overview

This project implements a Model Context Protocol (MCP) server that provides:

  1. A secure, read-only interface to a PostgreSQL database
  2. Integration with Claude Desktop through the MCP protocol
  3. SQL query validation to ensure only SELECT queries are executed
  4. Query timeout protection (10 seconds)

Prerequisites

  • Node.js (v14 or later)
  • npm (comes with Node.js)
  • PostgreSQL database (connection details provided via command line)

Installation

# 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

Connecting to Claude Desktop

You can configure Claude Desktop to automatically launch and connect to the MCP server:

  1. Access the Claude Desktop configuration file:

    • Open Claude Desktop
    • Go to Settings > Developer > Edit Config
    • This will open the configuration file in your default text editor
  2. Add the postgres-query-server to the mcpServers section of your claude_desktop_config.json:

{
    "mcpServers": {
        "postgres-query": {
            "command": "node",
            "args": [
                "/path/to/your/mcp-postgres-query-server/dist/index.js",
                "postgresql://username:password@hostname:port/database"
            ]
        }
    }
}
  1. Replace /path/to/your/ with the actual path to your project directory.
  2. Replace the PostgreSQL connection string with your actual database credentials.
  3. Save the file and restart Claude Desktop. The MCP server should now appear in the MCP server selection dropdown in Settings.

Example Configuration

Here's a complete example of a configuration file with 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"
            ]
        }
    }
}

Updating Configuration

To update your Claude Desktop configuration:

  1. Open Claude Desktop
  2. Go to Settings > Developer > Edit Config
  3. Make your changes to the configuration file
  4. Save the file
  5. Restart Claude Desktop for the changes to take effect
  6. If you've updated the MCP server code, make sure to rebuild it with npm run build before restarting

Features

  • Read-Only Database Access: Only SELECT queries are permitted for security
  • Query Validation: Prevents potentially harmful SQL operations
  • Timeout Protection: Queries running longer than 10 seconds are automatically terminated
  • MCP Protocol Support: Complete implementation of the Model Context Protocol
  • JSON Response Formatting: Query results are returned in structured JSON format

API

Tools

query-postgres

Executes a read-only SQL query against the configured PostgreSQL database.

Parameters:

  • query (string): A SQL SELECT query to execute

Response:

  • JSON object containing:
    • rows: The result set rows
    • rowCount: Number of rows returned
    • fields: Column metadata

Example:

query-postgres: SELECT * FROM users LIMIT 5

Development

The main server implementation is in src/index.ts. Key components:

  • PostgreSQL connection pool setup
  • Query validation logic
  • MCP server configuration
  • Tool and resource definitions

To modify the server's behavior, you can:

  • Edit the query validation logic in isReadOnlyQuery()
  • Add additional tools or resources to the MCP server
  • Modify the query timeout duration (currently 10 seconds)

Security Considerations

  • The server validates all queries to ensure they are read-only
  • Connection to the database uses SSL
  • Query timeout prevents resource exhaustion
  • No write operations are permitted
  • Database credentials are passed directly via command line arguments, not stored in files

License

ISC

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

相关推荐

  • NiKole Maxwell
  • I craft unique cereal names, stories, and ridiculously cute Cereal Baby images.

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Yusuf Emre Yeşilyurt
  • I find academic articles and books for research and literature reviews.

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • Bora Yalcin
  • Evaluator for marketplace product descriptions, checks for relevancy and keyword stuffing.

  • Carlos Ferrin
  • Encuentra películas y series en plataformas de streaming.

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • Contraband Interactive
  • Emulating Dr. Jordan B. Peterson's style in providing life advice and insights.

  • rustassistant.com
  • Your go-to expert in the Rust ecosystem, specializing in precise code interpretation, up-to-date crate version checking, and in-depth source code analysis. I offer accurate, context-aware insights for all your Rust programming questions.

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Alexandru Strujac
  • Efficient thumbnail creator for YouTube videos

  • apappascs
  • 发现市场上最全面,最新的MCP服务器集合。该存储库充当集中式枢纽,提供了广泛的开源和专有MCP服务器目录,并提供功能,文档链接和贡献者。

  • modelcontextprotocol
  • 模型上下文协议服务器

  • Mintplex-Labs
  • 带有内置抹布,AI代理,无代理构建器,MCP兼容性等的多合一桌面和Docker AI应用程序。

  • ShrimpingIt
  • MCP系列GPIO Expander的基于Micropython I2C的操作,源自ADAFRUIT_MCP230XX

  • OffchainLabs
  • 进行以太坊的实施

  • n8n-io
  • 具有本机AI功能的公平代码工作流程自动化平台。将视觉构建与自定义代码,自宿主或云相结合,400+集成。

  • huahuayu
  • 统一的API网关,用于将多个Etherscan样区块链Explorer API与对AI助手的模型上下文协议(MCP)支持。

    Reviews

    1 (1)
    Avatar
    user_AVN0SVdJ
    2025-04-15

    The Pushinator MCP from appricos is a game-changer for MCP applications. Its seamless integration and user-friendly interface make it incredibly convenient to use. The efficiency of push notifications ensures you stay updated without any hassle. Highly recommend checking it out at https://mcp.so/server/pushinator-mcp/appricos.