@marcelo-ochoa/server-postgres 1.0.1 → 1.0.2

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -229,4 +229,4 @@ As usual the code of this extension is at [GitHub](https://github.com/marcelo-oc
229
229
 
230
230
  ## License
231
231
 
232
- This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.
232
+ This MCP server is licensed under the [MIT License](https://github.com/marcelo-ochoa/servers/blob/main/src/postgres/LICENSE). This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.
package/dist/handlers.js CHANGED
@@ -1,4 +1,4 @@
1
- import { withConnection, getResourceBaseUrl } from "./db.js";
1
+ import { withConnection } from "./db.js";
2
2
  import { queryHandler } from "./tools/query.js";
3
3
  import { statsHandler } from "./tools/stats.js";
4
4
  import { connectHandler } from "./tools/connect.js";
@@ -20,14 +20,13 @@ export const callToolHandler = async (request) => {
20
20
  };
21
21
  const SCHEMA_PATH = "schema";
22
22
  export const listResourcesHandler = async (request) => {
23
- const resourceBaseUrl = getResourceBaseUrl();
24
23
  return await withConnection(async (client) => {
25
- const result = await client.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'");
24
+ const result = await client.query("SELECT table_name, table_schema FROM information_schema.tables WHERE table_schema NOT IN ('information_schema', 'pg_catalog')");
26
25
  return {
27
26
  resources: result.rows.map((row) => ({
28
- uri: new URL(`${row.table_name}/${SCHEMA_PATH}`, resourceBaseUrl).href,
27
+ uri: `postgres://${row.table_schema}/${row.table_name}/${SCHEMA_PATH}`,
29
28
  mimeType: "application/json",
30
- name: `"${row.table_name}" database schema`,
29
+ name: `"${row.table_schema}"."${row.table_name}" database schema`,
31
30
  })),
32
31
  };
33
32
  });
@@ -37,17 +36,52 @@ export const readResourceHandler = async (request) => {
37
36
  const pathComponents = resourceUrl.pathname.split("/");
38
37
  const schema = pathComponents.pop();
39
38
  const tableName = pathComponents.pop();
39
+ const schemaName = resourceUrl.hostname;
40
40
  if (schema !== SCHEMA_PATH) {
41
41
  throw new Error("Invalid resource URI");
42
42
  }
43
43
  return await withConnection(async (client) => {
44
- const result = await client.query("SELECT column_name, data_type FROM information_schema.columns WHERE table_name = $1", [tableName]);
44
+ const columnsResult = await client.query(`SELECT
45
+ column_name,
46
+ data_type,
47
+ is_nullable,
48
+ column_default,
49
+ character_maximum_length,
50
+ numeric_precision,
51
+ numeric_scale
52
+ FROM information_schema.columns
53
+ WHERE table_name = $1 AND table_schema = $2
54
+ ORDER BY ordinal_position`, [tableName, schemaName]);
55
+ const indexesResult = await client.query(`SELECT
56
+ ix.relname as index_name,
57
+ i.indisunique as is_unique,
58
+ a.attname as column_name
59
+ FROM
60
+ pg_class t,
61
+ pg_class ix,
62
+ pg_index i,
63
+ pg_attribute a,
64
+ pg_namespace n
65
+ WHERE
66
+ t.oid = i.indrelid
67
+ AND ix.oid = i.indexrelid
68
+ AND a.attrelid = t.oid
69
+ AND a.attnum = ANY(i.indkey)
70
+ AND t.relkind = 'r'
71
+ AND t.relname = $1
72
+ AND n.oid = t.relnamespace
73
+ AND n.nspname = $2
74
+ ORDER BY
75
+ ix.relname`, [tableName, schemaName]);
45
76
  return {
46
77
  contents: [
47
78
  {
48
79
  uri: request.params.uri,
49
80
  mimeType: "application/json",
50
- text: JSON.stringify(result.rows, null, 2),
81
+ text: JSON.stringify({
82
+ columns: columnsResult.rows,
83
+ indexes: indexesResult.rows
84
+ }, null, 2),
51
85
  },
52
86
  ],
53
87
  };
package/dist/server.js CHANGED
@@ -7,7 +7,7 @@ import { listResourcesHandler, readResourceHandler, callToolHandler } from "./ha
7
7
  import { tools } from "./tools.js";
8
8
  const server = new Server({
9
9
  name: "postgres-server",
10
- version: "1.0.1",
10
+ version: "1.0.2",
11
11
  }, {
12
12
  capabilities: {
13
13
  resources: {},
@@ -35,7 +35,7 @@ server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => {
35
35
  return {
36
36
  resourceTemplates: [
37
37
  {
38
- uriTemplate: "postgres://{database}/{table_name}/schema",
38
+ uriTemplate: "postgres://{schema}/{table_name}/schema",
39
39
  name: "Table Schema",
40
40
  description: "Schema information for a PostgreSQL database table including column names and data types",
41
41
  },
@@ -1,6 +1,12 @@
1
1
  import { withConnection } from "../db.js";
2
2
  export const statsHandler = async (request) => {
3
- const tableName = request.params.arguments?.name;
3
+ let schema = 'public';
4
+ let tableName = request.params.arguments?.name;
5
+ if (tableName.includes('.')) {
6
+ const parts = tableName.split('.');
7
+ schema = parts[0];
8
+ tableName = parts[1];
9
+ }
4
10
  return await withConnection(async (client) => {
5
11
  const result = await client.query(`
6
12
  SELECT json_build_object(
@@ -15,7 +21,7 @@ export const statsHandler = async (request) => {
15
21
  FROM pg_class c
16
22
  JOIN pg_namespace n ON n.oid = c.relnamespace
17
23
  LEFT JOIN pg_stat_user_tables s ON s.relid = c.oid
18
- WHERE c.relname = $1 AND n.nspname = 'public'
24
+ WHERE c.relname = $1 AND n.nspname = $2
19
25
  ),
20
26
  'index_stats', (
21
27
  SELECT json_agg(
@@ -30,7 +36,7 @@ export const statsHandler = async (request) => {
30
36
  JOIN pg_class c ON c.oid = i.indrelid
31
37
  JOIN pg_class c2 ON c2.oid = i.indexrelid
32
38
  JOIN pg_namespace n ON n.oid = c.relnamespace
33
- WHERE c.relname = $1 AND n.nspname = 'public'
39
+ WHERE c.relname = $1 AND n.nspname = $2
34
40
  ),
35
41
  'column_stats', (
36
42
  SELECT json_agg(
@@ -42,10 +48,10 @@ export const statsHandler = async (request) => {
42
48
  )
43
49
  )
44
50
  FROM pg_stats
45
- WHERE tablename = $1 AND schemaname = 'public'
51
+ WHERE tablename = $1 AND schemaname = $2
46
52
  )
47
53
  ) as stats_json
48
- `, [tableName]);
54
+ `, [tableName, schema]);
49
55
  return {
50
56
  content: [{ type: "text", text: JSON.stringify(result.rows[0].stats_json, null, 2), mimeType: "application/json" }],
51
57
  isError: false,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@marcelo-ochoa/server-postgres",
3
3
  "mcpName": "io.github.marcelo-ochoa/postgres",
4
- "version": "1.0.1",
4
+ "version": "1.0.2",
5
5
  "description": "MCP server for interacting with PostgreSQL databases",
6
6
  "keywords": [
7
7
  "read-only-mcp",