Skip to main content
Glama
MCPg - Production-grade PostgreSQL MCP Server

MCPg - Production-grade PostgreSQL MCP Server

Optimize query

optimize_query
Read-only

Analyze SQL queries for syntax anti-patterns and performance issues using EXPLAIN plan costs and index scans, then return an optimized query version.

Instructions

Analyze a SQL query for syntax anti-patterns and performance issues using EXPLAIN plan costs and index scans, returning an optimized version.

Tool call template

Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {
      "sql": "<sql>"
    },
    "name": "optimize_query"
  }
}

Input Schema

NameRequiredDescriptionDefault
sqlYes
databaseNoOptional: a database id from list_databases, to target a configured secondary (read-only) database; omit for the primary. Database names that are not configured ids are rejected.

Output Schema

NameRequiredDescriptionDefault
findingsYes
rationaleYes
original_sqlYes
optimized_sqlYes
explain_summaryYes

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed1 schema field changedv0.8.4
    • changedInput schema / properties / database / description
      Previous value: -"Optional: target a configured secondary (read-only) database by name; omit for the primary. Call list_databases to see the configured ids."New value: +"Optional: a database id from list_databases, to target a configured secondary (read-only) database; omit for the primary. Database names that are not configured ids are rejected."
  2. First observedv0.6.8

TDQS

B3.2/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already cover the safety profile (readOnlyHint=true, openWorldHint=false), so the description isn't burdened with that. It does add useful method context (EXPLAIN plans, index scans), but leaves a material ambiguity: whether the supplied SQL is actually executed or only parsed/planned, and whether the 'optimized version' is returned as a suggestion only. For a tool that accepts arbitrary SQL, that distinction matters.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

One front-loaded sentence with no filler; the core action and the deliverable come first. It is efficient, though it could have used a few more words to add routing guidance without bloat.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

An output schema exists, so the return shape needn't be spelled out, and annotations carry the read-only signal. What remains missing is execution semantics and sibling differentiation, which for a query-analysis tool with ~10 close alternatives is enough to leave the definition only adequate.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 50%: the optional 'database' parameter is documented thoroughly in the schema, while 'sql' has no schema description at all. The description refers to 'a SQL query' but adds nothing about single vs. multiple statements, dialect expectations, or whether parameterized statements are allowed, so it does not compensate for the gap.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description names a specific verb ('Analyze') and resource ('a SQL query') and adds concrete method detail (syntax anti-patterns, EXPLAIN plan costs, index scans) plus a concrete deliverable ('an optimized version'). It does not, however, distinguish itself from adjacent siblings such as explain_query, analyze_query_plan, why_is_this_slow, or run_advisors, so an agent cannot route between them from the text alone.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

There is no when-to-use or when-not-to-use guidance and no mention of any alternative. Given the crowded sibling set of query-diagnosis tools (explain_query, analyze_query_plan, analyze_session_cost, recommend_indexes), the absence of routing guidance is a real gap.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Deploy Server

Other Tools