Skip to main content

Genome MCP server - 优化版本:智能基因组数据访问

Project description

Genome MCP

NCBI基因组数据服务器,通过MCP协议提供智能基因信息查询和搜索功能。

PyPI version Python versions License: MIT

🚀 特性

  • 智能查询解析: 支持自然语言和结构化查询
  • 批量数据获取: 优化的批量查询,减少API调用
  • 语义搜索: 理解查询意图的智能搜索
  • 多传输模式: 支持STDIO、HTTP、SSE传输
  • 异步处理: 高性能异步架构
  • 完整文档: 详细的API文档和使用示例

安装

pip install genome-mcp

或使用 uv:

uv add genome-mcp

🛠️ MCP 接入配置

Claude Desktop

编辑配置文件:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json

推荐使用 uvx 运行:

{
  "mcpServers": {
    "genome-mcp": {
      "command": "uvx",
      "args": ["genome-mcp"],
      "env": {}
    }
  }
}

或使用传统方式:

{
  "mcpServers": {
    "genome-mcp": {
      "command": "python",
      "args": ["-m", "genome_mcp"],
      "env": {}
    }
  }
}

或使用 uv run:

{
  "mcpServers": {
    "genome-mcp": {
      "command": "uv",
      "args": ["run", "-m", "genome_mcp"],
      "env": {}
    }
  }
}

Continue.dev

在 VS Code 的 Continue.dev 扩展配置中:

{
  "mcpServers": {
    "genome-mcp": {
      "command": "uvx",
      "args": ["genome-mcp"]
    }
  }
}

Cursor (VS Code 扩展)

在 Cursor 设置中添加:

{
  "mcpServers": {
    "genome-mcp": {
      "command": "uvx",
      "args": ["genome-mcp"],
      "env": {
        "GENOME_MCP_LOG_LEVEL": "info"
      }
    }
  }
}

Cline (Claude for VS Code)

在 Cline 设置文件中:

{
  "mcpServers": {
    "genome-mcp": {
      "command": "uvx",
      "args": ["genome-mcp"],
      "timeout": 30000
    }
  }
}

其他支持 MCP 的客户端

  1. Windsurf: 使用与 Claude Desktop 相同的配置格式
  2. OpenHands: 在 config.json 中添加服务器配置
  3. Custom MCP Client: 参考下面的 Python 示例

自定义 MCP 客户端

使用 stdio 传输:

import subprocess
import json

# 启动 MCP 服务器
process = subprocess.Popen(
    ["python", "-m", "genome_mcp"],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    text=True
)

# 发送初始化消息
init_message = {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize",
    "params": {
        "protocolVersion": "2024-11-05",
        "capabilities": {},
        "clientInfo": {"name": "test-client", "version": "1.0.0"}
    }
}

process.stdin.write(json.dumps(init_message) + "\n")
response = process.stdout.readline()
print("Server response:", response)

🔧 API 功能

可用工具

  1. get_data - 智能数据获取

    • 支持基因符号、ID、区域搜索、同源基因查询
    • 自动类型识别和查询优化
    • 批量查询支持
  2. advanced_query - 高级批量查询

    • 复杂查询条件组合
    • 批量处理优化
    • 自定义输出格式
  3. smart_search - 语义搜索

    • 自然语言查询理解
    • 智能结果排序
    • 上下文感知搜索
  4. kegg_pathway_enrichment_tool - KEGG通路富集分析 🆕

    • 基因列表在KEGG通路中的富集分析
    • 超几何分布检验计算统计显著性
    • FDR多重检验校正
    • 支持人类、小鼠、大鼠等多种模式生物

使用示例

import asyncio
from genome_mcp import get_data, advanced_query, smart_search

async def main():
    # 获取基因信息
    gene_info = await get_data("TP53")
    print("Gene info:", gene_info)

    # 区域搜索
    region_data = await get_data("chr17:7565097-7590856", query_type="region")
    print("Region data:", region_data)

    # 批量查询
    batch_results = await get_data(["TP53", "BRCA1", "EGFR"], query_type="gene")
    print("Batch results:", batch_results)

    # 语义搜索
    search_results = await smart_search("tumor suppressor genes involved in cancer")
    print("Search results:", search_results)

    # 高级查询
    advanced_results = await advanced_query(
        query="cancer genes",
        query_type="search",
        database="gene",
        max_results=20
    )
    print("Advanced results:", advanced_results)

    # KEGG通路富集分析
    kegg_results = await kegg_pathway_enrichment_tool(
        gene_list=["7157", "672", "675"],  # TP53, BRCA1, BRCA2的Entrez ID
        organism="hsa",
        pvalue_threshold=0.05,
        min_gene_count=2
    )
    print("KEGG enrichment results:", kegg_results)

asyncio.run(main())

📋 JSON 响应格式

get_data 响应示例

{
  "success": true,
  "data": {
    "gene_info": {
      "uid": "7157",
      "name": "TP53",
      "description": "tumor protein p53",
      "status": "Gene",
      "chromosome": "17",
      "maplocation": "17p13.1",
      "genomicinfo": [
        {
          "chraccver": "GRCh38.p13",
          "chrstart": 7565097,
          "chrstop": 7590856
        }
      ]
    }
  },
  "query_info": {
    "query": "TP53",
    "query_type": "gene",
    "database": "gene"
  }
}

smart_search 响应示例

{
  "success": true,
  "data": {
    "results": [
      {
        "uid": "7157",
        "name": "TP53",
        "description": "tumor protein p53"
      }
    ],
    "total_count": 1,
    "query_understanding": {
      "intent": "gene_search",
      "key_terms": ["tumor", "suppressor", "genes", "cancer"]
    }
  }
}

kegg_pathway_enrichment_tool 响应示例

{
  "query_genes": ["7157", "672", "675"],
  "organism": "hsa",
  "total_pathways_found": 51,
  "significant_pathways": 15,
  "all_pathways": [
    {
      "pathway_id": "hsa01522",
      "pathway_name": "Path: hsa01522",
      "genes": ["7157"],
      "gene_count": 1,
      "pvalue": 0.0001,
      "fdr": 0.0051,
      "fold_enrichment": 6666.67
    }
  ],
  "query_info": {
    "analysis_date": "2025-10-24",
    "method": "KEGG Pathway Enrichment",
    "statistical_test": "Hypergeometric Test",
    "fdr_correction": "Benjamini-Hochberg"
  }
}

💻 命令行使用

# 启动 MCP 服务器 (stdio 模式)
python -m genome_mcp

# 或使用 uv
uv run -m genome_mcp

# HTTP 服务器模式
python -m genome_mcp --port 8080

# SSE 服务器模式
python -m genome_mcp --mode sse --port 8080

# 查看帮助
python -m genome_mcp --help

# 测试示例
python examples/mcp-client-example.py

传输模式:

  • stdio: 标准输入输出,用于MCP客户端集成
  • http: HTTP API服务器,用于Web集成
  • sse: Server-Sent Events,用于实时数据流

🔍 MCP 协议调试

初始化请求

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": {
    "protocolVersion": "2024-11-05",
    "capabilities": {
      "roots": {"listChanged": true}
    },
    "clientInfo": {"name": "debug-client", "version": "1.0.0"}
  }
}

服务器响应

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "protocolVersion": "2024-11-05",
    "capabilities": {
      "tools": {"listChanged": false}
    },
    "serverInfo": {
      "name": "Genome MCP",
      "version": "0.2.0"
    }
  }
}

工具调用示例

{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "tools/call",
  "params": {
    "name": "get_data",
    "arguments": {
      "query": "TP53",
      "query_type": "auto"
    }
  }
}

📁 配置文件

项目中包含以下配置文件模板:

  • examples/claude-desktop-config.json - Claude Desktop 配置
  • mcp-config.json - 通用 MCP 配置
  • examples/mcp-client-example.py - 完整的 Python MCP 客户端示例
  • examples/usage_examples.py - API 使用示例
  • examples/fastmcp_example.py - FastMCP 框架示例

🔧 故障排除

常见问题

  1. 导入错误: 确保已安装依赖

    pip install aiohttp fastmcp
    
  2. 网络错误: 检查到 NCBI 的网络连接

    curl -I "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
    
  3. MCP 协议错误: 确保使用正确的 JSON-RPC 2.0 格式

    • 消息必须以换行符结尾
    • 必须包含 jsonrpc: "2.0" 字段
  4. 权限错误: 确保有权限执行 Python 脚本

调试模式

启用详细日志:

GENOME_MCP_LOG_LEVEL=debug python -m genome_mcp

测试API功能:

python examples/mcp-client-example.py

性能优化

  • 使用批量查询减少API调用
  • 启用缓存机制
  • 调整超时设置
  • 使用适当的传输模式

📚 依赖

  • aiohttp>=3.8.0 - HTTP 客户端
  • fastmcp>=2.0.0 - MCP 协议支持
  • Python >= 3.10

🏗️ 开发

git clone https://github.com/gqy20/genome-mcp
cd genome-mcp
pip install -e ".[dev]"
make test
make lint

开发命令

make install    # 安装开发依赖
make format     # 格式化代码
make lint       # 代码质量检查
make test       # 运行测试
make check      # 完整检查
make build      # 构建包

📄 许可证

MIT License - 详见 LICENSE 文件

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📞 支持


Genome MCP - 让基因组数据访问更简单、更智能!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

genome_mcp-0.2.1.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

genome_mcp-0.2.1-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

Details for the file genome_mcp-0.2.1.tar.gz.

File metadata

  • Download URL: genome_mcp-0.2.1.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for genome_mcp-0.2.1.tar.gz
Algorithm Hash digest
SHA256 18bd65bc07f3de5c71fb13b017390ddc9c2b27d1fd43eafc08b07bd25d0b86ab
MD5 4495fd2a086af3b741b3477221ea8b94
BLAKE2b-256 562a2be599934297e4aadb677ae3ffc87309ed80123c911241342b7f25cde697

See more details on using hashes here.

File details

Details for the file genome_mcp-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: genome_mcp-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 33.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for genome_mcp-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f1b82c94b163f2768d39de6341f5afccb4bf8b451bedc46442208129250db73c
MD5 73b5909432e08b85537fe4e9f0a9489c
BLAKE2b-256 3ebdd15acb4fb1eadf69568cea17cb05fa2ae52fc40f4fdf40b5629c79436559

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page