Skip to main content
Glama
syrian963

django-chainsaw-mcp

by syrian963

sqlalchemy_nplusone

Read-onlyIdempotent

Detect N+1 query patterns in SQLAlchemy by monitoring lazy loads. Identify relationships that trigger extra queries per row.

Instructions

Relationships SQLAlchemy will load one row at a time.

    orders = db.query(Order).all()
    for order in orders:
        print(order.customer.name)      # one query per order

And the quieter FastAPI shape, where there is no loop to see:

    @app.get("/orders", response_model=list[OrderOut])
    def list_orders(db=Depends(get_db)):
        return db.query(Order).all()

OrderOut declares `items`, so serialisation walks the relationship once
per row - after the endpoint has returned, which is why nothing in the
function body mentions it.

nplusone finds this at runtime by watching lazy loads happen, and
lazy="raise" turns it into an exception; both need the code path to run.
A relationship declared lazy="selectin", "joined" or "raise" is never
reported here, since the first two are already eager and the third is the
recommended fix.

Nothing is imported. Needs no Django.

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.4/5.0
Behavior5/5

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

The description adds meaningful behavior beyond the readOnly/idempotent annotations: it is a runtime observer that requires the code path to run, it does not import anything, it does not require Django, and it will not report already-eager or raise-configured relationships. There is no contradiction with the annotations.

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 long but every major section earns its place: two illustrative code shapes, the runtime mechanism, the exclusions, the no-import/no-Django note, and the argument definition. The core idea is front-loaded with the first sentence, and the examples clarify a non-obvious FastAPI serialization case.

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 tool with one optional parameter, an output schema, and read-only/idempotent annotations, the description is complete enough for an agent to invoke it confidently. It covers what it detects, how detection works, when findings will not appear, and what the argument means. Nothing essential 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 description coverage is 0%, so the description carries the full burden for the single search_path parameter. It explains that it is the directory to scan and that it defaults to the configured project. This is useful and sufficient for a simple optional path parameter, though it does not discuss path format or relative-versus-absolute resolution.

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 strongly and concretely conveys what the tool addresses: SQLAlchemy lazy-loading that fetches one row at a time, with both a loop-based and a FastAPI serialization example. It is not a tautology, but it never states an explicit verb like 'detects' or 'reports' for the tool itself, and it does not explicitly differentiate itself from sibling tools such as serializer_nplusone or find_n_plus_one.

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 gives clear context: the tool works at runtime by watching lazy loads, so the relevant code path must actually execute. It also provides an explicit when-not: relationships declared lazy='selectin', 'joined', or 'raise' are never reported. It does not name alternative sibling tools or give a direct 'use this when...' statement, so it falls short of a 5.

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