使用说明
A progressive Node.js framework for building efficient and scalable server-side applications.
统计MCP服务器
一种模型上下文协议(MCP)服务器,为销售和工资数据提供统计分析工具。此服务器公开了可供AI助手和其他MCP兼容客户端使用的商业智能功能。
🚀 特性
- 业务销售分析:计算总销售额,查找热门产品,分析订单模式
- 产品情报:确定订购最多的产品和价值最高的订单
- 薪资统计:计算工资指标,包括平均值、最小值/最大值
- MCP兼容:与Claude Desktop和其他MCP客户端配合使用
- TypeScript:全型安全和现代开发经验
- NestJS 框架:使用企业级Node.js框架构建
📊 可用工具
| 工具名称 | 描述 | 参数 |
|---|---|---|
get_total_sales |
计算所有订单的总销售额 | 无 |
get_popular_product |
获取最常订购的产品 | 无 |
get_most_ordered_product |
获取总量最高的产品 | 无 |
get_highest_order_amount |
获取总金额最高的订单 | 无 |
get_salary_statistics |
获取薪资分析(平均、最低、最高) | 无 |
🏗️ 项目结构
mcp-server/
├── src/
│ ├── statistics/
│ │ ├── statistics.service.ts # Business logic and data operations
│ │ ├── statistics.controller.ts # REST API endpoints (NestJS)
│ │ ├── statistics.module.ts # NestJS module configuration
│ │ └── statistics.types.ts # TypeScript interfaces
│ ├── mcp/
│ │ └── mcp-server.ts # MCP server implementation
│ ├── mcp-main.ts # MCP server entry point
│ ├── main.ts # NestJS application entry point
│ └── app.* # NestJS app configuration
├── test/ # E2E tests
├── dist/ # Compiled JavaScript output
├── mcp-config.json # MCP client configuration
├── package.json # Dependencies and scripts
└── README.md # This file
🛠️ 技术栈
- 运行时:Node.js
- 语言:TypeScript
- 框架:NestJS
- 协议:模型上下文协议(MCP)
- 测试:杰斯特
- 代码检查ESLint
📋 先决条件
- Node.js(v18或更高版本)
- npm或yarn包管理器
⚡ 快速开始
1.安装依赖项
npm install
2.建设项目
npm run build
3.启动MCP服务器
npm run start:mcp
# or directly:
node dist/mcp-main.js
4.启动NestJS服务器(备选)
npm run start
# Development mode:
npm run start:dev
🔧 配置
MCP客户端配置
将此配置添加到您的MCP客户端(例如,Claude Desktop):
{
"mcpServers": {
"statistics-server": {
"command": "node",
"args": ["/home/mamun/official/mcp-server/dist/mcp-main.js"],
"env": {}
}
}
}
适用于克劳德桌面
增添 ~/.config/claude-desktop/claude_desktop_config.json (Linux):
{
"mcpServers": {
"statistics-server": {
"command": "node",
"args": ["/home/mamun/official/mcp-server/dist/mcp-main.js"],
"env": {}
}
}
}
🧪 测试
运行单元测试
npm run test
运行E2E测试
npm run test:e2e
使用MCP检查员进行测试
npx @modelcontextprotocol/inspector dist/mcp-main.js
📡 API端点(NestJS)
服务器还提供REST API端点:
| 方法 | 端点 | 描述 |
|---|---|---|
| 得到 | /statistics/total-sales |
获取总销售额 |
| 得到 | /statistics/popular-product |
获取最受欢迎的产品 |
| 得到 | /statistics/most-ordered-product |
获得订购最多的产品 |
| 得到 | /statistics/highest-order-amount |
获得最高订单金额 |
| 得到 | /statistics/salary-statistics |
获取薪资统计数据 |
API使用示例
# Start the NestJS server
npm run start
# Make API requests
curl http://localhost:3000/statistics/total-sales
curl http://localhost:3000/statistics/popular-product
🔄 MCP协议流
1. AI Client connects to MCP server
2. Client requests available tools
3. Server responds with tool definitions
4. Client calls a specific tool (e.g., "get_total_sales")
5. Server executes the corresponding service method
6. Server returns structured result
7. AI uses the result in its response to user
📊 样品数据
服务器包括用于演示的示例数据:
产品
- 笔记本电脑(999.99美元)
- 鼠标(24.99美元)
- 键盘(79.99美元)
- 显示器(299.99美元)
- 耳机(149.99美元)
订单
- 不同产品和数量的多个订单
- 所有订单的总销售额计算
工资
- 用于统计分析的员工薪资数据样本
🚀 发展
发展模式
npm run start:dev
为生产而建
npm run build
npm run start:prod
代码检查
npm run lint
npm run lint:fix
🔧 可用脚本
| 脚本 | 描述 |
|---|---|
npm run build |
将TypeScript编译为JavaScript |
npm run start |
启动NestJS应用程序 |
npm run start:dev |
以热重新加载的开发模式启动 |
npm run start:debug |
以调试模式启动 |
npm run start:prod |
以生产模式启动 |
npm run start:mcp |
启动MCP服务器 |
npm run lint |
运行ESLint |
npm run test |
运行单元测试 |
npm run test:watch |
在监视模式下运行测试 |
npm run test:e2e |
运行端到端测试 |
🏆 已实施的最佳实践
- 关注点分离:服务中的业务逻辑,专用服务器中的MCP逻辑
- 类型安全:具有适当接口的完整TypeScript实现
- 错误处理:MCP工具中的全面错误处理
- 验证:所有操作的输入验证
- 文档:清晰的文件和示例
- 测试:单元和E2E测试设置
- 代码质量:ESLint配置,实现一致的代码风格
🤝 贡献
- 分叉存储库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
📝 许可证
该项目根据MIT许可证获得许可。
📞 支持
如有疑问和支持:
🎯 你学到了什么
通过研究这个项目,你现在明白了:
- ✅ MCP基础知识:什么是MCP及其工作原理
- ✅ 服务器架构:如何构建MCP服务器项目
- ✅ 工具实施:如何将业务逻辑作为MCP工具公开
- ✅ 客户端集成:AI客户端如何连接和使用您的工具
- ✅ TypeScript开发:使用NestJS的现代TypeScript
- ✅ 测试:如何测试MCP服务器
- ✅ 部署:如何构建和部署MCP服务器
🚀 后续步骤
- 添加身份验证:为您的MCP服务器实施安全措施
- 数据库集成:连接到真实数据库而不是示例数据
- 更多工具:添加其他统计分析工具
- 资源:实现文件/数据访问的MCP资源
- 提示:为AI交互模板添加MCP提示
- 生产部署:部署到云平台
- 监控:添加日志记录和监控功能
编码愉快! 🎉
此MCP服务器演示了从零到英雄的完整实现。将其用作构建自己的MCP服务器的基础!
