定时任务服务

MCP Cron Server 是一个独立的定时任务调度服务,支持一次性、间隔和Cron表达式三种调度类型,提供SQLite持久化、审批系统、心跳机制等功能,适用于需要自动化任务调度的场景。

  • by nolan57
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 nolan57/opencode-mcp-cron

使用说明

MCP Cron服务器

MCP Cron Server是一个独立的调度服务,通过模型上下文协议(MCP)提供类似Cron的作业调度。它与OpenCode集成,允许用户使用自然语言管理计划任务。

特性

  • 三种日程类型:一次(at)、间隔(every)、Cron表达式
  • SQLite持久化:用于高并发读/写的WAL模式
  • 审批系统:作业在执行前可能需要手动批准
  • 心跳机制:自动僵尸任务检测
  • 执行状态机:完成状态转换(待定→ 跑步→ 成功/失败/等待审批/暂停/取消)
  • 自动错误回退:30秒→ 1m → 5m → 15m → 60m
  • 并发控制:最多3个并发执行
  • 执行日志:缓冲日志记录以避免阻塞

建筑

┌─────────────────────────────────────────────────────────────┐
│                     OpenCode CLI                             │
│                         │                                    │
│         ┌───────────────┴───────────────┐                   │
│         ▼                               ▼                   │
│  ┌──────────────┐              ┌──────────────┐            │
│  │   MCP Client │              │    Skill     │            │
│  │   (14 tools) │              │  (mcp-cron)  │            │
│  └──────────────┘              └──────────────┘            │
└─────────────────────────────────────────────────────────────┘
          │ stdio
          ▼
┌─────────────────────────────────────────────────────────────┐
│                  MCP Cron Server                            │
│  ┌─────────────────────────────────────────────────────┐   │
│  │                  CronScheduler                       │   │
│  │  ┌─────────────┐  ┌─────────┐  ┌──────────────┐  │   │
│  │  │ Repository   │  │  Timer  │  │   Executor   │  │   │
│  │  │  (SQLite)   │  │(setTimeout)│ │(subprocess) │  │   │
│  │  │ +LogBuffer  │  └─────────┘  └──────────────┘  │   │
│  │  └─────────────┘                                    │   │
│  └─────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘

项目结构

opencode-mcp-cron/
├── package.json              # Project configuration
├── tsconfig.json            # TypeScript configuration
└── src/
    ├── index.ts             # MCP server entry point
    ├── types.ts            # Type definitions
    ├── database.ts          # SQLite database management
    ├── repository.ts        # Data access layer (with log buffer)
    ├── scheduler.ts         # Scheduler
    ├── executor.ts          # Execution engine
    └── schedule.ts          # Schedule time calculation

类型定义

计划类型

type CronSchedule =
  | { kind: 'at'; atMs: number }                    // One-time task
  | { kind: 'every'; everyMs: number; anchorMs?: number }  // Interval task
  | { kind: 'cron'; expr: string; tz?: string };     // Cron expression

有效载荷类型

type CronPayload = {
  kind: 'agentTurn' | 'systemEvent';
  message: string;           // Prompt or message content
  deliver?: boolean;         // Whether to deliver result
  channel?: string;         // Delivery channel
  to?: string;              // Delivery target
  model?: string;           // Model override
};

执行状态

type ExecutionStatus = 
  | 'pending'              // Waiting to execute
  | 'running'              // Currently executing
  | 'success'              // Executed successfully
  | 'failed'               // Execution failed
  | 'waiting_for_approval' // Waiting for approval
  | 'paused'               // Paused
  | 'cancelled';          // Cancelled

核心组件

1.数据库(database.ts)

SQLite数据库管理。

特征:

  • 用于高并发读/写的WAL模式
  • 自动架构迁移
  • 准备语句缓存
  • 地点: ~/.local/share/mcp-cron/cron.db

2.存储库(repository.ts)

数据访问层。

特征:

  • 作业/执行/日志/审批CRUD
  • 日志缓冲(批量写入以避免阻塞)
  • 原子态转换
  • 预处理语句

3.时间表(schedule.ts)

计划时间计算。

功能:

// Calculate next execution time
computeNextRunAtMs(schedule: CronSchedule, nowMs: number): number | undefined

// Format time for display
formatNextRun(nextRunAtMs: number | undefined): string

4.调度程序(scheduler.ts)

主调度程序。

特征:

  • 动态睡眠调度
  • 批处理速率限制
  • 状态机驱动
  • 并发控制
  • 僵尸任务检测

5.执行人(executor.ts)

作业执行引擎。

特征:

  • 子流程执行
  • 流媒体日志
  • 心跳更新
  • 超时控制
  • 审批触发

MCP工具

工具 说明
cron_add 添加新的计划作业
cron_list 列出所有作业
cron_get 获取作业详细信息
cron_update 更新作业
cron_remove 删除作业
cron_run 立即执行作业
cron_status 获取调度程序状态
cron_get_approvals 获取待审批
cron_approve 批准执行
cron_reject 拒绝执行
cron_get_logs 获取执行日志
cron_get_stats 获取系统统计信息
cron_get_history 获取执行历史记录
cron_list_executions 列出带分页的执行情况

用法

构建

cd ~/Documents/opencode-mcp-cron
npm install
npm run build

配置

添加到OpenCode配置:

{
  "mcp": {
    "cron": {
      "type": "local",
      "command": ["node", "/path/to/dist/index.js"],
      "enabled": true
    }
  }
}

Cron表达式示例

表达式 描述
0 8 * * * 每天早上8:00
0 9 * * 1-5 工作日上午9:00
0 18 * * 1-5 工作日下午6:00
0 */2 * * * 每2小时
0 0 * * * 每天午夜

数据存储

  • 数据库: ~/.local/share/mcp-cron/cron.db
  • 日志:存储在SQLite中 logs 桌子

环境变量

变量 描述 默认值
OPENCODE_COMMAND opencode命令路径 opencode
MCP_CRON_DB_PATH 数据库路径 ~/.local/share/mcp-cron/cron.db

故障排除

作业未执行

  1. 检查调度程序状态:
opencode run "use cron_status tool to check scheduler status"
  1. 列出作业:
opencode run "use cron_list tool to list all jobs"
  1. 检查执行日志:
opencode run "use cron_get_logs tool to view logs"

MCP服务器连接失败

  1. 验证配置路径
  2. 手动测试服务器:
node ~/Documents/opencode-mcp-cron/dist/index.js

最后更新时间:2026-03-10