postgres-schema-mcp 0.1.1__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.
Files changed (61) hide show
  1. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/CHANGELOG.md +11 -0
  2. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/PKG-INFO +4 -2
  3. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/README.md +2 -0
  4. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/docs/DECISIONS.md +28 -0
  5. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/pyproject.toml +2 -2
  6. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/server.json +2 -2
  7. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/adapters/db.py +9 -1
  8. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/diagnostics.py +32 -1
  9. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/server.py +28 -6
  10. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/unit/test_diagnostics.py +22 -1
  11. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/unit/test_server.py +108 -1
  12. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/uv.lock +15 -15
  13. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/.gitignore +0 -0
  14. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/.python-version +0 -0
  15. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/CONTRIBUTING.md +0 -0
  16. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/Dockerfile +0 -0
  17. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/LICENSE +0 -0
  18. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/Makefile +0 -0
  19. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/SECURITY.md +0 -0
  20. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/docker-compose.yml +0 -0
  21. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/docs/SPEC.md +0 -0
  22. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/docs/architecture.md +0 -0
  23. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/docs/demo.gif +0 -0
  24. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/docs/demo.tape +0 -0
  25. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/evals/baseline/conformance.json +0 -0
  26. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/evals/baseline/titan-bedrock.json +0 -0
  27. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/evals/capture_catalog.py +0 -0
  28. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/evals/check_live.py +0 -0
  29. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/evals/demo.py +0 -0
  30. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/evals/faults.yaml +0 -0
  31. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/evals/fixtures/catalog.json +0 -0
  32. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/evals/measure.py +0 -0
  33. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/evals/questions.yaml +0 -0
  34. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/evals/results/latest.json +0 -0
  35. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/init/10-seed.sh +0 -0
  36. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/init/20-grants.sql +0 -0
  37. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/scripts/banned-words.txt +0 -0
  38. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/scripts/check-readme-numbers.py +0 -0
  39. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/scripts/check-writing.sh +0 -0
  40. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/scripts/strip-ai-trailers.sh +0 -0
  41. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/__init__.py +0 -0
  42. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/adapters/__init__.py +0 -0
  43. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/adapters/backends.py +0 -0
  44. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/__init__.py +0 -0
  45. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/ddl.py +0 -0
  46. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/guard.py +0 -0
  47. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/retrieval.py +0 -0
  48. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/core/types.py +0 -0
  49. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/src/postgres_schema_mcp/py.typed +0 -0
  50. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/__init__.py +0 -0
  51. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/conftest.py +0 -0
  52. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/integration/__init__.py +0 -0
  53. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/integration/conftest.py +0 -0
  54. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/integration/test_database.py +0 -0
  55. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/integration/test_readonly_role.py +0 -0
  56. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/integration/test_server_live.py +0 -0
  57. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/unit/__init__.py +0 -0
  58. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/unit/test_config_documented.py +0 -0
  59. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/unit/test_db_helpers.py +0 -0
  60. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/unit/test_guard.py +0 -0
  61. {postgres_schema_mcp-0.1.1 → postgres_schema_mcp-0.1.2}/tests/unit/test_retrieval.py +0 -0
@@ -4,6 +4,17 @@ 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
+
7
18
  ## [0.1.1] - 2026-11-23
8
19
 
9
20
  ### Added
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: postgres-schema-mcp
3
- Version: 0.1.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.0.0
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
@@ -261,6 +261,8 @@ Every setting is an environment variable. The audit and a test in this repo fail
261
261
  |---|---|---|
262
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
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 |
264
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) |
265
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 |
266
268
  | Tables you added are missing from the results | The schema is read once at start-up | Restart the server |
@@ -236,6 +236,8 @@ Every setting is an environment variable. The audit and a test in this repo fail
236
236
  |---|---|---|
237
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
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 |
239
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) |
240
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 |
241
243
  | Tables you added are missing from the results | The schema is read once at start-up | Restart the server |
@@ -94,3 +94,31 @@ One entry per decision that changes architecture or scope. Newest first.
94
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
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
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.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.0.0",
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.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.1",
14
+ "version": "0.1.2",
15
15
  "transport": { "type": "stdio" }
16
16
  }
17
17
  ]
@@ -39,7 +39,15 @@ from ..core.types import ColumnInfo, ForeignKey, QueryPlan, QueryResult, TableDe
39
39
 
40
40
  log = logging.getLogger(__name__)
41
41
 
42
- __all__ = ["Database", "Settings"]
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)
43
51
 
44
52
  DEFAULT_DSN = "postgresql://scout_ro:scout_ro_pw@localhost:55432/adventureworks"
45
53
 
@@ -5,7 +5,12 @@ from __future__ import annotations
5
5
  import re
6
6
  from collections.abc import Collection, Sequence
7
7
 
8
- __all__ = ["empty_index_message", "missing_schemas_warning"]
8
+ __all__ = [
9
+ "catalog_failure_message",
10
+ "connect_failure_message",
11
+ "empty_index_message",
12
+ "missing_schemas_warning",
13
+ ]
9
14
 
10
15
  _EXAMPLE_SCHEMAS = 3
11
16
  _LISTED_SCHEMAS = 20
@@ -17,6 +22,32 @@ _LISTED_SCHEMAS = 20
17
22
  _PLAIN_NAME = re.compile(r"[A-Za-z_][A-Za-z0-9_$-]{0,62}")
18
23
 
19
24
 
25
+ # The two messages below take the exception's class name and nothing else from it. The text of
26
+ # a driver error can repeat part of the connection string, and libpq does that for a malformed
27
+ # one, which holds the password. The class name is a hint, not a diagnosis: a refused connection,
28
+ # a wrong password and a malformed DSN all come out of the pool as PoolTimeout. The driver's own
29
+ # message reaches the server's log through psycopg_pool, never through the tool.
30
+
31
+
32
+ def connect_failure_message(error_name: str) -> str:
33
+ """The database could not be reached, so nothing was run. What to check."""
34
+ return (
35
+ f"Could not connect to the database ({error_name}). Check that PGSS_DSN points at a "
36
+ "PostgreSQL server that is running and reachable from the machine this server runs on, "
37
+ "and that the role in it can log in. Nothing was run. The next call tries again, and "
38
+ "the server's log has the driver's own message."
39
+ )
40
+
41
+
42
+ def catalog_failure_message(error_name: str) -> str:
43
+ """The connection worked, or dropped, while the schema was being read."""
44
+ return (
45
+ f"Could not read the schema from the database ({error_name}). Check that the database "
46
+ "is still running, that the role in PGSS_DSN can read the catalog, and that "
47
+ "PGSS_SCHEMAS names schemas it can use. The next call tries again."
48
+ )
49
+
50
+
20
51
  def empty_index_message(configured: Sequence[str], readable: Sequence[str] | None) -> str:
21
52
  """Why the index is empty, and what to set.
22
53
 
@@ -35,8 +35,13 @@ from mcp.types import ToolAnnotations
35
35
  # fails with "attempted relative import with no known parent package". The
36
36
  # package is installed, so absolute imports resolve under both loaders.
37
37
  from postgres_schema_mcp.adapters.backends import make_index
38
- from postgres_schema_mcp.adapters.db import Database
39
- from postgres_schema_mcp.core.diagnostics import empty_index_message, missing_schemas_warning
38
+ from postgres_schema_mcp.adapters.db import DATABASE_ERRORS, Database
39
+ from postgres_schema_mcp.core.diagnostics import (
40
+ catalog_failure_message,
41
+ connect_failure_message,
42
+ empty_index_message,
43
+ missing_schemas_warning,
44
+ )
40
45
  from postgres_schema_mcp.core.guard import BlockedQueryError, check_read_only
41
46
  from postgres_schema_mcp.core.retrieval import SchemaIndex, count_tokens, tokenizer_name
42
47
  from postgres_schema_mcp.core.types import (
@@ -84,7 +89,7 @@ READ_ONLY = ToolAnnotations(
84
89
 
85
90
  mcp = MCPServer(
86
91
  "postgres-schema-mcp",
87
- version="0.1.1",
92
+ version="0.1.2",
88
93
  instructions=INSTRUCTIONS,
89
94
  # Honest hints. The tool list is derived from this server's own code, so it
90
95
  # is stable for the life of the process and identical for every caller:
@@ -104,13 +109,25 @@ _lock = asyncio.Lock()
104
109
 
105
110
 
106
111
  async def get_db() -> Database:
107
- """Open the pool on first use and reuse it thereafter."""
112
+ """Open the pool on first use and reuse it thereafter.
113
+
114
+ A database that cannot be reached is a tool error that names PGSS_DSN. Left as it was
115
+ raised, the exception is not a ToolError, and a client on mcp 2.3.0 shows the model only
116
+ "Error executing tool X", with no hint to check the connection. Only the exception's class
117
+ name is repeated, never its text (see core/diagnostics.py). Nothing is kept on failure, so
118
+ the next call connects again.
119
+ """
108
120
  global _db
109
121
  if _db is None:
110
122
  async with _lock:
111
123
  if _db is None:
112
124
  db = Database()
113
- await db.open()
125
+ try:
126
+ await db.open()
127
+ except DATABASE_ERRORS as exc:
128
+ name = type(exc).__name__
129
+ log.warning("Could not connect to the database: %s", name)
130
+ raise ToolError(connect_failure_message(name)) from None
114
131
  _db = db
115
132
  return _db
116
133
 
@@ -129,7 +146,12 @@ async def get_index() -> SchemaIndex:
129
146
  db = await get_db()
130
147
  async with _lock:
131
148
  if _index is None:
132
- tables = await db.introspect()
149
+ try:
150
+ tables = await db.introspect()
151
+ except DATABASE_ERRORS as exc:
152
+ name = type(exc).__name__
153
+ log.warning("Could not read the schema from the database: %s", name)
154
+ raise ToolError(catalog_failure_message(name)) from None
133
155
  if not tables:
134
156
  raise ToolError(
135
157
  empty_index_message(db.settings.schemas, await db.readable_schemas())
@@ -2,7 +2,28 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from postgres_schema_mcp.core.diagnostics import empty_index_message, missing_schemas_warning
5
+ from postgres_schema_mcp.core.diagnostics import (
6
+ catalog_failure_message,
7
+ connect_failure_message,
8
+ empty_index_message,
9
+ missing_schemas_warning,
10
+ )
11
+
12
+
13
+ def test_a_connection_failure_names_the_setting_and_the_exception_class() -> None:
14
+ message = connect_failure_message("PoolTimeout")
15
+ assert "Could not connect to the database (PoolTimeout)" in message
16
+ assert "PGSS_DSN" in message
17
+ assert "reachable" in message
18
+ assert "again" in message
19
+
20
+
21
+ def test_a_catalog_failure_says_the_connection_may_have_worked() -> None:
22
+ message = catalog_failure_message("OperationalError")
23
+ assert "Could not read the schema from the database (OperationalError)" in message
24
+ assert "PGSS_DSN" in message
25
+ assert "PGSS_SCHEMAS" in message
26
+ assert "again" in message
6
27
 
7
28
 
8
29
  def test_empty_index_names_the_configured_schemas_and_a_value_that_would_work() -> None:
@@ -8,14 +8,18 @@ The same tools run against a live database in tests/integration/test_server_live
8
8
  from __future__ import annotations
9
9
 
10
10
  import asyncio
11
+ import socket
11
12
  import sys
13
+ from collections.abc import Awaitable, Callable
12
14
  from typing import Any
13
15
 
16
+ import psycopg
17
+ import psycopg_pool
14
18
  import pytest
15
19
  from mcp.server.mcpserver.exceptions import ToolError
16
20
 
17
21
  from postgres_schema_mcp import server
18
- from postgres_schema_mcp.adapters.db import DEFAULT_SCHEMAS, Settings
22
+ from postgres_schema_mcp.adapters.db import DATABASE_ERRORS, DEFAULT_SCHEMAS, Settings
19
23
  from postgres_schema_mcp.core.retrieval import SchemaIndex
20
24
  from postgres_schema_mcp.core.types import QueryPlan, QueryResult, TableDetail
21
25
 
@@ -31,13 +35,19 @@ class FakeDatabase:
31
35
  self.introspect_calls = 0
32
36
  self.statements: list[str] = []
33
37
  self.error: Exception | None = None
38
+ self.open_error: Exception | None = None
39
+ self.introspect_error: Exception | None = None
34
40
 
35
41
  async def open(self) -> None:
36
42
  self.open_calls += 1
37
43
  await asyncio.sleep(0) # yield, so concurrent callers really do interleave
44
+ if self.open_error:
45
+ raise self.open_error
38
46
 
39
47
  async def introspect(self) -> list[TableDetail]:
40
48
  self.introspect_calls += 1
49
+ if self.introspect_error:
50
+ raise self.introspect_error
41
51
  return self.tables
42
52
 
43
53
  async def readable_schemas(self) -> list[str] | None:
@@ -271,6 +281,103 @@ async def test_a_misspelt_schema_is_warned_about_and_a_good_list_is_not(
271
281
  assert "person" not in caplog.text
272
282
 
273
283
 
284
+ # --- a database that cannot be reached is an error the model can read and act on ---
285
+ #
286
+ # mcp 2.3.0 sends the model only "Error executing tool X" for an exception that is not a
287
+ # ToolError, so a bare PoolTimeout used to arrive as no reason at all.
288
+
289
+ TOOL_CALLS: dict[str, Callable[[], Awaitable[object]]] = {
290
+ "search_schema": lambda: server.search_schema("which customers placed orders"),
291
+ "describe_table": lambda: server.describe_table("sales.salesorderheader"),
292
+ "explain_query": lambda: server.explain_query("SELECT 1"),
293
+ "run_query": lambda: server.run_query("SELECT 1"),
294
+ }
295
+
296
+
297
+ @pytest.mark.parametrize("tool", TOOL_CALLS)
298
+ async def test_an_unreachable_database_is_a_tool_error_that_names_the_fix(
299
+ cold_server: FakeDatabase, tool: str
300
+ ) -> None:
301
+ cold_server.open_error = psycopg_pool.PoolTimeout("pool initialization incomplete")
302
+ with pytest.raises(ToolError) as caught:
303
+ await TOOL_CALLS[tool]()
304
+ assert "Could not connect to the database" in str(caught.value)
305
+ assert "PGSS_DSN" in str(caught.value)
306
+ assert cold_server.statements == []
307
+
308
+
309
+ async def test_the_error_names_the_exception_class_and_not_its_text(
310
+ cold_server: FakeDatabase, caplog: pytest.LogCaptureFixture
311
+ ) -> None:
312
+ """libpq repeats part of a malformed connection string in its error, and that string
313
+ holds the password. Neither the message nor the log may carry the exception's text."""
314
+ cold_server.open_error = psycopg.ProgrammingError(
315
+ 'missing "=" after "postgresql://scout:hunter2@db.internal" in connection info string'
316
+ )
317
+ with (
318
+ caplog.at_level("WARNING", logger="postgres_schema_mcp.server"),
319
+ pytest.raises(ToolError) as caught,
320
+ ):
321
+ await server.search_schema("anything")
322
+ assert "ProgrammingError" in str(caught.value)
323
+ assert caught.value.__cause__ is None
324
+ for leaked in ("hunter2", "db.internal"):
325
+ assert leaked not in str(caught.value)
326
+ assert leaked not in caplog.text
327
+ assert "ProgrammingError" in caplog.text
328
+
329
+
330
+ async def test_a_failed_connection_is_not_remembered(cold_server: FakeDatabase) -> None:
331
+ cold_server.open_error = psycopg_pool.PoolTimeout("pool initialization incomplete")
332
+ for _ in range(2):
333
+ with pytest.raises(ToolError):
334
+ await server.get_db()
335
+ assert server._db is None
336
+ assert cold_server.open_calls == 2
337
+ cold_server.open_error = None
338
+ await server.get_db()
339
+ assert cold_server.open_calls == 3
340
+ assert (await server.search_schema("which customers placed orders")).tables
341
+
342
+
343
+ async def test_a_failed_schema_read_is_a_tool_error_and_is_not_remembered(
344
+ cold_server: FakeDatabase,
345
+ ) -> None:
346
+ cold_server.introspect_error = psycopg.OperationalError("server closed the connection")
347
+ with pytest.raises(ToolError, match=r"Could not read the schema.*OperationalError") as caught:
348
+ await server.search_schema("which customers placed orders")
349
+ assert "PGSS_DSN" in str(caught.value)
350
+ assert "closed the connection" not in str(caught.value)
351
+ cold_server.introspect_error = None
352
+ assert len((await server.get_index()).tables) == 68
353
+ assert cold_server.introspect_calls == 2
354
+
355
+
356
+ async def test_a_bug_in_this_server_is_not_dressed_up_as_a_database_problem(
357
+ cold_server: FakeDatabase,
358
+ ) -> None:
359
+ cold_server.introspect_error = RuntimeError("index out of range")
360
+ with pytest.raises(RuntimeError, match="index out of range"):
361
+ await server.search_schema("anything")
362
+
363
+
364
+ @pytest.mark.parametrize(
365
+ "error",
366
+ [
367
+ psycopg_pool.PoolTimeout("t"),
368
+ psycopg_pool.PoolClosed("c"),
369
+ psycopg.OperationalError("down"),
370
+ psycopg.ProgrammingError("bad dsn"),
371
+ ConnectionRefusedError(),
372
+ socket.gaierror("no such host"),
373
+ TimeoutError(),
374
+ ],
375
+ ids=lambda e: type(e).__name__,
376
+ )
377
+ def test_what_counts_as_a_database_problem(error: Exception) -> None:
378
+ assert isinstance(error, DATABASE_ERRORS)
379
+
380
+
274
381
  def test_the_instructions_do_not_claim_the_demo_database() -> None:
275
382
  assert "AdventureWorks" not in server.INSTRUCTIONS
276
383
 
@@ -1,5 +1,5 @@
1
1
  version = 1
2
- revision = 3
2
+ revision = 5
3
3
  requires-python = ">=3.12"
4
4
  resolution-markers = [
5
5
  "python_full_version >= '3.15'",
@@ -104,16 +104,16 @@ wheels = [
104
104
 
105
105
  [[package]]
106
106
  name = "boto3"
107
- version = "1.44.6"
107
+ version = "1.44.9"
108
108
  source = { registry = "https://pypi.org/simple" }
109
109
  dependencies = [
110
110
  { name = "botocore" },
111
111
  { name = "jmespath" },
112
112
  { name = "s3transfer" },
113
113
  ]
114
- sdist = { url = "https://files.pythonhosted.org/packages/b1/b9/62397b5ebab952bdb583f12ded9302b849297e9f2f0b1696c3e4bca4a315/boto3-1.44.6.tar.gz", hash = "sha256:af75aa81455f1c4dbbdd9bbd14c6ee3e66fb827430d1615014d4c45878e79c49", size = 112750, upload-time = "2026-11-18T20:45:39.072Z" }
114
+ sdist = { url = "https://files.pythonhosted.org/packages/24/43/993eec3594eab145ec751af0ef1dadbad0beb01b304b99d7ed0b68c31a71/boto3-1.44.9.tar.gz", hash = "sha256:da4199076833547445a7181e38e833f476808142df95084b2b46c8b2a107aa66", size = 112771, upload-time = "2026-11-21T01:33:36.737Z" }
115
115
  wheels = [
116
- { url = "https://files.pythonhosted.org/packages/49/59/4aece84bc2d600f70e5b8a8bc409d028b60e38586d2dd5c0eac00cdf10e5/boto3-1.44.6-py3-none-any.whl", hash = "sha256:7d5f43c824bdfcf203fd7c115cfb7bd4283f63caac01c731d8a667f8da096d52", size = 140158, upload-time = "2026-11-18T20:45:37.444Z" },
116
+ { url = "https://files.pythonhosted.org/packages/9e/66/73c8c57119a8acf3688dfbe95c4648e8b3e4d6a7a3783b0619b65d201f02/boto3-1.44.9-py3-none-any.whl", hash = "sha256:aff889e8ca6f6006f0b5658b712941471d21e4675d6d48421974a20a5a17a918", size = 140157, upload-time = "2026-11-21T01:33:34.542Z" },
117
117
  ]
118
118
 
119
119
  [[package]]
@@ -136,16 +136,16 @@ bedrock-runtime = [
136
136
 
137
137
  [[package]]
138
138
  name = "botocore"
139
- version = "1.44.6"
139
+ version = "1.44.9"
140
140
  source = { registry = "https://pypi.org/simple" }
141
141
  dependencies = [
142
142
  { name = "jmespath" },
143
143
  { name = "python-dateutil" },
144
144
  { name = "urllib3" },
145
145
  ]
146
- sdist = { url = "https://files.pythonhosted.org/packages/65/c4/b6e931509ec545017684f80faebd77dac0cb632dffad6368b625761760c9/botocore-1.44.6.tar.gz", hash = "sha256:9deb4d9479d63aabf8fbd9169800821c1f47be77352d8b9b3d020de9a824794a", size = 16887706, upload-time = "2026-11-18T20:45:34.183Z" }
146
+ sdist = { url = "https://files.pythonhosted.org/packages/04/10/cf5cb95dee4b956eb785e5225aa47054872bbcf263241e926fa796c388ce/botocore-1.44.9.tar.gz", hash = "sha256:1f783e0b31f0053f41f4239b7a7e2e73957f25cdc6b2f45d113f1b575a4db10b", size = 17206061, upload-time = "2026-11-21T01:33:31.469Z" }
147
147
  wheels = [
148
- { url = "https://files.pythonhosted.org/packages/3a/6a/fee453894524071664742111b265604b33eb2aaac7efc5cfc4602e3826fa/botocore-1.44.6-py3-none-any.whl", hash = "sha256:757c7f33d006e8a3c873e1fed71611baaaa2ee4d72e6f830ec0423d02ec5de2d", size = 16582320, upload-time = "2026-11-18T20:45:31.014Z" },
148
+ { url = "https://files.pythonhosted.org/packages/20/ad/f8143d7a78503c3b67d135589cd1da82d3867ed1f37149e64d6f9bef6cfb/botocore-1.44.9-py3-none-any.whl", hash = "sha256:6966752d38514a8e457ea062ea171b2b320c55bad23c8ae4f74f88296e00890d", size = 16905925, upload-time = "2026-11-21T01:33:28.733Z" },
149
149
  ]
150
150
 
151
151
  [[package]]
@@ -818,7 +818,7 @@ wheels = [
818
818
 
819
819
  [[package]]
820
820
  name = "mcp"
821
- version = "2.0.0"
821
+ version = "2.3.0"
822
822
  source = { registry = "https://pypi.org/simple" }
823
823
  dependencies = [
824
824
  { name = "anyio" },
@@ -836,22 +836,22 @@ dependencies = [
836
836
  { name = "typing-inspection" },
837
837
  { name = "uvicorn", marker = "sys_platform != 'emscripten'" },
838
838
  ]
839
- sdist = { url = "https://files.pythonhosted.org/packages/74/33/32d4dff2c95bb5d897c3ef4c83649a08996b17b58f0a326d2495d4c81179/mcp-2.0.0.tar.gz", hash = "sha256:0f440e735c13ece8bb19bc62cf0b86f4313448432fbb77d35e14034f4e050728", size = 1662284, upload-time = "2026-07-28T13:45:32.346Z" }
839
+ sdist = { url = "https://files.pythonhosted.org/packages/9d/8d/e0d339616f4810e9051d4aba6887afab289ab1f81875fe908b606cdfd0e3/mcp-2.3.0.tar.gz", hash = "sha256:8b147a50441cf059dc88c684e0aeed3687f0aa0f39c6cde7b90330effd2b34d8", size = 4171955, upload-time = "2026-10-02T22:06:56.091Z" }
840
840
  wheels = [
841
- { url = "https://files.pythonhosted.org/packages/67/72/7d7897418912c1d12e87556630dfb7bf0eac71160e9bef8b447960804ee3/mcp-2.0.0-py3-none-any.whl", hash = "sha256:1cb4c75d2d2c7b8c1d756355e5d82a39f2822cc7f13e22a2051d7ca3592349d6", size = 349980, upload-time = "2026-07-28T13:45:28.853Z" },
841
+ { url = "https://files.pythonhosted.org/packages/20/d7/4a2878f128df729db3ca04e71bfce26405cc2e0c9ebe19c592feeb4c61b9/mcp-2.3.0-py3-none-any.whl", hash = "sha256:dd0c44c089d16453e8ae31a3877a0054d7a2314caaa81f5e0541b9b1734b2377", size = 368664, upload-time = "2026-10-02T22:06:52.119Z" },
842
842
  ]
843
843
 
844
844
  [[package]]
845
845
  name = "mcp-types"
846
- version = "2.0.0"
846
+ version = "2.3.0"
847
847
  source = { registry = "https://pypi.org/simple" }
848
848
  dependencies = [
849
849
  { name = "pydantic" },
850
850
  { name = "typing-extensions" },
851
851
  ]
852
- sdist = { url = "https://files.pythonhosted.org/packages/bb/56/9b8e1c152f61f6c6b07c4b5896c88c7d0ae90bac6ee6306f852fcc5c1eb0/mcp_types-2.0.0.tar.gz", hash = "sha256:d7d939b9285c9961ae8866ba75ef85da34d12bafe276efbf4eb6a131786d8379", size = 66632, upload-time = "2026-07-28T13:45:33.804Z" }
852
+ sdist = { url = "https://files.pythonhosted.org/packages/9e/2d/7c251e34207f6c51000312fc8839111ac45cfe02023f90b44e7f1051dd8e/mcp_types-2.3.0.tar.gz", hash = "sha256:d1e46549edb35ee19a94940fcee6d1addd7e589ab7ea92dda83f5d84781fc362", size = 65898, upload-time = "2026-10-02T22:06:57.863Z" }
853
853
  wheels = [
854
- { url = "https://files.pythonhosted.org/packages/f5/4c/c78d78c3d52b0ac594ad7cc8ef5972adfe070e3597a8a4c6ce0cd39196ea/mcp_types-2.0.0-py3-none-any.whl", hash = "sha256:6b2de797ca2797f568b79529e1b25948e34de511bcc0bd82fef1039a6d1b8eb0", size = 69649, upload-time = "2026-07-28T13:45:30.713Z" },
854
+ { url = "https://files.pythonhosted.org/packages/ce/48/32583066b155bb5d4507e7887dc88ee23118cc4de8e2a12870c49719e2a8/mcp_types-2.3.0-py3-none-any.whl", hash = "sha256:968efdbdaedfab06adae40d378a34395f1090c5921d4be3c9cde283aaf76d91d", size = 69108, upload-time = "2026-10-02T22:06:54.099Z" },
855
855
  ]
856
856
 
857
857
  [[package]]
@@ -1058,7 +1058,7 @@ wheels = [
1058
1058
 
1059
1059
  [[package]]
1060
1060
  name = "postgres-schema-mcp"
1061
- version = "0.1.1"
1061
+ version = "0.1.2"
1062
1062
  source = { editable = "." }
1063
1063
  dependencies = [
1064
1064
  { name = "mcp" },
@@ -1089,7 +1089,7 @@ dev = [
1089
1089
  [package.metadata]
1090
1090
  requires-dist = [
1091
1091
  { name = "boto3", marker = "extra == 'bedrock'", specifier = ">=1.35" },
1092
- { name = "mcp", specifier = "==2.0.0" },
1092
+ { name = "mcp", specifier = "==2.3.0" },
1093
1093
  { name = "psycopg", extras = ["binary", "pool"], specifier = ">=3.2,<4" },
1094
1094
  { name = "pyyaml", specifier = ">=6.0,<7" },
1095
1095
  { name = "rank-bm25", specifier = ">=0.2.2,<0.3" },