Skip to main content
Glama
Elekto MCP for SQL Server

Elekto MCP for SQL Server

Official

Find statements in the plan cache

find_cached_queries
Read-onlyIdempotent

Lists SQL Server plan-cache statements with execution counts, durations, CPU, reads, and plan handles to diagnose slow queries when Query Store is off or inspect current plans.

Instructions

Lists this database's statements in SQL Server's plan cache, leaving out the queries this server sends to read the cache and the sessions, with what their runs cost since each plan was cached: per statement, when its plan was compiled and how many times (plan_generation_num), its last run, executions, total, average, maximum and last duration, CPU and logical reads, physical reads, rows, degree of parallelism, memory grant and spills, the query and plan hashes, the first 300 characters of the statement, and the plan_handle with the statement's offsets, which get_query_plan_summary takes. With object_name, the statements of that procedure, function or trigger, the variants SQL Server 2022+ keeps of a parameter-sensitive one included, and the totals SQL Server keeps for a procedure, scalar function or trigger. Use it when a query is slow and the Query Store is off, as it is by default before SQL Server 2022, or to see the plan in use now. The cache keeps no history: a plan evicted, recompiled or lost to a restart takes its figures with it, so where the Query Store is on use find_query_store_queries and get_query_store_history for what changed over days. Needs VIEW SERVER STATE (2022+: VIEW SERVER PERFORMANCE STATE).

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": {
      "database": "<database>"
    },
    "name": "find_cached_queries"
  }
}

Input Schema

NameRequiredDescriptionDefault
topNoMost statements to return (default 20, at most 100).
schemaNoSchema of object_name, which it needs. Empty searches every schema. Example: 'AutoRegister'
databaseYesName of the database as registered in the configuration.
order_byNo'duration' (default): the most total time since the plan was cached; 'cpu', 'logical_reads' or 'physical_reads': the most of that in total; 'executions': the most run; 'avg_duration': the slowest execution on average; 'recompiles': the highest plan_generation_num; 'newest': the plans compiled most recently.duration
object_nameNoProcedure, function or trigger whose statements to list, bare, with no schema prefix; the answer then adds, for a procedure, scalar function or trigger, its own totals in module_stats. Empty means any statement, inside a module or not. Example: 'AutoRegisterEquity'
text_patternNoPart of the statement's text, matched anywhere in it as written: brackets, % and _ are plain characters, so a fragment copied from a statement_text finds it. At most 1000 characters. Empty means any text. Example: '[dbo].[GenericSecurity]'

Schema Changelog

Changes observed during successful MCP inspections.

  1. Addedv2.6.1

TDQS

A4.5/5.0
Behavior4/5

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

Annotations already declare the read-only, idempotent, non-destructive, closed-world profile, and the description goes well beyond them: required permissions (VIEW SERVER STATE, 2022+: VIEW SERVER PERFORMANCE STATE), the fact that plans are evicted/recompiled/lost on restart along with their figures, and that object_name also surfaces parameter-sensitive plan variants and module_stats totals. What it does not cover is result size or completeness guarantees (e.g. whether top truncates silently), which keeps it off a 5.

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

Conciseness3/5

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

Purpose and alternatives are front-loaded, and the long column inventory is defensible since there is no output schema. But it is delivered as one sprawling sentence whose middle is a comma-spliced list of ~20 metrics, which makes it slow to scan and harder for an agent to extract the operative constraints from.

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

Completeness5/5

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

For a six-parameter tool with no output schema, the description carries the full return-value burden: it enumerates the metrics returned, notes the 300-character statement prefix, explains the cache's volatility, names the permission needed, and routes to the Query Store siblings when history is required. Nothing an agent needs to call it correctly or interpret its limits is missing.

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

Parameters4/5

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

Schema coverage is 100%, so the schema already documents all six parameters; the description still adds cross-parameter behavior — that schema is needed alongside object_name, that object_name must be bare with no schema prefix and adds module_stats, and that the returned plan_handle plus offsets is exactly what get_query_plan_summary consumes. That inter-tool contract is meaning the schema cannot express.

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?

Opens with a specific verb and resource ('Lists this database's statements in SQL Server's plan cache') and immediately bounds it by excluding the server's own cache-reading queries. It is unmistakable against find_query_store_queries, which the description later names as the alternative.

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

Usage Guidelines5/5

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

States explicit triggers ('when a query is slow and the Query Store is off, as it is by default before SQL Server 2022, or to see the plan in use now') and an explicit exclusion plus named alternatives ('where the Query Store is on use find_query_store_queries and get_query_store_history'). The when-not condition is grounded in a concrete reason: the cache keeps no history.

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