PubMed Client for Rust
A comprehensive Rust workspace for accessing PubMed and PMC (PubMed Central) APIs, with multiple language bindings and tools.
Features
- PubMed API Integration: Search and fetch article metadata using E-utilities
- PMC Full Text: Retrieve and parse structured full-text articles
- ELink API: Discover related articles, citations, and PMC full-text availability
- EInfo API: Query database information and searchable fields
- Advanced Search Builder: Construct complex queries with filters, date ranges, and boolean logic
- MeSH Term Support: Extract and search using Medical Subject Headings (MeSH) vocabulary
- Markdown Export: Convert PMC articles to well-formatted Markdown
- Async Support: Built on tokio for async/await support
- Type Safety: Strongly typed data structures for all API responses
- Automatic Retry: Built-in retry logic with exponential backoff for handling transient failures
- Rate Limiting: Automatic compliance with NCBI API rate limits (3 req/sec default, 10 req/sec with API key)
- Multiple Language Bindings: Rust, JavaScript/TypeScript (WASM), and Python (PyO3)
- Command-Line Interface: CLI for common operations (search, conversion, figure extraction)
- MCP Server: Model Context Protocol server for AI assistant integration
Workspace Structure
This is a Cargo workspace containing multiple packages:
- pubmed-client - Core Rust library
- pubmed-client-napi - Native Node.js bindings via napi-rs
- pubmed-client-wasm - WebAssembly bindings for npm
- pubmed-client-py - Python bindings via PyO3
- pubmed-cli - Command-line interface
- pubmed-mcp - MCP server for AI assistants
Installation
Rust
Add this to your Cargo.toml:
[]
= "0.1.0"
Node.js (Native)
# or
JavaScript/TypeScript (WASM)
# or
Python
# or
Command-Line Interface
Quick Start
Rust
Using the Unified Client
use Client;
async
Advanced Search with Filters
use ;
async
Discovering Related Articles
use Client;
async
Node.js (Native)
import { PubMedClient, SearchQuery, type Config } from 'pubmed-client';
async function main() {
// Create client with configuration
const config: Config = {
apiKey: process.env.NCBI_API_KEY,
email: 'you@example.com',
tool: 'MyApp',
};
const client = PubMedClient.withConfig(config);
// Search for articles
const articles = await client.search('covid-19 vaccine', 10);
for (const article of articles) {
console.log(`Title: ${article.title}`);
console.log(`PMID: ${article.pmid}`);
}
// Use SearchQuery builder for complex queries
const query = new SearchQuery()
.query('cancer')
.publishedBetween(2020, 2024)
.articleType('Clinical Trial')
.freeFullTextOnly()
.setLimit(50);
const results = await client.executeQuery(query);
// Fetch PMC full-text and convert to Markdown
const markdown = await client.fetchPmcAsMarkdown('PMC7906746', {
includeMetadata: true,
useYamlFrontmatter: true,
});
}
main().catch(console.error);
JavaScript/TypeScript (WASM)
import { WasmPubMedClient, WasmClientConfig } from 'pubmed-client-wasm';
async function main() {
// Create client with configuration
const config = new WasmClientConfig();
config.with_api_key("your_api_key");
config.with_email("you@example.com");
const client = WasmPubMedClient.with_config(config);
// Search for articles
const articles = await client.search_and_fetch("covid-19", 10);
for (const article of articles) {
console.log(`Title: ${article.title}`);
console.log(`PMID: ${article.pmid}`);
}
}
main().catch(console.error);
Python
# Create a unified client
=
# Search PubMed
=
# Fetch PMC full text
=
# Find related articles
=
Command-Line Interface
# Search PubMed
# Convert PMID to PMCID
# Extract figures from PMC articles
# Convert PMC article to Markdown
Advanced Features
Converting PMC Articles to Markdown
use ;
async
Working with MeSH Terms
Medical Subject Headings (MeSH) terms provide standardized vocabulary for biomedical literature. This library supports extracting and searching with MeSH terms.
Searching with MeSH Terms
use ;
async
Extracting MeSH Information
use Client;
async
Comparing Articles by MeSH Terms
use Client;
async
MCP Server for AI Assistants
The Model Context Protocol (MCP) server enables AI assistants like Claude to interact with PubMed APIs.
Claude Desktop Integration
Add to your Claude Desktop configuration:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
After restarting Claude Desktop, you can ask Claude to search PubMed:
User: Search PubMed for recent COVID-19 vaccine research
Claude: [Uses the search_pubmed tool automatically]
Found 10 articles about COVID-19 vaccines:
1. mRNA vaccine effectiveness... (PMID: 12345678)
2. Booster dose efficacy... (PMID: 23456789)
...
Available MCP Tools
- search_pubmed: Search PubMed with query string and result limit
- Support for all PubMed field tags ([ti], [au], [ta], etc.)
- Future: PMC full-text retrieval, citation networks
For more details, see the MCP server README.
Configuration
Rate Limiting and API Keys
The client automatically handles rate limiting according to NCBI guidelines:
use ;
async
Retry Configuration
The client includes automatic retry logic with exponential backoff for handling transient failures:
use ;
use Duration;
async
Development
Prerequisites
- Rust 1.70 or later
- mise (optional, for tool management)
- Node.js and pnpm (for WASM development)
- Python 3.12+ and uv (for Python bindings)
Setup
Clone the repository:
If using mise, install tools:
Workspace Commands
# Build all workspace members
# Test all workspace members
# Build specific package
Running Tests
This project uses nextest for Rust tests:
# Run all Rust tests
# Run tests for specific package
# Run specific integration test suite
&&
&&
# Using mise tasks
# Node.js native tests (TypeScript)
&&
# WASM tests (TypeScript)
&&
# Python tests
&&
&&
Code Quality
Rust Code Quality
# Format code (dprint + cargo fmt)
# Run linter
# Check code
Node.js/WASM TypeScript Code Quality
# From pubmed-client-napi/ or pubmed-client-wasm/
Python Code Quality
# From pubmed-client-py/
Code Coverage
Generate and view test coverage reports:
# Generate HTML coverage report
# Generate and open HTML report
# Generate LCOV format for CI
# Generate JSON format
Documentation
Generate and view Rust documentation:
Online documentation is available at:
- Core library: docs.rs/pubmed-client
- Node.js package: npm package
- WASM package: npm package
- Python package: PyPI
API Coverage
This library provides comprehensive access to NCBI E-utilities:
- ESearch: Search PubMed with advanced query builder
- EFetch: Retrieve article metadata and PMC full-text
- ELink: Discover related articles, citations, and PMC links
- EInfo: Query database information and searchable fields
- PMC OAI: Access PMC full-text articles in structured XML format
See CLAUDE.md for implementation details and API design patterns.
Examples
The repository includes comprehensive examples:
- Rust examples - Core library usage
- Node.js examples - Native TypeScript integration
- WASM examples - WebAssembly TypeScript integration
- Python examples - Python usage patterns
- CLI usage - Command-line examples
CI/CD and GitHub Actions
The project uses comprehensive GitHub Actions workflows:
- Test: Cross-platform testing (Ubuntu, Windows, macOS) with coverage
- Docs: Auto-deploy documentation to GitHub Pages
- Release: Automated publishing to crates.io, npm, and PyPI
See .github/workflows/ for workflow details.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
When contributing, please:
- Follow the existing code style and patterns
- Add tests for new features
- Update documentation as needed
- Ensure all CI checks pass
Related Projects
There are several excellent PubMed and NCBI E-utilities client libraries available in different programming languages:
Python
-
biocommons/eutils - A comprehensive Python package for simplified searching, fetching, and parsing records from NCBI using their E-utilities interface. Features automatic request throttling (3/10 req/sec without/with API key) and promise-based piping.
-
gijswobben/pymed - PyMed provides access to PubMed through the PubMed API in a consistent, readable and performant way. Available on PyPI.
-
metapub/metapub - Python toolkit for NCBI metadata (via eutils) and PubMed article text mining. Provides abstraction layers over Medgen, PubMed, ClinVar, and CrossRef for building scholarly paper libraries.
-
krassowski/easy-entrez - Retrieve PubMed articles, text-mining annotations, or molecular data from >35 Entrez databases via easy-to-use Python package built on top of Entrez E-utilities API.
JavaScript/TypeScript
-
linjoey/ncbi-eutils - NCBI E-utilities client for Node.js and the browser. Uses ES6 promises to support "piping" to combine successive E-utility calls (e.g., piping esearch results to elink, then to esummary).
-
node-ncbi - A Node.js wrapper for the NCBI eUtils API that allows searching PubMed or other databases with results returned as JavaScript objects.
Go
- biogo/ncbi - Package entrez provides support for interaction with the NCBI Entrez Utility Programs (E-utilities). Part of the biogo bioinformatics library collection.
PHP
- jorgetutor/ncbi - Provides an implementation of the Guzzle library to query NCBI E-Utils service.
Rust
- pubmed crate - A Rust crate that implements reading publications from the PubMed API.
All of these libraries interact with the NCBI E-utilities, which provide the public API to the NCBI Entrez system for accessing PubMed, PMC, Gene, and other databases.
Acknowledgments
This project relies on NCBI's excellent E-utilities API. Please cite NCBI appropriately when using this library in research:
National Center for Biotechnology Information (NCBI). E-utilities API. https://www.ncbi.nlm.nih.gov/books/NBK25501/