postgres-schema-mcp 0.1.0__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/CHANGELOG.md +27 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/PKG-INFO +191 -3
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/README.md +189 -1
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/docs/DECISIONS.md +37 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/pyproject.toml +2 -2
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/server.json +2 -2
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/adapters/db.py +44 -1
- postgres_schema_mcp-0.1.2/src/postgres_schema_mcp/core/diagnostics.py +100 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/server.py +54 -10
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/integration/test_database.py +51 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/integration/test_server_live.py +9 -0
- postgres_schema_mcp-0.1.2/tests/unit/test_config_documented.py +43 -0
- postgres_schema_mcp-0.1.2/tests/unit/test_diagnostics.py +116 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/unit/test_server.py +182 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/uv.lock +15 -15
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/.gitignore +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/.python-version +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/CONTRIBUTING.md +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/Dockerfile +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/LICENSE +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/Makefile +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/SECURITY.md +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/docker-compose.yml +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/docs/SPEC.md +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/docs/architecture.md +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/docs/demo.gif +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/docs/demo.tape +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/evals/baseline/conformance.json +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/evals/baseline/titan-bedrock.json +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/evals/capture_catalog.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/evals/check_live.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/evals/demo.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/evals/faults.yaml +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/evals/fixtures/catalog.json +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/evals/measure.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/evals/questions.yaml +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/evals/results/latest.json +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/init/10-seed.sh +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/init/20-grants.sql +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/scripts/banned-words.txt +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/scripts/check-readme-numbers.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/scripts/check-writing.sh +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/scripts/strip-ai-trailers.sh +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/__init__.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/adapters/__init__.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/adapters/backends.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/__init__.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/ddl.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/guard.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/retrieval.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/types.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/py.typed +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/__init__.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/conftest.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/integration/__init__.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/integration/conftest.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/integration/test_readonly_role.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/unit/__init__.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/unit/test_db_helpers.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/unit/test_guard.py +0 -0
- {postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/tests/unit/test_retrieval.py +0 -0
|
@@ -4,6 +4,33 @@ All notable changes are listed here. The format follows [Keep a Changelog](https
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [0.1.2] - 2026-11-25
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- The `mcp` SDK pin moves from 2.0.0 to 2.3.0. The protocol version, the tool list and schemas, the tool annotations, the instructions and the guard's refusal text are the same on both. The one difference a client can see is the error text below, which is why the fix that follows ships with the bump.
|
|
12
|
+
- The locked `boto3` moves to 1.44.9. It is only used by the optional `bedrock` extra, whose requirement (`boto3>=1.35`) is unchanged.
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- When the database cannot be reached, a tool now fails with "Could not connect to the database", which names `PGSS_DSN`, instead of raising the driver's exception as it was. The `mcp` SDK from 2.3.0 shows the model only "Error executing tool X" for such an exception, so the model would have had no hint. A schema read that fails gets the same treatment ("Could not read the schema from the database"). Both messages carry the exception's class name and never its text, because libpq repeats part of a malformed connection string, password included. Neither failure is remembered, so the next call connects again.
|
|
17
|
+
|
|
18
|
+
## [0.1.1] - 2026-11-23
|
|
19
|
+
|
|
20
|
+
### Added
|
|
21
|
+
|
|
22
|
+
- README sections for adding the server to Claude Code, Claude Desktop, Codex, Cursor and VS Code, for using it on your own database (with the SQL for a read-only role), a Configuration table with every setting, Troubleshooting and Privacy. Only Claude Code was run. The other clients follow their own documentation as read on 2026-11-22.
|
|
23
|
+
- A test that fails when an environment variable the code reads has no row in the README's Configuration table.
|
|
24
|
+
- A pull request template, and Dependabot for GitHub Actions and uv.
|
|
25
|
+
|
|
26
|
+
### Fixed
|
|
27
|
+
|
|
28
|
+
- On a database whose schemas are not the AdventureWorks default, `search_schema` returned `"tables": []` with no error. It now fails with a message that lists the schemas the role can read and the `PGSS_SCHEMAS` value to set, and the empty result is not remembered, so a restart or a grant is picked up. A misspelt schema in `PGSS_SCHEMAS` logs a warning.
|
|
29
|
+
- That message repeats only schema names made of plain identifier characters, lists at most 20 and counts the rest, because a schema name can hold any text and the message goes to the model. If listing the schemas fails, the message says so.
|
|
30
|
+
- The instructions sent to the model name schema, table and column names as untrusted text, and say so for error messages that list them.
|
|
31
|
+
- README: the role SQL says it needs a superuser, not the database owner, and that `GRANT SELECT ON ALL TABLES` exposes every row to the model provider.
|
|
32
|
+
- The instructions sent to the model said the database was AdventureWorks with 68 tables, whatever it was connected to.
|
|
33
|
+
|
|
7
34
|
## [0.1.0] - 2026-11-20
|
|
8
35
|
|
|
9
36
|
First release.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: postgres-schema-mcp
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: An MCP server that retrieves the relevant slice of a wide Postgres schema instead of dumping all of it, with read-only enforced in three layers.
|
|
5
5
|
Project-URL: Homepage, https://github.com/akshayyswami/postgres-schema-mcp
|
|
6
6
|
Project-URL: Issues, https://github.com/akshayyswami/postgres-schema-mcp/issues
|
|
@@ -13,7 +13,7 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.13
|
|
14
14
|
Classifier: Topic :: Database
|
|
15
15
|
Requires-Python: >=3.12
|
|
16
|
-
Requires-Dist: mcp==2.
|
|
16
|
+
Requires-Dist: mcp==2.3.0
|
|
17
17
|
Requires-Dist: psycopg[binary,pool]<4,>=3.2
|
|
18
18
|
Requires-Dist: pyyaml<7,>=6.0
|
|
19
19
|
Requires-Dist: rank-bm25<0.3,>=0.2.2
|
|
@@ -85,12 +85,200 @@ Then point your MCP client at it:
|
|
|
85
85
|
}
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
-
The package is also on PyPI, so a client can run it without a clone. Set `"command": "uvx"` and `"args": ["postgres-schema-mcp"]`, with the same `env`, and point `PGSS_DSN` at your own database through a role that can only `SELECT`.
|
|
88
|
+
The package is also on PyPI, so a client can run it without a clone. Set `"command": "uvx"` and `"args": ["postgres-schema-mcp"]`, with the same `env`, and point `PGSS_DSN` at your own database through a role that can only `SELECT`, and set `PGSS_SCHEMAS`. [Add it to your client](#add-it-to-your-client) and [Your own database](#your-own-database) have the steps.
|
|
89
89
|
|
|
90
90
|
stdio by default. `postgres-schema-mcp --http` serves Streamable HTTP on port 3000 instead. The credentials above belong to a local throwaway container holding public sample data. There are no real credentials in this repo.
|
|
91
91
|
|
|
92
92
|
---
|
|
93
93
|
|
|
94
|
+
## Add it to your client
|
|
95
|
+
|
|
96
|
+
The server is a local command that talks over stdio, so every client needs the same three things: the command `uvx postgres-schema-mcp`, the environment variable `PGSS_DSN`, and on your own database `PGSS_SCHEMAS` (see [Your own database](#your-own-database)). Only Claude Code was run by me. The other snippets follow each client's own documentation as I read it on 2026-11-22, and each has a link, so check the page if a client has changed. Any other client that can start a stdio MCP server takes the same command, `args` and `env`.
|
|
97
|
+
|
|
98
|
+
The DSN below is the demo database from the Quickstart. Swap in your own.
|
|
99
|
+
|
|
100
|
+
### Claude Code
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
claude mcp add postgres-schema-mcp \
|
|
104
|
+
-e PGSS_DSN=postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks \
|
|
105
|
+
-- uvx postgres-schema-mcp
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The server is added for you in the current project only. Add `--scope user` to have it in every project. Do not use `--scope project` with a password in the DSN: it writes `.mcp.json`, which is meant to be committed. `claude mcp list` should show `✔ Connected`. The first start downloads the package. One cold check I ran reported a 30 second timeout and the next one connected, so run it again if the first fails.
|
|
109
|
+
|
|
110
|
+
### Claude Desktop
|
|
111
|
+
|
|
112
|
+
Open Settings, Developer, Edit Config. That opens `claude_desktop_config.json`: `~/Library/Application Support/Claude/` on macOS, `%APPDATA%\Claude\` on Windows, `~/.config/Claude/` on Linux. Add the server, then quit and restart Claude Desktop ([docs](https://modelcontextprotocol.io/docs/develop/connect-local-servers)).
|
|
113
|
+
|
|
114
|
+
```json
|
|
115
|
+
{
|
|
116
|
+
"mcpServers": {
|
|
117
|
+
"postgres-schema-mcp": {
|
|
118
|
+
"command": "uvx",
|
|
119
|
+
"args": ["postgres-schema-mcp"],
|
|
120
|
+
"env": {
|
|
121
|
+
"PGSS_DSN": "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Codex (CLI, desktop app and IDE extension)
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
codex mcp add postgres-schema-mcp \
|
|
132
|
+
--env PGSS_DSN=postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks \
|
|
133
|
+
-- uvx postgres-schema-mcp
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Or put it in `~/.codex/config.toml`, or in `.codex/config.toml` for a trusted project ([docs](https://developers.openai.com/codex/mcp)). Codex waits 10 seconds for a server to start by default, and a first `uvx` run can take longer, so the example raises it:
|
|
137
|
+
|
|
138
|
+
```toml
|
|
139
|
+
[mcp_servers.postgres-schema-mcp]
|
|
140
|
+
command = "uvx"
|
|
141
|
+
args = ["postgres-schema-mcp"]
|
|
142
|
+
startup_timeout_sec = 60
|
|
143
|
+
|
|
144
|
+
[mcp_servers.postgres-schema-mcp.env]
|
|
145
|
+
PGSS_DSN = "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Check it with `codex mcp list`, or `/mcp` inside Codex.
|
|
149
|
+
|
|
150
|
+
### Cursor
|
|
151
|
+
|
|
152
|
+
Put this in `~/.cursor/mcp.json` for every project, or `.cursor/mcp.json` for one ([docs](https://cursor.com/docs/context/mcp)):
|
|
153
|
+
|
|
154
|
+
```json
|
|
155
|
+
{
|
|
156
|
+
"mcpServers": {
|
|
157
|
+
"postgres-schema-mcp": {
|
|
158
|
+
"type": "stdio",
|
|
159
|
+
"command": "uvx",
|
|
160
|
+
"args": ["postgres-schema-mcp"],
|
|
161
|
+
"env": {
|
|
162
|
+
"PGSS_DSN": "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Cursor's docs let you write `"${env:PGSS_DSN}"` in `env` so the password stays out of the file. Server output is in the Output panel under "MCP Logs".
|
|
170
|
+
|
|
171
|
+
### VS Code (GitHub Copilot)
|
|
172
|
+
|
|
173
|
+
Put this in `.vscode/mcp.json` in the workspace, or run "MCP: Add Server" from the Command Palette ([docs](https://code.visualstudio.com/docs/copilot/customization/mcp-servers)). The top-level key here is `servers`, not `mcpServers`:
|
|
174
|
+
|
|
175
|
+
```json
|
|
176
|
+
{
|
|
177
|
+
"servers": {
|
|
178
|
+
"postgres-schema-mcp": {
|
|
179
|
+
"type": "stdio",
|
|
180
|
+
"command": "uvx",
|
|
181
|
+
"args": ["postgres-schema-mcp"],
|
|
182
|
+
"env": {
|
|
183
|
+
"PGSS_DSN": "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
"MCP: List Servers" shows whether it connected. VS Code's docs also describe a newer Copilot location, `.mcp.json` at the workspace root with the `mcpServers` key, so try that if `.vscode/mcp.json` is not picked up.
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Use it
|
|
195
|
+
|
|
196
|
+
### Your own database
|
|
197
|
+
|
|
198
|
+
Give the server a role that can read and nothing else. It is the layer that holds, so do not skip it. Run this as a superuser, with your own names. Creating a role needs `CREATEROLE` or superuser, and only a superuser (or, from PostgreSQL 15, a role granted `SET` on the setting) can set `temp_file_limit`, so the database owner is not enough. Run it with `psql -v ON_ERROR_STOP=1` so one failed line stops the rest instead of leaving a half-made role:
|
|
199
|
+
|
|
200
|
+
```sql
|
|
201
|
+
CREATE ROLE scout_ro LOGIN PASSWORD 'change-me' NOSUPERUSER NOCREATEDB NOCREATEROLE;
|
|
202
|
+
GRANT CONNECT ON DATABASE shop TO scout_ro;
|
|
203
|
+
GRANT USAGE ON SCHEMA public, billing TO scout_ro;
|
|
204
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA public, billing TO scout_ro;
|
|
205
|
+
-- Tables that app_owner creates later are readable too:
|
|
206
|
+
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public, billing GRANT SELECT ON TABLES TO scout_ro;
|
|
207
|
+
-- What one connection can cost:
|
|
208
|
+
ALTER ROLE scout_ro CONNECTION LIMIT 8;
|
|
209
|
+
ALTER ROLE scout_ro SET temp_file_limit = '256MB';
|
|
210
|
+
ALTER ROLE scout_ro SET work_mem = '16MB';
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
`GRANT SELECT ON ALL TABLES` lets the model read every row of every table in those schemas, and the rows go to the model provider (see Privacy). If a schema also holds password hashes, tokens or other secrets, grant `SELECT` on the tables you want to expose instead, one by one.
|
|
214
|
+
|
|
215
|
+
What this does not block: on a default Postgres any role can create temp tables and call `pg_sleep`, and before PostgreSQL 15 any role can create tables in the `public` schema. The AST guard refuses the first two. `init/20-grants.sql` shows how the demo database refuses them at the database too, but those revokes apply to `PUBLIC`, so they change what every role on that database can do. Use them only on a database that is dedicated to this.
|
|
216
|
+
|
|
217
|
+
Set `PGSS_SCHEMAS` to the schemas to index, comma separated, for example `PGSS_SCHEMAS=public,billing`. The default is the five AdventureWorks schemas, so on any other database the server finds no tables until you set it. It chooses what is indexed, not what the role can read.
|
|
218
|
+
|
|
219
|
+
The server reads the schema once at start-up, so restart it after a migration.
|
|
220
|
+
|
|
221
|
+
### Each time
|
|
222
|
+
|
|
223
|
+
There is nothing to type. Ask the question in plain words and Claude uses the tools in the same loop every time: `search_schema` for the relevant tables, `describe_table` for one it needs by name, `explain_query` for anything large, then `run_query`. On the demo database, try:
|
|
224
|
+
|
|
225
|
+
- Which sales orders had the highest total due in 2013?
|
|
226
|
+
- What products were on each sales order, with quantities and unit prices?
|
|
227
|
+
- Which employees have had their pay rate changed, and when?
|
|
228
|
+
|
|
229
|
+
Your client may ask you to approve the tools the first time. Ask it to change data and `run_query` refuses with a message saying the server is read-only.
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## Configuration
|
|
234
|
+
|
|
235
|
+
Every setting is an environment variable. The audit and a test in this repo fail if one the code reads is missing from this table.
|
|
236
|
+
|
|
237
|
+
| Variable | Default | What it changes |
|
|
238
|
+
|---|---|---|
|
|
239
|
+
| `PGSS_DSN` | the demo database, `postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks` | The database to connect to. Use a role that can only `SELECT` |
|
|
240
|
+
| `PGSS_SCHEMAS` | `person,humanresources,production,purchasing,sales` | The schemas to index. It chooses what is indexed, not what the role can read |
|
|
241
|
+
| `PGSS_STATEMENT_TIMEOUT_MS` | `10000` | Cancels a query that runs longer than this |
|
|
242
|
+
| `PGSS_IDLE_TX_TIMEOUT_MS` | `10000` | Ends a session left idle inside a transaction |
|
|
243
|
+
| `PGSS_MAX_ROWS_CAP` | `1000` | The most rows `run_query` returns, whatever `max_rows` asks for |
|
|
244
|
+
| `PGSS_MAX_CELL_CHARS` | `2000` | A longer text value is cut, with a note of how much was dropped |
|
|
245
|
+
| `PGSS_MAX_RESULT_BYTES` | `200000` | Cap on the JSON size of all rows from one query |
|
|
246
|
+
| `PGSS_POOL_MIN` | `1` | Connections kept open |
|
|
247
|
+
| `PGSS_POOL_MAX` | `4` | The most connections open at once |
|
|
248
|
+
| `PGSS_TRANSPORT` | `stdio` | `stdio`, or `streamable-http` (the same as the `--http` flag) |
|
|
249
|
+
| `PGSS_HOST` | `127.0.0.1` | The address the HTTP transport binds. A non-loopback address is refused unless `PGSS_ALLOW_REMOTE=1` |
|
|
250
|
+
| `PGSS_PORT_HTTP` | `3000` | The port for the HTTP transport |
|
|
251
|
+
| `PGSS_ALLOW_REMOTE` | unset | Set to `1` to allow a non-loopback bind, only behind a proxy that does the authentication |
|
|
252
|
+
| `PGSS_RETRIEVAL_BACKEND` | `bm25` | `bm25`, or `titan` (needs the `bedrock` extra) |
|
|
253
|
+
| `PGSS_TITAN_MODEL_ID` | `amazon.titan-embed-text-v2:0` | The embedding model when the backend is `titan` |
|
|
254
|
+
| `AWS_REGION` | `us-east-1` | The Bedrock region when the backend is `titan` |
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
## Troubleshooting
|
|
259
|
+
|
|
260
|
+
| What you see | Why | Fix |
|
|
261
|
+
|---|---|---|
|
|
262
|
+
| `search_schema` fails with "No tables were found in the indexed schemas" (version 0.1.0 returns `"tables": []` instead) | The default schemas are the AdventureWorks ones, and your database holds none of them | Set `PGSS_SCHEMAS` to the schemas that hold your tables. The message lists the ones the role can read |
|
|
263
|
+
| The client says the server failed to connect, right after you added it | The first `uvx` run downloads the package and can take longer than the client waits | Run the check again. In Codex raise `startup_timeout_sec` |
|
|
264
|
+
| A tool fails with "Could not connect to the database" | `PGSS_DSN` is wrong or unset, the database is down or not reachable from this machine, or the role cannot log in. The first attempt waits up to 30 seconds. The message names the exception class, and a refused connection, a wrong password and a malformed DSN all show as `PoolTimeout`, so the reason is in the server's log, which has the driver's own message | Check the DSN with `psql "$PGSS_DSN"`, then call the tool again. The next call connects again, so a restart is not needed |
|
|
265
|
+
| A tool fails with "Could not read the schema from the database" | The connection dropped while the schema was read, or the role cannot read the catalog | Check that the database is up and that the role can read the schemas in `PGSS_SCHEMAS`, then call the tool again |
|
|
266
|
+
| `permission denied for table ...` | The role has no `SELECT` on that table, or no `USAGE` on its schema | Grant both, as in [Your own database](#your-own-database) |
|
|
267
|
+
| `run_query` returns "Blocked: ..." | The guard refused a statement that is not a single read-only `SELECT` | Rewrite it as one `SELECT`. See the safety model below |
|
|
268
|
+
| Tables you added are missing from the results | The schema is read once at start-up | Restart the server |
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## Privacy
|
|
273
|
+
|
|
274
|
+
The server connects to the database you give it, and it sends no telemetry. Table and column names, comments and the rows a query returns go to the model through your client, so they leave your machine for whatever model that client uses.
|
|
275
|
+
|
|
276
|
+
Two downloads happen on first use, and neither carries anything from your database. `uvx` fetches the package from PyPI. The first time the server counts tokens, the `tiktoken` library downloads its `cl100k_base` vocabulary file from `openaipublic.blob.core.windows.net` and keeps it in the system temp folder (`TIKTOKEN_CACHE_DIR` changes where). If that download fails, token counts fall back to a rough estimate, and the tokenizer is named as approximate in the result.
|
|
277
|
+
|
|
278
|
+
With `PGSS_RETRIEVAL_BACKEND=titan`, the text describing each table is also sent to Amazon Bedrock. The default, `bm25`, sends nothing.
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
94
282
|
## How it works
|
|
95
283
|
|
|
96
284
|
```mermaid
|
|
@@ -60,12 +60,200 @@ Then point your MCP client at it:
|
|
|
60
60
|
}
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
-
The package is also on PyPI, so a client can run it without a clone. Set `"command": "uvx"` and `"args": ["postgres-schema-mcp"]`, with the same `env`, and point `PGSS_DSN` at your own database through a role that can only `SELECT`.
|
|
63
|
+
The package is also on PyPI, so a client can run it without a clone. Set `"command": "uvx"` and `"args": ["postgres-schema-mcp"]`, with the same `env`, and point `PGSS_DSN` at your own database through a role that can only `SELECT`, and set `PGSS_SCHEMAS`. [Add it to your client](#add-it-to-your-client) and [Your own database](#your-own-database) have the steps.
|
|
64
64
|
|
|
65
65
|
stdio by default. `postgres-schema-mcp --http` serves Streamable HTTP on port 3000 instead. The credentials above belong to a local throwaway container holding public sample data. There are no real credentials in this repo.
|
|
66
66
|
|
|
67
67
|
---
|
|
68
68
|
|
|
69
|
+
## Add it to your client
|
|
70
|
+
|
|
71
|
+
The server is a local command that talks over stdio, so every client needs the same three things: the command `uvx postgres-schema-mcp`, the environment variable `PGSS_DSN`, and on your own database `PGSS_SCHEMAS` (see [Your own database](#your-own-database)). Only Claude Code was run by me. The other snippets follow each client's own documentation as I read it on 2026-11-22, and each has a link, so check the page if a client has changed. Any other client that can start a stdio MCP server takes the same command, `args` and `env`.
|
|
72
|
+
|
|
73
|
+
The DSN below is the demo database from the Quickstart. Swap in your own.
|
|
74
|
+
|
|
75
|
+
### Claude Code
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
claude mcp add postgres-schema-mcp \
|
|
79
|
+
-e PGSS_DSN=postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks \
|
|
80
|
+
-- uvx postgres-schema-mcp
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The server is added for you in the current project only. Add `--scope user` to have it in every project. Do not use `--scope project` with a password in the DSN: it writes `.mcp.json`, which is meant to be committed. `claude mcp list` should show `✔ Connected`. The first start downloads the package. One cold check I ran reported a 30 second timeout and the next one connected, so run it again if the first fails.
|
|
84
|
+
|
|
85
|
+
### Claude Desktop
|
|
86
|
+
|
|
87
|
+
Open Settings, Developer, Edit Config. That opens `claude_desktop_config.json`: `~/Library/Application Support/Claude/` on macOS, `%APPDATA%\Claude\` on Windows, `~/.config/Claude/` on Linux. Add the server, then quit and restart Claude Desktop ([docs](https://modelcontextprotocol.io/docs/develop/connect-local-servers)).
|
|
88
|
+
|
|
89
|
+
```json
|
|
90
|
+
{
|
|
91
|
+
"mcpServers": {
|
|
92
|
+
"postgres-schema-mcp": {
|
|
93
|
+
"command": "uvx",
|
|
94
|
+
"args": ["postgres-schema-mcp"],
|
|
95
|
+
"env": {
|
|
96
|
+
"PGSS_DSN": "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Codex (CLI, desktop app and IDE extension)
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
codex mcp add postgres-schema-mcp \
|
|
107
|
+
--env PGSS_DSN=postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks \
|
|
108
|
+
-- uvx postgres-schema-mcp
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Or put it in `~/.codex/config.toml`, or in `.codex/config.toml` for a trusted project ([docs](https://developers.openai.com/codex/mcp)). Codex waits 10 seconds for a server to start by default, and a first `uvx` run can take longer, so the example raises it:
|
|
112
|
+
|
|
113
|
+
```toml
|
|
114
|
+
[mcp_servers.postgres-schema-mcp]
|
|
115
|
+
command = "uvx"
|
|
116
|
+
args = ["postgres-schema-mcp"]
|
|
117
|
+
startup_timeout_sec = 60
|
|
118
|
+
|
|
119
|
+
[mcp_servers.postgres-schema-mcp.env]
|
|
120
|
+
PGSS_DSN = "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Check it with `codex mcp list`, or `/mcp` inside Codex.
|
|
124
|
+
|
|
125
|
+
### Cursor
|
|
126
|
+
|
|
127
|
+
Put this in `~/.cursor/mcp.json` for every project, or `.cursor/mcp.json` for one ([docs](https://cursor.com/docs/context/mcp)):
|
|
128
|
+
|
|
129
|
+
```json
|
|
130
|
+
{
|
|
131
|
+
"mcpServers": {
|
|
132
|
+
"postgres-schema-mcp": {
|
|
133
|
+
"type": "stdio",
|
|
134
|
+
"command": "uvx",
|
|
135
|
+
"args": ["postgres-schema-mcp"],
|
|
136
|
+
"env": {
|
|
137
|
+
"PGSS_DSN": "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Cursor's docs let you write `"${env:PGSS_DSN}"` in `env` so the password stays out of the file. Server output is in the Output panel under "MCP Logs".
|
|
145
|
+
|
|
146
|
+
### VS Code (GitHub Copilot)
|
|
147
|
+
|
|
148
|
+
Put this in `.vscode/mcp.json` in the workspace, or run "MCP: Add Server" from the Command Palette ([docs](https://code.visualstudio.com/docs/copilot/customization/mcp-servers)). The top-level key here is `servers`, not `mcpServers`:
|
|
149
|
+
|
|
150
|
+
```json
|
|
151
|
+
{
|
|
152
|
+
"servers": {
|
|
153
|
+
"postgres-schema-mcp": {
|
|
154
|
+
"type": "stdio",
|
|
155
|
+
"command": "uvx",
|
|
156
|
+
"args": ["postgres-schema-mcp"],
|
|
157
|
+
"env": {
|
|
158
|
+
"PGSS_DSN": "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
"MCP: List Servers" shows whether it connected. VS Code's docs also describe a newer Copilot location, `.mcp.json` at the workspace root with the `mcpServers` key, so try that if `.vscode/mcp.json` is not picked up.
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Use it
|
|
170
|
+
|
|
171
|
+
### Your own database
|
|
172
|
+
|
|
173
|
+
Give the server a role that can read and nothing else. It is the layer that holds, so do not skip it. Run this as a superuser, with your own names. Creating a role needs `CREATEROLE` or superuser, and only a superuser (or, from PostgreSQL 15, a role granted `SET` on the setting) can set `temp_file_limit`, so the database owner is not enough. Run it with `psql -v ON_ERROR_STOP=1` so one failed line stops the rest instead of leaving a half-made role:
|
|
174
|
+
|
|
175
|
+
```sql
|
|
176
|
+
CREATE ROLE scout_ro LOGIN PASSWORD 'change-me' NOSUPERUSER NOCREATEDB NOCREATEROLE;
|
|
177
|
+
GRANT CONNECT ON DATABASE shop TO scout_ro;
|
|
178
|
+
GRANT USAGE ON SCHEMA public, billing TO scout_ro;
|
|
179
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA public, billing TO scout_ro;
|
|
180
|
+
-- Tables that app_owner creates later are readable too:
|
|
181
|
+
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public, billing GRANT SELECT ON TABLES TO scout_ro;
|
|
182
|
+
-- What one connection can cost:
|
|
183
|
+
ALTER ROLE scout_ro CONNECTION LIMIT 8;
|
|
184
|
+
ALTER ROLE scout_ro SET temp_file_limit = '256MB';
|
|
185
|
+
ALTER ROLE scout_ro SET work_mem = '16MB';
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
`GRANT SELECT ON ALL TABLES` lets the model read every row of every table in those schemas, and the rows go to the model provider (see Privacy). If a schema also holds password hashes, tokens or other secrets, grant `SELECT` on the tables you want to expose instead, one by one.
|
|
189
|
+
|
|
190
|
+
What this does not block: on a default Postgres any role can create temp tables and call `pg_sleep`, and before PostgreSQL 15 any role can create tables in the `public` schema. The AST guard refuses the first two. `init/20-grants.sql` shows how the demo database refuses them at the database too, but those revokes apply to `PUBLIC`, so they change what every role on that database can do. Use them only on a database that is dedicated to this.
|
|
191
|
+
|
|
192
|
+
Set `PGSS_SCHEMAS` to the schemas to index, comma separated, for example `PGSS_SCHEMAS=public,billing`. The default is the five AdventureWorks schemas, so on any other database the server finds no tables until you set it. It chooses what is indexed, not what the role can read.
|
|
193
|
+
|
|
194
|
+
The server reads the schema once at start-up, so restart it after a migration.
|
|
195
|
+
|
|
196
|
+
### Each time
|
|
197
|
+
|
|
198
|
+
There is nothing to type. Ask the question in plain words and Claude uses the tools in the same loop every time: `search_schema` for the relevant tables, `describe_table` for one it needs by name, `explain_query` for anything large, then `run_query`. On the demo database, try:
|
|
199
|
+
|
|
200
|
+
- Which sales orders had the highest total due in 2013?
|
|
201
|
+
- What products were on each sales order, with quantities and unit prices?
|
|
202
|
+
- Which employees have had their pay rate changed, and when?
|
|
203
|
+
|
|
204
|
+
Your client may ask you to approve the tools the first time. Ask it to change data and `run_query` refuses with a message saying the server is read-only.
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## Configuration
|
|
209
|
+
|
|
210
|
+
Every setting is an environment variable. The audit and a test in this repo fail if one the code reads is missing from this table.
|
|
211
|
+
|
|
212
|
+
| Variable | Default | What it changes |
|
|
213
|
+
|---|---|---|
|
|
214
|
+
| `PGSS_DSN` | the demo database, `postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks` | The database to connect to. Use a role that can only `SELECT` |
|
|
215
|
+
| `PGSS_SCHEMAS` | `person,humanresources,production,purchasing,sales` | The schemas to index. It chooses what is indexed, not what the role can read |
|
|
216
|
+
| `PGSS_STATEMENT_TIMEOUT_MS` | `10000` | Cancels a query that runs longer than this |
|
|
217
|
+
| `PGSS_IDLE_TX_TIMEOUT_MS` | `10000` | Ends a session left idle inside a transaction |
|
|
218
|
+
| `PGSS_MAX_ROWS_CAP` | `1000` | The most rows `run_query` returns, whatever `max_rows` asks for |
|
|
219
|
+
| `PGSS_MAX_CELL_CHARS` | `2000` | A longer text value is cut, with a note of how much was dropped |
|
|
220
|
+
| `PGSS_MAX_RESULT_BYTES` | `200000` | Cap on the JSON size of all rows from one query |
|
|
221
|
+
| `PGSS_POOL_MIN` | `1` | Connections kept open |
|
|
222
|
+
| `PGSS_POOL_MAX` | `4` | The most connections open at once |
|
|
223
|
+
| `PGSS_TRANSPORT` | `stdio` | `stdio`, or `streamable-http` (the same as the `--http` flag) |
|
|
224
|
+
| `PGSS_HOST` | `127.0.0.1` | The address the HTTP transport binds. A non-loopback address is refused unless `PGSS_ALLOW_REMOTE=1` |
|
|
225
|
+
| `PGSS_PORT_HTTP` | `3000` | The port for the HTTP transport |
|
|
226
|
+
| `PGSS_ALLOW_REMOTE` | unset | Set to `1` to allow a non-loopback bind, only behind a proxy that does the authentication |
|
|
227
|
+
| `PGSS_RETRIEVAL_BACKEND` | `bm25` | `bm25`, or `titan` (needs the `bedrock` extra) |
|
|
228
|
+
| `PGSS_TITAN_MODEL_ID` | `amazon.titan-embed-text-v2:0` | The embedding model when the backend is `titan` |
|
|
229
|
+
| `AWS_REGION` | `us-east-1` | The Bedrock region when the backend is `titan` |
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## Troubleshooting
|
|
234
|
+
|
|
235
|
+
| What you see | Why | Fix |
|
|
236
|
+
|---|---|---|
|
|
237
|
+
| `search_schema` fails with "No tables were found in the indexed schemas" (version 0.1.0 returns `"tables": []` instead) | The default schemas are the AdventureWorks ones, and your database holds none of them | Set `PGSS_SCHEMAS` to the schemas that hold your tables. The message lists the ones the role can read |
|
|
238
|
+
| The client says the server failed to connect, right after you added it | The first `uvx` run downloads the package and can take longer than the client waits | Run the check again. In Codex raise `startup_timeout_sec` |
|
|
239
|
+
| A tool fails with "Could not connect to the database" | `PGSS_DSN` is wrong or unset, the database is down or not reachable from this machine, or the role cannot log in. The first attempt waits up to 30 seconds. The message names the exception class, and a refused connection, a wrong password and a malformed DSN all show as `PoolTimeout`, so the reason is in the server's log, which has the driver's own message | Check the DSN with `psql "$PGSS_DSN"`, then call the tool again. The next call connects again, so a restart is not needed |
|
|
240
|
+
| A tool fails with "Could not read the schema from the database" | The connection dropped while the schema was read, or the role cannot read the catalog | Check that the database is up and that the role can read the schemas in `PGSS_SCHEMAS`, then call the tool again |
|
|
241
|
+
| `permission denied for table ...` | The role has no `SELECT` on that table, or no `USAGE` on its schema | Grant both, as in [Your own database](#your-own-database) |
|
|
242
|
+
| `run_query` returns "Blocked: ..." | The guard refused a statement that is not a single read-only `SELECT` | Rewrite it as one `SELECT`. See the safety model below |
|
|
243
|
+
| Tables you added are missing from the results | The schema is read once at start-up | Restart the server |
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## Privacy
|
|
248
|
+
|
|
249
|
+
The server connects to the database you give it, and it sends no telemetry. Table and column names, comments and the rows a query returns go to the model through your client, so they leave your machine for whatever model that client uses.
|
|
250
|
+
|
|
251
|
+
Two downloads happen on first use, and neither carries anything from your database. `uvx` fetches the package from PyPI. The first time the server counts tokens, the `tiktoken` library downloads its `cl100k_base` vocabulary file from `openaipublic.blob.core.windows.net` and keeps it in the system temp folder (`TIKTOKEN_CACHE_DIR` changes where). If that download fails, token counts fall back to a rough estimate, and the tokenizer is named as approximate in the result.
|
|
252
|
+
|
|
253
|
+
With `PGSS_RETRIEVAL_BACKEND=titan`, the text describing each table is also sent to Amazon Bedrock. The default, `bm25`, sends nothing.
|
|
254
|
+
|
|
255
|
+
---
|
|
256
|
+
|
|
69
257
|
## How it works
|
|
70
258
|
|
|
71
259
|
```mermaid
|
|
@@ -85,3 +85,40 @@ One entry per decision that changes architecture or scope. Newest first.
|
|
|
85
85
|
- The demo database image downloads its sample data at build time with no checksum, and the base image is pinned by tag, not digest.
|
|
86
86
|
- Local refs `backup/pre-rewrite` and `refs/original/refs/heads/main` hold commits with attribution trailers from an earlier history rewrite. They are not on any remote. Pushes in this repo name their refs, never `--all` or `--mirror`.
|
|
87
87
|
- **Alternatives:** an allowlist of known-safe functions instead of a denylist (stronger, but it refuses a lot of ordinary SQL, and a general query tool needs those functions); dropping the denylist and relying on the role alone (the role is the layer that holds, but a parser layer gives the model a clear error and stops a bad query before the database sees it).
|
|
88
|
+
|
|
89
|
+
## 2026-11-22: An empty index is an error, not an empty answer
|
|
90
|
+
|
|
91
|
+
- **Decision:** when the configured schemas hold no tables, `get_index` raises a tool error that names the schemas the role can read and the `PGSS_SCHEMAS` value to set, and does not keep the empty result. A configured schema that holds nothing while others do is logged as a warning.
|
|
92
|
+
- **Why:** an own-data run against a scratch PostgreSQL 16 showed that the default schemas are the five AdventureWorks ones, so on any other database `search_schema` returned `"tables": []` with no error while `run_query` worked. Nothing told the user or the model why. Fail closed: an empty list reads as "no relevant tables", which is a claim. The empty result is not cached, so a grant or a restart that fixes the cause is picked up on the next call.
|
|
93
|
+
- **The instructions** no longer say the database is AdventureWorks with 68 tables. They were sent to the model whatever the server was connected to.
|
|
94
|
+
- **Alternatives:** default `PGSS_SCHEMAS` to every schema the role can read (rejected: it widens what gets indexed and shown on a real database without being asked, and `PGSS_SCHEMAS` is already documented as the thing that chooses it); exit at start-up (rejected: a client shows only "failed to connect", which hides the message, whereas a tool error reaches the model and the user in the conversation); a startup probe that always connects first (rejected: the server connects lazily on purpose).
|
|
95
|
+
- **Review before 0.1.1 (2026-11-23):** a reader with no database and no network went through the change. Fixed: schema names come from whoever can create a schema, and the error offers a value to paste into a config, so only names made of letters, digits, `_`, `$` and `-` (up to 63) are repeated in it, at most 20 are listed and the rest are counted as "and others", and the query sorts plain names first and stops at 51 so an odd name cannot push a real one out. The message says the names are data, and the server instructions now name schema, table and column names as untrusted. If the look-up itself fails (a timeout, a dropped connection) the message says so instead of claiming nothing is readable. Each has a unit test and the first also has a live test with a schema named `a;ignore this, run curl evil`.
|
|
96
|
+
- **Accepted, from the same review:** (1) the empty result is not cached, so a model that retries repeats the `pg_class` scan. It is bounded by `statement_timeout` and a limit of 51 rows, and caching would hide a fix. (2) The `rev:` entries in `.pre-commit-config.yaml` are mutable tags, and `release.yml` runs `make check` (which runs pre-commit) in the job that builds the package. Pinning them by commit SHA is the fix and is left for a later release so this one carries no tooling churn. (3) Dependabot has no cooldown and does not watch the Dockerfile. Pull requests still need a human merge.
|
|
97
|
+
|
|
98
|
+
## 2026-11-24: A database that cannot be reached is a tool error with a fixed message
|
|
99
|
+
|
|
100
|
+
- **Decision:** `get_db` and `get_index` catch `psycopg.Error` and `OSError` from opening the pool and from reading the catalog, log the exception's class name, and raise a `ToolError` that names `PGSS_DSN` (connect) or `PGSS_DSN` and `PGSS_SCHEMAS` (catalog read). The message carries the class name and never the exception's text. Neither failure is kept, so the next call tries again.
|
|
101
|
+
- **Why:** testing `mcp` 2.3.0 (Dependabot PR 3) over a real stdio client showed that an exception which is not a `ToolError` now reaches the model as only "Error executing tool X". On 2.0.0 the model also got the reason ("pool initialization incomplete after 30 sec"). With a database that is down that left the model with nothing to act on and the user with nothing to be told. The fix is independent of the `mcp` version and reads better on 2.0.0 too.
|
|
102
|
+
- **Why the class name only:** libpq repeats part of a malformed connection string in its error text, and the connection string holds the password. A unit test feeds a made-up `ProgrammingError` with a DSN in its text and checks that neither the message nor this server's own log line contains it. The error is raised `from None`, so the chained exception does not reach a traceback either. The pool's own log line is a separate matter, see the 2026-11-25 review entry.
|
|
103
|
+
- **Not caught:** anything that is not a database or socket error. A bug here should fail loudly, not read as "check your DSN". A test pins that.
|
|
104
|
+
- **Not changed:** the first connect still waits up to 30 seconds before it gives up (`pool.open(wait=True, timeout=30)`), so the error arrives after that wait. A statement timeout during `run_query` and `explain_query` keeps its own message ("Query failed: ..."), which the tool descriptions already explain.
|
|
105
|
+
- **Alternatives:** catch `Exception` (rejected: it hides our own bugs behind a connection message); repeat the driver's text (rejected: the DSN case above, and it names hosts); exit at start-up when the database is down (rejected for the same reason as in the empty-index decision: a client shows "failed to connect" and hides the message).
|
|
106
|
+
|
|
107
|
+
## 2026-11-24: Leave the two merge commits that carry the account's public email
|
|
108
|
+
|
|
109
|
+
- **Decision:** leave `f763d73` and `2cabd03` on `main` as they are. `f763d73` is the squash merge of pull request 5. `2cabd03` is the rebase merge of pull request 6, the first record of this decision.
|
|
110
|
+
- **What happened:** GitHub authored the squash commit `f763d73` with the merging account's primary email, which is the address shown on that account's public profile, and not with the repo's noreply address. It also added a `Co-authored-by` trailer naming the original commit identity, which is the same person. A squash merge with `--author-email` set to the noreply address was refused by GitHub ("Invalid email address"), so pull request 6 went in as a rebase merge. That keeps the noreply author on `2cabd03`, but GitHub sets the committer of a rebase merge to the merging account's primary email, so the public address is on `2cabd03` as committer. Every other commit in this repository uses a noreply address, or GitHub's own for merges of bot pull requests.
|
|
111
|
+
- **Why leave them:** the address is already public on the profile. Removing the commits would take a force-push of `main`, and that would not take the address out of what GitHub serves, because the old commits and the pull request pages stay reachable by their links.
|
|
112
|
+
- **Effect:** the repo audit's check that every commit uses a noreply email fails on these two commits. That failure is known and accepted. It is not a regression.
|
|
113
|
+
- **Going forward:** do not merge a pull request that holds human commits with the GitHub merge button on this account, because both squash and rebase put the public address on the new commit. Fast-forward `main` from a clean local branch with a plain `git push`, which keeps the noreply identity as author and committer, or turn on "Keep my email addresses private" in the account's email settings. Dependabot pull requests are not affected, because their author stays the bot and GitHub is the committer.
|
|
114
|
+
|
|
115
|
+
## 2026-11-25: Review before 0.1.2
|
|
116
|
+
|
|
117
|
+
- **Review:** a reader with no database and no network went through everything since v0.1.1: the unreachable-database change, the `mcp` 2.3.0 pin, the boto3 lock bump and the setup-uv pin that Dependabot moved. Nothing at high or medium. Two claims of mine were wrong and are fixed.
|
|
118
|
+
- **Fixed, the log claim:** the 2026-11-24 entry said neither the message nor the log carries the driver's text. That is true of this server's own log line only. `psycopg_pool` writes its own warning, with the driver's text, to stderr, which is where the MCP client keeps this server's log. The model never sees it. A malformed DSN could put part of the DSN in that log. It is left as it is: that line is where the real reason shows up, and the tool message points there.
|
|
119
|
+
- **Fixed, the class name claim:** a refused connection, a wrong password and a malformed port were each run against the built wheel, and each came out as `PoolTimeout` after 30 seconds, because the pool swallows a failed connect in its worker. The class name is a hint, not a diagnosis. The `ProgrammingError` test uses a made-up error to pin that no driver text is repeated.
|
|
120
|
+
- **Accepted, not new:** a client that cancels a call during the 30 second connect skips `close()`. The pool it leaves retries for up to five minutes and, if the database comes back, holds one read-only connection until the process ends.
|
|
121
|
+
- **Accepted, not new:** the lock is held while the pool opens, so calls that arrive while the database is down take turns, each waiting up to 30 seconds.
|
|
122
|
+
- **Accepted, not new:** `run_query` and `explain_query` still put the driver's message in their error. It can hold names or values from the database. The instructions sent to the model already say to treat it as untrusted.
|
|
123
|
+
- **Checked on 2026-11-23, not by a test:** the tool list, schemas, annotations, instructions and guard refusal text are the same on `mcp` 2.0.0 and 2.3.0, by driving the server with a stdio client of each version. There is no snapshot test of the tool list.
|
|
124
|
+
- **Not checked:** the setup-uv commit against its v10.4.0 tag, because the review had no network. CI passes with it.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "postgres-schema-mcp"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.2"
|
|
4
4
|
description = "An MCP server that retrieves the relevant slice of a wide Postgres schema instead of dumping all of it, with read-only enforced in three layers."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "MIT"
|
|
@@ -16,7 +16,7 @@ classifiers = [
|
|
|
16
16
|
dependencies = [
|
|
17
17
|
# Pinned exactly. The v1 line continues in parallel, so an unpinned install
|
|
18
18
|
# can land on either major version, and the two APIs are not compatible.
|
|
19
|
-
"mcp==2.
|
|
19
|
+
"mcp==2.3.0",
|
|
20
20
|
"sqlglot>=30.0,<31",
|
|
21
21
|
"psycopg[binary,pool]>=3.2,<4",
|
|
22
22
|
"rank-bm25>=0.2.2,<0.3",
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
|
|
3
3
|
"name": "io.github.akshayyswami/postgres-schema-mcp",
|
|
4
4
|
"description": "Schema access for an agent is a retrieval problem, not a dump.",
|
|
5
|
-
"version": "0.1.
|
|
5
|
+
"version": "0.1.2",
|
|
6
6
|
"repository": {
|
|
7
7
|
"url": "https://github.com/akshayyswami/postgres-schema-mcp",
|
|
8
8
|
"source": "github"
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
{
|
|
12
12
|
"registryType": "pypi",
|
|
13
13
|
"identifier": "postgres-schema-mcp",
|
|
14
|
-
"version": "0.1.
|
|
14
|
+
"version": "0.1.2",
|
|
15
15
|
"transport": { "type": "stdio" }
|
|
16
16
|
}
|
|
17
17
|
]
|
{postgres_schema_mcp-0.1.0 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/adapters/db.py
RENAMED
|
@@ -22,6 +22,7 @@ from __future__ import annotations
|
|
|
22
22
|
|
|
23
23
|
import datetime as dt
|
|
24
24
|
import json
|
|
25
|
+
import logging
|
|
25
26
|
import os
|
|
26
27
|
from dataclasses import dataclass, field
|
|
27
28
|
from decimal import Decimal
|
|
@@ -36,7 +37,17 @@ from psycopg_pool import AsyncConnectionPool
|
|
|
36
37
|
from ..core.ddl import render_ddl
|
|
37
38
|
from ..core.types import ColumnInfo, ForeignKey, QueryPlan, QueryResult, TableDetail
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
log = logging.getLogger(__name__)
|
|
41
|
+
|
|
42
|
+
__all__ = ["DATABASE_ERRORS", "Database", "Settings"]
|
|
43
|
+
|
|
44
|
+
# What a database that is down, unreachable, refusing the login or given a malformed DSN raises.
|
|
45
|
+
# PoolTimeout and PoolClosed are psycopg.OperationalError. The pool swallows a failed connect in
|
|
46
|
+
# its worker, so a refused connection, a wrong password and a malformed DSN all arrive as
|
|
47
|
+
# PoolTimeout after the wait; psycopg.ProgrammingError and OSError cover a failure raised
|
|
48
|
+
# directly, such as an unparsable DSN or a failed lookup.
|
|
49
|
+
# Anything else is a bug in this server and is left to fail loudly.
|
|
50
|
+
DATABASE_ERRORS = (psycopg.Error, OSError)
|
|
40
51
|
|
|
41
52
|
DEFAULT_DSN = "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
|
|
42
53
|
|
|
@@ -213,6 +224,24 @@ LEFT JOIN pg_namespace tn ON tn.oid = tc.relnamespace
|
|
|
213
224
|
WHERE con.contype IN ('p', 'f') AND sn.nspname = ANY(%(schemas)s)
|
|
214
225
|
"""
|
|
215
226
|
|
|
227
|
+
# Schemas holding a table this role can read, so an empty index can say what to set. Capped
|
|
228
|
+
# at 51 (the message names 20), and plain-looking names sort first, so a schema with an odd
|
|
229
|
+
# name cannot push the ordinary ones out of the answer. The regex matches `_PLAIN_NAME` in
|
|
230
|
+
# core/diagnostics.py.
|
|
231
|
+
_READABLE_SCHEMAS_SQL = """
|
|
232
|
+
SELECT n.nspname
|
|
233
|
+
FROM pg_class c
|
|
234
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
235
|
+
WHERE c.relkind = 'r'
|
|
236
|
+
AND n.nspname <> 'information_schema'
|
|
237
|
+
AND n.nspname NOT LIKE 'pg\\_%'
|
|
238
|
+
AND has_schema_privilege(n.oid, 'USAGE')
|
|
239
|
+
AND has_table_privilege(c.oid, 'SELECT')
|
|
240
|
+
GROUP BY n.nspname
|
|
241
|
+
ORDER BY (n.nspname !~ '^[A-Za-z_][A-Za-z0-9_$-]{0,62}$'), n.nspname
|
|
242
|
+
LIMIT 51
|
|
243
|
+
"""
|
|
244
|
+
|
|
216
245
|
|
|
217
246
|
class Database:
|
|
218
247
|
"""Async connection pool, always connected as the read-only role."""
|
|
@@ -350,6 +379,20 @@ class Database:
|
|
|
350
379
|
),
|
|
351
380
|
)
|
|
352
381
|
|
|
382
|
+
async def readable_schemas(self) -> list[str] | None:
|
|
383
|
+
"""Up to 51 schemas that hold a table this role can read, system schemas excluded.
|
|
384
|
+
|
|
385
|
+
Plain names come first. None when the look-up fails (a timeout, a dropped connection):
|
|
386
|
+
it only decorates an error message, so it must not replace that message.
|
|
387
|
+
"""
|
|
388
|
+
try:
|
|
389
|
+
async with self.pool.connection() as conn, conn.transaction(), conn.cursor() as cur:
|
|
390
|
+
await cur.execute(_READABLE_SCHEMAS_SQL)
|
|
391
|
+
return [row[0] for row in await cur.fetchall()]
|
|
392
|
+
except psycopg.Error as exc:
|
|
393
|
+
log.warning("Could not list the schemas this role can read: %s", type(exc).__name__)
|
|
394
|
+
return None
|
|
395
|
+
|
|
353
396
|
async def introspect(self) -> list[TableDetail]:
|
|
354
397
|
"""Read the schema once. Called at startup to build the retrieval index."""
|
|
355
398
|
params = {"schemas": list(self.settings.schemas)}
|