Metadata-Version: 2.5
Name: mcp-server-milan-datawarehouse
Version: 0.1.0
Summary: Read-only Snowflake MCP server with token-efficient TSV output
Project-URL: Homepage, https://github.com/Saideh-Heshmati/mcp-server-milan-datawarehouse
Project-URL: Issues, https://github.com/Saideh-Heshmati/mcp-server-milan-datawarehouse/issues
Author: Saideh-Heshmati
License: MIT
License-File: LICENSE
Keywords: claude,data-warehouse,llm,mcp,snowflake
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: mcp[cli]<2,>=1.9.0
Requires-Dist: snowflake-connector-python>=3.12.0
Provides-Extra: dev
Requires-Dist: mypy>=1.11; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Requires-Dist: tiktoken>=0.7; extra == 'dev'
Description-Content-Type: text/markdown

# mcp-server-milan-datawarehouse

A read-only [MCP](https://modelcontextprotocol.io) server for Snowflake. It lets an
LLM explore your warehouse schema and run `SELECT` queries — and nothing else.

Built for two things: **safety by default** and **low token cost**.

---

## Why another Snowflake MCP server

Most database MCP servers dump the whole schema into the tool descriptions. That
schema is then re-sent to the model on *every* request, whether or not a tool is
called. This one does the opposite:

| Decision | Effect |
|---|---|
| 4 tools, not 30 | A few hundred tokens of tool definitions per request, not thousands |
| Schema discovered on demand (`search_schema`, `describe_table`) | No schema in the prompt at all |
| TSV output instead of JSON | Keys are not repeated per row; typically 60–75% fewer output tokens |
| 100-row default, continue via `RESULT_SCAN` | The query is not re-run to page; less compute and fewer tokens |
| Static tool text | The prefix stays cacheable across turns |

Run `python scripts/measure_tokens.py` to see the current cost of `tools/list` on your
build. The budget is 1,200 tokens and `tests/test_tools.py` fails if it is exceeded.

---

## Install

```bash
uvx mcp-server-milan-datawarehouse
```

or

```bash
pipx install mcp-server-milan-datawarehouse
```

Requires Python 3.11+.

---

## Configure

Everything comes from environment variables. Copy `.env.example` and fill it in.

| Variable | Required | Purpose |
|---|---|---|
| `SNOWFLAKE_ACCOUNT` | yes | `<org>-<account>` |
| `SNOWFLAKE_USER` | yes\* | login name of the read-only service user |
| `SNOWFLAKE_PASSWORD` | \* | password, if not using key-pair |
| `SNOWFLAKE_PRIVATE_KEY_PATH` | \* | path to `rsa_key.p8` (preferred) |
| `SNOWFLAKE_PRIVATE_KEY_PASSPHRASE` | no | passphrase for the key |
| `SNOWFLAKE_CONNECTION_NAME` | \* | reuse an entry from `~/.snowflake/connections.toml` |
| `SNOWFLAKE_ROLE` | recommended | the read-only role |
| `SNOWFLAKE_WAREHOUSE` | recommended | warehouse to run on |
| `SNOWFLAKE_DATABASE` | recommended | default database |
| `SNOWFLAKE_SCHEMA` | no | default schema |
| `ALLOWED_SCHEMAS` | recommended | `DB.SCHEMA,DB.SCHEMA` allowlist |
| `MAX_ROWS_HARD_CAP` | no | ceiling for `max_rows` (default `500`) |
| `STATEMENT_TIMEOUT` | no | seconds, set on every session (default `60`) |
| `MCP_TRANSPORT` | no | `stdio` (default) or `streamable-http` |

\* exactly one auth method is required: key-pair, password, or connection name.

Key-pair is recommended: it survives MFA enforcement and cannot be replayed.

### Client config (Claude Desktop / Cursor)

```json
{
  "mcpServers": {
    "milan-dw": {
      "command": "uvx",
      "args": ["mcp-server-milan-datawarehouse"],
      "env": {
        "SNOWFLAKE_ACCOUNT": "myorg-myaccount",
        "SNOWFLAKE_USER": "DW_MCP_READONLY",
        "SNOWFLAKE_PRIVATE_KEY_PATH": "/Users/me/.snowflake/rsa_key.p8",
        "SNOWFLAKE_ROLE": "DW_MCP_READONLY",
        "SNOWFLAKE_WAREHOUSE": "COMPUTE_WH",
        "SNOWFLAKE_DATABASE": "ANALYTICS",
        "ALLOWED_SCHEMAS": "ANALYTICS.PUBLIC,ANALYTICS.MARTS"
      }
    }
  }
}
```

---

## Tools

| Tool | What it does |
|---|---|
| `search_schema(query, limit=15)` | Finds tables/columns by name or comment. One line per table. |
| `describe_table(table, sample_rows=0)` | Columns as TSV: name, type, nullable, comment. Optional ≤5 sample rows. |
| `run_query(sql, max_rows=100)` | Runs one read-only statement. Returns TSV. |
| `fetch_more(cursor_id, offset=0, max_rows=100)` | Pages a truncated result via `RESULT_SCAN`. |

### Output contract

One header line, then one row per line, tab-separated. `NULL` is an empty cell.
Dates are ISO-8601, decimals keep at most 6 places, cells over 200 characters are
truncated with `…`, columns that are entirely null are dropped. The executed SQL is
never echoed back. When a result is cut, the last line starts with `#` and carries
the `cursor_id` needed to continue.

```
order_id	city	amount
1001	Tehran	12500
1002	Karaj	8300.5
# truncated at 2 rows; fetch_more(cursor_id="01b2c3d4-...")
```

---

## Security model

Two layers, and only the second one is a real boundary.

**Layer 1 — the SQL guard** (`guard.py`). One statement only. Comments are stripped,
string literals and quoted identifiers are masked before keyword scanning, and the
statement must start with `SELECT`, `WITH`, `EXPLAIN`, `SHOW`, `DESCRIBE` or `DESC`.
Anything containing `INSERT`, `UPDATE`, `DELETE`, `MERGE`, `CREATE`, `DROP`, `ALTER`,
`TRUNCATE`, `GRANT`, `REVOKE`, `COPY`, `PUT`, `GET`, `CALL`, `USE`, `COMMIT` and
friends is rejected. Every session also sets `STATEMENT_TIMEOUT_IN_SECONDS` and a
`QUERY_TAG` so runaway queries and their origin are visible in `QUERY_HISTORY`.

**Layer 2 — the role.** This is what actually protects your warehouse. Use a role
that cannot write:

```sql
CREATE ROLE DW_MCP_READONLY;
GRANT USAGE ON WAREHOUSE COMPUTE_WH TO ROLE DW_MCP_READONLY;
GRANT USAGE ON DATABASE ANALYTICS TO ROLE DW_MCP_READONLY;
GRANT USAGE ON SCHEMA ANALYTICS.PUBLIC TO ROLE DW_MCP_READONLY;
GRANT SELECT ON ALL TABLES IN SCHEMA ANALYTICS.PUBLIC TO ROLE DW_MCP_READONLY;
GRANT SELECT ON FUTURE TABLES IN SCHEMA ANALYTICS.PUBLIC TO ROLE DW_MCP_READONLY;
GRANT ROLE DW_MCP_READONLY TO USER DW_MCP_READONLY;
```

The guard is a guard rail for mistakes, not a sandbox. If you run this server with
an account that can write, nothing here will stop it. Do not do that.

For the strongest posture, point the server at **secure views** instead of base
tables, and never at a schema containing PII.

---

## Limits

- Only objects created with unquoted (upper-case) identifiers are found; quoted
  lower-case identifiers are not supported.
- `INFORMATION_SCHEMA` is read per database, so `ALLOWED_SCHEMAS` must list every
  database you want to search.
- `fetch_more` works within Snowflake's result-retention window (24h by default).
- No write path, by design. There is no plan to add one.

## Development

```bash
git clone https://github.com/Saideh-Heshmati/mcp-server-milan-datawarehouse
cd mcp-server-milan-datawarehouse
pip install -e ".[dev]"
pytest
python scripts/measure_tokens.py
```

Tests need no Snowflake account — the connector is stubbed.

## License

MIT
