npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pg-ro-mcp

v0.1.0

Published

PostgreSQL MCP server (read-only)

Readme

pg-ro-mcp

A read-only PostgreSQL server for the Model Context Protocol (MCP). Lets AI assistants query your PostgreSQL database safely — with zero risk of data modification, even against adversarial prompt injection attempts.

Forked from Anthropic's @modelcontextprotocol/server-postgres (v0.6.2), rewritten in TypeScript with security patches applied.

Security

The original server had a known SQL injection vulnerability documented by Datadog Security Labs: the client.query() method accepts stacked queries, allowing an attacker to inject COMMIT; to escape the read-only transaction and execute arbitrary write operations.

This server applies three layers of defense:

  1. Prepared statements — the name field in the query config causes PostgreSQL to use the extended query protocol, which rejects multiple statements at the protocol level. This blocks stacked query injection entirely.
  2. Read-only transactions — every user query runs inside BEGIN TRANSACTION READ ONLY, so even if an attacker bypasses other defenses, the database rejects writes.
  3. Connection destruction — connections are destroyed (not recycled) after each query via client.release(true), preventing session state poisoning across queries.

Note: Internal metadata queries (listing tables, reading schemas) use pool.query() directly without the security layers. These queries are hardcoded against information_schema and do not accept user input, so the overhead of prepared statements and connection destruction is unnecessary.

Installation

npm install pg-ro-mcp

Or clone and build from source:

git clone https://github.com/SimonJang/pg-ro-mcp.git
cd pg-ro-mcp
npm install
npm run build

Usage

pg-ro-mcp --database-url postgresql://user:password@localhost:5432/mydb

The --database-url flag is required. The server communicates over stdio using the MCP protocol.

Claude Desktop

Add to your claude_desktop_config.json:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["pg-ro-mcp", "--database-url", "postgresql://user:password@localhost:5432/mydb"]
    }
  }
}

Claude Code

Add to your .mcp.json:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["pg-ro-mcp", "--database-url", "postgresql://user:password@localhost:5432/mydb"]
    }
  }
}

MCP Resources

The server exposes database tables as MCP resources:

| Resource | URI Pattern | Description | |----------|-------------|-------------| | List tables | (resource listing) | Returns all tables in the public schema with postgres://{table}/schema URIs | | Table schema | postgres://{table}/schema | Returns column names and data types for the given table as JSON |

MCP Tools

query

Execute a read-only SQL query against the database.

| Parameter | Type | Description | |-----------|------|-------------| | sql | string | SQL query to execute |

Returns a JSON array of result rows on success. On failure, returns a structured MCP error response with isError: true containing the error message and the original SQL.

Example:

SELECT id, name, email FROM users WHERE active = true LIMIT 10

Development

npm run test      # run 17 unit tests (no database required)
npm run build     # compile TypeScript to dist/
npx tsc --noEmit  # type check without emitting

All tests use mocked pg.Pool — no real PostgreSQL connection is needed.

Requirements

  • Node.js >= 20
  • PostgreSQL database

License

MIT