Metadata-Version: 2.4
Name: elabftw-toolkit
Version: 0.1.4
Summary: A Python toolkit for automating and standardizing eLabFTW API workflows
Author-email: Takahiro Matsumoto <matumot@jasri.jp>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/jasri-jp/elabftw-toolkit
Project-URL: Documentation, https://jasri-jp.github.io/elabftw-toolkit/
Project-URL: Repository, https://github.com/jasri-jp/elabftw-toolkit.git
Project-URL: Issues, https://github.com/jasri-jp/elabftw-toolkit/issues
Keywords: eLabFTW,ELN,electronic lab notebook,MaiML,synchrotron,research data management,MCP
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Natural Language :: English
Classifier: Natural Language :: Japanese
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: elabapi-python
Requires-Dist: pandas
Requires-Dist: python-dotenv
Requires-Dist: requests
Requires-Dist: beautifulsoup4
Requires-Dist: pillow
Requires-Dist: urllib3
Requires-Dist: ipython
Provides-Extra: maiml
Requires-Dist: pymaiml<2.0,>=1.0; extra == "maiml"
Provides-Extra: mcp
Requires-Dist: mcp>=1.0; extra == "mcp"
Provides-Extra: benten
Requires-Dist: pyyaml; extra == "benten"
Requires-Dist: openpyxl; extra == "benten"
Dynamic: license-file

# elabftw-toolkit

[![PyPI](https://img.shields.io/pypi/v/elabftw-toolkit)](https://pypi.org/project/elabftw-toolkit/) [![Python](https://img.shields.io/pypi/pyversions/elabftw-toolkit)](https://pypi.org/project/elabftw-toolkit/) [![Docs](https://img.shields.io/badge/docs-GitHub%20Pages-blue)](https://jasri-jp.github.io/elabftw-toolkit/) [![License](https://img.shields.io/badge/license-Apache--2.0-green)](https://github.com/jasri-jp/elabftw-toolkit/blob/main/LICENSE)

An unofficial Python toolkit for working with eLabFTW Experiments and Resources (Database items).
Common tasks like creating entries from templates, reading and writing fields, attaching files, linking, setting permissions, and locking take just a few lines of code.

**📖 Documentation (user guide, cheat sheet, recipes): https://jasri-jp.github.io/elabftw-toolkit/en/**

[日本語](https://github.com/jasri-jp/elabftw-toolkit/blob/main/README.md) / [User guide](https://jasri-jp.github.io/elabftw-toolkit/en/manual/01) / [Documentation site](https://jasri-jp.github.io/elabftw-toolkit/en/)

> This project is independent of eLabFTW and its official SDKs. Behavior can vary with your eLabFTW version and settings, so try it on test entries before using it in production.

## What it does

- **Create**: create Experiments and Resources from templates (body, fields, Steps, and permissions carry over)
- **Read and write**: use field values like a dict, for example `entry.fields["purity"]`
- **Find**: get lists as a table (DataFrame) or every entry as JSON, search by condition, or look up by title
- **Attach**: attach files (skipping identical ones), download in bulk, add images to the body, and export as PDF, ELN, or JSON
- **Link and manage**: links between entries, tags, permissions, locks and timestamps, body tables, and Steps
- **Write safely**: try changes without writing (dry run), validate with a schema, prevent duplicates, and record who changed what
- **Export**: export to MaiML (JIS K 0200) (requires an extra install)

## Installation

Python 3.10 or later is required. Tested with eLabFTW 6.0.x (6.0.6), using REST API v2.

```bash
python -m pip install elabftw-toolkit            # basic
python -m pip install "elabftw-toolkit[maiml]"   # also for MaiML export
python -m pip install "elabftw-toolkit[mcp]"     # also for the MCP server (Claude Desktop and others)
python -m pip install "elabftw-toolkit[benten]"  # also for BENTEN metadata (YAML, Excel)
```

For development (editing the source while using it), clone and use an editable install:

```bash
git clone https://github.com/jasri-jp/elabftw-toolkit.git
cd elabftw-toolkit
python -m pip install -e ".[maiml,mcp,benten]"
```

## Configuration

Set your eLabFTW server and API key as environment variables.

```bash
ELABFTW_HOST_URL="https://your-elabftw.example.jp"
ELABFTW_API_KEY="your-api-key"
```

- To keep your API key in a file, use `ELABFTW_KEY_FILE="/path/to/api_key.txt"` instead of `ELABFTW_API_KEY`.
- `.env` files aren't loaded automatically. If you use one, call `from dotenv import load_dotenv; load_dotenv()` first.
- For passing settings as arguments and SSL options, see [1. Getting started](https://jasri-jp.github.io/elabftw-toolkit/en/manual/01).

## Try it in five minutes

```python
import elabftw_toolkit

elab = elabftw_toolkit.client("your-team-name")

# Find a sample by title and open it
sample = elab.items.find("TEST_SAMPLE", category_title="Sample")
entry = elab.items.entry(sample.id)

# Read and write fields
print(entry.fields["purity"])
entry.fields.update({"purity": "99.9%", "form": "powder"})

# Edit a locked entry by unlocking it temporarily (the lock is restored afterwards)
with entry.unlocked():
    entry.fields["purity"] = "99.99%"

# Attach a file (skipped if the same file is already there)
entry.files.upload("coa.pdf")

# Create an Experiment from a template and link it to the sample
exp_id = elab.experiments.init(
    title="2026T0001-XAFS-TEST_SAMPLE",
    template_name="Measurement test",
    category_id=elab.experiments.categories.id("Measurement"),
)
elab.experiments.entry(exp_id).set_item_field_and_link(
    field_name="sample_id", item_title="TEST_SAMPLE", item_category_title="Sample",
)
```

Names like `"Sample"`, `"Measurement"`, and `"Measurement test"` are examples. Use the names set up in your eLabFTW.

## Command line

| Command | What it does | More |
| --- | --- | --- |
| `elabftw-mcp` | Start the MCP server (needs `[mcp]`) | [16. MCP server](https://jasri-jp.github.io/elabftw-toolkit/en/manual/16) |
| `python -m elabftw_toolkit.maiml check <folders…>` | Check MaiML files and the links between them (needs `[maiml]`) | [10. MaiML export](https://jasri-jp.github.io/elabftw-toolkit/en/manual/10) |
| `python -m elabftw_toolkit.maiml graph <folders…>` | Draw the links between MaiML files in several places as a Mermaid diagram | [10. MaiML export](https://jasri-jp.github.io/elabftw-toolkit/en/manual/10) |
| `python -m elabftw_toolkit.benten_schema check <folder>` | Check BENTEN metadata (`metadata.all.yml`) against the schema (needs `[benten]`) | [16. MCP server](https://jasri-jp.github.io/elabftw-toolkit/en/manual/16) |

Every command lists its arguments with `--help`.

## MCP server (use it from Claude and other AI assistants)

Register `elabftw-mcp` in Claude Desktop or another MCP client, and the AI can read and write eLabFTW.

| Kind | Tools |
| --- | --- |
| Read (10) | `list_categories`, `list_entries`, `get_entry`, `load_schema`, `validate_entry`, `get_structured_metadata`, `validate_structured_metadata`, `read_maiml`, `check_maiml`, `maiml_graph` |
| Write (10) | `create_entry`, `update_fields`, `set_body`, `add_tags`, `update_status`, `add_link`, `upload_file`, `set_structured_metadata`, `lock_entry`, `unlock_entry` |

- With `ELABFTW_READ_ONLY=1`, all write tools are disabled (previews with `dry_run=true` still work).
- Write tools other than `lock_entry` / `unlock_entry` return only "old value → new value" with `dry_run=true`, and record the reason as a comment on the entry with `reason="…"`.
- For setup and each tool's arguments, see [16. MCP server](https://jasri-jp.github.io/elabftw-toolkit/en/manual/16).

## User guide

| If you want to | Read |
| --- | --- |
| Get started | [1. Getting started](https://jasri-jp.github.io/elabftw-toolkit/en/manual/01), [12. Troubleshooting](https://jasri-jp.github.io/elabftw-toolkit/en/manual/12) |
| Read and find entries | [2. Entries and fields](https://jasri-jp.github.io/elabftw-toolkit/en/manual/02), [3. Listing and search](https://jasri-jp.github.io/elabftw-toolkit/en/manual/03) |
| Create, attach, and link | [4. Creating entries and templates](https://jasri-jp.github.io/elabftw-toolkit/en/manual/04), [5. Files and images](https://jasri-jp.github.io/elabftw-toolkit/en/manual/05), [6. Links and tags](https://jasri-jp.github.io/elabftw-toolkit/en/manual/06) |
| Permissions, locks, and body tables | [7. Permissions](https://jasri-jp.github.io/elabftw-toolkit/en/manual/07), [8. Lock and timestamp](https://jasri-jp.github.io/elabftw-toolkit/en/manual/08), [9. HTML tables and steps](https://jasri-jp.github.io/elabftw-toolkit/en/manual/09) |
| Work in bulk and export | [11. Recipes](https://jasri-jp.github.io/elabftw-toolkit/en/manual/11), [10. MaiML export](https://jasri-jp.github.io/elabftw-toolkit/en/manual/10) |
| Write safely | [15. Validation and plans](https://jasri-jp.github.io/elabftw-toolkit/en/manual/15) |
| Look up functions or update old scripts | [14. API reference](https://jasri-jp.github.io/elabftw-toolkit/en/manual/14) |
| Contribute | [13. Testing and development](https://jasri-jp.github.io/elabftw-toolkit/en/manual/13) |
| Use it from Claude or other AI assistants, work with AI | [16. MCP server](https://jasri-jp.github.io/elabftw-toolkit/en/manual/16), [17. Working with AI](https://jasri-jp.github.io/elabftw-toolkit/en/manual/17) |

## Documentation site

Published at **https://jasri-jp.github.io/elabftw-toolkit/en/** (rebuilt by GitHub Actions on every push to `main`; `.github/workflows/docs.yml`).

The same guide can be read in a browser together with an introduction for new users, a cheat sheet, and recipes
([VitePress](https://vitepress.dev/); needs [Node.js](https://nodejs.org/)).

```bash
npm install
npm run dev      # import the guide and open http://localhost:5173
npm run build    # write HTML to docs/.vitepress/dist/
```

On Windows, **double-click `docs_preview.bat`**: it imports the guide and opens it in your browser (needs Node.js).
To work inside sync folders such as Google Drive, it copies the site to `%USERPROFILE%\.cache\elabftw-toolkit-docs` and runs it there
(the first run installs VitePress and takes a little longer). After editing the sources, double-click it again.

| Content | Location |
| --- | --- |
| User guide sources (Japanese, English) | `docs/ja`, `docs/en` (chapter order in `mkdocs.yml`) |
| Top page, first steps, cheat sheet, recipes | `docs/index.md`, `docs/guide/`, `docs/recipes/`, `docs/en/index.md`, `docs/en/guide/` |
| Figures and images | `docs/public/` |
| Site configuration | `docs/.vitepress/` |

`scripts/sync-manual.mjs` copies the sources to chapter-number URLs (`/manual/04`, `/en/manual/04`) and places the files in `examples/` for download (both are generated and not committed).
Inside a sync folder such as Google Drive, `node_modules` links cannot be created and `npm run dev` / `build` fail; use `docs_preview.bat` or copy the project outside the sync folder.

## Tests

```bash
python -m pip install -e ".[maiml]" pytest
pytest    # doesn't connect to eLabFTW
```

`tests/live/` contains scripts that write to a real server; `pytest` doesn't run them. See [13. Testing and development](https://jasri-jp.github.io/elabftw-toolkit/en/manual/13).

## License

Apache License 2.0 ([LICENSE](https://github.com/jasri-jp/elabftw-toolkit/blob/main/LICENSE))

This library is an independent implementation of a helper library for the eLabFTW REST API. It doesn't include, modify, or redistribute eLabFTW's server-side source code. It's meant to be used with `elabapi-python` (MIT License), the Python client for the eLabFTW API.

In designing this library and its usage examples, we referred to NAIST's eLabFTW-related repository [naist-eln/eln](https://github.com/naist-eln/eln), but this library doesn't copy its code as-is. Note that the `API/` directory of the NAIST repository contains a license file for the GNU Affero General Public License v3.0 (AGPL-3.0).

---

Copyright 2026 Japan Synchrotron Radiation Research Institute (JASRI) / Takahiro Matsumoto
