RESTful API to query laboratory inventory data from MongoDB
Laboratory Inventory HTTP Server
这是一个独立的 HTTP 服务器,用于提供实验室库存数据的 REST API 接口。通过 HTTP 端点查询 MongoDB 中的库存数据,可被任何 HTTP 客户端调用。
功能特性
✅ RESTful API - 标准的 HTTP GET 端点,易于集成 ✅ Token 优化 - 只查询需要的数据,节省 80-98% 的 Token 消耗 ✅ MongoDB 集成 - 直接查询 MongoDB 数据库 ✅ 独立运行 - 不依赖主应用,可单独部署 ✅ 灵活查询 - 提供多种查询方式,支持按条件筛选
可用 API 端点
| 端点 | 描述 | 使用场景 |
|---|---|---|
GET /tools/inventory-summary |
获取库存统计摘要 | 查看整体库存状况 |
GET /tools/expired-items |
获取已过期耗材 | 查找过期耗材 |
GET /tools/expiring-items |
获取即将过期耗材 | 查找快过期的耗材 |
GET /tools/low-stock-items |
获取库存不足耗材 | 查找低库存耗材 |
GET /tools/out-of-stock-items |
获取缺货耗材 | 查找缺货项目 |
GET /tools/search-inventory |
根据关键词搜索耗材 | 搜索特定耗材 |
GET /tools/check-item |
检查耗材可用性 | 查询特定耗材详情 |
GET /tools/purchase-suggestions |
获取采购建议 | 获取采购推荐 |
GET /health |
健康检查 | 检查服务状态 |
快速开始
1. 安装依赖
cd mcp
npm install
2. 配置环境变量
复制示例配置文件并修改:
# 复制示例配置文件
cp .env.example .env
# 编辑 .env 文件,设置你的 MongoDB 连接
.env 文件内容:
MONGODB_URI=mongodb://localhost:27017/test
PORT=3001
⚠️ 重要: .env 文件包含敏感信息,已在 .gitignore 中,不会提交到 git。
3. 启动服务器
npm start
服务器将在 http://localhost:3001 上运行(默认端口)。
HTTP API 使用指南
基础 URL
http://localhost:3001
端点详解
1. 获取库存统计摘要
GET /tools/inventory-summary?labName=<实验室名>
示例:
curl "http://localhost:3001/tools/inventory-summary?labName=实验室A"
响应:
{
"success": true,
"queryType": "summary",
"data": {
"totalItems": 128,
"normalItems": 98,
"expired": 5,
"expiringSoon": 12,
"lowStock": 8,
"outOfStock": 5,
"issueItems": [
{
"name": "胰蛋白酶",
"quantity": 3,
"minQuantity": 5,
"expiryDate": "2024-03-15",
"status": "库存不足"
}
]
}
}
2. 获取已过期耗材
GET /tools/expired-items?labName=<实验室名>&limit=5
示例:
curl "http://localhost:3001/tools/expired-items?labName=实验室A&limit=10"
响应:
{
"success": true,
"queryType": "expired",
"count": 3,
"items": [
{
"name": "PBS缓冲液",
"quantity": 2,
"expiryDate": "2024-01-15"
}
]
}
3. 获取即将过期耗材
GET /tools/expiring-items?labName=<实验室名>&days=30&limit=10
参数:
days: 天数范围(默认30天)limit: 返回数量限制(默认10)
示例:
curl "http://localhost:3001/tools/expiring-items?labName=实验室A&days=30&limit=10"
响应:
{
"success": true,
"queryType": "expiring",
"count": 5,
"items": [
{
"name": "胎牛血清",
"quantity": 8,
"expiryDate": "2024-04-01",
"daysLeft": 15
}
]
}
4. 获取库存不足耗材
GET /tools/low-stock-items?labName=<实验室名>&limit=10
示例:
curl "http://localhost:3001/tools/low-stock-items?labName=实验室A"
响应:
{
"success": true,
"queryType": "low_stock",
"count": 8,
"items": [
{
"name": "胰蛋白酶",
"current": 3,
"min": 5,
"deficit": 2
}
]
}
5. 获取缺货耗材
GET /tools/out-of-stock-items?labName=<实验室名>&limit=5
示例:
curl "http://localhost:3001/tools/out-of-stock-items?labName=实验室A"
响应:
{
"success": true,
"queryType": "out_of_stock",
"count": 5,
"items": [
{"name": "细胞培养板"}
]
}
6. 搜索库存
GET /tools/search-inventory?labName=<实验室名>&keyword=<关键词>&limit=5
参数:
keyword: 搜索关键词(匹配名称、编码、规格)limit: 返回数量限制(默认5)
示例:
curl "http://localhost:3001/tools/search-inventory?labName=实验室A&keyword=胰蛋白"
响应:
{
"success": true,
"queryType": "specific",
"count": 2,
"items": [
{
"name": "胰蛋白酶",
"quantity": 3,
"minQuantity": 5,
"expiryDate": "2024-06-01",
"status": "low_stock"
}
]
}
7. 检查特定耗材
GET /tools/check-item?labName=<实验室名>&itemName=<耗材名>
示例:
curl "http://localhost:3001/tools/check-item?labName=实验室A&itemName=胰蛋白酶"
响应:
{
"success": true,
"found": true,
"queryType": "check",
"item": {
"name": "胰蛋白酶",
"quantity": 3,
"minQuantity": 5,
"expiryDate": "2024-06-01",
"status": "low_stock",
"statusMessage": "库存不足"
}
}
8. 获取采购建议
GET /tools/purchase-suggestions?labName=<实验室名>
示例:
curl "http://localhost:3001/tools/purchase-suggestions?labName=实验室A"
响应:
{
"success": true,
"queryType": "purchase",
"summary": {
"totalItems": 128
},
"lowStock": [
{
"name": "胰蛋白酶",
"current": 3,
"min": 5,
"deficit": 2
}
],
"expiringSoon": [
{
"name": "胎牛血清",
"quantity": 8,
"expiryDate": "2024-04-01",
"daysLeft": 15
}
]
}
9. 健康检查
GET /health
示例:
curl "http://localhost:3001/health"
响应:
{
"status": "ok",
"service": "MCP HTTP Server for Lab Inventory",
"timestamp": "2024-03-11T08:00:00.000Z"
}
错误处理
所有端点在发
Tools (8)
inventory-summaryGet a summary of overall inventory statusexpired-itemsRetrieve a list of expired laboratory itemsexpiring-itemsRetrieve a list of items nearing their expiration datelow-stock-itemsRetrieve a list of items with low stock levelsout-of-stock-itemsRetrieve a list of items that are currently out of stocksearch-inventorySearch for specific items by keywordcheck-itemCheck the availability and status of a specific itempurchase-suggestionsGet recommendations for items that need to be purchasedEnvironment Variables
MONGODB_URIrequiredThe connection string for the MongoDB databasePORTThe port number for the HTTP serverConfiguration
{ "mcpServers": { "lab-inventory": { "command": "node", "args": ["/path/to/mcp/index.js"], "env": { "MONGODB_URI": "mongodb://localhost:27017/test", "PORT": "3001" } } } }