本项目全面展示了如何将模型上下文协议(MCP)与 SQLite 数据库集成,并搭配 LlamaIndex 客户端使用。项目包含一个简单演示和一个功能完备的人力资源管理系统,为你呈现了如何通过 MCP 服务器将数据库操作作为工具暴露出来,并通过基于 LlamaIndex 的客户端与支持 Ollama 的大语言模型进行连接。
git clone https://github.com/ELISHA1994/mcp-llama-sqlite-bridge.git
cd mcp-llama-sqlite-bridge
python -m venv .venv
source .venv/bin/activate # 在 Windows 系统上使用:.venv\Scripts\activate
pip install -r requirements.txt
ollama pull llama3.2
python server.py --server_type=sse
python client.py
python hr_server.py --server_type=sse
python hr_client.py
客户端将执行以下操作:
# 简单演示示例
# 输入:添加一个名为 John Doe 的人,他 30 岁,职业是工程师
Enter your message: Add a person named John Doe who is 30 years old and works as an Engineer
# 输出:数据已成功添加到数据库
Agent: Data has been successfully added to the database.
# 输入:显示数据库中的所有人
Enter your message: Show me all people in the database
# 输出:以下是数据库中的所有人
Agent: Here are all the people in the database:
1. John Doe, 30 years old, Engineer
# 人力资源管理系统示例
# 输入:将 Sarah Johnson(sarah.johnson@company.com)添加到工程部门,职位为高级开发人员,薪水为 105,000 美元
HR Assistant> Add Sarah Johnson (sarah.johnson@company.com) to Engineering as Senior Developer with $105,000 salary
# 调用工具:添加员工
🔧 Calling tool: add_employee
# 输出:员工添加成功
✅ Employee added successfully!
- Employee ID: EMP00001
- Name: Sarah Johnson
# 输入:查询 Sarah Johnson 的休假余额
HR Assistant>
# 调用工具:检查员工休假余额
🔧 Calling tool: check_employee_leave_balance
Parameters: {
"employee_name": "Sarah Johnson"
}
# 输出:工具调用成功,显示休假余额
✅ Tool result received
**Leave Balance for Sarah Johnson (2024)**
- Annual Leave: 21 days remaining
- Sick Leave: 10 days remaining
# 输入:生成人力资源仪表盘
HR Assistant> Generate HR dashboard
# 调用工具:生成人力资源仪表盘
🔧 Calling tool: generate_hr_dashboard
# 输出:人力资源仪表盘摘要
**HR Dashboard Summary**
- Total Employees: 45
- Active: 42
- Departments: Engineering (15), Sales (10), Marketing (8)...
demo.db 包含一个 people 表:
| 属性 | 详情 |
|---|---|
| id | 整数类型,主键,自动递增 |
| name | 文本类型,不能为空 |
| age | 整数类型,不能为空 |
| profession | 文本类型,不能为空 |
hr_management.db 包含多个相互关联的表:
"Add Elisha Bello (elisha.bello@company.com) to Engineering as Senior Software Engineer with salary $420,000"
"Hire John Doe (john.doe@company.com) as Marketing Manager starting January 15, 2024"
"Onboard Alice Chen (alice.chen@company.com) to Sales as Account Executive, phone: 555-0123"
"List all employees"
"Find all employees in Engineering department"
"Show me employees hired in the last 6 months"
"Who reports to John Smith?"
"Search for Senior Engineers"
"Update John Doe's phone number to 555-9876"
"Transfer Sarah Johnson from Engineering to Product Management"
"Change Michael Brown's manager to Jennifer Wilson"
"Update Emily Davis's salary to $95,000 effective next month" ← 直接使用姓名!
"Check Sarah Johnson's leave balance" ← 直接使用员工姓名!
"Submit leave request for John Doe from Dec 20 to Dec 27" ← 使用姓名!
"Approve leave request #45"
"Show all pending leave requests"
"How many vacation days does Michael Brown have left?" ← 使用姓名!
"Generate compensation report for Engineering department"
"Show average salaries by position"
"Calculate total payroll for Q4 2024"
"List employees eligible for salary review"
"Compare department salary ranges"
"Show org chart for Engineering department"
"Generate HR dashboard"
"Analyze turnover for last quarter"
"Show diversity metrics"
"List employees with upcoming work anniversaries"
"Who are the top performers this quarter?"
"Create performance review for Sarah Johnson"
"Show pending performance reviews"
"Update performance rating for John Doe to 'Exceeds Expectations'"
"List employees due for annual review"
如需更多示例,请在 HR 客户端中输入 'help' 查看交互式帮助。
本项目展示了如何构建和使用 MCP 服务器,将数据库操作作为工具暴露出来,并通过基于 LlamaIndex 的客户端与支持 Ollama 的大语言模型进行连接。项目包含两个主要部分:简单演示和人力资源管理系统。
服务器和客户端均支持异步操作,提高了系统的性能和响应速度。
采用基于工具的方法进行数据库操作,使得系统更加灵活和可扩展。
本项目采用 MIT 许可证。
人力资源系统在添加新员工时必须提供电子邮件地址。不包含电子邮件的命令将失败。
❌ 错误示例:Add John Doe to Engineering as Developer
✅ 正确示例:Add John Doe (john.doe@company.com) to Engineering as Developer
如果不提供电子邮件地址,你将看到错误信息:❌ Tool failed: Missing required field: email
如果你遇到模块导入错误,请确保所有依赖项都已安装:
pip install -r requirements.txt
如果你收到关于模型不支持工具的错误,请确保你使用的是支持函数调用的模型(如 llama3.2、mistral 或 qwen2.5-coder)。
在启动客户端之前,请确保 MCP 服务器正在运行。
如果添加员工的命令不起作用:
❌ Tool failed: Missing required field: emailAdd Name (email@company.com) to Department as Position如果你在使用员工姓名时遇到错误: