mcp-postgres 2.1.0

High-performance MCP server for PostgreSQL with CPU-aware connection pooling and optimized buffers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# mcp-postgres

High-performance MCP (Model Context Protocol) server for PostgreSQL, written in pure Rust with async/await.

## Overview

**mcp-postgres** brings your PostgreSQL database into Claude and other MCP-compatible AI tools. Execute queries, manage schema, monitor performance, and handle bulk operations—all through a clean JSON-RPC interface.

- **46 PostgreSQL tools** — query execution, schema inspection, DDL operations, batch operations, monitoring, maintenance, replication, and more
- **Dual-protocol transport** — TCP (port 3000) and HTTP/2 (port 3001) for flexibility
- **Sub-10ms latency** — optimized for interactive AI workflows
- **Production-grade** — connection pooling, health checks, input validation, SQL injection prevention
- **Stateless HTTP** — each request is independent (no transaction state across requests)
- **Claude Desktop ready** — works with stdio transport for seamless integration

## Quick Start

### Installation

**See [INSTALLATION.md](./guides/INSTALLATION.md) for complete instructions** covering:
- crates.io, source build, and Homebrew installation
- Configuration and verification
- Claude Desktop setup
- Troubleshooting

Quick start:
```bash
# From crates.io (easiest)
cargo install mcp-postgres

# Or from Homebrew (macOS)
brew tap corporatepiyush/mcp-postgres
brew install mcp-postgres
```

### Run

```bash
# TCP server (default, port 3000)
mcp-postgres --database-url "postgres://user:pass@localhost:5432/mydb"

# HTTP/2 server (port 3001)
mcp-postgres --database-url "postgres://user:pass@localhost:5432/mydb" --http-port 3001

# Stdio mode for Claude Desktop
mcp-postgres --database-url "postgres://user:pass@localhost:5432/mydb" --stdio

# Restricted (read-only) mode
mcp-postgres --database-url "postgres://user:pass@localhost:5432/mydb" --stdio --access-mode restricted
```

### Claude Desktop Configuration

Add to `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "postgres": {
      "command": "mcp-postgres",
      "args": [
        "--database-url",
        "postgres://user:pass@localhost:5432/mydb",
        "--stdio"
      ]
    }
  }
}
```

See [guides/INSTALLATION.md](./guides/INSTALLATION.md) for complete setup and troubleshooting.

## Command-Line Options

```
Usage: mcp-postgres [OPTIONS]

Options:
  -d, --database-url <URL>       PostgreSQL connection string
  -H, --host <HOST>              Server host (TCP) [default: 127.0.0.1]
  -p, --port <PORT>              TCP server port [default: 3000]
      --http-port <PORT>         HTTP/2 server port [default: 3001]
      --min-connections <N>      Min pool connections [default: 5]
      --max-connections <N>      Max pool connections [default: 20]
      --log-level <LEVEL>        Log level [default: info]
      --enable-metrics           Enable Prometheus /metrics endpoint
      --metrics-port <PORT>      Metrics port [default: 9090]
      --stdio                    Stdio mode (Claude Desktop)
      --access-mode <MODE>       unrestricted or restricted [default: unrestricted]
  -h, --help                     Print help
  -V, --version                  Print version
```

---

## Protocol & API

All tools follow the [MCP JSON-RPC 2.0](https://spec.modelcontextprotocol.io) specification.

### Request Format

```json
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "<tool_name>",
    "arguments": { ... }
  },
  "id": 1
}
```

### Response Format (Success)

```json
{
  "jsonrpc": "2.0",
  "result": { ... },
  "id": 1
}
```

### Response Format (Error)

```json
{
  "jsonrpc": "2.0",
  "error": {
    "code": -32602,
    "message": "Invalid params: Missing 'sql' parameter"
  },
  "id": 1
}
```

---

## Tools Reference (46 Total)

### Query Execution (6 tools)

**`execute_query`** — Execute SELECT and return rows

```json
{ "sql": "SELECT id, name FROM users LIMIT 10" }
→ { "rows": [[1, "Alice"], [2, "Bob"]] }
```

**`execute_insert`** — Execute INSERT and return rows affected

```json
{ "sql": "INSERT INTO users (name) VALUES ('Charlie')" }
→ { "rows_affected": 1 }
```

**`execute_update`** — Execute UPDATE and return rows affected

```json
{ "sql": "UPDATE users SET status='active' WHERE id=1" }
→ { "rows_affected": 1 }
```

**`execute_delete`** — Execute DELETE and return rows affected

```json
{ "sql": "DELETE FROM users WHERE id=1" }
→ { "rows_affected": 1 }
```

**`explain_query`** — Show query execution plan

```json
{
  "sql": "SELECT * FROM users WHERE id = 1",
  "analyze": true,
  "buffers": true,
  "format": "json"
}
→ { "plan": [...], "options": {...} }
```

**`async_*` variants** — High-performance versions with temporary sync commit disabled

```json
{ "sql": "INSERT INTO large_table VALUES (...)" }
// for async_execute_insert, async_execute_update, async_execute_delete
```

---

### Schema Inspection (8 tools)

**`list_tables`** — List all tables in database

```json
{}
→ { "tables": [{"schema": "public", "name": "users", "type": "BASE TABLE"}, ...] }
```

**`list_schemas`** — List all schemas

```json
{}
→ { "schemas": [{"name": "public", "owner": "postgres"}, ...] }
```

**`list_columns`** — List columns in a table

```json
{ "table": "users" }
→ { "columns": [{"name": "id", "type": "bigint", "nullable": "NO"}, ...] }
```

**`list_indexes`** — List all indexes

```json
{}
→ { "indexes": [{"schema": "public", "table": "users", "name": "users_pkey", ...}, ...] }
```

**`list_triggers`** — List triggers on a table

```json
{ "table": "users" }
→ { "triggers": [...] }
```

**`list_views`** — List all views

```json
{}
→ { "views": [{"schema": "public", "name": "active_users", ...}, ...] }
```

**`list_sequences`** — List all sequences

```json
{}
→ { "sequences": [{"schema": "public", "name": "users_id_seq", ...}, ...] }
```

**`describe_table`** — Get detailed table metadata

```json
{ "table": "users" }
→ { "columns": [...], "constraints": [...], "size": "256 MB", ... }
```

---

### DDL Operations (16 tools)

Create, modify, and drop database objects safely.

**`create_table`** — Create a new table

```json
{
  "table": "users",
  "columns": [
    "id SERIAL PRIMARY KEY",
    "name VARCHAR(255) NOT NULL",
    "email VARCHAR(255) UNIQUE"
  ]
}
```

**`drop_table`** — Drop a table

```json
{ "table": "users" }
```

**`create_view`** — Create a view

```json
{
  "view_name": "active_users",
  "query": "SELECT * FROM users WHERE status='active'"
}
```

**`drop_view`** — Drop a view

```json
{ "view_name": "active_users" }
```

**`alter_view`** — Rename a view

```json
{ "view_name": "active_users", "rename_to": "active_accounts" }
```

**`create_schema`** — Create a schema

```json
{ "schema_name": "analytics" }
```

**`drop_schema`** — Drop a schema

```json
{ "schema_name": "analytics" }
```

**`create_index`** — Create an index

```json
{
  "index_name": "idx_users_email",
  "table": "users",
  "columns": ["email"]
}
```

**`drop_index`** — Drop an index

```json
{ "index_name": "idx_users_email" }
```

**`alter_index`** — Rename an index

```json
{ "index_name": "idx_users_email", "rename_to": "idx_email" }
```

**`create_sequence`** — Create a sequence

```json
{ "sequence_name": "app_id_seq", "start": 1000, "increment": 1 }
```

**`drop_sequence`** — Drop a sequence

```json
{ "sequence_name": "app_id_seq" }
```

**`create_partition`** — Create table partition

```json
{
  "table": "orders",
  "partition_name": "orders_2024",
  "partition_type": "RANGE",
  "column": "created_at",
  "values": "FROM ('2024-01-01') TO ('2025-01-01')"
}
```

**`delete_table_partition`** — Drop a partition

```json
{ "partition_name": "orders_2024" }
```

**`list_partitions`** — List partitions on a table

```json
{ "table": "orders" }
→ { "partitions": [...] }
```

**`backup_table`** — Create a backup copy of a table

```json
{ "table": "users" }
→ Creates table: backup_users with all data
```

---

### Batch Operations (4 tools)

High-performance bulk DML. Max 1000 rows per request.

**`async_batch_insert`** — Insert multiple rows

```json
{
  "table": "users",
  "columns": ["name", "email"],
  "rows": [
    ["Alice", "alice@example.com"],
    ["Bob", "bob@example.com"]
  ],
  "returning": "id"
}
→ { "rows_affected": 2, "inserted_ids": [1, 2] }
```

**`async_batch_update`** — Update multiple rows with different conditions

```json
{
  "table": "users",
  "updates": { "status": "inactive" },
  "where_clauses": ["id = 1", "id = 2"]
}
→ { "rows_affected": 2 }
```

**`async_batch_delete`** — Delete multiple rows

```json
{
  "table": "users",
  "where_clauses": ["id = 1", "id = 2"],
  "returning": "id"
}
→ { "rows_affected": 2, "inserted_ids": [1, 2] }
```

**`async_batch_insert_copy`** — Bulk insert with configurable batch size

```json
{
  "table": "events",
  "columns": ["user_id", "event_type"],
  "rows": [[...5000 rows...]],
  "batch_size": 1000
}
→ { "rows_affected": 5000, "batches": 5 }
```

---

### Monitoring & Analysis (6 tools)

**`list_connections`** — Show active database connections

```json
{}
→ { "connections": [{"pid": 12345, "user": "postgres", "state": "active", ...}, ...] }
```

**`show_current_user`** — Show current user and database

```json
{}
→ { "user": "postgres", "database": "mydb", "version": "PostgreSQL 16" }
```

**`analyze_table`** — Update table statistics

```json
{ "table": "users" }
→ { "status": "success", "action": "ANALYZE", "table": "users" }
```

**`vacuum_table`** — Clean dead tuples and optimize

```json
{ "table": "users" }
→ { "status": "success", "action": "VACUUM", "table": "users" }
```

**`get_table_size`** — Get table size in bytes and human-readable format

```json
{ "table": "users" }
→ { "size": "256 MB", "size_bytes": 268435456 }
```

**`get_database_size`** — Get total database size

```json
{}
→ { "size": "2.5 GB", "size_bytes": 2684354560 }
```

---

### Connection & Security (4 tools)

**`show_running_queries`** — Show all non-idle queries

```json
{}
→ { "queries": [{"pid": 12345, "user": "postgres", "query": "SELECT ...", ...}, ...] }
```

**`list_users`** — List all database users

```json
{}
→ { "users": [{"username": "alice", "superuser": false, "canlogin": true}, ...] }
```

**`list_user_privileges`** — List privileges for a user

```json
{ "username": "alice" }
→ { "privileges": [{"schema": "public", "table": "users", "privilege": "SELECT"}, ...] }
```

**`show_session_info`** — Show current session details

```json
{}
→ { "current_user": "postgres", "current_database": "mydb", "client_address": "127.0.0.1", ... }
```

---

### Configuration (2 tools)

**`show_all_settings`** — List all PostgreSQL settings

```json
{}
→ { "settings": [{"name": "work_mem", "value": "4096", "unit": "kB", ...}, ...] }
```

**`get_setting`** — Get a specific setting

```json
{ "setting": "work_mem" }
→ { "name": "work_mem", "value": "4096", "unit": "kB", "description": "...", ... }
```

---

## Architecture

### Dual-Protocol Design

```
┌─────────────────┐         ┌─────────────────┐
│   TCP Client    │         │   HTTP Client   │
│  (port 3000)    │         │  (port 3001)    │
└────────┬────────┘         └────────┬────────┘
         │                           │
         └─────────────┬─────────────┘
              ┌────────┴────────┐
              │   JSON-RPC 2.0   │
              │  (MCP Protocol)  │
              └────────┬────────┘
          ┌────────────┴────────────┐
          │   Tool Dispatcher       │
          │   (46 tools)            │
          └────────────┬────────────┘
        ┌──────────────┴──────────────┐
        │   Connection Pool           │
        │   (deadpool-postgres)       │
        │   Min: 5, Max: 20           │
        └────────────┬────────────────┘
            ┌────────┴────────┐
            │  PostgreSQL DB  │
            └─────────────────┘
```

### Key Design Decisions

- **Stateless HTTP** — Each request is independent. Transaction tools (BEGIN, COMMIT, ROLLBACK) not available over HTTP.
- **Connection pooling** — Deadpool maintains 5-20 connections with health checks and idle timeouts.
- **Sub-10ms latency** — Optimized for interactive AI workflows. TCP: < 10ms, HTTP: < 10ms (> 20ms is unacceptable).
- **Input validation** — All parameters validated at system boundary:
  - SQL statements: max 10,000 characters
  - Identifiers: max 255 characters
  - Batch rows: max 1,000 per request
  - SQL injection prevention via identifier validation

---

## License

Apache-2.0

## Support

For issues, questions, or tool requests: [GitHub Issues](https://github.com/corporatepiyush/mcp-pg-rust/issues)