Skip to main content
Glama
@yawlabs/postgres-mcp

@yawlabs/postgres-mcp

by YawLabs

Run read-only SQL

pg_readonly
Read-onlyIdempotent

Run SELECT, WITH, VALUES, TABLE, SHOW, or EXPLAIN statements inside an always-rolled-back READ ONLY transaction to inspect PostgreSQL data without writes.

Instructions

Run one SQL statement inside a BEGIN READ ONLY transaction that is always rolled back, regardless of ALLOW_WRITES. Postgres rejects INSERT/UPDATE/DELETE/DDL there. Use this to read: SELECT, WITH ... SELECT, VALUES, TABLE, SHOW, EXPLAIN. Statements that escape or reshape the transaction are refused (SET, RESET, DO, CALL, PREPARE, EXECUTE, COPY, LOCK, transaction control and similar), as are calls to known out-of-transaction functions (dblink*, set_config, pg_read_file, lo_export, pg_terminate_backend, ...). These checks are best-effort and are NOT a security boundary: a function wrapper gets past them, and a function can write through its own connection. The DATABASE_URL role is the boundary, so do not auto-allow this tool unless that role is least-privilege. Use params for parameterized queries to avoid SQL injection. Params can be strings, numbers, booleans, null, arrays (for postgres arrays / ANY), or objects (for json/jsonb columns). Large result sets are truncated to POSTGRES_MAX_ROWS (default 1000) with a truncated: true flag; a result over POSTGRES_MAX_RESULT_BYTES fails instead, and a call past POSTGRES_QUERY_DEADLINE_MS has its backend terminated.

Tool call template

Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {
      "sql": "<sql>"
    },
    "name": "pg_readonly"
  }
}

Input Schema

NameRequiredDescriptionDefault
sqlYesThe SQL statement to execute. Hard cap of 1 MB.
paramsNoPositional parameters referenced as $1, $2, ... in the SQL.

Output Schema

NameRequiredDescriptionDefault
rowsYesResult rows, capped at POSTGRES_MAX_ROWS. Values are whatever JSON type pg parsed the column into.
fieldsYesResult column descriptors, in select-list order.
commandNoPostgres command tag (`INSERT`, `CREATE TABLE`, ...). Absent on the cursor path -- read absence as 'row-returning statement, command unknown'.
rowCountYesRows AFFECTED for DML -- not necessarily rows.length -- and rows returned on the cursor path. Null when pg reported no count.
truncatedNoPresent and true only when the result hit POSTGRES_MAX_ROWS and rows were dropped.

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed2 schema fields changedv0.12.1
    • removedOutput schema / properties / rowCount / anyOf
      Removed value: -[
      -  {
      -    "type": "number"
      -  },
      -  {
      -    "type": "null"
      -  }
      -]
    • addedOutput schema / properties / rowCount / type
      Added value: +[
      +  "number",
      +  "null"
      +]
  2. Changed5 schema fields changedv0.12.0
    • addedInput schema / $defs
      Added value: +{
      +  "__schema0": {
      +    "anyOf": [
      +      {
      +        "type": "string"
      +      },
      +      {
      +        "type": "number"
      +      },
      +      {
      +        "type": "boolean"
      +      },
      +      {
      +        "type": "null"
      +      },
      +      {
      +        "items": {
      +          "$ref": "#/$defs/__schema0"
      +        },
      +        "type": "array"
      +      },
      +      {
      +        "additionalProperties": {
      +          "$ref": "#/$defs/__schema0"
      +        },
      +        "propertyNames": {
      +          "type": "string"
      +        },
      +        "type": "object"
      +      }
      +    ]
      +  }
      +}
    • changedInput schema / $schema
      Previous value: -"http://json-schema.org/draft-07/schema#"New value: +"https://json-schema.org/draft/2020-12/schema"
    • removedInput schema / definitions
      Removed value: -{
      -  "__schema0": {
      -    "anyOf": [
      -      {
      -        "type": "string"
      -      },
      -      {
      -        "type": "number"
      -      },
      -      {
      -        "type": "boolean"
      -      },
      -      {
      -        "type": "null"
      -      },
      -      {
      -        "items": {
      -          "$ref": "#/definitions/__schema0"
      -        },
      -        "type": "array"
      -      },
      -      {
      -        "additionalProperties": {
      -          "$ref": "#/definitions/__schema0"
      -        },
      -        "propertyNames": {
      -          "type": "string"
      -        },
      -        "type": "object"
      -      }
      -    ]
      -  }
      -}
    • changedInput schema / properties / params / items / $ref
      Previous value: -"#/definitions/__schema0"New value: +"#/$defs/__schema0"
    • changedOutput schema / (root)
      Previous value: -nullNew value: +{
      +  "$schema": "https://json-schema.org/draft/2020-12/schema",
      +  "additionalProperties": false,
      +  "properties": {
      +    "command": {
      +      "description": "Postgres command tag (`INSERT`, `CREATE TABLE`, ...). Absent on the cursor path -- read absence as 'row-returning statement, command unknown'.",
      +      "type": "string"
      +    },
      +    "fields": {
      +      "description": "Result column descriptors, in select-list order.",
      +      "items": {
      +        "additionalProperties": false,
      +        "properties": {
      +          "dataTypeID": {
      +            "type": "number"
      +          },
      +          "dataTypeName": {
      +            "type": "string"
      +          },
      +          "name": {
      +            "type": "string"
      +          }
      +        },
      +        "required": [
      +          "name",
      +          "dataTypeID"
      +        ],
      +        "type": "object"
      +      },
      +      "type": "array"
      +    },
      +    "rowCount": {
      +      "anyOf": [
      +        {
      +          "type": "number"
      +        },
      +        {
      +          "type": "null"
      +        }
      +      ],
      +      "description": "Rows AFFECTED for DML -- not necessarily rows.length -- and rows returned on the cursor path. Null when pg reported no count."
      +    },
      +    "rows": {
      +      "description": "Result rows, capped at POSTGRES_MAX_ROWS. Values are whatever JSON type pg parsed the column into.",
      +      "items": {
      +        "additionalProperties": {},
      +        "propertyNames": {
      +          "type": "string"
      +        },
      +        "type": "object"
      +      },
      +      "type": "array"
      +    },
      +    "truncated": {
      +      "description": "Present and true only when the result hit POSTGRES_MAX_ROWS and rows were dropped.",
      +      "type": "boolean"
      +    }
      +  },
      +  "required": [
      +    "rows",
      +    "rowCount",
      +    "fields"
      +  ],
      +  "type": "object"
      +}
  3. First observedv0.7.0

TDQS

A4.3/5.0
Behavior5/5

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

Adds substantial context beyond the annotations: rollback semantics independent of `ALLOW_WRITES`, best-effort checks explicitly declared NOT a security boundary, the function-wrapper escape, truncation at `POSTGRES_MAX_ROWS`, hard failure past `POSTGRES_MAX_RESULT_BYTES`, and backend termination at `POSTGRES_QUERY_DEADLINE_MS`. This is exactly the behavioral detail annotations like readOnlyHint cannot convey, and it is consistent with them.

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

Conciseness4/5

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

The core semantic — one statement, read-only transaction, always rolled back — is front-loaded, and the dense sentences each carry distinct information. The cautionary paragraph and the limits paragraph are lengthy but justified for a tool with escape hatches. Slightly sprawling, with the safety caveat interleaved before the return-behavior limits.

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

Completeness5/5

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

An output schema exists, so return values need no explanation, and the description still covers truncation signaling, size failures, and timeout behavior. For a potentially dangerous SQL-execution tool, the security caveat and rollback guarantee complete what an agent needs to invoke it responsibly.

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

Parameters4/5

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

Schema coverage is 100%, so the baseline would be 3, but the description adds real meaning: `params` is positional ($1, $2, ...), accepts strings/numbers/booleans/null/arrays for Postgres arrays and ANY, and objects for json/jsonb columns, plus the SQL-injection rationale. That goes beyond the recursive anyOf in the schema.

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

Purpose4/5

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

Names a specific verb and resource: run one SQL statement inside a `BEGIN READ ONLY` transaction that is always rolled back. The transaction model and the allowed statement classes (SELECT, WITH ... SELECT, VALUES, TABLE, SHOW, EXPLAIN) make the tool's scope unmistakable. It does not name the writable sibling (`pg_query`) explicitly, so the read-vs-write distinction is left implicit rather than routed.

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

Usage Guidelines4/5

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

Strong when-to-use coverage: an explicit allowlist of statement forms plus an explicit refusal list (SET, DO, CALL, COPY, LOCK, transaction control, out-of-transaction functions). It also warns against auto-allowing the tool unless the `DATABASE_URL` role is least-privilege. It does not point to alternative tools such as `pg_explain` or `pg_query`, so tool-selection routing is incomplete.

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