@marcelo-ochoa/server-postgres 1.0.0 → 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 +21 -0
- package/README.md +3 -3
- package/dist/handlers.js +41 -7
- package/dist/server.js +20 -2
- package/dist/tools/stats.js +11 -5
- package/package.json +2 -2
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
|
@@ -40,7 +40,7 @@ The server provides schema information for each table in the database:
|
|
|
40
40
|
|
|
41
41
|
## Change Log
|
|
42
42
|
|
|
43
|
-
See [Change Log](CHANGELOG.md) for the history of changes.
|
|
43
|
+
See [Change Log](https://github.com/marcelo-ochoa/servers/blob/main/src/postgres/CHANGELOG.md) for the history of changes.
|
|
44
44
|
|
|
45
45
|
## Configuration
|
|
46
46
|
|
|
@@ -213,7 +213,7 @@ Optionally, you can add it to a file called `.vscode/mcp.json` in your workspace
|
|
|
213
213
|
|
|
214
214
|
## PostgreSQL AWR in action
|
|
215
215
|
|
|
216
|
-
See [PostgreSQL AWR in action](AWR_example.md) for an example of a performance report generated by the `pg-awr` tool for a production Moodle database, highlighting critical performance issues and optimization opportunities.
|
|
216
|
+
See [PostgreSQL AWR in action](https://github.com/marcelo-ochoa/servers/blob/main/src/postgres/AWR_example.md) for an example of a performance report generated by the `pg-awr` tool for a production Moodle database, highlighting critical performance issues and optimization opportunities.
|
|
217
217
|
|
|
218
218
|
## Building
|
|
219
219
|
|
|
@@ -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
|
|
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
|
|
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:
|
|
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
|
|
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(
|
|
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
|
@@ -1,23 +1,41 @@
|
|
|
1
1
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
2
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
3
|
import { CallToolRequestSchema, ListResourcesRequestSchema, ListResourceTemplatesRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
4
|
+
import { z } from "zod";
|
|
4
5
|
import { initializePool } from "./db.js";
|
|
5
6
|
import { listResourcesHandler, readResourceHandler, callToolHandler } from "./handlers.js";
|
|
6
7
|
import { tools } from "./tools.js";
|
|
7
8
|
const server = new Server({
|
|
8
9
|
name: "postgres-server",
|
|
9
|
-
version: "1.0.
|
|
10
|
+
version: "1.0.2",
|
|
10
11
|
}, {
|
|
11
12
|
capabilities: {
|
|
12
13
|
resources: {},
|
|
13
14
|
tools: {},
|
|
15
|
+
prompts: {}, // Add this line to indicate support for prompts
|
|
14
16
|
},
|
|
15
17
|
});
|
|
18
|
+
const prompts = [
|
|
19
|
+
{ id: 1, text: "pg-query select * from test" },
|
|
20
|
+
{ id: 2, text: "pg-explain select * from test" },
|
|
21
|
+
{ id: 3, text: "pg-stats test" },
|
|
22
|
+
{ id: 4, text: "pg-connect to PostgreSQL using a connection string like host.docker.internal:5432/dbname with user name and password" },
|
|
23
|
+
{ id: 5, text: "pg-awr to generate a PostgreSQL performance report (requires pg_stat_statements extension)" }
|
|
24
|
+
];
|
|
25
|
+
const PromptsListRequestSchema = z.object({
|
|
26
|
+
method: z.literal("prompts/list"),
|
|
27
|
+
params: z.object({}),
|
|
28
|
+
});
|
|
29
|
+
server.setRequestHandler(PromptsListRequestSchema, async () => {
|
|
30
|
+
return {
|
|
31
|
+
prompts,
|
|
32
|
+
};
|
|
33
|
+
});
|
|
16
34
|
server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => {
|
|
17
35
|
return {
|
|
18
36
|
resourceTemplates: [
|
|
19
37
|
{
|
|
20
|
-
uriTemplate: "postgres://{
|
|
38
|
+
uriTemplate: "postgres://{schema}/{table_name}/schema",
|
|
21
39
|
name: "Table Schema",
|
|
22
40
|
description: "Schema information for a PostgreSQL database table including column names and data types",
|
|
23
41
|
},
|
package/dist/tools/stats.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { withConnection } from "../db.js";
|
|
2
2
|
export const statsHandler = async (request) => {
|
|
3
|
-
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"description": "MCP server for interacting with PostgreSQL databases",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"read-only-mcp",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"watch": "tsc --watch"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@modelcontextprotocol/sdk": "1.
|
|
30
|
+
"@modelcontextprotocol/sdk": "^1.24.2",
|
|
31
31
|
"@toon-format/toon": "^1.0.0",
|
|
32
32
|
"pg": "^8.13.0"
|
|
33
33
|
},
|