医院MCP服务器

通过模型上下文协议(MCP)实现安全的PostgreSQL访问,为医院管理系统提供只读数据查询服务。

  • by SheshuGit
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 SheshuGit/MCP_Postgres

使用说明

🏥 医院MCP服务器

通过模型上下文协议(MCP)保护PostgreSQL访问
Claude Desktop充当MCP客户端

此项目实现了安全 模型上下文协议(MCP) 连接到基于PostgreSQL的医院管理系统的服务器。
Claude Desktop与此服务器通信,可以通过定义的只读MCP工具安全地查询医院数据。


📁 项目结构


MY-FIRST-MCP-SERVER/
│
├── .venv/                # Virtual environment
├── .env                  # Environment variables (DB credentials)
├── .gitignore
├── .python-version
├── main.py               # MCP server implementation
├── pyproject.toml        # Project dependencies (uv/poetry/pdm)
├── README.md             # This documentation
└── uv.lock               # Dependency lockfile


🗄️ 1.PostgreSQL设置

您必须创建 安全只读视图 在将数据暴露给Claude之前。

创建视图(安全、非敏感)

CREATE VIEW doctors_view AS
SELECT doctor_id, department_id, specialization
FROM Doctors;

CREATE VIEW departments_view AS
SELECT department_id, name, head_doctor_id
FROM Departments;

CREATE VIEW public_appointments_view AS
SELECT appointment_id, patient_id, doctor_id, appointment_date
FROM Appointments;

CREATE VIEW rooms_view AS
SELECT room_id, room_number, current_status
FROM Rooms;

CREATE VIEW billing_summary_view AS
SELECT bill_id, patient_id, appointment_id, admission_id, total_amount
FROM Billing;

🔐 2.创建受限数据库角色

CREATE ROLE mcp_readonly LOGIN PASSWORD 'your_password';

GRANT SELECT ON doctors_view TO mcp_readonly;
GRANT SELECT ON departments_view TO mcp_readonly;
GRANT SELECT ON public_appointments_view TO mcp_readonly;
GRANT SELECT ON rooms_view TO mcp_readonly;
GRANT SELECT ON billing_summary_view TO mcp_readonly;

REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM mcp_readonly;

ALTER ROLE mcp_readonly SET statement_timeout = '3s';
ALTER ROLE mcp_readonly SET idle_in_transaction_session_timeout = '2s';

只有视图被公开,原始PHI表受到保护。


⚙️ 3.环境变量(.env)

您的MCP服务器从这里读取DB凭据:

DB_HOST=localhost
DB_PORT=5432
DB_NAME=hospital_db
DB_USER=mcp_readonly
DB_PASSWORD=your_password

🧠 4.MCP服务器(main.py)

main.py 运行MCP服务器并公开以下工具:

  • get_doctor_by_id
  • list_doctors_by_department
  • get_appointments_for_doctor
  • check_room_status
  • get_patient_billing_summary

所有SQL查询都是:

✔ 参数化的 ✔ 只读的 ✔ 安全约束


🔌 5.安装依赖项

在项目文件夹中:

uv sync

或者,如果手动使用pip:

pip install -r requirements.txt

(依赖关系定义见 pyproject.toml)


🤝 6.将MCP服务器连接到克劳德桌面

Claude Desktop会自动检测放置在其扩展目录中的MCP服务器。

In your local file system "C:\Users\shesh\AppData\Roaming\Claude\claude_desktop_config.json" edit this file with your configurations

{
  "mcpServers": {
    "postgres": {
      "command": "C:\\Users\\shesh\\my-first-mcp-server\\.venv\\Scripts\\python.exe",
      "args": [
        "C:\\Users\\shesh\\my-first-mcp-server\\main.py",
        "stdio"
      ],
      "cwd": "C:\\Users\\shesh\\my-first-mcp-server",
      "env": {
        "DB_HOST": "localhost",
        "DB_PORT": "5432",
        "DB_NAME": "db_name",
        "DB_USER": "postgres",
        "DB_PASSWORD": "your_password"
      }
    }
  }
}

📍 步骤3--重新启动克劳德桌面

首选

克劳德桌面→ 设置→ 开发者 您应该看到:

hospital-mcp (running)

克劳德现在将:

  1. 启动您的 main.py MCP服务器
  2. 发现可用工具
  3. 在对话过程中自动呼叫他们

🧪 7.克劳德内部测试

在Claude Desktop中,您可以问:

Get the list of doctors in the cardiology department.

克劳德将致电:

list_doctors_by_department(department_id=1)

或者:

Show me appointments for doctor 3.

克劳德→ 调用您的MCP工具→ 您的数据库→ 安全输出返回。


🎯 总结

您现在拥有:

✔ A工作 Python MCP服务器 ✔ 安全 PostgreSQL集成 ✔ 完全接线 Claude桌面工具访问 ✔ 安全的只读医院数据视图 ✔ 清洁项目结构

Claude Desktop与您的数据库通信 只能通过安全的MCP工具,从不直接访问原始表。