easy-pg-mcp 1.0.1 → 1.1.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/MANUAL.md ADDED
@@ -0,0 +1,120 @@
1
+ # easy-pg-mcp Manual
2
+
3
+ `easy-pg-mcp` 是一個用來操作 PostgreSQL 的 MCP server,提供查詢、寫入、批次執行、CSV 匯入,以及 schema / 權限檢查工具。
4
+
5
+ ## Overview
6
+
7
+ 適合用在:
8
+ - PostgreSQL 資料查詢
9
+ - 參數化寫入
10
+ - `RETURNING` 型流程
11
+ - 需要 CTE、複雜 join、聚合查詢的場景
12
+
13
+ 不適合用在:
14
+ - 多語句 SQL
15
+ - 直接字串拼接參數
16
+ - 過度依賴隱式型別轉換
17
+
18
+ ## When to Consult This Manual
19
+
20
+ 如果你遇到以下情況,先看這份手冊:
21
+ - 不確定 `pg_query`、`pg_execute`、`pg_batch_execute` 要怎麼用
22
+ - SQL 被拒絕或回傳錯誤
23
+ - 不確定 `$1, $2, ...` 參數要怎麼綁定
24
+ - 不確定哪些 SQL 寫法是安全且允許的
25
+ - 不確定應該先查 schema 還是直接執行
26
+
27
+ ## Modes
28
+
29
+ - `readonly`: 只提供讀取相關工具
30
+ - `readwrite`: 預設模式,允許一般讀寫,但不包含 DDL
31
+ - `advanced`: 允許 schema / DDL 類工具
32
+
33
+ ## Tools
34
+
35
+ - `pg_query`: 讀取資料
36
+ - `pg_execute`: 執行單一寫入或變更 SQL
37
+ - `pg_batch_execute`: 以多組參數批次執行同一段 SQL
38
+ - `pg_import_csv`: 匯入 UTF-8 CSV
39
+ - `pg_export_csv`: 匯出表格為 UTF-8 CSV
40
+ - `pg_schema_execute`: 執行 schema / DDL 變更,僅 advanced 模式可用
41
+ - `pg_list_pending_approvals`: 列出待審核命令
42
+ - `pg_run_approved_command`: 執行已核准命令
43
+ - `pg_cancel_approval`: 取消審核
44
+ - `explain_query`: 檢查 query plan
45
+ - `list_tables`, `list_views`, `describe_table`, `describe_index`, `list_triggers`
46
+ - `get_current_privileges`
47
+
48
+ ## Execute Usage
49
+
50
+ `pg_execute` 應使用 PostgreSQL placeholder:
51
+ - `"$1", "$2", "$3"...`
52
+
53
+ 規則:
54
+ - SQL 只描述結構
55
+ - 值由 `params` 陣列提供
56
+ - `$n` 與參數順序必須完全對應
57
+ - 不要用 `?`
58
+ - 不要把使用者輸入直接插入 SQL 字串
59
+
60
+ `pg_batch_execute` 適合:
61
+ - 同一個 SQL 搭配多組參數重複執行
62
+ - 大量資料寫入
63
+
64
+ 參數寫法補充:
65
+ - SQL 內使用 `$1`, `$2`, ... 代表參數位置
66
+ - `params` 陣列的第 1 個值會綁定到 `$1`
67
+ - `params` 陣列的第 2 個值會綁定到 `$2`
68
+ - 依此類推,順序必須完全一致
69
+
70
+ ## SQL Algebra / Composition Rules
71
+
72
+ PostgreSQL 查詢組裝可視為一個由上而下的代數式:
73
+
74
+ 1. 先定義中間結果:`WITH`
75
+ 2. 選擇欄位:`SELECT`
76
+ 3. 指定資料來源:`FROM`
77
+ 4. 關聯資料:`JOIN ... ON`
78
+ 5. 篩選資料:`WHERE`
79
+ 6. 聚合:`GROUP BY`
80
+ 7. 聚合後篩選:`HAVING`
81
+ 8. 排序:`ORDER BY`
82
+ 9. 限制筆數:`LIMIT` / `OFFSET`
83
+
84
+ PostgreSQL 常用特性:
85
+ - `RETURNING`:寫入後直接取回欄位
86
+ - `::type`:明確型別轉換
87
+ - CTE:提升複雜查詢可讀性
88
+ - 參數化日期區間查詢:搭配 `$1`, `$2`
89
+
90
+ 值與型別的規則:
91
+ - 值優先用參數
92
+ - 型別轉換要明確
93
+ - 時區與日期欄位要一致處理
94
+
95
+ ## Safety Rules
96
+
97
+ - 不支援 multi-statement
98
+ - 不要把使用者輸入直接嵌入 SQL
99
+ - schema execute 不支援參數化
100
+ - 某些 DDL 解析與授權限制需特別注意
101
+
102
+ ## Examples
103
+
104
+ - 參數化查詢:
105
+ - `SELECT * FROM users WHERE id = $1`
106
+ - 寫入後取回 id:
107
+ - `INSERT INTO users (name) VALUES ($1) RETURNING id`
108
+ - CTE:
109
+ - `WITH recent_orders AS (...) SELECT * FROM recent_orders`
110
+ - 聚合:
111
+ - `SELECT status, COUNT(*) FROM orders GROUP BY status`
112
+ - 日期區間查詢:
113
+ - `SELECT * FROM logs WHERE created_at >= $1 AND created_at < $2`
114
+
115
+ ## Troubleshooting
116
+
117
+ - 如果參數沒生效,先檢查 `$n` 與 `params` 順序
118
+ - 如果欄位型別不對,補上 `::type`
119
+ - 如果查詢太複雜,先改寫成 `WITH`
120
+ - 如果執行失敗,先用 `describe_table` 確認 schema
package/README.md CHANGED
@@ -17,7 +17,7 @@ This project uses Node.js, TypeScript, the official MCP SDK, and [`pg`](https://
17
17
 
18
18
  ## Requirements
19
19
 
20
- - Node.js 18 or newer
20
+ - Node.js 20 or newer
21
21
  - npm
22
22
  - A reachable PostgreSQL database
23
23
 
@@ -29,7 +29,7 @@ Run the server directly with `npx`:
29
29
  npx -y easy-pg-mcp
30
30
  ```
31
31
 
32
- For local development:
32
+ For local development after cloning the repository:
33
33
 
34
34
  ```bash
35
35
  cd easy-pg-mcp
@@ -39,6 +39,8 @@ npm run build
39
39
 
40
40
  ## Configuration
41
41
 
42
+ Configure the server with environment variables. You can provide them through your MCP client configuration or by creating a local `.env` file.
43
+
42
44
  | Variable | Required | Default | Description |
43
45
  | --- | --- | --- | --- |
44
46
  | `PG_CONNECTION_STRING` | No | - | PostgreSQL connection string. When set, host/user/password/database fields are optional |
@@ -73,9 +75,27 @@ PG_PASSWORD=your_password
73
75
  PG_DATABASE=your_database
74
76
  ```
75
77
 
76
- ## MCP Client Examples
78
+ ## Usage
79
+
80
+ Configure your MCP client to launch the package through `npx`.
81
+
82
+ For local development, build the TypeScript source first:
83
+
84
+ ```bash
85
+ npm run build
86
+ ```
87
+
88
+ Start the MCP server:
89
+
90
+ ```bash
91
+ npm start
92
+ ```
77
93
 
78
- Claude Desktop:
94
+ The server communicates over stdio and is normally launched by an MCP client rather than run manually.
95
+
96
+ If you are unsure how a tool should be used, or an operation fails, call `pg_manual` first. It returns the built-in manual with safe usage rules, placeholder guidance, and SQL composition notes.
97
+
98
+ ## Claude Desktop Example
79
99
 
80
100
  ```json
81
101
  {
@@ -95,7 +115,9 @@ Claude Desktop:
95
115
  }
96
116
  ```
97
117
 
98
- Codex `config.toml`:
118
+ Restart Claude Desktop after updating the configuration.
119
+
120
+ ## Codex config.toml Example
99
121
 
100
122
  ```toml
101
123
  [mcp_servers.easy-pg-mcp]
@@ -111,10 +133,33 @@ PG_PASSWORD = "YOUR PASSWORD"
111
133
  PG_DATABASE = "YOUR DB NAME"
112
134
  ```
113
135
 
136
+ ## OpenCode opencode.jsonc Example
137
+
138
+ ```jsonc
139
+ {
140
+ "$schema": "https://opencode.ai/config.json",
141
+ "mcp": {
142
+ "easy-pg-mcp": {
143
+ "type": "local",
144
+ "command": ["npx", "-y", "easy-pg-mcp"],
145
+ "enabled": true,
146
+ "environment": {
147
+ "PG_HOST": "localhost",
148
+ "PG_PORT": "5432",
149
+ "PG_USER": "YOUR USERNAME",
150
+ "PG_PASSWORD": "YOUR PASSWORD",
151
+ "PG_DATABASE": "YOUR DB NAME"
152
+ }
153
+ }
154
+ }
155
+ }
156
+ ```
157
+
114
158
  ## Available Tools
115
159
 
116
160
  | Tool | Description |
117
161
  | --- | --- |
162
+ | `pg_manual` | Return the built-in manual. Use this first when you are unsure how to use PostgreSQL tools or need help diagnosing an operation error |
118
163
  | `pg_query` | Execute a read-only SQL query, such as `SELECT` |
119
164
  | `pg_execute` | Execute a data modification statement, such as `INSERT`, `UPDATE`, or `DELETE` |
120
165
  | `pg_schema_execute` | Execute advanced schema statements, such as table, view, index, trigger, or function DDL. Only registered when `PG_MCP_MODE=advanced` |
package/README.zh-TW.md CHANGED
@@ -17,7 +17,7 @@
17
17
 
18
18
  ## 需求
19
19
 
20
- - Node.js 18 以上
20
+ - Node.js 20 以上
21
21
  - npm
22
22
  - 可連線的 PostgreSQL 資料庫
23
23
 
@@ -113,8 +113,11 @@ PG_DATABASE = "YOUR DB NAME"
113
113
 
114
114
  ## 可用工具
115
115
 
116
+ 如果你不確定某個工具該怎麼用,或操作失敗,請先呼叫 `pg_manual`。它會回傳內建手冊,包含安全使用規則、placeholder 指引,以及 SQL 組合方式。
117
+
116
118
  | 工具 | 說明 |
117
119
  | --- | --- |
120
+ | `pg_manual` | 回傳內建手冊。當你不確定如何使用 PostgreSQL 工具,或需要排查操作錯誤時,請先查這個工具 |
118
121
  | `pg_query` | 執行唯讀 SQL,例如 `SELECT` |
119
122
  | `pg_execute` | 執行資料修改 SQL,例如 `INSERT`、`UPDATE`、`DELETE` |
120
123
  | `pg_schema_execute` | 執行進階 schema SQL,例如 table、view、index、trigger、function DDL。僅在 `PG_MCP_MODE=advanced` 時註冊 |
package/build/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env node
2
+ import { readFileSync } from 'node:fs';
3
+ import { fileURLToPath } from 'node:url';
2
4
  import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3
5
  import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
4
6
  import { z } from 'zod';
@@ -13,8 +15,15 @@ const server = new McpServer({
13
15
  version: '1.0.1',
14
16
  description: `PostgreSQL Database: ${PG_HOST}:${PG_PORT ?? 5432}/${PG_DATABASE}`,
15
17
  });
18
+ const manualText = readFileSync(fileURLToPath(new URL('../MANUAL.md', import.meta.url)), 'utf8');
16
19
  // --- Register Tools ---
17
20
  const transactionModeSchema = z.enum(['all', 'batch', 'each', 'none']);
21
+ server.registerTool('pg_manual', {
22
+ description: 'Return the PostgreSQL MCP manual. Use this first when you are unsure how to use pg_query, pg_execute, pg_batch_execute, or when an operation fails and you need the safe usage rules, placeholder rules, or SQL composition guidance.',
23
+ inputSchema: z.object({}),
24
+ }, async () => ({
25
+ content: [{ type: 'text', text: manualText }],
26
+ }));
18
27
  server.registerTool('pg_query', {
19
28
  description: 'Execute a read-only SQL query (e.g., SELECT). Use this for data retrieval.',
20
29
  inputSchema: z.object({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-pg-mcp",
3
- "version": "1.0.1",
3
+ "version": "1.1.2",
4
4
  "description": "High performance PostgreSQL MCP Server using node-postgres",
5
5
  "main": "build/index.js",
6
6
  "type": "module",
@@ -9,10 +9,12 @@
9
9
  },
10
10
  "files": [
11
11
  "build",
12
- "README.md"
12
+ "README.md",
13
+ "README.zh-TW.md",
14
+ "MANUAL.md"
13
15
  ],
14
16
  "engines": {
15
- "node": ">=18"
17
+ "node": ">=20"
16
18
  },
17
19
  "keywords": [
18
20
  "mcp",
@@ -37,13 +39,13 @@
37
39
  "dependencies": {
38
40
  "@modelcontextprotocol/sdk": "^1.29.0",
39
41
  "node-sql-parser": "^5.4.0",
40
- "pg": "^8.21.0",
42
+ "pg": "^8.22.0",
41
43
  "zod": "^4.4.3"
42
44
  },
43
45
  "devDependencies": {
44
- "@types/node": "^25.6.0",
45
- "@types/pg": "^8.15.6",
46
- "typescript": "^6.0.3"
46
+ "@types/node": "^26.1.1",
47
+ "@types/pg": "^8.20.0",
48
+ "typescript": "^7.0.2"
47
49
  },
48
50
  "repository": {
49
51
  "type": "git",