Mcp Cron

Mcp Cron

🚀 MCP 定时任务控制器

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

✨ 主要特性

  • 任务管理:支持添加、删除、更新和查询任务,方便用户对任务进行全生命周期管理。
  • 任务类型:涵盖 shell 命令任务和 AI 任务,满足不同场景的需求。
  • 定时调度:使用 cron 表达式进行任务调度,支持秒级别精度,实现精准的任务执行。
  • 状态跟踪:每个任务都有详细的状态信息,包括运行状态、上次运行时间和下次运行时间等,便于用户实时掌握任务动态。

📦 安装指南

安装

# 克隆仓库
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 服务器

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 的关注与支持!

  • 0 关注
  • 0 收藏,8 浏览
  • system 提出于 2025-09-25 00:33

相似服务问题