Skip to main content
Glama

rewrite_query

After preflight flags a slow or dangerous query, rewrites it into an equivalent cheaper one, proves the gain by comparing EXPLAIN planner costs, and returns changes, cost deltas, index suggestions.

Instructions

Rewrite a slow query into a cheaper, equivalent one and PROVE the improvement by EXPLAIN-ing both and comparing planner cost. Call after a preflight flags a query expensive/dangerous. Returns the rewritten query, what changed, before/after cost, and index suggestions — ONLY suggesting a btree index when the filter is selective enough to help (an HNSW index for a vector target). Includes equivalence caveats. Pass target= in a multi-DB setup.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sqlYes
targetNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.2.0

TDQS

A4.1/5.0
Behavior4/5

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

With no annotations, the description carries the full burden and does well: it discloses the equivalence-caveat output, the btree-vs-HNSW index heuristic, and that improvement is proven by comparing planner cost rather than asserted. It never states whether the rewrite is applied or merely returned, and says nothing about required permissions or whether execution touches the target DB.

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?

Front-loaded with the core purpose and the trigger condition, then the return contents. Dense and mostly waste-free, though the long multi-clause middle sentence about index suggestions is information-dense enough to slow reading.

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

Completeness4/5

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

An output schema exists, so enumerating return fields (rewritten query, changes, before/after cost, index suggestions) is arguably redundant, yet the description still adds the equivalence-caveat and indexing-rule context. What remains missing is the read-only/apply question, which matters for a tool that inspects and rewrites SQL.

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 description coverage is 0% with 2 parameters, so the description is the only source of meaning. It explains target adequately ('Pass target=<name> in a multi-DB setup'), but sql is left entirely implicit — no hint that it expects a concrete statement or what dialect/normalization is assumed.

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

Purpose5/5

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

States a specific verb+resource (rewrite a slow query into a cheaper equivalent) plus the verification method (EXPLAIN both, compare planner cost). It is clearly distinguishable from siblings like preflight_query, suggest_query, and explain_query_working. An agent can tell exactly what this tool produces without opening the schema.

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

Usage Guidelines4/5

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

Explicitly states the trigger condition: 'Call after a preflight flags a query expensive/dangerous,' which links it to preflight_query/preflight_vector_search. It also scopes the multi-DB case via target=<name>. It stops short of naming when NOT to use it or explicitly contrasting with suggest_query as the lighter-weight alternative.

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