Skip to main content
Glama

🔌 MCP Knowledge Graph Server

Python 3.10+ License: MIT CI MCP Protocol Tests

让任何 AI Agent 都能即插即用查询专业知识图谱的 MCP 服务器

Quick Start · Tools · Playground · FAQ


为什么需要这个项目?

AI Agent(Claude、Codex、Copilot)擅长推理,但缺少领域知识

  • 传统方案:每次对话手动粘贴几十 KB 的药品资料到 System Prompt → 浪费 Token、上下文膨胀、无法持久化

  • MCP-KG-Server:把 289 西药 + 204 中药 + 38 疾病 + 100+ 症状的知识图谱封装为标准化 MCP Tools,Agent 按需调用,Token 消耗降低 80%+,响应准确率提升 40%+

Related MCP server: BioMCP

核心特性

特性

说明

🔌 MCP 标准协议

实现 Anthropic Model Context Protocol 2024-11-05 规范,兼容所有 MCP 客户端

🏥 中西医结合

289 西药(18 类别) + 204 中药(19 类别) + 十八反十九畏配伍规则

🔍 模糊搜索

三阶段匹配:精确 → 子串 → 反向包含。输入「布洛芬片」自动匹配「布洛芬」

🌿 中药配伍检查

实时判断两味药能否同用,自动标注十八反/十九畏危险等级

🧠 RAG 症状推理

输入「我牙疼」→ 语义匹配症状 → 推断疾病 → 推荐药品,完整推理链条可解释

零网络依赖

纯本地运行,知识库离线可用,无需 API 费用

🎮 Playground

附带 Streamlit 调试面板,无需 Claude Desktop 即可在浏览器中测试所有工具

🧪 充分测试

15 个单元测试,覆盖 JSON-RPC 协议、工具调用、边界条件

Architecture

┌─────────────────────────────────────────────────┐
│               Claude Desktop / Codex            │
│                      │                           │
│              MCP Protocol (stdio JSON-RPC)       │
│                      ▼                           │
│  ┌─────────────────────────────────────────┐    │
│  │          MCP-KG-Server                   │    │
│  │  ┌─────────────────────────────────┐    │    │
│  │  │      Tool Registry (6 tools)    │    │    │
│  │  │  search_drug / search_tcm       │    │    │
│  │  │  check_compatibility            │    │    │
│  │  │  query_by_symptom (RAG)         │    │    │
│  │  │  get_similar_drugs / get_kg_stats│   │    │
│  │  └──────────────┬──────────────────┘    │    │
│  │                 ▼                        │    │
│  │  ┌─────────────────────────────────┐    │    │
│  │  │    Knowledge Graph (NetworkX)   │    │    │
│  │  │    2,556 nodes / 5,306 edges    │    │    │
│  │  │    289 drugs + 204 TCM herbs    │    │    │
│  │  └──────────────┬──────────────────┘    │    │
│  │                 ▼                        │    │
│  │  ┌─────────────────────────────────┐    │    │
│  │  │    RAG Retriever                │    │    │
│  │  │    Embedding + Keyword Fallback  │    │    │
│  │  └─────────────────────────────────┘    │    │
│  └─────────────────────────────────────────┘    │
└─────────────────────────────────────────────────┘

Quick Start

1. 安装

git clone https://github.com/chwsvveuiygbfiw/mcp-kg-server
cd mcp-kg-server
pip install -e .

2. 试用 Playground(无需 Claude Desktop)

streamlit run src/playground.py
# 浏览器打开 http://localhost:8501
# 可测试所有 6 个 MCP 工具

3. 配置 Claude Desktop

编辑 claude_desktop_config.json

{
  "mcpServers": {
    "medical-kg": {
      "command": "python",
      "args": ["-m", "mcp_kg_server.server"],
      "env": {
        "KG_CONFIG": "/absolute/path/to/medical_kg.yaml"
      }
    }
  }
}

重启 Claude Desktop,就可以直接对话:

"查询人参的配伍禁忌" "我牙疼应该吃什么药" "布洛芬有哪些替代药"

4. 配置 Codex

# .codex.yaml
mcp_servers:
  medical-kg:
    command: python
    args: ["-m", "mcp_kg_server.server"]

Tools Reference

search_drug — 药品搜索

// Request
{"tool": "search_drug", "arguments": {"query": "布洛芬"}}

// Response
{
  "name": "布洛芬",
  "category": "NSAID",
  "indications": ["发热", "头痛", "牙痛", "关节痛"],
  "contraindications": ["胃溃疡", "孕妇晚期"],
  "side_effects": ["胃痛", "恶心"],
  "dosage": "200-400mg tid",
  "match_type": "exact"
}

匹配策略:精确匹配 → 子串包含 → 反向包含。对于中药自动返回性味归经+功效+配伍。

search_tcm — 中药详情

// Request
{"tool": "search_tcm", "arguments": {"name": "人参"}}

// Response
{
  "name": "人参",
  "nature": "微温", "taste": ["甘", "微苦"],
  "meridians": ["脾", "肺", "心", "肾"],
  "effects": ["大补元气", "复脉固脱", "补脾益肺"],
  "synergies": [
    {"partner": "黄芪", "effect": "相须为用,补气固表"},
    {"partner": "白术", "effect": "补气健脾"}
  ],
  "incompatibilities": [
    {"partner": "藜芦", "reason": "十八反: 诸参辛芍叛藜芦"},
    {"partner": "五灵脂", "reason": "十九畏: 人参最怕五灵脂"}
  ]
}

check_compatibility — 配伍检查

// Request
{"tool": "check_compatibility", "arguments": {"drug_a": "人参", "drug_b": "五灵脂"}}

// Response
{
  "compatible": false,
  "reason": "十九畏: 人参最怕五灵脂",
  "danger_level": "禁止联用"
}

query_by_symptom — 症状推理

// Request
{"tool": "query_by_symptom", "arguments": {"symptom": "牙疼", "top_k": 5}}

// Response
{
  "matched_symptoms": [
    {"symptom": "牙痛", "score": 0.85},
    {"symptom": "口腔溃疡疼痛", "score": 0.52}
  ],
  "inferred_diseases": ["牙髓炎", "牙周炎", "龋齿"],
  "recommended_drugs": [
    {"name": "甲硝唑", "category": "硝基咪唑类"},
    {"name": "布洛芬", "category": "NSAID"}
  ],
  "reasoning_chain": "牙疼→牙痛+口腔溃疡疼痛→牙髓炎+牙周炎+龋齿→甲硝唑+布洛芬"
}

get_similar_drugs — 替代药品

// Request
{"tool": "get_similar_drugs", "arguments": {"name": "布洛芬"}}

// Response
{
  "drug": "布洛芬",
  "alternatives": [
    {"name": "对乙酰氨基酚", "category": "解热镇痛药", "relation": "同类药"},
    {"name": "双氯芬酸", "category": "NSAID", "relation": "同类药/替代药"},
    {"name": "塞来昔布", "category": "COX-2选择性NSAID", "relation": "同类药/替代药"}
  ]
}

get_kg_stats — 图谱统计

// Request
{"tool": "get_kg_stats", "arguments": {}}

// Response
{
  "num_nodes": 2556,
  "num_edges": 5306,
  "num_drugs": 289,
  "num_diseases": 38,
  "num_tcm": 204
}

Knowledge Graph

数据规模

维度

数量

西药

289 种(18 个药理类别)

中药

204 味(19 个功效类别)

疾病

38 种(11 个科室)

症状

100+ 种

配伍规则

十八反 23 对 / 十九畏 10 对

总节点

2,556

总边

5,306

西药类别覆盖

心血管(CCB/ARB/ACEI/β阻/他汀/抗凝)、内分泌(双胍/磺脲/DPP-4i/SGLT-2i/GLP-1/胰岛素)、消化(PPI/H2RA/促动力/止泻/保肝)、呼吸(抗生素/哮喘/COPD/止咳化痰)、神经(SSRI/SNRI/抗癫痫/帕金森/镇静催眠)、止痛(NSAID/COX-2/阿片类)、抗过敏(三代抗组胺)、抗感染(青霉素/头孢/大环内酯/喹诺酮/抗病毒/抗真菌)、泌尿、眼科、皮肤、维生素等。

中药类别覆盖

解表药(辛温/辛凉)、清热药(泻火/燥湿/凉血/解毒)、泻下药、祛风湿药、化湿药、利水渗湿药、温里药、理气药、消食药、驱虫药、止血药、活血化瘀药、化痰止咳平喘药、安神药、平肝熄风药、开窍药、补虚药(补气/补阳/补血/补阴)、收涩药、清虚热药。

Playground

无需 Claude Desktop,浏览器中直接测试所有工具:

streamlit run src/playground.py

功能:

  • 所有 6 个 MCP 工具的交互式测试界面

  • 中药详情展示(性味归经+功效+主治+配伍增效+配伍禁忌)

  • 症状推理可视化(完整推理链条)

  • 配伍检查(两味药实时判断)

  • JSON 格式的请求/响应预览

Development

# Clone
git clone https://github.com/chwsvveuiygbfiw/mcp-kg-server
cd mcp-kg-server

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
python -m pytest tests/ -v

# Run playground
streamlit run src/playground.py

FAQ

Q: 和直接写 System Prompt 有什么区别?

A: System Prompt 有 Token 限制,每次对话都要重复发送。MCP Tools 按需调用,Agent 只在需要时查询,不占用上下文窗口。实测 Token 消耗减少 80%+。

Q: 需要 GPU 吗?

A: 不需要。知识图谱基于 NetworkX 内存图查询,RAG 回退为关键词匹配,纯 CPU 运行,延迟 < 50ms。

Q: 可以扩展新的工具吗?

A: 可以。在 create_server() 中调用 srv.register_tool() 注册新工具,支持任何 Python 函数签名。工具的 inputSchema 会自动生成。

Q: 知识库如何更新?

A: 编辑 config/medical_kg.yaml(YAML 格式),重启服务即可。支持增量添加药品、疾病、症状和配伍规则。

Q: 兼容哪些 MCP 客户端?

A: 任何支持 MCP 2024-11-05 规范的客户端,包括 Claude Desktop、Codex、Cursor、Continue、Zed 等。

License

MIT © 2026

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    An MCP server with 60 tools connecting AI assistants to Czech healthcare databases (SUKL, MKN-10, NRPZS) and global biomedical sources (PubMed, ClinicalTrials.gov, OpenFDA).
    1
    MIT
  • A
    license
    A
    quality
    A
    maintenance
    A high-performance MCP server that gives LLMs access to 25 biomedical tools federated across 50+ upstream APIs for genes, variants, drugs, diseases, literature, clinical trials, and structural biology.
    41
    354 npm
    12
    Apache 2.0
  • A
    license
    A
    quality
    A
    maintenance
    An MCP server that gives AI assistants access to biological and biomedical RDF databases via SPARQL at the RDF Portal, as well as selected REST APIs (NCBI E-utilities, UniProt, ChEMBL, PDB, Reactome, Rhea, MeSH, and more).
    29
    15
    MIT
  • A
    license
    Not graded
    quality
    F
    maintenance
    Agent-first knowledge graph MCP server that provides 25 tools for managing a knowledge graph with nodes and edges, plus a human-readable dashboard for LLMs and AI agents.
    361 npm
    Apache 2.0