使用说明
PubMed MCP服务器
用于PubMed文献搜索和管理的综合模型上下文协议(MCP)服务器。该服务器通过MCP协议提供高级搜索功能、引文格式和研究分析工具。
特性
- 高级PubMed搜索:使用复杂的过滤器进行搜索,包括日期范围、文章类型、作者、期刊和MeSH术语
- 文章详情:检索特定PMID的详细信息,包括摘要、作者和元数据
- 引文导出:以多种格式导出引文(BibTeX、APA、MLA、芝加哥、温哥华、EndNote、RIS)
- 作者搜索:查找具有合著者信息的特定作者的文章
- 相关文章:发现与特定PMID相关的文章
- MeSH术语搜索:搜索和探索医学主题标题
- 期刊分析:从特定期刊获取指标和最新文章
- 研究趋势:分析随时间推移的出版趋势
- 文章比较:并排比较多篇文章
- 缓存:内置缓存以提高性能
- 速率限制:尊重API的使用,可配置速率限制
安装
先决条件
- Python 3.8或更高版本
- NCBI API密钥(需要免费注册)
- NCBI API标识的有效电子邮件地址
快速开始
-
克隆存储库:
git clone https://github.com/your-org/pubmed-mcp.git cd pubmed-mcp -
安装依赖项:
pip install -r requirements.txt -
设置环境变量:
cp env.example .env # Edit .env with your NCBI API key and email -
运行服务器:
python -m src.main
开发安装
使用其他工具进行开发:
make install-dev
或手动:
pip install -r requirements.txt
pip install -e .
pip install black isort mypy flake8
配置
创建一个 .env 项目根目录中的文件,包含以下变量:
# Required
PUBMED_API_KEY=your_ncbi_api_key_here
PUBMED_EMAIL=your.email@example.com
# Optional
CACHE_TTL=300
CACHE_MAX_SIZE=1000
RATE_LIMIT=3.0
LOG_LEVEL=info
获取NCBI API密钥
- 访问 NCBI帐户设置
- 登录或创建帐户
- 导航到“API密钥管理”
- 创建新的API密钥
- 将密钥复制到您的
.env文件
用法
可用工具
服务器提供以下MCP工具:
1. search_pubmed
使用高级过滤选项搜索PubMed。
{
"query": "machine learning healthcare",
"max_results": 20,
"date_range": "5y",
"article_types": ["Journal Article", "Review"],
"has_abstract": true
}
2. get_article_details
获取特定PMID的详细信息。
{
"pmids": ["12345678", "87654321"],
"include_abstracts": true,
"include_citations": false
}
3. search_by_author
搜索特定作者的文章。
{
"author_name": "Smith J",
"max_results": 10,
"include_coauthors": true
}
4. export_citations
以各种格式导出引文。
{
"pmids": ["12345678"],
"format": "bibtex",
"include_abstracts": false
}
5. find_related_articles
查找与特定PMID相关的文章。
{
"pmid": "12345678",
"max_results": 10
}
6. search_mesh_terms
使用MeSH术语搜索。
{
"term": "Machine Learning",
"max_results": 20
}
7. analyze_research_trends
分析一段时间内的出版趋势。
{
"topic": "artificial intelligence",
"years_back": 5,
"include_subtopics": false
}
MCP客户端使用示例
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def main():
server_params = StdioServerParameters(
command="python",
args=["-m", "src.main"]
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the session
await session.initialize()
# Search PubMed
result = await session.call_tool(
"search_pubmed",
{
"query": "COVID-19 vaccines",
"max_results": 5,
"date_range": "1y"
}
)
print(result.content[0].text)
if __name__ == "__main__":
asyncio.run(main())
发展
运行测试
# Run all tests
make test
# Run with coverage
make test-coverage
# Run specific test types
python run_tests.py unit
python run_tests.py integration
python run_tests.py coverage
代码质量
# Format code
make format
# Run linting
make lint
# Type checking
mypy src/
项目结构
pubmed-mcp/
├── src/
│ ├── __init__.py
│ ├── main.py # Entry point
│ ├── server.py # MCP server implementation
│ ├── models.py # Pydantic models
│ ├── pubmed_client.py # PubMed API client
│ ├── tool_handler.py # Tool request handlers
│ ├── citation_formatter.py # Citation formatting
│ ├── tools.py # Tool definitions
│ └── utils.py # Utility functions
├── tests/ # Test suite
├── requirements.txt # Dependencies
├── setup.py # Package setup
├── pyproject.toml # Modern Python config
├── Makefile # Development commands
├── Dockerfile # Container setup
└── README.md # This file
码头工人
构建并运行
# Build Docker image
make docker-build
# Run with environment variables
make docker-run PUBMED_API_KEY=your_key PUBMED_EMAIL=your_email
Docker Compose
version: '3.8'
services:
pubmed-mcp:
build: .
environment:
- PUBMED_API_KEY=your_key
- PUBMED_EMAIL=your_email
- LOG_LEVEL=info
volumes:
- ./data:/app/data
api参考
搜索参数
query:使用PubMed语法搜索查询max_results:最大结果数(1-200)sort_order:排序顺序(相关性、发布日期、作者、期刊、标题)date_from/date_to:日期范围筛选器date_range:预定义的范围(1y,5y,10y,all)article_types:按发布类型筛选authors:按作者姓名筛选journals:按日志名称筛选mesh_terms:按MeSH术语筛选language:语言过滤器(例如“eng”、“fre”)has_abstract:只有有摘要的文章has_full_text:只有全文文章humans_only:仅人类研究
引文格式
bibtex:BibTeX格式apa:APA风格mla:MLA风格chicago:芝加哥风格vancouver:温哥华风格endnote:尾注格式ris:RIS格式
贡献
- 分叉存储库
- 创建要素分支
- 进行更改
- 添加新功能的测试
- 运行测试套件
- 提交拉取请求
开发指南
- 遵循PEP 8风格指南
- 为所有函数添加类型提示
- 编写全面的测试
- 更新新功能的文档
- 使用常规提交消息
许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
支持
致谢
- NCBI电子公用事业 用于PubMed API访问
- 模型上下文协议 对于MCP规范
- Anthropic 用于MCP开发和支持
更新日志
看 更改日志.md 查看详细的变化历史。
备注:此服务器需要有效的NCBI API密钥,并遵循NCBI的使用指南。请遵守API价格限制和服务条款。