Metadata-Version: 2.5
Name: nextpyp-mcp
Version: 0.1.0
Summary: MCP server for nextPYP: let AI assistants check, change, and run your cryo-EM/ET sessions and projects
Project-URL: Homepage, https://nextpyp.app
Project-URL: Source, https://gitlab.cs.duke.edu/nextpyp/nextpyp-mcp
Project-URL: Mirror, https://github.com/nextpyp/nextpyp-mcp
License: BSD-3-Clause
License-File: LICENSE
Keywords: ai-assistant,cryo-em,cryo-et,mcp,nextpyp
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: >=3.10
Requires-Dist: anyio>=4.5
Requires-Dist: mcp<2,>=1.10
Requires-Dist: nextpyp-client<3,>=2.2
Requires-Dist: requests>=2.31
Requires-Dist: toml>=0.10.2
Description-Content-Type: text/markdown

# nextpyp-mcp

An [MCP](https://modelcontextprotocol.io) server for [nextPYP](https://nextpyp.app). It lets an AI assistant
(Cursor, Claude Desktop, Claude Code, …) work with your nextPYP sessions and projects: check on them, read
logs, change parameters, add and run blocks, and stop or clean up work when you confirm.

It talks to nextPYP through [nextpyp-client](https://pypi.org/project/nextpyp-client/), using an
app token that you approve on the nextPYP website and can revoke at any time.

## Requirements

- A nextPYP website with **API v2.2.0 or later**. That version adds the cluster-job log and project-block
  services this server uses. The server checks the version and explains what's wrong if it doesn't match.
- [uv](https://docs.astral.sh/uv/) (recommended), or Python 3.10+ and pip.

## Install

Nothing to install by hand with uv: your assistant starts the server with `uvx nextpyp-mcp` (see
[Connect your assistant](#connect-your-assistant)), which downloads it on first use. To check it works:

```bash
uvx nextpyp-mcp --version
```

Or install it with pip: `pip install nextpyp-mcp`, then use `nextpyp-mcp` as the command below.

## Get an app token

Ask nextPYP for a token with the permissions you want, approve the request on the website (your account
page), and copy the token it shows you.

```bash
uvx --from nextpyp-mcp python - <<'EOF'
from nextpyp.client import Client
client = Client("https://nextpyp.example.org", None)
request = client.services.apps.request_token("your-user-id", "nextpyp-mcp", [
    # read
    "project_list", "project_read", "session_list", "session_read", "group_list", "cluster_job_read",
    # change parameters, add blocks, create sessions
    "project_write", "session_write", "session_create",
    # run, start, stop, cancel
    "project_control", "session_control",
    # delete and wipe (leave these out if you don't want the assistant to have them)
    "project_delete", "session_delete",
])
print("approve request", request.request_id, "on the website")
EOF
```

Leave out any permission you don't want to grant; the tools that need it will report that it's missing.
**Treat the token like a password.** It acts as you. Revoke it on your account page if it leaks.

## Connect your assistant

Add this to your MCP host's configuration (eg Cursor's `.cursor/mcp.json`, or Claude Desktop's
`claude_desktop_config.json`), with your own URL, user id, and token:

```json
{
  "mcpServers": {
    "nextpyp": {
      "command": "uvx",
      "args": ["nextpyp-mcp"],
      "env": {
        "NEXTPYP_URL": "https://nextpyp.example.org",
        "NEXTPYP_USER_ID": "your-user-id",
        "NEXTPYP_APP_TOKEN": "paste-the-approved-token-here"
      }
    }
  }
}
```

For Claude Code:

```bash
claude mcp add nextpyp \
  -e NEXTPYP_URL=https://nextpyp.example.org -e NEXTPYP_USER_ID=your-user-id -e NEXTPYP_APP_TOKEN=… \
  -- uvx nextpyp-mcp
```

`uvx` uses the newest release; pin one with `nextpyp-mcp==0.1.0` in place of `nextpyp-mcp`. If you
installed with pip, use `"command": "nextpyp-mcp"` and no `args` (the full path to it if the host can't
find it).

Optional: `NEXTPYP_TIMEOUT_S` (default 60) is how long to wait for each request to nextPYP.

**If your website is behind single sign-on** (eg Shibboleth in front of Apache), requests from this server
get the login page instead of nextPYP. Point `NEXTPYP_URL` at nextPYP's own port behind the proxy instead,
eg `http://nextpyp-host.my.org:8080`, from a machine that can reach it. If you're not sure, this should print
JSON with an `apiVersion`, not HTML:

```bash
curl -s -X POST -H 'Content-Type: application/json' \
  --data '{"id":1,"method":"/kv/apps/version","params":[]}' "$NEXTPYP_URL/kv/apps/version"
```

## Using it from Cursor

1. Put the configuration above in your **global** `~/.cursor/mcp.json`, not a project's `.cursor/mcp.json`,
   so the token never ends up in a repository.
2. Open **Cursor Settings → MCP** (or **Tools & Integrations**). `nextpyp` should be listed with a green
   dot and its tools. If it shows an error, run `uvx nextpyp-mcp --version` in a terminal to see the
   message.
3. In the chat, use **Agent** mode and ask in plain language. Cursor picks the tools and asks before
   running each one, unless you allow them automatically.

Some things to ask, and what happens:

**What's going on?**

> Which of my nextPYP projects are running right now? For each, which blocks are still going and which failed?

The assistant calls `list_running_projects`, then `list_runs` for each project, and summarizes the
block and cluster job statuses.

**Why did it fail?**

> The last run of the Particle picking block in project test-frames failed. Show me why.

The assistant finds the block with `list_jobs`, the failed cluster job with `list_runs`, the failing
micrographs or tilt-series with `list_cluster_job_series` (errors only), and reads their logs with
`get_cluster_job_log`. Logs are trimmed to their last 20,000 characters, where errors usually are.

**Is the data any good?**

> In session EMPIAR-11306, how fast is data coming in, and does the CTF fit of TS_01 look reasonable?

The assistant uses `get_session_speeds` and `get_session_ctf`, and reads the processing log for
that tilt-series if something looks off.

**Build part of a pipeline**

> In project test-mcp, add a pre-processing block after the raw data block. Use the same settings as
> block 2 in project test-frames, but with ctf_min_res = 25. Then run it.

The assistant reads block 2's parameters with `get_job`, looks up anything it's unsure of with
`get_block_args_schema`, adds and connects the block with `add_job`, and runs it with `run_jobs`. A block
that has never run starts right away.

**Change parameters and rerun**

> Lower the particle radius in block 6 of test-frames to 50 Å and rerun it.

`edit_job` changes only that parameter and keeps the rest. Because block 6 already ran, `run_jobs` first
reports which outputs would be replaced and which downstream blocks would become stale, and the
assistant asks you before rerunning.

**Stop or clean up**

> Cancel the run that's going in project MissAlign-validation-10164.

> Delete the blocks I added to test-mcp today.

Cancelling, stopping, clearing, wiping, and deleting always need your go-ahead: the tools refuse until
the assistant calls them again with `confirm=true`, so it has to check with you first.

Tips:

- Name projects, sessions, and blocks as you see them on the website; the assistant looks up the ids.
- To work in someone else's project that's shared with you, say whose it is (their user id).
- Nothing updates live. Ask again ("how is it doing now?") to get fresh status.

## Tools

| Area | Read | Change | Run / stop | Delete |
|---|---|---|---|---|
| Sessions | `list_groups`, `list_sessions`, `get_session`, `list_running_sessions`, `is_session_running`, `get_session_speeds`, `get_session_log_index`, `get_session_cluster_jobs`, `get_session_data_log`, `get_session_ctf`, `get_session_motion`, `list_session_filters`, `get_session_filter` | `create_session`, `edit_session` | `start_session`, `restart_session`, `stop_session`\*, `cancel_session`\* | `clear_session`\*, `delete_session`\* |
| Projects | `list_projects`, `get_project`, `list_running_projects`, `get_project_stats`, `list_block_types`, `get_block_args_schema`, `list_jobs`, `get_job`, `get_job_stats`, `get_job_data_log`, `get_job_ctf`, `get_job_motion`, `list_runs`, `new_job_values` | `add_job`, `edit_job`, `rename_job` | `run_jobs`\*, `cancel_run`\*, `cancel_job`\* | `delete_jobs`\*, `wipe_job`\* |
| Cluster jobs | `get_cluster_job_log`, `list_cluster_job_elements`, `list_cluster_job_series`, `get_cluster_job_waiting_reason` | | | |

\* Needs `confirm=true`. Without it, the tool explains what it would do and asks the assistant to check with
you first. `run_jobs` runs right away when it would only run blocks that haven't run yet. Rerunning a block
replaces its outputs, so that needs confirmation, and the tool lists the downstream blocks that would
become stale.

`clear_session` on the pre-processing daemon (`Pypd`) erases every pre-processed micrograph or tilt-series,
CTF and motion results, saved filters, and particles; with `delete_session_files` it also deletes the
processed files and logs on disk, keeping only the raw data.

Notes:

- **Edits change only what you name.** nextPYP replaces all of a block's or session's parameters on every
  edit, so `edit_job` and `edit_session` read the newest values first and merge your changes into them.
- **Parameters** are flat keys like `ctf_min_res`. `get_block_args_schema` (with `search`) finds them.
- **Long output is trimmed.** Logs keep their last 20,000 characters (adjustable per call); long lists
  say how many items were left out.
- **Shared projects:** project tools take an optional `owner_id` for projects other users shared with you.

Not yet supported: live (websocket) updates, so ask the assistant to check again instead; saving or deleting
filters; session data export; creating, deleting, or sharing projects.

## Source code

| | GitHub | GitLab (where development happens) |
|---|---|---|
| This server | https://github.com/nextpyp/nextpyp-mcp | https://gitlab.cs.duke.edu/nextpyp/nextpyp-mcp |
| [nextpyp-client](https://pypi.org/project/nextpyp-client/) | https://github.com/nextpyp/nextpyp-client | https://gitlab.cs.duke.edu/nextpyp/nextpyp-client |

## Development

Check out both projects side by side; `uv sync` uses the client checkout next to this one
(`[tool.uv.sources]` in `pyproject.toml`), so you can change both at once:

```bash
git clone git@gitlab.cs.duke.edu:nextpyp/nextpyp-client.git
git clone git@gitlab.cs.duke.edu:nextpyp/nextpyp-mcp.git
cd nextpyp-mcp
uv sync
uv run pytest
```

To point your assistant at this checkout instead of the release, use
`"command": "uv", "args": ["run", "--directory", "/path/to/nextpyp-mcp", "nextpyp-mcp"]`.

Tests use a fake nextPYP client, so they don't need a website. When nextPYP's exported API changes,
`scripts/release-client.sh` in the nextPYP repo regenerates the client; release the client first, then
raise the `nextpyp-client` requirement in `pyproject.toml` if this server needs the new API.

### Releasing

Bump `version` in `pyproject.toml`, commit, and push a tag `v<version>`. The tag's GitLab pipeline tests and
builds the package; run its `publish-testpypi` job, check the TestPyPI install, then run `publish-pypi`.
