Skip to content

About

Define a metric once, serve it everywhere: a spec, a linter, an MCP server for AI agents, a query gate, a CLI, and a Snowflake semantic view compiler.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

MEASUREMENT.md

Define a metric once. Serve it to every surface: AI agents over MCP, a CLI, a query gate, and the warehouse's own semantic layer.

Agents now write the SQL, build the dashboard and narrate the result, and they still get the number wrong, because "conversions", "net revenue" and "active customer" mean something different in every organization and none of it is written where an agent can read it. MEASUREMENT.md is a plain file that holds those definitions, and this repo is everything that keeps every surface faithful to it.

flowchart LR
  C["MEASUREMENT.md<br/>one definition per metric"] --> L["lint<br/>is the contract consistent?"]
  C --> M["MCP server<br/>Claude, ChatGPT and other agents read it"]
  C --> G["query gate<br/>refuse queries that break it"]
  C --> X["CLI<br/>explain, check, export"]
  C --> S["Snowflake semantic view<br/>compiled YAML for Cortex Analyst"]
Loading
Surface Command What it does
Lint measurement-md lint FILE 12 rules: missing fields, alias collisions, unknown references, cycles, stale reviews
MCP measurement-md mcp FILE Stdio MCP server with list_metrics, get_metric (aliases and deprecated names resolve) and check_query
Gate measurement-md check FILE request.json Allows a metric request or refuses it naming the clause (G001-G008)
Explain measurement-md explain FILE "Net Sales" The governed definition, resolved from any alias
Snowflake measurement-md export FILE --snowflake NAME A semantic view YAML: base metrics on their tables, derived metrics at view level, aliases as synonyms, anti-patterns as SQL-generation instructions

Run any of them with PYTHONPATH=src python3 -m measurement_md .... Python 3.9+, standard library only.

Use it from Claude Desktop (or any MCP client)

{
  "mcpServers": {
    "measurement": {
      "command": "python3",
      "args": ["-m", "measurement_md", "mcp", "/path/to/MEASUREMENT.md"],
      "env": { "PYTHONPATH": "/path/to/measurement-md/src" }
    }
  }
}

Ask "what was gross sales last week?" and the agent can learn that Gross Sales is an alias for Gross Revenue, that tax is excluded, and that weeks start on Monday in Los Angeles time, before it writes any SQL.

Should definitions live in the warehouse, in the BI tool, or in a file?

In a file, compiled into both. The file is reviewable, versioned and tool-neutral. The warehouse gets a semantic view (export --snowflake) so Cortex Analyst and SQL users see the same definitions. Agents get the MCP server and the gate. A definition changes in one place and every surface follows, and every answer can cite the contract version that produced it.

Snowflake export: what maps to what

MEASUREMENT.md Snowflake semantic view YAML
metric with table and sql table-level metric on that logical table
metric with source: derived view-level metric; {Other Metric} becomes table.metric, derived references inlined
aliases synonyms
definition description
anti_patterns, deprecated names, timezone, attribution model module_custom_instructions.sql_generation
A base metric without a table and sql binding is reported and left out, never guessed (exit code 3). The Acme example compiles fully; the other two have no bindings yet and show that report.

Evaluation (eval/results.md)

Run: python3 eval/run_eval.py. Tests: python3 -m unittest discover -s tests.

Check Result
Unit tests 37 pass: one per lint rule, the gate rules, the Snowflake export, an MCP session over stdio, CLI exit codes
Planted defects: one mutation per rule, 10 variants x 3 exemplars 360 of 360 detected; 100% of findings were the planted rule
Clean controls: reordered, CRLF, extra spaces, upper-case references 0 of 18 flagged

A bug the eval found. An alias that collided with another metric's name produced a second, phantom "cycle" finding, because the ambiguous reference was added to the dependency graph. First run: 7 collateral findings. Fix: ambiguous references are reported once (M003) and kept out of the graph. Collateral is now 0 and a test pins it.

Limits (read these)

  • The Snowflake export follows Snowflake's published YAML specification for semantic views and parses as YAML, but it has not been loaded into a Snowflake account. It emits metrics only: no dimensions, time dimensions or relationships yet, so Cortex Analyst could not yet answer "by week" questions from it alone.
  • The MCP server implements the stdio transport and the three tool calls (initialize, tools/list, tools/call, ping), tested with a scripted session. It has no auth, because it serves a local file.
  • The linter checks structure and internal consistency. It cannot tell you a definition is right.
  • Defects are mutations I wrote, against rules I wrote, on three fictional files. 100% recall here is a regression guard, not evidence about real-world files.
  • Exemplars are fictional. Nothing here uses any employer's definitions or data.

The real test

Write a MEASUREMENT.md for a real company and have a second person agree with every definition. The brief's riskiest assumption is that teams will write the file at all, because definition disputes are usually political, not technical. A linter does not fix that.

Roadmap

v0.2: dimension definitions, a machine-checkable formula grammar, and an agent skill that loads the file and refuses to invent a metric.

Query gate (v0.2 draft)

The linter checks the contract. The gate checks an agent against the contract, before its query runs.

python3 -m measurement_md.check examples/acme-outdoors.MEASUREMENT.md request.json

It returns the canonical metric, its definition and a contract version to stamp on the answer, or a refusal naming the clause, for example G002: 'Total Sales' is deprecated; use 'Gross Revenue'. Rules are in SPEC.md.

Eval (eval/run_gate_eval.py, results in eval/gate-results.md): 117 of 117 correct requests allowed, 62 of 62 planted mistakes refused with exactly the planted rule, and 10 of 10 realistic agent mistakes decided as expected (for example daily MRR, orders counted from carts, gross revenue with tax mixed in). The first run scored 59 of 62; both misses were bugs in the eval, recorded in the results.

Limits: SQL checks are regular expressions, so an alias, a view or a CTE can slip past a pattern; a production gate needs a SQL parser and column lineage. The gate also trusts the agent's description of its own request.

About

Define a metric once, serve it everywhere: a spec, a linter, an MCP server for AI agents, a query gate, a CLI, and a Snowflake semantic view compiler.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages