mcp-postgres 2.0.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
/// Input validation for tool parameters
/// Validates tool arguments against defined schemas and provides helpful error messages
use serde_json::Value;

#[derive(Debug, Clone)]
pub struct ValidationError {
    pub tool: String,
    pub param: String,
    pub error: String,
    pub suggestion: String,
}

impl std::fmt::Display for ValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "❌ Validation Error in tool '{}' parameter '{}': {}\n💡 Suggestion: {}",
            self.tool, self.param, self.error, self.suggestion
        )
    }
}

pub fn validate_tool_input(tool_name: &str, arguments: &Value) -> Result<(), Vec<ValidationError>> {
    let mut errors = Vec::new();

    match tool_name {
        // Schema Inspection Tools
        "list_tables" => {
            // No required parameters
        }
        "describe_table" => {
            if let Some(table) = arguments.get("table") {
                if !table.is_string() {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "table".to_string(),
                        error: format!("Expected string, got {}", table.type_str()),
                        suggestion: "Example: {\"table\": \"users\"} or {\"table\": \"public.orders\"}".to_string(),
                    });
                } else {
                    let table_str = table.as_str().unwrap();
                    if table_str.is_empty() {
                        errors.push(ValidationError {
                            tool: tool_name.to_string(),
                            param: "table".to_string(),
                            error: "Table name cannot be empty".to_string(),
                            suggestion: "Provide a valid table name like 'users' or 'public.products'".to_string(),
                        });
                    }
                    if table_str.len() > 255 {
                        errors.push(ValidationError {
                            tool: tool_name.to_string(),
                            param: "table".to_string(),
                            error: format!("Table name too long: {} characters (max 255)", table_str.len()),
                            suggestion: "Use a shorter table name".to_string(),
                        });
                    }
                }
            } else {
                errors.push(ValidationError {
                    tool: tool_name.to_string(),
                    param: "table".to_string(),
                    error: "Required parameter missing".to_string(),
                    suggestion: "Include 'table' parameter: {\"table\": \"users\"}".to_string(),
                });
            }
        }

        // Query Execution Tools
        "execute_query" | "execute_insert" | "execute_update" | "execute_delete" => {
            if let Some(sql) = arguments.get("sql") {
                if !sql.is_string() {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "sql".to_string(),
                        error: format!("Expected string SQL, got {}", sql.type_str()),
                        suggestion: "Example: {\"sql\": \"SELECT * FROM users LIMIT 10\"}".to_string(),
                    });
                } else {
                    let sql_str = sql.as_str().unwrap();
                    if sql_str.is_empty() {
                        errors.push(ValidationError {
                            tool: tool_name.to_string(),
                            param: "sql".to_string(),
                            error: "SQL statement cannot be empty".to_string(),
                            suggestion: "Provide a valid SQL statement".to_string(),
                        });
                    }
                    if sql_str.len() > 10000 {
                        errors.push(ValidationError {
                            tool: tool_name.to_string(),
                            param: "sql".to_string(),
                            error: format!("SQL too long: {} characters (max 10,000)", sql_str.len()),
                            suggestion: "Break the query into smaller parts or use a subquery".to_string(),
                        });
                    }

                    // Validate SQL type for specific tools
                    let sql_upper = sql_str.trim().to_uppercase();
                    match tool_name {
                        "execute_query" => {
                            if !sql_upper.starts_with("SELECT") && !sql_upper.starts_with("WITH") {
                                errors.push(ValidationError {
                                    tool: tool_name.to_string(),
                                    param: "sql".to_string(),
                                    error: "execute_query requires a SELECT statement".to_string(),
                                    suggestion: "Use 'execute_query' only for SELECT queries. Use 'execute_insert', 'execute_update', or 'execute_delete' for modifications.".to_string(),
                                });
                            }
                        }
                        "execute_insert" => {
                            if !sql_upper.starts_with("INSERT") {
                                errors.push(ValidationError {
                                    tool: tool_name.to_string(),
                                    param: "sql".to_string(),
                                    error: "execute_insert requires an INSERT statement".to_string(),
                                    suggestion: "Example: {\"sql\": \"INSERT INTO users (email) VALUES ('user@example.com')\"}".to_string(),
                                });
                            }
                        }
                        "execute_update" => {
                            if !sql_upper.starts_with("UPDATE") {
                                errors.push(ValidationError {
                                    tool: tool_name.to_string(),
                                    param: "sql".to_string(),
                                    error: "execute_update requires an UPDATE statement".to_string(),
                                    suggestion: "Example: {\"sql\": \"UPDATE users SET status = 'active' WHERE id = 1\"}".to_string(),
                                });
                            }
                            if !sql_str.contains("WHERE") && !sql_str.contains("where") {
                                errors.push(ValidationError {
                                    tool: tool_name.to_string(),
                                    param: "sql".to_string(),
                                    error: "UPDATE without WHERE clause will modify all rows".to_string(),
                                    suggestion: "Add a WHERE clause: UPDATE users SET ... WHERE <condition>".to_string(),
                                });
                            }
                        }
                        "execute_delete" => {
                            if !sql_upper.starts_with("DELETE") {
                                errors.push(ValidationError {
                                    tool: tool_name.to_string(),
                                    param: "sql".to_string(),
                                    error: "execute_delete requires a DELETE statement".to_string(),
                                    suggestion: "Example: {\"sql\": \"DELETE FROM users WHERE id = 999\"}".to_string(),
                                });
                            }
                            if !sql_str.contains("WHERE") && !sql_str.contains("where") {
                                errors.push(ValidationError {
                                    tool: tool_name.to_string(),
                                    param: "sql".to_string(),
                                    error: "DELETE without WHERE clause will delete all rows".to_string(),
                                    suggestion: "Add a WHERE clause: DELETE FROM users WHERE <condition>".to_string(),
                                });
                            }
                        }
                        _ => {}
                    }
                }
            } else {
                errors.push(ValidationError {
                    tool: tool_name.to_string(),
                    param: "sql".to_string(),
                    error: "Required parameter 'sql' missing".to_string(),
                    suggestion: format!("Include SQL: {{\"sql\": \"<{} statement>\"}}", tool_name.split('_').nth(1).unwrap_or("SQL")),
                });
            }
        }

        // Batch Insert Tools
        "batch_insert" | "batch_insert_copy" => {
            validate_batch_insert(tool_name, arguments, &mut errors);
        }

        // Explain Query Tool
        "explain_query" => {
            if let Some(sql) = arguments.get("sql") {
                if !sql.is_string() {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "sql".to_string(),
                        error: format!("Expected string, got {}", sql.type_str()),
                        suggestion: "Example: {\"sql\": \"SELECT * FROM users\"}".to_string(),
                    });
                } else if sql.as_str().unwrap().is_empty() {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "sql".to_string(),
                        error: "SQL cannot be empty".to_string(),
                        suggestion: "Provide a SELECT query to explain".to_string(),
                    });
                }
            } else {
                errors.push(ValidationError {
                    tool: tool_name.to_string(),
                    param: "sql".to_string(),
                    error: "Required parameter 'sql' missing".to_string(),
                    suggestion: "Include SQL: {\"sql\": \"SELECT * FROM users\"}".to_string(),
                });
            }

            if let Some(format) = arguments.get("format") {
                if let Some(fmt) = format.as_str() {
                    if !["json", "text", "xml", "yaml"].contains(&fmt) {
                        errors.push(ValidationError {
                            tool: tool_name.to_string(),
                            param: "format".to_string(),
                            error: format!("Invalid format '{}' (must be json, text, xml, or yaml)", fmt),
                            suggestion: "Use one of: json (default), text, xml, yaml".to_string(),
                        });
                    }
                }
            }
        }

        // Configuration Tool
        "get_setting" => {
            if let Some(setting) = arguments.get("setting_name") {
                if !setting.is_string() {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "setting_name".to_string(),
                        error: format!("Expected string, got {}", setting.type_str()),
                        suggestion: "Example: {\"setting_name\": \"max_connections\"}".to_string(),
                    });
                } else if setting.as_str().unwrap().is_empty() {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "setting_name".to_string(),
                        error: "Setting name cannot be empty".to_string(),
                        suggestion: "Examples: max_connections, shared_buffers, work_mem, effective_cache_size".to_string(),
                    });
                }
            } else {
                errors.push(ValidationError {
                    tool: tool_name.to_string(),
                    param: "setting_name".to_string(),
                    error: "Required parameter 'setting_name' missing".to_string(),
                    suggestion: "Include setting: {\"setting_name\": \"max_connections\"}".to_string(),
                });
            }
        }

        // Object Details Tool
        "get_object_details" => {
            if let Some(table) = arguments.get("table") {
                if !table.is_string() {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "table".to_string(),
                        error: format!("Expected string, got {}", table.type_str()),
                        suggestion: "Example: {\"table\": \"users\"}".to_string(),
                    });
                } else if table.as_str().unwrap().is_empty() {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "table".to_string(),
                        error: "Table name cannot be empty".to_string(),
                        suggestion: "Provide a valid table name".to_string(),
                    });
                }
            } else {
                errors.push(ValidationError {
                    tool: tool_name.to_string(),
                    param: "table".to_string(),
                    error: "Required parameter 'table' missing".to_string(),
                    suggestion: "Include table name: {\"table\": \"users\"}".to_string(),
                });
            }

            if let Some(schema) = arguments.get("schema") {
                if !schema.is_string() {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "schema".to_string(),
                        error: format!("Expected string, got {}", schema.type_str()),
                        suggestion: "Example: {\"schema\": \"public\"}".to_string(),
                    });
                }
            }
        }

        _ => {
            // Unknown tool - no specific validation
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

fn validate_batch_insert(tool_name: &str, arguments: &Value, errors: &mut Vec<ValidationError>) {
    // Validate table
    if let Some(table) = arguments.get("table") {
        if !table.is_string() {
            errors.push(ValidationError {
                tool: tool_name.to_string(),
                param: "table".to_string(),
                error: format!("Expected string, got {}", table.type_str()),
                suggestion: "Example: {\"table\": \"users\"}".to_string(),
            });
        } else if table.as_str().unwrap().is_empty() {
            errors.push(ValidationError {
                tool: tool_name.to_string(),
                param: "table".to_string(),
                error: "Table name cannot be empty".to_string(),
                suggestion: "Provide a valid table name".to_string(),
            });
        }
    } else {
        errors.push(ValidationError {
            tool: tool_name.to_string(),
            param: "table".to_string(),
            error: "Required parameter 'table' missing".to_string(),
            suggestion: "Include table name: {\"table\": \"users\"}".to_string(),
        });
    }

    // Validate columns
    if let Some(columns) = arguments.get("columns") {
        if !columns.is_array() {
            errors.push(ValidationError {
                tool: tool_name.to_string(),
                param: "columns".to_string(),
                error: format!("Expected array, got {}", columns.type_str()),
                suggestion: "Example: {\"columns\": [\"email\", \"name\", \"created_at\"]}".to_string(),
            });
        } else {
            let cols = columns.as_array().unwrap();
            if cols.is_empty() {
                errors.push(ValidationError {
                    tool: tool_name.to_string(),
                    param: "columns".to_string(),
                    error: "Columns array cannot be empty".to_string(),
                    suggestion: "Provide at least one column name".to_string(),
                });
            }
            for (i, col) in cols.iter().enumerate() {
                if !col.is_string() {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: format!("columns[{}]", i),
                        error: format!("Expected string column name, got {}", col.type_str()),
                        suggestion: "Column names must be strings".to_string(),
                    });
                }
            }
        }
    } else {
        errors.push(ValidationError {
            tool: tool_name.to_string(),
            param: "columns".to_string(),
            error: "Required parameter 'columns' missing".to_string(),
            suggestion: "Include column names: {\"columns\": [\"email\", \"name\"]}".to_string(),
        });
    }

    // Validate rows
    if let Some(rows) = arguments.get("rows") {
        if !rows.is_array() {
            errors.push(ValidationError {
                tool: tool_name.to_string(),
                param: "rows".to_string(),
                error: format!("Expected array of arrays, got {}", rows.type_str()),
                suggestion: "Example: {\"rows\": [[\"user@test.com\", \"John\"], [\"jane@test.com\", \"Jane\"]]}".to_string(),
            });
        } else {
            let rows_arr = rows.as_array().unwrap();
            if rows_arr.is_empty() {
                errors.push(ValidationError {
                    tool: tool_name.to_string(),
                    param: "rows".to_string(),
                    error: "Rows array cannot be empty".to_string(),
                    suggestion: "Provide at least one row of data".to_string(),
                });
            } else {
                let max_rows = if tool_name == "batch_insert" { 1000 } else { 100000 };
                if rows_arr.len() > max_rows {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "rows".to_string(),
                        error: format!("Too many rows: {} (max {})", rows_arr.len(), max_rows),
                        suggestion: format!("Split into multiple calls with max {} rows each", max_rows),
                    });
                }

                // Validate each row is an array
                for (i, row) in rows_arr.iter().enumerate() {
                    if !row.is_array() {
                        errors.push(ValidationError {
                            tool: tool_name.to_string(),
                            param: format!("rows[{}]", i),
                            error: format!("Row must be array, got {}", row.type_str()),
                            suggestion: "Each row must be an array of values".to_string(),
                        });
                    }
                }
            }
        }
    } else {
        errors.push(ValidationError {
            tool: tool_name.to_string(),
            param: "rows".to_string(),
            error: "Required parameter 'rows' missing".to_string(),
            suggestion: "Include rows: {\"rows\": [[\"value1\", \"value2\"], ...]}".to_string(),
        });
    }

    // Validate batch_size for batch_insert_copy
    if tool_name == "batch_insert_copy" {
        if let Some(batch_size) = arguments.get("batch_size") {
            if !batch_size.is_number() {
                errors.push(ValidationError {
                    tool: tool_name.to_string(),
                    param: "batch_size".to_string(),
                    error: format!("Expected integer, got {}", batch_size.type_str()),
                    suggestion: "Example: {\"batch_size\": 1000}".to_string(),
                });
            } else if let Some(size) = batch_size.as_i64() {
                if !((100..=5000).contains(&size)) {
                    errors.push(ValidationError {
                        tool: tool_name.to_string(),
                        param: "batch_size".to_string(),
                        error: format!("Batch size {} out of range (must be 100-5000)", size),
                        suggestion: "Use default (1000) or set between 100 and 5000".to_string(),
                    });
                }
            }
        }
    }
}

/// Helper trait to get type name from Value
trait ValueType {
    fn type_str(&self) -> &str;
}

impl ValueType for Value {
    fn type_str(&self) -> &str {
        match self {
            Value::Null => "null",
            Value::Bool(_) => "boolean",
            Value::Number(_) => "number",
            Value::String(_) => "string",
            Value::Array(_) => "array",
            Value::Object(_) => "object",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_missing_required_param() {
        let args = json!({});
        let result = validate_tool_input("describe_table", &args);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_type() {
        let args = json!({"table": 123});
        let result = validate_tool_input("describe_table", &args);
        assert!(result.is_err());
    }

    #[test]
    fn test_valid_input() {
        let args = json!({"table": "users"});
        let result = validate_tool_input("describe_table", &args);
        assert!(result.is_ok());
    }
}