Metrics MCP Server
# Metrics MCP Server
**Let an AI assistant answer "what was our NRR last quarter?" without giving it the keys to
the database.** A [Model Context Protocol](https://modelcontextprotocol.io) server built around
least privilege: allow-listed tools, validated parameters, a read-only role and an audit log.
[](https://github.com/Itsraza099/metrics-mcp-server/actions/workflows/ci.yml)


<!-- Add here once built: screenshot of an assistant answering a revenue question through this server. -->
| | |
|---|---|
| **Problem** | Giving an AI assistant SQL access is convenient and dangerous: it can read anything, run anything, and can be manipulated by text it reads. |
| **Solution** | Expose a few narrow, validated tools instead of SQL, over a database role that physically can't write. |
| **Shows** | MCP servers, tools and resources, least privilege, read-only roles, prompt-injection risk, audit logging, parameter validation |
| **Stack** | Python · MCP SDK · Pydantic · Postgres · psycopg |
## Security model
```mermaid
flowchart LR
A[AI assistant] -->|tool call| V[Validate params<br/>allow-list + ranges]
V -->|rejected| LOG[(Audit log)]
V -->|ok| Q[Parameterised SQL]
Q -->|metrics_reader role<br/>read-only, 5s timeout| M[(marts only)]
Q --> LOG
```
| Tool / resource | What it does |
|---|---|
| `list_metrics` | Lists available metrics with definitions |
| `get_metric` | Monthly values for one metric over at most 36 months |
| `metrics://definitions` | Plain-English metric definitions |
## Engineering highlights
- **No SQL tool, on purpose.** The assistant picks a metric from an allow-list; the column
name comes only from that list and dates travel as bound parameters.
([queries.py](src/metrics_mcp/queries.py))
- **Three layers of read-only.** A role with `select` on `marts` only, read-only transactions
by default, and a 5-second statement timeout. ([readonly_role.sql](sql/readonly_role.sql))
- **Every call is audited**, including rejected ones, so manipulation attempts leave a trail.
([audit.py](src/metrics_mcp/audit.py))
- **Prompt injection is treated as a data problem.** Tests feed back a row containing
"IGNORE PREVIOUS INSTRUCTIONS"; the server's job is to limit what any manipulated request can do.
- Why allow-listed tools beat a guarded SQL tool:
[decision 0001](docs/decisions/0001-allow-listed-tools-not-raw-sql.md).
## Proving it's safe
| Break it on purpose | What catches it | Status |
|---|---|---|
| Ask for metric `"mrr; drop table customers"` | Rejected before reaching the database | ✅ tested |
| Ask for 5 years of data | Rejected: 36-month limit | ✅ tested |
| Any rejected call | Still written to the audit log | ✅ tested |
| `delete from marts...` as `metrics_reader` | Permission denied by Postgres | planned |
| Injection text in a customer name | Assistant reports it as data | planned |
## Build progress
- [x] Tools, resource, parameter validation and audit log
- [x] Read-only role definition
- [ ] Connect to live marts from [saas-metrics-platform](https://github.com/Itsraza099/saas-metrics-platform)
- [ ] Demo with an MCP client
## How I worked
Each choice has a [decision record](docs/decisions/); the [glossary](docs/glossary.md) is my
own explanation of each concept. See all projects on [my profile](https://github.com/Itsraza099).
<details>
<summary><b>Run locally</b></summary>
Needs [uv](https://docs.astral.sh/uv/) and the Postgres database from saas-metrics-platform.
```bash
cp .env.example .env
make install
psql -f sql/readonly_role.sql # once, as the database owner
uv run metrics-mcp # stdio transport
make check
```
</details>
TDQS
Scored across 2 tools
list_metrics is purely a discovery/metadata operation while get_metric retrieves actual time-series values, so the two tools have clearly separate roles. There is no plausible way an agent would confuse which one to call for a given need.
Both names follow a clean verb_noun snake_case pattern (list_metrics, get_metric) with no stray conventions. The only variation is singular/plural on the noun, which matches the semantics of each operation.
Two tools is quite thin for a metrics server; there is discovery plus a single retrieval path and nothing else. It is a reasonable minimal surface but feels under-scoped rather than well-rounded.
The core loop of 'find a metric, fetch its values' is covered, but there are notable gaps: no finer-than-monthly granularity, no aggregation or summary/latest-value operation, no multi-metric comparison, and no way to browse dimensions or metadata beyond definitions. Agents can do the basics but will hit dead ends on common analytical requests.