药品库存查询服务

一个基于模型上下文协议(MCP)的药品库存查询服务器,为AI助手提供药品库存、位置和区域统计信息的查询功能。

  • by MediFinderBot
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 medifinderbot/medifinder-mcp

使用说明

MedifinderMCP服务器

用于药品库存查询的MCP(模型上下文协议)服务器,旨在与Claude等人工智能助手配合使用。

概述

MedifinderMCP服务器提供了通过模型上下文协议(MCP)查询药品库存数据库的工具和资源。它允许AI助手和其他客户端:

  • 按名称或位置搜索药物
  • 检查不同医疗机构的药品供应情况
  • 获取特定药物的库存信息
  • 按地区查看药品供应统计数据
  • 分析整个医疗保健系统的库存状况

数据库模式

应用程序使用规范化的数据库模式:

Region
  - region_id (PK)
  - name
  - code
  - created_at
  - updated_at

MedicalCenter
  - center_id (PK)
  - code
  - name
  - region_id (FK -> Region)
  - category
  - reporter_name
  - institution_type
  - reporter_type
  - address
  - latitude
  - longitude
  - created_at
  - updated_at

ProductType
  - type_id (PK)
  - code
  - name
  - description
  - created_at
  - updated_at

Product
  - product_id (PK)
  - code
  - name
  - type_id (FK -> ProductType)
  - description
  - dosage_form
  - strength
  - created_at
  - updated_at

Inventory
  - inventory_id (PK)
  - center_id (FK -> MedicalCenter)
  - product_id (FK -> Product)
  - current_stock
  - avg_monthly_consumption
  - accumulated_consumption_4m
  - measurement
  - last_month_consumption
  - last_month_stock
  - status_indicator
  - cpma_12_months_ago
  - cpma_24_months_ago
  - cpma_36_months_ago
  - accumulated_consumption_12m
  - report_date
  - status
  - created_at
  - updated_at

User
  - user_id (PK)
  - phone_number
  - name
  - preferred_location
  - created_at
  - updated_at

SearchHistory
  - search_id (PK)
  - user_id (FK -> User)
  - product_query
  - location_query
  - search_radius
  - results_count
  - created_at

项目结构

medifinder-mcp/
├── app/
│   ├── __init__.py
│   ├── config.py            # Configuration management
│   ├── db/
│   │   ├── __init__.py
│   │   ├── connection.py    # Database connection handling
│   │   └── queries.py       # SQL queries
│   ├── models/
│   │   ├── __init__.py
│   │   ├── base.py          # Base model with timestamp fields
│   │   ├── region.py        # Region model
│   │   ├── medical_center.py # Medical center model
│   │   ├── product_type.py  # Product type model
│   │   ├── product.py       # Product model
│   │   ├── inventory.py     # Inventory model
│   │   ├── user.py          # User model
│   │   └── search_history.py # Search history model
│   ├── mcp/
│   │   ├── __init__.py
│   │   ├── server.py        # MCP server setup
│   │   ├── tools.py         # Tool implementations
│   │   ├── resources.py     # Resource implementations
│   │   └── prompts.py       # Prompt templates
│   └── utils/
│       ├── __init__.py
│       └── helpers.py       # Helper functions
├── main.py                  # Application entry point
├── requirements.txt         # Dependencies
└── README.md                # Documentation

MCP功能

工具

  • search_medicines:按名称或位置搜索药物
  • get_medicine_locations:查找药品供应地点
  • get_medicine_stock:获取特定药物的库存信息
  • get_regional_statistics:按地区获取药品统计数据
  • get_medicine_status:获取整体医学统计数据
  • diagnose_database:检查数据库连接和内容
  • troubleshoot_connection:详细的数据库连接诊断
  • create_database_schema:基于模型创建数据库表

资源

  • product://{id}:按ID获取产品详细信息
  • stock://{name}:按名称获取产品的库存信息
  • locations://{region}:在特定地区建立医疗中心
  • statistics://stock:获取整体库存统计数据
  • statistics://regions:获取区域统计数据

鼓励

  • medicine_search_prompt:按名称搜索药物的模板
  • medicine_availability_prompt:检查药品可用性的模板
  • medicine_statistics_prompt:药品统计分析模板
  • regional_availability_prompt:区域药品可用性分析模板

安装

  1. 克隆存储库:

    git clone https://github.com/yourusername/medifinder-mcp.git
    cd medifinder-mcp
    
  2. 创建虚拟环境并安装依赖项:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    pip install -r requirements.txt
    
  3. 通过创建 .env 文件:

    DB_HOST=localhost
    DB_PORT=5432
    DB_NAME=medifinderbot
    DB_USER=your_user
    DB_PASSWORD=your_password
    DEBUG=True
    ENV=development
    SERVER_NAME=MedifinderMCP
    SERVER_VERSION=1.0.0
    MCP_SERVER_NAME=MedifinderMCP
    MCP_SERVER_DESCRIPTION=MCP server for medicine inventory queries
    MAX_SEARCH_RESULTS=50
    SEARCH_SIMILARITY_THRESHOLD=0.3
    
  4. 创建数据库:

    # Connect to PostgreSQL
    psql -U postgres
    
    # Create database and user
    CREATE DATABASE medifinderbot;
    CREATE USER your_user WITH PASSWORD 'your_password';
    GRANT ALL PRIVILEGES ON DATABASE medifinderbot TO your_user;
    
    # Exit PostgreSQL
    \q
    
  5. 初始化数据库架构: 启动服务器后,使用 create_database_schema 创建表格的工具。

用法

在本地运行服务器

您可以直接运行MCP服务器:

python main.py

使用MCP检查器

对于开发和测试,MCP检查器提供了一种与服务器交互的便捷方式:

  1. 安装MCP CLI:

    pip install mcp[cli]
    
  2. 在开发模式下运行服务器:

    python -m mcp dev main.py
    
  3. MCP检查器将在您的浏览器中打开,允许您:

    • 测试工具和资源
    • 查看诊断工具的输出
    • 尝试不同的查询

与Claude Desktop集成

要将服务器与Claude Desktop一起使用,请执行以下操作:

  1. 为可靠的启动创建批处理文件(运行mcp-server.bat):

    @echo off
    cd /d %~dp0
    call venv\Scripts\activate.bat
    python main.py
    
  2. 在Claude Desktop中安装服务器:

    mcp install run-mcp-server.bat -f .env
    
  3. 或者,手动编辑Claude Desktop的配置文件:

    {
      "mcpServers": {
        "MedifinderMCP": {
          "command": "C:\\path\\to\\project\\venv\\Scripts\\python.exe",
          "args": ["C:\\path\\to\\project\\main.py"],
          "env": {
            "DB_HOST": "localhost",
            "DB_PORT": "5432",
            "DB_NAME": "medifinderbot",
            "DB_USER": "your_user",
            "DB_PASSWORD": "your_password",
            "DEBUG": "True",
            "ENV": "development",
            "SERVER_NAME": "MedifinderMCP",
            "SERVER_VERSION": "1.0.0",
            "MCP_SERVER_NAME": "MedifinderMCP",
            "MCP_SERVER_DESCRIPTION": "MCP server for medicine inventory queries",
            "MAX_SEARCH_RESULTS": "50",
            "SEARCH_SIMILARITY_THRESHOLD": "0.3"
          }
        }
      }
    }
    
  4. 在Claude Desktop中,从服务器下拉列表中选择MedifinderMCP服务器,以启用它进行对话。

故障排除

常见问题

  1. 数据库连接问题:

    • 使用 troubleshoot_connection 诊断连接问题的工具
    • 验证.env文件中的数据库凭据
    • 确保PostgreSQL在指定端口上运行
  2. 缺少表:

    • 使用 create_database_schema 创建数据库表的工具
    • 在架构创建过程中检查日志是否有任何错误
  3. 空结果:

    • 如果查询未返回结果,则表中可能没有任何数据
    • 您需要使用数据摄取过程将数据导入表中
  4. 会话绑定错误:

    • 如果您看到“实例未绑定到会话”错误,请确保在活动会话中将模型实例转换为字典
    • 有关示例,请参阅queries.py文件中如何处理此问题
  5. 缺少的依赖:

    • 跑 pip install -r requirements.txt 确保所有依赖项都已安装
    • 常见的缺失依赖项有:mcp、python-dotenv、psycopg2-binary

诊断工具

进行故障排除时,请使用以下内置诊断工具:

  1. diagnose_database:检查是否:

    • 数据库连接正常
    • 表存在
    • 表包含数据
  2. troubleshoot_connection:提供以下详细信息:

    • 数据库连接设置
    • 连接错误
    • 表格结构
    • 建议修复
  3. create_database_schema:创建数据库表并提供:

    • 创建的表列表
    • 发生的任何错误
    • 测试记录创建结果

许可证

MIT许可证

贡献者

  • 列宁卡拉斯科-初步工作