Skip to main content
Glama
syrian963

django-chainsaw-mcp

by syrian963

multiplied_aggregates

Read-onlyIdempotent

Scan a Django project for annotations where multi-valued joins multiply rows, causing Count and Sum aggregates to return inflated values. Identifies these incorrect queries for correction.

Instructions

Aggregates whose numbers are wrong because a join multiplied the rows.

    Order.objects.annotate(lines=Count("lines"), shipments=Count("shipments"))

Joining two multi-valued relations gives the cartesian product of them: an
order with 3 lines and 2 shipments produces 6 rows, and both counts come
back as 6. Nothing raises. Two plausible numbers, both the product of the
two, usually on a dashboard nobody can check by hand.

Only Count and Sum are reported. A join repeats rows uniformly within each
group, so Min and Max return the value they would anyway and Avg divides a
multiplied total by a multiplied count - including them reported a correct
query as a defect on the first real project this saw.

Count(distinct=True) is treated as correct. Sum has no equivalent and needs
a Subquery, so a query is still reported when every Count in it is distinct
but a Sum crosses a second relation.

Django's own documentation warns about this and no linter checks it.

Args:
    search_path: directory to scan. Defaults to the configured project.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
search_pathNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.1.3

TDQS

A4.7/5.0
Behavior5/5

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

Annotations already mark the tool as read-only, idempotent, and non-destructive. The description goes well beyond that by explaining what gets reported, what does not, and why edge cases like Min, Max, Avg, and distinct counts are handled the way they are. This gives an agent a precise mental model of the tool's behavior.

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

Conciseness5/5

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

The description is longer than average, but every paragraph earns its place: it explains the core problem, the reporting scope, edge-case rationale, and external context. The information is well-organized and front-loaded with the central concept before diving into specifics.

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 complex analysis rule with a single parameter and an output schema, the description is essentially complete. It covers the defect being detected, the exact aggregate functions considered, the reasoning behind exclusions, and the parameter behavior. No critical operational detail is missing for an agent to select and invoke the tool.

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?

The schema has 0% description coverage, so the description must carry the weight for the single search_path parameter. It does: 'directory to scan. Defaults to the configured project' adds meaning beyond the raw schema field name, though it could provide more detail about path resolution or validity.

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 clearly identifies the tool's purpose: reporting aggregates corrupted by join multiplication, with a concrete Django ORM example. It distinguishes itself from the many sibling analysis tools by specifying exactly which aggregate expressions are in scope (Count and Sum) and which are not.

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 explains when the tool is relevant and gives detailed coverage rules: only Count and Sum are reported, Count(distinct=True) is treated as correct, and Min/Max/Avg are intentionally excluded. It does not explicitly name sibling alternatives or state 'use this when...', but the detection scope is clear and actionable.

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