Skip to main content
Glama
syrian963

django-chainsaw-mcp

by syrian963

find_n_plus_one

Read-onlyIdempotent

Scan Django templates for relation traversals inside loops that trigger extra queries, flagging potential N+1 hotspots by resolving attribute chains against model relationships.

Instructions

Find relation traversals in a template that each cost a query.

Resolves attribute chains against the real model graph and flags the ones
that cross a relation inside a loop, which is where N+1 queries come from.
Reports candidates: whether a crossing really costs a query depends on the
queryset in the view, which this does not read.

Args:
    template_path: path to the template file.
    root_models: context variable to model label, e.g.
        {"orders": "shop.Order"}. Loop variables inherit from these.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
root_modelsYes
template_pathYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.1.3

TDQS

A4.3/5.0
Behavior4/5

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

The description goes beyond the readOnly/idempotent annotations by explaining that results are candidates, not confirmed costs, and that queryset context is intentionally ignored. It also describes the resolution mechanism (attribute chains against the real model graph) and the loop condition. This appropriately frames the tool's limitations.

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?

The description starts with a one-line purpose, then explains method and limitation, then documents args. Each sentence adds a distinct fact and there is no boilerplate. The only minor issue is that 'that each cost a query' in the first sentence overstates what is later correctly labeled as a candidate.

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?

For a two-parameter, read-only analyzer with an output schema, the description covers what the tool scans, how it resolves models, its main limitation, and how to pass inputs. It does not need to document return values because an output schema exists. The description is complete enough to select and invoke the tool correctly.

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 0%, so the Args block carries the descriptive burden. Both parameters are explained: template_path is a path to the template, and root_models is a context-variable-to-model-label mapping with an example and a note on loop-variable inheritance. This is sufficient to construct a valid call, though root_models semantics could be expanded slightly.

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?

The description names a concrete resource ('relation traversals in a template') and a specific diagnostic action ('Find... flags the ones that cross a relation inside a loop'). It is clearly template-scoped and distinct from sibling tools focused on ORM, serializer, or query paths. There is no ambiguity about what the tool analyzes.

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?

The description states a clear context: it scans templates for candidate N+1 traversal patterns and explicitly warns that whether it actually costs a query depends on the view queryset, which the tool does not read. This tells an agent when not to treat results as definitive, but it does not name alternative sibling tools for the view/ORM side, so guidance is clear but not fully explicit.

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