Skip to main content
Glama
README.md
# sar-mcp

An MCP server that lets an LLM client (Claude Code, Claude Desktop, etc.) answer structure-activity
questions over **your own** assay data, with every number traceable to a compound and an assay.

- **Local and offline at query time.** Runs over stdio on your machine; compound structures never leave it.
- **Three tools:** substructure search, matched molecular pairs, compound detail.
- **No bare numbers.** Every value is returned with its `compound_id` and `assay_id`.
- **Same-assay only.** Matched-pair deltas are never computed across assays (inter-assay noise ~0.3-0.5 log).
- **Cached.** Results are stored on disk keyed by (tool, args, data fingerprint, code version).

Validation results and measured limits are in [REPORT.md](REPORT.md).

## Demo
Questions asked in the Claude Code panel (VS Code) against the ChEMBL VEGFR2 demo data. The model calls the
tools; every number comes back with its compound and assay id.

![Substructure search: 1,568 records contain the quinazoline pattern](docs/demo-substructure.png)

![Matched pairs: top indole pair, CHEMBL201511 vs CHEMBL521201 in assay CHEMBL957977, delta -3.64](docs/demo-matched-pairs.png)

## Setup

```bash
uv sync                      # creates .venv with everything needed
```

### Load data (one parquet file per target)

**Your own data (CSV):**
```bash
uv run data/import_csv.py --csv my.csv --out data/mytarget.parquet \
  --id-col compound --smiles-col smiles --assay-col assay_id \
  --type-col endpoint --value-col value --units-col units \
  [--relation-col relation] [--source-col notebook] [--target-col target]
```
Keeps IC50/Ki with exact (`=`) values, converts to nM, computes pActivity = 9 - log10(nM), strips salts and
canonicalizes structures, drops assays with a single compound, dedupes by `(smiles, assay_id)` with the
median, and prints how many rows were dropped and why. With `--target-col` it writes one parquet per target.

**Public ChEMBL data (network, one-time):**
```bash
uv run data/pull_chembl.py                      # default CHEMBL279 (VEGFR2)
uv run data/pull_chembl.py --target CHEMBL203
```
ChEMBL data is CC BY-SA 3.0: credit ChEMBL when you share it or anything derived from it.

### Dataset schema
`compound_id, smiles, assay_id, target_id, standard_type, standard_value (nM), standard_units, pactivity,
assay_type, source_ref, n_measurements`. See [schema.py](schema.py). One file = one target; mixed-target
files are rejected so activities from different targets can never be compared by accident.
`assay_id` must identify one protocol under fixed conditions, because pairs are only formed inside an assay.

## Tools

| Tool | Returns |
|---|---|
| `substructure_search(smarts, limit=50)` | `{n_total, results:[{compound_id, assay_id, smiles, pactivity, standard_type}]}` by pActivity desc |
| `matched_pairs(smarts_core, max_heavy_atom_delta=3)` | `{n_total, pairs:[{assay_id, a, b, transformation, context, delta_pactivity, heavy_atom_delta}]}` top 25 by abs delta |
| `compound_detail(compound_id)` | structure, InChIKey, all measurements (assay_id, assay type, raw value + units, pActivity, source_ref) |

Matched pairs: single acyclic-bond cut; the core SMARTS must lie wholly in the shared context; `delta = b - a`.
Ring changes and multi-cut transformations are not found (see REPORT.md).

## MCP client config

```json
{
  "mcpServers": {
    "sar": {
      "command": "uv",
      "args": ["run", "--directory", "/ABSOLUTE/PATH/TO/SAR-MCP", "server.py"],
      "env": {"SAR_DATA": "/ABSOLUTE/PATH/TO/SAR-MCP/data/mytarget.parquet"}
    }
  }
}
```
Claude Code: `claude mcp add sar -- uv run --directory /path/to/SAR-MCP server.py`. The VS Code extension
reads a project-level `.mcp.json` with the same block: copy `.mcp.json.example` to `.mcp.json` and fix the paths. Environment: `SAR_DATA` (parquet path),
`SAR_CACHE_DIR`, `SAR_WORKERS` (processes used for large matched-pair queries; default = CPU count).

## Cache
`.cache/<tool>/<hash>.json`. The key includes a hash of the parquet and `CACHE_VERSION` in `cache.py`
(bump it when tool logic changes), so changed data or code never serves stale results. `rm -rf .cache` to reset.

## Tests and evals
```bash
uv run --extra dev pytest -q                    # 11 tests (pairs, units, schema, cache, importer round trip)
uv run evals/build_questions.py                 # regenerate 19 eval questions (ground truth from raw data)
uv run evals/run_evals.py --answers evals/answers.json    # grade answers offline, free
uv run --extra evals evals/run_evals.py --run --limit 3   # PAID: Claude answers via the tools, then grades
uv run bench/scale.py 10000 50000 100000        # speed / memory on synthetic data
```
Eval grading is deterministic string/number matching; unanswerable questions use a heuristic (decline
language, no invented decimals), so read printed failures. Details and caveats: REPORT.md.

## License
Code: MIT (see `LICENSE`). ChEMBL data is not included and is CC BY-SA 3.0.