Search for a command to run...

CqrsLens is a static analysis tool for .NET solutions that use the mediator pattern (CQRS). It reads a solution's source code without building it and produces a structured architecture report: the complete request–handler–validator map, pipeline behaviour ordering, the EF Core data model, the project dependency graph and layer model, and a set of architecture conformance checks. It is distributed as a .NET global tool via NuGet (current release 0.9.0, MIT licence) and additionally exposes its analysis through a Model Context Protocol (MCP) server for use by AI coding agents.
In a mediator-based codebase, the wiring between requests, handlers, validators, and pipeline behaviours is distributed across registration code, naming conventions, and folder structure. No single file shows the whole system, and the failure modes are silent: a command whose validator was never written, a handler registered twice, an event with subscribers but no publisher. These defects do not fail compilation and are rarely caught in review, because reviewing them requires reconstructing exactly the global map that the codebase does not present. I encountered this in my own production-shaped codebase — the backend of this site — where a manual audit found 63 requests and 63 handlers but only 9 validators: 33 of 39 commands accepted input with no validation at all. CqrsLens exists to produce that map mechanically, and to check it.
Four decisions define the tool's character.
Static analysis without a build. The target solution is never restored or compiled. The tool therefore works on code that does not currently compile, on a clean CI agent, and on an unfamiliar codebase within seconds of cloning it. The cost of this decision is that all information must be recovered from source text and project files, which shapes the analysis engine below.
Two engines, one model. The default engine is text-based: deliberately fast pattern analysis over source files. An optional Roslyn engine (--analysis roslyn) performs semantic analysis for cases where the compiler's view is required. Both engines produce the same intermediate model, so reports, checks, and renderers are engine-independent. The trade-off — speed and robustness against precision on exotic indirection — is explicit and left to the user.
Only provable edges are drawn. The report's flow diagrams connect endpoints → requests → handlers → notifications → data access. An edge is emitted only when it can be justified from the code. Concretely, in codebases where handlers depend on abstractions (a repository, or an interface such as INorthwindDbContext), the tool draws no handler→database edge rather than guessing one. A reported zero is a measured zero; the tool is designed to under-claim rather than over-claim.
Registration order is reported as registration order. Pipeline behaviours are listed in the order they are registered in dependency injection, with source provenance. The tool does not relabel this as execution order, which is a runtime property of the mediator library's contract and is not statically provable from the codebase.
The codebase is four projects with a single direction of data flow — files in, model built, everything else downstream of the model:
CqrsLens.Core — the architecture model (projects, requests, handlers, behaviours, DbContexts, entities, endpoints, checks) plus baseline and configuration types. No scanning logic.
CqrsLens.Analysis — the two scanning engines (text and Roslyn), project loading, EF Core model extraction, the flow/linking analysis that connects dispatch sites to requests and handlers to notifications, slicing, and the 11 conformance checks.
CqrsLens.Reporting — renderers: Markdown, HTML (with an interactive explorer), JSON, a compact agent-oriented format, Mermaid and SVG diagrams, and SARIF export.
CqrsLens.Cli — command-line surface, .cqrslens.json configuration, baseline and snapshot-diff support, exit codes, and the MCP server.
The mediator library is detected, not assumed: MediatR, the martinothamar Mediator, and Wolverine are supported, including codebases that mix them.
Northwind Traders is the reference Clean Architecture sample (10 projects). Actual console output of the published tool:
The generated flow diagram for the Categories feature, reproduced verbatim from that report:
Findings are reported with rule, target, and exact source location. Three of the scan's eight warnings:
The full check set: MissingHandler, MultipleHandlers, CircularProjectDependency (errors); LayerViolation, ApplicationLayerViolation (error when a layer model is asserted, warning otherwise); CommandWithoutValidator, OrphanValidator, OrphanNotification, UnpublishedNotification, UnreachableRequest, AmbiguousProjectName (warnings).
Performance and accuracy are measured against a fixed corpus of six real codebases, re-run for every release with the published NuGet package (text engine, median of 10 runs, 4-vCPU / 8 GB virtual machine):
Jason Taylor CleanArchitecture — 8 projects · 12 requests · 12 handlers — 0.69 s
eShop Ordering — 3 projects · 8 requests · 8 handlers — 0.72 s
Ardalis CleanArchitecture — 22 projects · 23 requests · 23 handlers — 1.12 s
Northwind Traders — 10 projects · 19 requests · 19 handlers — 1.37 s
Modular Monolith — 47 projects · 105 requests · 105 handlers — 2.02 s
ABP Framework — 678 projects · 0 requests · 80 DbContexts — 12.63 s
Two properties of this evaluation matter more than the timings. First, accuracy is regression-gated: each corpus has frozen expected counts, and a release is not published if any count changes without an intended cause. Second, the ABP row is a negative control. ABP does not use a mediator, and the tool reports exactly that — zero requests, zero handlers — while still recovering the structural model (678 projects, 80 DbContexts). An analysis tool that cannot report zero cannot be trusted when it reports a number.
cqrslens --mcp starts an MCP server with four tools: get_architecture_dna (system shape), trace_slice (one feature end to end), trace_entity_impact (the reach of a data entity), and check_architecture (the conformance checks). The design intent is a strict division of labour: the agent proposes; the tool verifies. An agent answering questions about a codebase from this server is constrained to facts produced by the scan, and the server's answers fail closed — what the scan cannot establish, it does not assert.
Detection is centred on mediator-based CQRS architectures. The structural sections (projects, layers, data model, endpoints, configuration) are general; the request/handler analysis is not intended for non-mediator codebases, as the ABP result demonstrates.
The text engine trades semantic precision for speed and build-independence; unusual indirection can require the Roslyn engine, and some dynamic registration patterns remain out of reach of both.
Indirect data access (handlers → interfaces/repositories → DbContext) is deliberately not drawn as a direct edge; the report states what is provable and no more.
Pipeline execution order is a runtime property; the tool reports registration order with provenance and labels it as such.
Version 0.9.0, MIT licence, developed in the open. CI integration: --strict fails a build on error-level findings (exit code 4), --sarif exports findings in the OASIS SARIF format, baselines allow adopting teams to accept existing findings while failing on new ones, and --diff compares scans across revisions. Exit codes are contractual: 0 clean · 1 usage error · 2 path not found · 3 no projects found · 4 strict failure · 5 unexpected failure.