mcp-cron
是一个基于 Model Context Protocol (MCP) 的定时任务控制器。它借助 cron 表达式,让用户能够轻松定义并管理定期任务,同时支持 shell 命令和 AI 任务,为任务调度提供了高效且灵活的解决方案。
mcp-cron
是一个基于 Model Context Protocol (MCP) 的定时任务控制器,允许用户通过 cron 表达式定义和管理定期任务,支持 shell 命令和 AI 任务。以下是快速开始使用该项目的步骤:
# 克隆仓库
git clone https://github.com/yourusername/mcp-cron.git
cd mcp-cron
# 安装依赖
go mod tidy
# 启动 MCP 服务器(默认监听 8080 端口)
go run cmd/mcp-cron/main.go
# 克隆仓库
git clone https://github.com/yourusername/mcp-cron.git
cd mcp-cron
# 安装依赖
go mod tidy
# 启动 MCP 服务器(默认监听 8080 端口)
go run cmd/mcp-cron/main.go
MCP 服务器负责处理与模型的交互,提供以下接口:
POST /api/v1/interact
:发送用户输入到模型,获取模型响应。GET /api/v1/ping
:检查服务器是否正常运行。{
"messages": [
{
"role": "user",
"content": "你的查询或指令"
}
],
"model_id": "gpt-3.5-turbo",
"temperature": 0.7,
"max_tokens": 1000
}
{
"choices": [
{
"message": {
"role": "assistant",
"content": "模型的响应"
},
"finish_reason": "stop",
"index": 0
}
],
"created": 1678945200,
"id": "chatcmpl-abcde123",
"model": "gpt-3.5-turbo"
}
定时任务控制器负责管理用户定义的任务,提供以下接口:
GET /tasks
:获取所有任务GET /tasks/:id
:获取特定任务信息POST /tasks
:添加新任务PUT /tasks/:id
:更新现有任务DELETE /tasks/:id
:删除任务{
"id": "task_1234567890",
"name": "示例任务",
"schedule": "0 */5 * * * *",
"command": "echo '任务执行中...'",
"prompt": "分析昨天的销售数据并提供摘要",
"type": "shell_command", // 或者 AI
"description": "一个每五分钟运行一次的任务示例",
"enabled": true,
"lastRun": "2025-01-01T12:00:00Z",
"nextRun": "2025-01-01T12:05:00Z",
"status": "completed",
"createdAt": "2025-01-01T00:00:00Z",
"updatedAt": "2025-01-01T12:00:00Z"
}
任务状态可以是以下之一:
pending
:未运行running
:正在运行completed
:已完成failed
:执行失败disabled
:已禁用mcp-cron/
├── cmd/ # 应用程序入口点
│ └── mcp-cron/
├── internal/ # 核心功能包
│ ├── agent/ # AI 代理执行逻辑
│ ├── command/ # shell 命令执行逻辑
│ ├── config/ # 配置处理
│ ├── errors/ # 错误处理
│ ├── logging/ # 日志工具
│ ├── model/ # 数据模型和类型
│ ├── scheduler/ # 任务调度器
│ └── server/ # MCP 服务器实现
└── scripts/ # 工具脚本
# 启动 MCP 服务器和定时任务控制器
go run cmd/mcp-cron/main.go
使用 curl
或 Postman 进行 API 测试:
curl http://localhost:8080/tasks
curl -X POST -H "Content-Type: application/json" -d '{"name":"我的第一个任务","schedule":"0 */1 * * * *","command":"echo \"测试成功!\""}' http://localhost:8080/tasks
项目使用 Go 模块进行依赖管理:
module mcp-cron
go 1.20
require (
github.com/gin-gonic/gin v1.9.1
github.com/segmentio/go-uuid v1.4.3
)
本项目遵循 MIT 许可证,详见 LICENSE 文件。
感谢您对 mcp-cron
的关注与支持!