Skip to main content
Glama
field-analytics

field-analytics

by cicerao

Field analytics assistant

A small, working example of how I build AI analytics: governed metrics, an MCP server that puts them in front of Claude, skills that tell Claude how to answer, a screen people can use, and an evaluation set that has to pass before any change ships.

The screen: output per hour by site and shift, with what the number covers

The data is synthetic. My production work lives in private company repositories, so I wrote this sample to show the same way of working on numbers I can publish. It covers two subjects I know well: harvesting machines running two shifts across three sites, and machine sales through a dealer channel in three regions.

The problem it solves

People stop using an assistant the first time it gives a number that disagrees with the report they already trust. I've watched two departments bring two different production totals for the same day to the same meeting. The cause was small. The night shift crosses midnight, and one side counted those hours on the next calendar day.

So this project starts with the definition and works outward:

  1. semantic/metrics.yaml holds one written definition per metric, with an owner.

  2. src/field_analytics/metrics.py implements each definition once, in SQL.

  3. The MCP server serves both the number and the definition, so the model reads the rule before it reports.

  4. evals/expected.py recomputes every expected value from the raw file with separate code. If the two disagree, the build fails.

Related MCP server: factory-floor-mcp

What is in here

Path

What it does

semantic/metrics.yaml

Metric definitions and written rules for two subjects: production (by site) and dealer sales (by region)

semantic/roles.yaml

Which sites and regions each role may read, and who may request actions

src/field_analytics/metrics.py

The metric SQL (DuckDB), with every model supplied argument validated

src/field_analytics/access.py

Access by role, applied to every query

src/field_analytics/server.py

The MCP server: four tools over stdio

src/field_analytics/actions.py

Actions create a pending request. A person approves it

src/field_analytics/assistant.py

The tool loop that asks Claude, shared by the app and the evaluation set

src/field_analytics/usage.py

The usage log: what people ask for and what gets refused

app/app.py, app.yaml

The Streamlit screen, ready to deploy as a Databricks App

skills/

Two Agent Skills: how Claude should answer on production and on dealer sales, and what it must never do

evals/

The evaluation set, the independent expected values, and the grader

databricks/build_gold_production.py

The same rules in PySpark, writing a Delta table on Databricks

tests/

Unit tests, access tests, and a parity test between PySpark and the metric layer

CLAUDE.md

The project instructions Claude Code reads when I work here

Run it

python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"

pytest                        # 40 tests (the Spark parity test is skipped without PySpark)
python evals/run_evals.py     # 12 evaluation cases, graded against the tools

The screen

pip install -e ".[dev,app]"
streamlit run app/app.py

Pick a role in the sidebar. Without an API key the screen runs with no model: a few selectors call the same tools Claude would, and every result shows what the number covers and how the metric is defined. Set ANTHROPIC_API_KEY and the selectors give way to a question box.

To run it as a Databricks App, create an app in your workspace from this repository. app.yaml starts the screen and requirements.txt lists what Databricks needs to install. To turn on the question box there, add the API key as a secret resource on the app and uncomment the two lines in app.yaml.

What people ask

Every query is logged with the role, the metric, the grouping, and whether it was refused. The person and the rows are never logged.

python scripts/top_questions.py

The same summary is the second tab of the screen. I read it as a backlog: the view people request most is the next dashboard worth building, and a refusal that keeps repeating is either missing access or a question nobody has defined yet.

Claude through MCP

To ask questions in plain language, connect the server to Claude Desktop or Claude Code:

{
  "mcpServers": {
    "field-analytics": {
      "command": "field-analytics-mcp",
      "env": { "FIELD_ANALYTICS_ROLE": "north_supervisor" }
    }
  }
}

Then try "What was output per hour by shift in September?" As north_supervisor you get the north site only, and the answer says so. Ask about the river site and the tool refuses.

To grade Claude's own answers instead of the tools:

pip install -e ".[app]"
export ANTHROPIC_API_KEY=...
python evals/run_evals.py --mode claude

To build the gold table with PySpark locally (needs Java):

pip install -e ".[spark]"
python databricks/build_gold_production.py

On Databricks, import the same file as a notebook. It writes main.field.gold_production as a Delta table.

Decisions I would defend in a review

The model never picks its own role. The role is set by whoever starts the server or the session for a person. Every tool call is checked against it, and a request for another site or region is refused out loud, not returned empty. An empty answer looks like "no production", which is worse than a refusal.

A new subject is a definition, a query, and an access scope. Dealer sales went in as a block in metrics.yaml, one base query, and a regions list on the roles. The server, the skills, the screen, and the grader did not need a second code path.

Model input is untrusted input. Metric and dimension names are checked against a fixed list before any SQL exists. Dates and sites are bound as parameters. There is a test that sends site; drop table x as a dimension.

Expected values are computed, not typed. A typed expected value goes stale the day the data changes. Here the grader recomputes each one from the raw file with code that shares nothing with the metric layer.

Actions wait for a person. The assistant can ask for a maintenance window. That writes a pending request and stops. scripts/approve.py is the human step.

Refusals and bad arguments come back as answers. The model gets {"ok": false, "error": "..."} and can tell the person what happened, instead of the server crashing mid conversation.

What I would add for production

This is a sample, so a few things are simpler than what I run at work:

  • The role would come from the signed in user, which Databricks Apps passes to the app, not from a selector or an environment variable.

  • The metric layer would read the Delta gold table through a SQL warehouse, not a CSV through DuckDB.

  • The usage log would be a Delta table, and the evaluation set would grow from the questions in it.

How this was built

I built it with Claude Code, the way I work every day: I write the definitions and the review rules, Claude Code writes much of the code and tests with me, and I read every line before it is committed. CLAUDE.md is the instruction file from that process.

License

MIT

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      F
      maintenance
      Enables agents and Claude Desktop to query a single canonical source for multifamily marketing metrics, with 22 tools for retrieving metric definitions and values from a synthetic portfolio database.
      MIT
    • A
      license
      A
      quality
      C
      maintenance
      Provides read-only access to manufacturing plant data from Excel or SAP S/4HANA, enabling natural-language queries for OEE, downtime Pareto, late work orders, scrap, and inventory alerts. It enforces data classification, masking, row limits, and audit logging for safe Claude/MCP client access.
      10
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables Claude to answer plain-English metric questions by selecting validated metrics, dimensions, filters, and dates from a MetricForge semantic layer, ensuring answers use only controlled definitions and never model-written SQL.
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Gives Claude governed access to a curated registry of go-to-market KPIs, with definitions, sources, and formulas, so it can draft a leadership business review from approved numbers rather than pasted-in data. It also validates every figure in the draft against its source KPI and flags anything unsupported before the review or 5-slide deck reaches leadership.
      MIT