🧬 PubMed高级MCP服务器
   
一个全面的模型上下文协议(MCP)服务器,将PubMed和PubMed Central研究文献API作为LLM应用程序的智能工具公开。
建于❤️ 通过 Suyash Ekhande
https://github.com/user-attachments/assets/892978f7-88d8-4b26-992e-41ba87c1b1cf
______________________________________________________________________
🌟 特性
- 16智能工具 分为5类,用于全面的生物医学文献检索
- 34M+PubMed文章 -搜索世界上最大的生物医学摘要数据库
- 7M+PMC全文文章 -从PubMed Central访问完整的文章内容
- 智能速率限制 -自动符合NCBI速率限制(3-10要求/秒)
- 跨数据库链接 -将文章与基因、蛋白质、临床变异等联系起来
- ID转换 -在PMID、PMCID、DOI和手稿ID之间无缝转换
- BioC格式支持 -NLP和文本挖掘应用程序的预解析文本
- 管道运营 -使用Entrez历史服务器构建复杂的多步骤查询
- 批处理 -通过分块操作高效处理10K+文章
📦 安装
先决条件
- Python 3.10或更高版本
- pip或uv包管理器
从源代码安装
# Clone the repository
git clone https://github.com/yourusername/pubmed-advanced-mcp.git
cd pubmed-advanced-mcp
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Or install as package
pip install -e .配置API密钥(推荐)
# Copy example environment file
cp .env.example .env
# Edit .env and add your NCBI API key
# Get one at: https://www.ncbi.nlm.nih.gov/account/注: 如果没有API密钥,则每秒只能有3个请求。使用API密钥,每秒可以得到10个请求。
🚀 快速开始
运行MCP服务器
# Using Python directly (Streamable HTTP on port 8000)
python -m src.server
# With custom host/port
MCP_HOST=127.0.0.1 MCP_PORT=9000 python -m src.server运输: 此服务器使用 可流式传输的HTTP 作为唯一的传输协议。它继续运行 http://0.0.0.0:8000/mcp 默认情况下。使用Docker运行
# Build the image
docker build -t pubmed-mcp .
# Run the container
docker run -d -p 8000:8000 --name pubmed-mcp pubmed-mcp
# Run with NCBI API key for higher rate limits
docker run -d -p 8000:8000 -e NCBI_API_KEY=your-api-key pubmed-mcp🛠 可用工具
类别1:搜索和发现(5个工具)
| 工具 | 描述 | 用例示例 |
|---|---|---|
pubmed_search | 搜索34M+PubMed摘要 | 查找关于CAR-T疗法的评论 |
pmc_search | PMC中的全文搜索 | 协议的搜索方法部分 |
mesh_term_search | MeSH控制词汇搜索 | 查找所有癌症治疗文章 |
advanced_search | 多字段布尔查询 | 复杂的作者+主题+日期搜索 |
global_search | 跨数据库点击数 | 跨NCBI发现数据 |
第2类:文档检索(4种工具)
| 工具 | 描述 | 用例示例 |
|---|---|---|
fetch_article_summary | 获取文章元数据 | 检索作者和摘要信息 |
fetch_full_article | 获取完整的文章内容 | 下载完整的PMC文章 |
fetch_bioc_article | NLP的BioC格式 | 文本挖掘和NER任务 |
batch_fetch_articles | 批量文章检索 | 高效下载1000+篇文章 |
第3类:交叉引用和链接(3个工具)
| 工具 | 描述 | 用例示例 |
|---|---|---|
find_related_articles | 引文/相似性链接 | 建立引文网络 |
link_to_databases | 与基因、蛋白质等的交联。 | 查找文章中提到的基因 |
find_citations_by_authors | 作者发表历史 | 跟踪研究人员输出 |
第4类:ID转换(2个工具)
| 工具 | 描述 | 用例示例 |
|---|---|---|
convert_article_ids | 批处理ID转换 | 将DOI转换为PMID |
resolve_article_identifier | 单ID解析 | 按任何ID类型查找文章 |
第5类:高级操作(2个工具)
| 工具 | 描述 | 用例示例 |
|---|---|---|
build_search_pipeline | 多步骤查询流程 | 复杂的研究工作流程 |
batch_process_articles | 大规模加工 | 处理10K+件 |
📖 使用示例
基本搜索
User: Find recent reviews about CRISPR gene editing in cancer
AI uses: pubmed_search(
query="CRISPR gene editing cancer",
filters={"publication_types": ["Review"], "publication_date_start": "2023"},
max_results=10
)基于MeSH的搜索
User: Find all articles about breast cancer treatment using MeSH terms
AI uses: mesh_term_search(
mesh_term="Breast Neoplasms",
qualifiers=["therapy", "drug therapy"],
explode=True,
max_results=50
)查找相关文章
User: What articles are similar to PMID 37000000?
AI uses: find_related_articles(
pmid="37000000",
relationship_type="similar",
max_results=20
)转换文章ID
User: Convert these DOIs to PMIDs: 10.1038/nature12373, 10.1126/science.1225829
AI uses: convert_article_ids(
ids=["10.1038/nature12373", "10.1126/science.1225829"],
from_type="auto"
)建立研究管道
User: Find diabetes review articles that are linked to HLA genes
AI uses: build_search_pipeline(
steps=[
{"operation": "search", "database": "pubmed",
"parameters": {"query": "diabetes[mh] AND review[pt]"}},
{"operation": "link", "database": "gene",
"parameters": {"from_db": "pubmed"}}
]
)批处理
User: Get metadata for these 500 PMIDs for my literature review
AI uses: batch_fetch_articles(
pmids=["12345678", "23456789", ...], # 500 IDs
include_metadata=True,
include_abstract=True,
batch_size=100
)🏗 建筑
┌─────────────────────────────────────────────────────────────────────────┐
│ LLM / AI Agent Client │
└────────────────────────────────────┬────────────────────────────────────┘
│
MCP Protocol (Streamable HTTP)
│
┌────────────────────────────────────▼────────────────────────────────────┐
│ FastMCP Server (Python) │
│ │
│ ┌────────────────────────────────────────────────────────────────────┐ │
│ │ 16 MCP Tools │ │
│ │ Search │ Retrieval │ Linking │ ID Conversion │ Advanced Ops │ │
│ └────────────────────────────────┬───────────────────────────────────┘ │
│ │ │
│ ┌────────────────────────────────▼───────────────────────────────────┐ │
│ │ API Clients (with Rate Limiting) │ │
│ │ E-Utilities │ BioC API │ ID Converter │ Session Manager │ │
│ └────────────────────────────────┬───────────────────────────────────┘ │
└────────────────────────────────────┼────────────────────────────────────┘
│
HTTP/REST API Calls
│
┌────────────────────────────┼────────────────────────────┐
▼ ▼ ▼
┌─────────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ NCBI E-Utilities│ │ BioC APIs │ │ ID Converter │
│ (34M+ articles) │ │ (29M+ articles) │ │ (200 IDs/batch) │
└─────────────────┘ └──────────────────┘ └──────────────────┘📁 项目结构
pubmed-advanced-mcp/
├── src/
│ ├── __init__.py
│ ├── server.py # FastMCP server with all 16 tools
│ ├── config.py # Configuration management
│ │
│ ├── clients/ # API client modules
│ │ ├── base.py # Base HTTP client with rate limiting
│ │ ├── eutilities.py # NCBI E-Utilities client
│ │ ├── bioc_api.py # BioC text mining API
│ │ ├── id_converter.py # PMC ID Converter
│ │ └── session_manager.py # Entrez History management
│ │
│ ├── tools/ # MCP Tool implementations
│ │ ├── search_tools.py # 5 search tools
│ │ ├── retrieval_tools.py # 4 retrieval tools
│ │ ├── linking_tools.py # 3 linking tools
│ │ ├── id_conversion_tools.py # 2 ID tools
│ │ └── advanced_tools.py # 2 advanced tools
│ │
│ ├── schemas/ # Pydantic models
│ │ └── tool_schemas.py # Input/output schemas
│ │
│ └── utils/ # Utilities
│ ├── rate_limiter.py # Token bucket rate limiter
│ ├── query_builder.py # E-utilities query builder
│ └── error_handler.py # Custom exceptions
│
├── docs/
│ ├── implementation/ # Implementation documentation
│ └── *.md # Original requirements
│
├── requirements.txt
├── pyproject.toml
├── .env.example
└── README.md⚙️ 配置
环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
NCBI_API_KEY | 用于更高速率限制的NCBI API密钥 | 无(3个请求/秒) |
TOOL_NAME | NCBI的工具标识符 | pubmed-mcp-server |
TOOL_EMAIL | 联系电子邮件(NCBI要求) | pubmed-mcp@example.com |
速率限制
| 场景 | 利率限制 |
|---|---|
| 没有API密钥 | 3个请求/秒 |
| 使用API密钥 | 10个请求/秒 |
| 违规 | IP被封锁24+小时 |
🔬 LLM提示示例
以下是可用于Claude或其他LLM客户端的示例提示:
文献综述
"Find all systematic reviews about COVID-19 vaccine efficacy published in 2023-2024.
Include the abstracts and MeSH terms."基因疾病研究
"Search for articles about TP53 mutations in breast cancer. Then link these articles
to related gene records in NCBI Gene database."分析
"Find all publications by Jennifer Doudna in the last 5 years and summarize
her research focus areas."ID转换
"I have these DOIs from my reference manager. Convert them to PMIDs so I can
search for related articles: 10.1038/nature12373, 10.1126/science.1225829"文本挖掘管道
"Get the full text of PMC7611378 in BioC format. I need it for named entity
recognition to extract drug names and disease mentions."🧪 测试
运行测试
# Install dev dependencies
pip install -e ".[dev]"
# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test file
pytest tests/test_search_tools.py📚 API 文档
电子实用程序查询语法
服务器支持完整的电子实用程序查询语法:
# Basic search
cancer
# Field-specific search
cancer[ti] # Title
CRISPR[ab] # Abstract
"Zhang F"[au] # Author
Nature[ta] # Journal
# Boolean operators (MUST be uppercase)
cancer AND therapy
cancer OR tumor
cancer NOT lung
# Date ranges
cancer AND 2023[dp] # Year
cancer AND 2020:2024[dp] # Range
# MeSH terms
"Breast Neoplasms"[mh] # MeSH heading
"Neoplasms/therapy"[mh] # With qualifier
# Publication types
review[pt]
clinical trial[pt]🤝 贡献
欢迎投稿!请随时提交拉取请求。
- 分叉存储库
- 创建功能分支(
git checkout -b feature/AmazingFeature) - 提交您的更改(
git commit -m 'Add some AmazingFeature') - 推到分支(
git push origin feature/AmazingFeature) - 打开拉取请求
致谢
- 国家生物技术信息中心 用于提供电子实用程序和相关API
- FastMCP 优秀的MCP框架
- 生物医学研究界对PubMed的贡献
______________________________________________________________________
制作❤️ 生物医学研究界
由...建造 Suyash Ekhande
