自然语言待办应用

一款基于自然语言处理的待办事项管理应用,支持实时更新和MongoDB数据存储。

  • by theatulanand
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 theatulanand/to-do-mcp-server

使用说明

使用MCP服务器的Todo应用程序

一个简单的Todo应用程序,使用Google Gemini API、用于持久性的MongoDB和用于通过Server-Sent事件(SSE)实时更新的MCP(模型上下文协议)进行自然语言处理。使用Express.js的模块化MVC架构构建。


特性

  • 使用自然语言添加待办事项(例如,“添加购买牛奶的任务”)
  • 列出待办事项(可选择包括已完成的待办事项)
  • 使用他们的MongoDB ID将待办事项标记为完成
  • 按MongoDB ID删除待办事项
  • 使用MCP的服务器发送事件(SSE)接收实时更新

技术栈

  • Node.js:服务器运行时
  • Express.js:路由和中间件
  • MongoDB(Atlas):基于云的NoSQL数据库
  • Mongoose:MongoDB的ODM
  • Google Gemini API:处理自然语言命令解释
  • @modelcontextprotocol/sdk:用于MCP集成和SSE
  • dotenv:用于管理环境变量

先决条件


安装

  1. 克隆存储库

    git clone https://github.com/theatulanand/to-do-mcp-server.git
    cd todo-mcp-single
    
  2. 安装依赖项

    npm install
    
  3. 设置环境变量

    创建一个 .env 根目录中的文件:

    PORT=3000
    MONGO_URI=URI
    GEMINI_API_KEY=
    

运行应用程序

启动服务器:

npm start

您应该看到:

Connected to MongoDB with Mongoose
Server running on http://localhost:3000

要停止服务器,请按 Ctrl + C.


如何使用

向发送POST请求 /api/command 使用JSON正文:

{ "command": "Add a task to buy milk" }

示例命令

  • 添加:

    curl -X POST http://localhost:3000/api/command -H "Content-Type: application/json" -d '{"command": "Add a task to buy milk"}'
    
  • 列表:

    curl -X POST http://localhost:3000/api/command -H "Content-Type: application/json" -d '{"command": "List my todos"}'
    
  • 完成:

    curl -X POST http://localhost:3000/api/command -H "Content-Type: application/json" -d '{"command": "Complete todo with ID some_mongodb_id"}'
    
  • 删除:

    curl -X POST http://localhost:3000/api/command -H "Content-Type: application/json" -d '{"command": "Delete todo with ID some_mongodb_id"}'
    
  • 清单包括已完成的:

    curl -X POST http://localhost:3000/api/command -H "Content-Type: application/json" -d '{"command": "List my todos including completed"}'
    

实时更新(SSE)

连接到 /sse 接收实时更新。(需要MCP兼容客户端。)


项目结构

todo-mcp-single/
├── models/           # Mongoose schemas
│   └── Todo.js
├── controllers/      # Business logic
│   └── todoController.js
├── routes/           # API routes
│   └── todoRoutes.js
├── utils/            # Utility functions (e.g., Gemini integration)
│   └── gemini.js
├── .env              # Environment configuration
└── server.js         # App entry point

故障排除

  • MongoDB连接失败:查看Atlas中的MONGO_URI和IP白名单。
  • Gemini API错误:验证API密钥和对的网络访问 generativelanguage.googleapis.com.
  • 无法识别的命令:检查日志中的Gemini原始输出以进行调试。