# HANDOFF: Agentic Mass Spec Data Exchange Portal

**URL:** https://researchannounce-mcp.gnps2.org
**This file:** https://researchannounce-mcp.gnps2.org/HANDOFF.md

## What it is
A password-protected upload portal for mass spec data (mzML, mzXML, MGF, raw, zip, ...).
**Each instrument has its own password, and the password identifies the instrument.**
Each upload batch gets a UUID. The portal returns that UUID and the absolute path on disk, plus a ready-made message to send to an analysis agent running on the same host. The agent reads the files directly from disk.

The portal can only receive files. You cannot list or download files through it.

## On-disk layout
```
data/uploads/
  <instrument>/                  one folder per instrument
    <batch_uuid>/                one folder per upload batch
      manifest.json              what is in the batch (see below)
      <file_uuid>.<ext>          each file, renamed to a unique UUID; the extension is kept (.mzML, .mzML.gz, .d.zip, ...)
```

`manifest.json`:
```json
{
  "batch_id": "d7abea2e-9091-4634-8bb5-76ca8bdf4a62",
  "instrument": "orbitrap-1",
  "created_at": "2026-11-24T18:44:02+00:00",
  "note": "free text from the uploader",
  "files": [
    {
      "file_uuid": "e38cda48-...",
      "stored_filename": "e38cda48-....mzML.gz",
      "original_filename": "QC run 1.mzML.gz",
      "path": "/abs/host/path/.../e38cda48-....mzML.gz",
      "size": 115343360,
      "sha256": "e8c3...",
      "uploaded_at": "2026-11-24T18:44:04+00:00"
    }
  ]
}
```

## If you are the analysis agent and were sent a message
The message looks like this:
```
New mass spec upload from instrument 'orbitrap-1'.
Batch UUID: d7abea2e-9091-4634-8bb5-76ca8bdf4a62
Folder: /data/.../data/uploads/orbitrap-1/d7abea2e-9091-4634-8bb5-76ca8bdf4a62
Manifest: /data/.../manifest.json
Files (2):
  e38cda48-....mzML.gz  (original: QC run 1.mzML.gz, 115343360 bytes)
  ...
Note: QC test
```
- Read `manifest.json` first. It maps each UUID file to its original name, size and sha256, and records which instrument it came from.
- Every file listed in the manifest is complete. A file is only added after all of its chunks have arrived.
- `sha256` is computed in the background after upload. It can be `null` for a few seconds on very large files; re-read the manifest if you need it.
- Treat the folder as read-only input. Copy it somewhere else if you need to work on the files.

## Uploading from a browser
Open https://researchannounce-mcp.gnps2.org, enter the instrument's password, add an optional note, choose files, and click Upload. When it finishes, the page shows the batch UUID and the message for the analysis agent, with a copy button.

## Uploading from code (agents)
Use the password for your instrument. Get it from whoever handed this off; it is never written in this file.
Send the header `X-Upload-Password: <password>` on every call.

| Step | Call | Body | Response |
|---|---|---|---|
| 0 | `POST /api/auth` (optional) | none | `{"instrument": "<name>"}`, to check which instrument the password belongs to |
| 1 | `POST /api/upload/init` | JSON `{"filename": "...", "size": <bytes>, "batch_id": "<optional>", "note": "<optional, first file only>"}` | `upload_id`, `batch_id` (the batch UUID), `instrument`, `chunk_size`, `total_chunks` |
| 2 | `PUT /api/upload/<upload_id>/<n>` for n = 0 .. total_chunks-1 | raw bytes `[n*chunk_size, (n+1)*chunk_size)` | `{"received": n}` |
| 3 | `POST /api/upload/<upload_id>/complete` | none | `batch_id`, `batch_path`, `manifest_path`, `file` (this file's manifest entry), `agent_message` (covers every file in the batch so far) |

Rules:
- Every chunk except the last must be exactly `chunk_size` bytes (currently 50 MB). A chunk of the wrong size is rejected with 400.
- Chunks can be sent in any order or in parallel. Retrying a chunk is safe.
- To put several files in one batch, pass the `batch_id` from the first file's response into each later `init`. You can only add to batches that belong to your own instrument.
- After the last file, send the `agent_message` from the last `complete` response to the analysis agent.
- Errors come back as JSON `{"error": "..."}`. 401 means a wrong password. 429 means too many wrong passwords from your IP; wait 15 minutes. 409 on `complete` lists the missing chunks.
- An upload left unfinished for 48 hours is deleted.

### Fastest: the ready-made uploader (Python 3 standard library, no installs)
```bash
curl -fsSL https://researchannounce-mcp.gnps2.org/upload.py -o upload.py
UPLOAD_PASSWORD=<instrument password> python3 upload.py --note "QC run, plate 3" a.mzML b.mzML
```
It prints the message to send to the analysis agent. Add `--json` to get the full JSON result instead.
Progress goes to stderr and the result goes to stdout, so `MSG=$(python3 upload.py ...)` works.
It retries failed chunks and sends its own User-Agent header, because Cloudflare often blocks Python's default one.
Set `UPLOAD_URL=http://10.10.14.56:8105` to skip Cloudflare when you are on the lab network.

If you write your own client, follow the API table above and set a `User-Agent` header.

## Operations (for whoever maintains the portal)
- **Code:** `/data/researchdata-botnet/ming/SourceCode/AgenticMassSpecDataExchangePortal` on host `ming-agent-disposable`.
- **Start or rebuild:** `make server-compose`. **Logs:** `make logs`. **Stop:** `make down`.
- **Config:** `.env`, which is gitignored.
  - `INSTRUMENT_PASSWORDS=name:password,name:password`. Names use a-z0-9_-, and each password is unique and at least 12 characters.
  - To add an instrument, run `make add-instrument NAME=orbitrap-1`. It prints the new password. Then run `make server-compose`.
  - To rotate or remove a password, edit the line and run `make server-compose`.
  - Also: `PORT=8105`, `CHUNK_SIZE_MB=50`, `MAX_FILE_SIZE_GB=200`, and optionally `HOST_DATA_DIR`.
- **Storage:** `./data/uploads/<instrument>/<batch_uuid>/`. Partial uploads sit in `./data/uploads/.partial/`. Nothing deletes finished uploads; clean them up by hand.
- **Network:** the container listens only on `127.0.0.1:8105` and the LAN address `10.10.14.56:8105` (set `LAN_IP` in `.env` to change it). The public hostname comes from a cloudflared tunnel ingress rule:
  ```yaml
  ingress:
    - hostname: researchannounce-mcp.gnps2.org
      service: http://localhost:8105   # or http://10.10.14.56:8105 if cloudflared runs on another host
    - service: http_status:404
  ```
  Keep chunks under Cloudflare's 100 MB request limit (`CHUNK_SIZE_MB` ≤ 90).
- **Security:**
  - The portal has no read endpoints. Stored files are named by UUID, so user-supplied names never touch the filesystem.
  - An instrument cannot see or add to another instrument's batches or uploads.
  - The container runs as a non-root user with a read-only root filesystem, all extra permissions dropped, and no-new-privileges.
  - After 10 wrong passwords in 15 minutes from one IP (taken from `CF-Connecting-IP`), that IP is locked out. After 200 wrong passwords in 15 minutes from all IPs combined, all logins are refused until the window resets or the container restarts.
  - Pages are served with a strict CSP.
  - Anyone with an instrument password can fill the disk, so share passwords only with trusted people and agents.
