mcp-db-analyzer 0.2.5 → 0.2.6

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.
@@ -10,7 +10,9 @@ export async function explainQuery(sql, analyze = false) {
10
10
  // EXPLAIN ANALYZE actually executes the query, so we must reject anything
11
11
  // that could modify data — including CTEs with write operations.
12
12
  if (analyze) {
13
- const upperSql = sql.trim().toUpperCase();
13
+ // Strip single-quoted string literals before scanning for DML keywords so that
14
+ // a query like `SELECT ... WHERE status = 'DELETE me'` is not falsely rejected.
15
+ const upperSql = sql.trim().toUpperCase().replace(/'[^']*'/g, "''");
14
16
  const DML_KEYWORDS = ["INSERT", "UPDATE", "DELETE", "DROP", "ALTER", "TRUNCATE", "CREATE", "GRANT", "REVOKE", "COPY"];
15
17
  const containsDml = DML_KEYWORDS.some((kw) => upperSql.includes(kw + " ") || upperSql.includes(kw + "\n") || upperSql.includes(kw + "\t") || upperSql.endsWith(kw));
16
18
  if (containsDml) {
@@ -187,13 +187,16 @@ export async function inspectTable(tableName, schema = "public") {
187
187
  return lines.join("\n");
188
188
  }
189
189
  async function inspectTableSqlite(tableName) {
190
- const cols = await query(`PRAGMA table_info("${tableName}")`);
190
+ // Escape double-quote characters so a table name containing `"` (e.g. `weird"table`)
191
+ // does not break the PRAGMA queries or the row-count SELECT.
192
+ const escaped = tableName.replace(/"/g, '""');
193
+ const cols = await query(`PRAGMA table_info("${escaped}")`);
191
194
  if (cols.rows.length === 0) {
192
195
  return `Table '${tableName}' not found.`;
193
196
  }
194
197
  const lines = [`## Table: main.${tableName}\n`];
195
198
  // Row count
196
- const countResult = await query(`SELECT count(*) as cnt FROM "${tableName}"`);
199
+ const countResult = await query(`SELECT count(*) as cnt FROM "${escaped}"`);
197
200
  lines.push(`- **Rows**: ${countResult.rows[0]?.cnt ?? 0}`);
198
201
  lines.push("");
199
202
  lines.push("### Columns\n");
@@ -203,7 +206,7 @@ async function inspectTableSqlite(tableName) {
203
206
  lines.push(`| ${col.cid + 1} | ${col.name} | ${col.type || 'ANY'} | ${col.notnull ? 'NO' : 'YES'} | ${col.dflt_value ?? '-'} | ${col.pk ? 'YES' : '-'} |`);
204
207
  }
205
208
  // Foreign keys
206
- const fks = await query(`PRAGMA foreign_key_list("${tableName}")`);
209
+ const fks = await query(`PRAGMA foreign_key_list("${escaped}")`);
207
210
  if (fks.rows.length > 0) {
208
211
  lines.push("\n### Foreign Keys\n");
209
212
  lines.push("| Column | References |");
@@ -213,13 +216,13 @@ async function inspectTableSqlite(tableName) {
213
216
  }
214
217
  }
215
218
  // Indexes
216
- const indexes = await query(`PRAGMA index_list("${tableName}")`);
219
+ const indexes = await query(`PRAGMA index_list("${escaped}")`);
217
220
  if (indexes.rows.length > 0) {
218
221
  lines.push("\n### Indexes\n");
219
222
  lines.push("| Name | Unique | Columns |");
220
223
  lines.push("|------|--------|---------|");
221
224
  for (const idx of indexes.rows) {
222
- const idxCols = await query(`PRAGMA index_info("${idx.name}")`);
225
+ const idxCols = await query(`PRAGMA index_info("${idx.name.replace(/"/g, '""')}")`);
223
226
  const colNames = idxCols.rows.map(c => c.name).join(", ");
224
227
  lines.push(`| ${idx.name} | ${idx.unique ? 'YES' : 'NO'} | ${colNames} |`);
225
228
  }
@@ -119,6 +119,20 @@ async function analyzeMysqlSlowQueries(limit) {
119
119
  const truncated = r.DIGEST_TEXT.length > 80 ? r.DIGEST_TEXT.slice(0, 77) + "..." : r.DIGEST_TEXT;
120
120
  sections.push(`| ${i + 1} | ${r.AVG_TIMER_WAIT.toFixed(1)}ms | ${r.SUM_TIMER_WAIT.toFixed(0)}ms | ${r.COUNT_STAR} | \`${truncated.replace(/\|/g, "\\|")}\` |`);
121
121
  }
122
+ // Recommendations
123
+ sections.push("");
124
+ sections.push("### Recommendations");
125
+ const highCallSlow = result.rows.filter((r) => r.COUNT_STAR > 100 && r.AVG_TIMER_WAIT > 100);
126
+ if (highCallSlow.length > 0) {
127
+ sections.push(`- **${highCallSlow.length} high-impact queries** — called >100 times with >100ms avg. Prioritize these for optimization.`);
128
+ }
129
+ const fewRowsSlow = result.rows.filter((r) => r.AVG_TIMER_WAIT > 50 && r.SUM_ROWS_SENT / Math.max(r.COUNT_STAR, 1) < 10);
130
+ if (fewRowsSlow.length > 0) {
131
+ sections.push(`- **${fewRowsSlow.length} queries returning few rows but slow** — likely missing indexes. Use \`explain_query\` to check.`);
132
+ }
133
+ if (highCallSlow.length === 0 && fewRowsSlow.length === 0) {
134
+ sections.push("- No critical patterns detected. Monitor trends over time.");
135
+ }
122
136
  return sections.join("\n");
123
137
  }
124
138
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-db-analyzer",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "MCP server for PostgreSQL, MySQL, and SQLite schema analysis, index optimization, and query plan inspection",
5
5
  "mcpName": "io.github.dmitriusan/mcp-db-analyzer",
6
6
  "author": "Dmytro Lisnichenko",