Skip to main content

PyNCBI

PyNCBI

PyPI Python CI License: MIT

Python tools, a command-line interface, and an MCP server for discovering NCBI studies, inspecting sample metadata, and downloading scientific data.

Documentation · Migration to 1.0 · Issues · Changelog

PyNCBI 1.0.0 provides a Python API, CLI and MCP server using only Python's standard library. Upgrading from an earlier version? Read the migration guide for the native data API and other breaking changes.

Install

pip install PyNCBI

For the development version, clone this repository and run pip install -e ".[dev,docs]". Python 3.10 remains a legacy compatibility target. CI also checks newer Python versions; see CONTRIBUTING.md for the release validation policy.

There are no third-party runtime dependencies. PyNCBI uses only the Python standard library. An NCBI API key is optional. Configure it with NCBI_API_KEY; use NCBI_EMAIL for your contact address.

Find a study, inspect it, then fetch the data

pyncbi search geo "fibromyalgia methylation" --organism "Homo sapiens"
pyncbi inspect GSE85506 --markdown
pyncbi samples GSE85506 -o samples.csv
pyncbi files GSE85506

# Review the download plan and sizes first.
pyncbi download GSE85506 --source supp --dry-run --json

# Download the matching samples' files with a transfer budget.
pyncbi download GSE85506 --where "group == 'case'" --pattern "*.idat.gz" --max-download-mb 500 -o ./fm_idats/

# Resolve a processed matrix and write its aligned sample sheet too.
pyncbi expression GSE152418 -o counts.csv

Sample metadata uses GEO's batch view rather than making a separate request for every sample. File previews read a bounded prefix. Actual latency depends on the study, server, network, rate limits, and cache state.

Python

import PyNCBI

with PyNCBI.Session(cache_dir="./pyncbi-cache", max_download_mb=500) as session:
    report = session.inspect_accession("GSE152418")
    samples, characteristics, summary = session.extract_gse_samples("GSE152418")
    result = session.load_expression("GSE152418", save_to="counts.csv")
    print(result.source, len(result.matrix["rows"]))
    print(result.matrix["columns"])

The module-level functions expose the same operations. GSE, GSM, and GEOReader remain available as object-oriented facades.

Other workflows

Task Entry point
Harmonized sample metadata, provenance, completeness checks pyncbi metadata, pyncbi samples --harmonize
Study overview, sample sheet, file inventory and linked papers pyncbi bundle
Compare candidate studies pyncbi screen
Series Matrix, RNA-seq counts and platform/gene annotation pyncbi download, pyncbi gpl
PubMed abstracts, PMC Methods and citations pyncbi pubmed
SRA runs, FASTQ files and FASTA sequences pyncbi sra, pyncbi fastq, pyncbi fasta
ClinVar/dbSNP, assemblies, taxonomy and cross-database links pyncbi variant, pyncbi genome, pyncbi taxonomy, pyncbi link
Sequence similarity pyncbi blast

Run pyncbi --help or pyncbi <command> --help for the installed version's options. The docs contain worked examples and the generated CLI/API references.

Data and output contracts

Raw downloads preserve the hosted bytes. Parsed tables are plain Python dictionaries:

{"columns": ["feature_id", "GSM1"], "rows": [["0001234", 9007199254740993]]}

IDs stay strings, counts stay exact Python integers even beside missing values, and decimal measurements use Python floats. JSON exports use null for missing/non-finite values. JSON, CSV and TSV are built in. DataFrame conversion, YAML, Parquet and analysis belong to the caller; PyNCBI never installs an analysis library.

For large files, consume rows without building a full table:

from contextlib import closing

# A downloaded file or an HTTPS URL can be used here.
with closing(PyNCBI.iter_supplementary_records("counts.tsv.gz")) as rows:
    for record in rows:
        print(record)
        break

Use --json for an agent envelope and -o for the complete saved result. Terminal/agent previews can be truncated. Metadata harmonization preserves raw fields and records the normalization rule and confidence. The 0–100 metadata score measures completeness and consistency; it does not assess scientific validity.

PYNCBI_MAX_DOWNLOAD_MB sets the default transfer budget (2048 MB; 0 disables it). Download operations share a budget across nested calls and workers, and enforce it while reading bodies even when sizes are unknown. --max-download-mb overrides it where exposed. Metadata requests made inside an operation also count. A size plan is an estimate when servers omit sizes. The limit counts bytes delivered by the HTTP transport, not expanded archive size, exported CSV size, or total lifetime session traffic. Raw file writes check free space.

Agents and MCP

pyncbi mcp                         # stdio
pyncbi mcp --http --port 8765       # local Streamable HTTP endpoint /mcp
pyncbi schema mcp                  # current tool, resource and prompt manifest
pyncbi skill install --target codex  # or --target claude / cursor
{
  "mcpServers": {
    "pyncbi": {"command": "pyncbi", "args": ["mcp"]}
  }
}

After installing, connect the MCP server once in your client:

codex mcp add pyncbi -- pyncbi mcp
claude mcp add --transport stdio --scope user pyncbi -- pyncbi mcp

Choose the command for your client. The skill installer includes supporting workflow references; it does not register an MCP server. The agent setup guide covers executable paths, repository skills, long-operation timeouts and output-file access.

Set PYNCBI_AGENT_MODE=1 to suppress colors/progress. MCP errors include error_code, hint, and retryable. HTTP clients initialize a session and send its Mcp-Session-Id on subsequent requests. Each POST contains one JSON-RPC message. Network-facing hosting requires your own authentication; the default address is loopback.

To run a local container, first build it:

docker build -t pyncbi .
docker run --rm -i pyncbi mcp

Development and citation

See CONTRIBUTING.md for tests, generated artifacts and release steps. CITATION.cff contains the software citation. PyNCBI is distributed under the MIT license.

Metadata

Release files for PyNCBI 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyNCBI 1.0.0
File Size Uploaded Provenance Actions
pyncbi-1.0.0.tar.gz 401.5 kB Without provenance Details for pyncbi-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for PyNCBI 1.0.0
File Interpreter ABI Platform Actions
pyncbi-1.0.0-py3-none-any.whl Python 3 none any Details for pyncbi-1.0.0-py3-none-any.whl

Total release size: 683.6 kB

Release files / pyncbi-1.0.0.tar.gz

Download URL pyncbi-1.0.0.tar.gz
Size 401.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4b47d9fd1dcc1b4ed446b4a228eab53fd38a8fd896ae0b98ea89ab346537a82f
BLAKE2b-256 checksum
How to use checksums
67278bad6571950f92d9c0f3561a5a7a6870d5f3d155f12fa97b48be84b6d003
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pyncbi-1.0.0-py3-none-any.whl

Download URL pyncbi-1.0.0-py3-none-any.whl
Size 282.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc29a3eb3e95a107a07a143138dd4ddf9a6c60a1e2da4f40302910aeff5261ae
BLAKE2b-256 checksum
How to use checksums
5e827bc30d6178cb267f6efd5cb8eaa0cc6ac01d0d856f2d0e7fbcd2b7ebbfda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for PyNCBI 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page