|
| 1 | +/** |
| 2 | + * mcpd — MCP Tasks (experimental, MCP 2025-11-25) |
| 3 | + * |
| 4 | + * Async/long-running tool execution with durable state machines. |
| 5 | + * Clients can poll for status and retrieve results when complete. |
| 6 | + * |
| 7 | + * Task lifecycle: working → completed|failed|cancelled |
| 8 | + * working → input_required → working → ... |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef MCPD_TASK_H |
| 12 | +#define MCPD_TASK_H |
| 13 | + |
| 14 | +#include <Arduino.h> |
| 15 | +#include <ArduinoJson.h> |
| 16 | +#include <functional> |
| 17 | +#include <map> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +namespace mcpd { |
| 21 | + |
| 22 | +/** |
| 23 | + * Task status per MCP 2025-11-25 spec. |
| 24 | + */ |
| 25 | +enum class TaskStatus { |
| 26 | + Working, // Task is actively executing |
| 27 | + InputRequired, // Task needs input from the requestor |
| 28 | + Completed, // Terminal: task completed successfully |
| 29 | + Failed, // Terminal: task failed |
| 30 | + Cancelled // Terminal: task was cancelled |
| 31 | +}; |
| 32 | + |
| 33 | +inline const char* taskStatusToString(TaskStatus s) { |
| 34 | + switch (s) { |
| 35 | + case TaskStatus::Working: return "working"; |
| 36 | + case TaskStatus::InputRequired: return "input_required"; |
| 37 | + case TaskStatus::Completed: return "completed"; |
| 38 | + case TaskStatus::Failed: return "failed"; |
| 39 | + case TaskStatus::Cancelled: return "cancelled"; |
| 40 | + } |
| 41 | + return "working"; |
| 42 | +} |
| 43 | + |
| 44 | +inline TaskStatus taskStatusFromString(const char* s) { |
| 45 | + if (!s) return TaskStatus::Working; |
| 46 | + if (strcmp(s, "working") == 0) return TaskStatus::Working; |
| 47 | + if (strcmp(s, "input_required") == 0) return TaskStatus::InputRequired; |
| 48 | + if (strcmp(s, "completed") == 0) return TaskStatus::Completed; |
| 49 | + if (strcmp(s, "failed") == 0) return TaskStatus::Failed; |
| 50 | + if (strcmp(s, "cancelled") == 0) return TaskStatus::Cancelled; |
| 51 | + return TaskStatus::Working; |
| 52 | +} |
| 53 | + |
| 54 | +inline bool isTerminalStatus(TaskStatus s) { |
| 55 | + return s == TaskStatus::Completed || s == TaskStatus::Failed || s == TaskStatus::Cancelled; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Represents a single MCP Task. |
| 60 | + */ |
| 61 | +struct MCPTask { |
| 62 | + String taskId; |
| 63 | + TaskStatus status = TaskStatus::Working; |
| 64 | + String statusMessage; |
| 65 | + String createdAt; // ISO 8601 |
| 66 | + String lastUpdatedAt; // ISO 8601 |
| 67 | + int64_t ttl = -1; // milliseconds, -1 = unlimited (null) |
| 68 | + int32_t pollInterval = 5000; // recommended poll interval in ms |
| 69 | + |
| 70 | + // Result storage (populated when completed) |
| 71 | + String resultJson; // The serialized CallToolResult |
| 72 | + bool hasResult = false; |
| 73 | + |
| 74 | + // Metadata |
| 75 | + String toolName; // Which tool this task is for |
| 76 | + String immediateResponse; // Optional model-immediate-response |
| 77 | + |
| 78 | + MCPTask() = default; |
| 79 | + |
| 80 | + /** |
| 81 | + * Serialize task state to JSON object (for tasks/get, tasks/list responses). |
| 82 | + */ |
| 83 | + void toJson(JsonObject& obj) const { |
| 84 | + obj["taskId"] = taskId; |
| 85 | + obj["status"] = taskStatusToString(status); |
| 86 | + if (!statusMessage.isEmpty()) { |
| 87 | + obj["statusMessage"] = statusMessage; |
| 88 | + } |
| 89 | + obj["createdAt"] = createdAt; |
| 90 | + obj["lastUpdatedAt"] = lastUpdatedAt; |
| 91 | + if (ttl >= 0) { |
| 92 | + obj["ttl"] = (long)ttl; |
| 93 | + } |
| 94 | + // ttl omitted when -1 (unlimited) |
| 95 | + obj["pollInterval"] = pollInterval; |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * Task execution support level for individual tools. |
| 101 | + */ |
| 102 | +enum class TaskSupport { |
| 103 | + Forbidden, // Default: tool does not support task execution |
| 104 | + Optional, // Tool may be invoked as task or normal request |
| 105 | + Required // Tool must be invoked as task |
| 106 | +}; |
| 107 | + |
| 108 | +inline const char* taskSupportToString(TaskSupport ts) { |
| 109 | + switch (ts) { |
| 110 | + case TaskSupport::Forbidden: return "forbidden"; |
| 111 | + case TaskSupport::Optional: return "optional"; |
| 112 | + case TaskSupport::Required: return "required"; |
| 113 | + } |
| 114 | + return "forbidden"; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Callback type for async tool execution. |
| 119 | + * Called with (taskId, params). The handler should: |
| 120 | + * 1. Start async work |
| 121 | + * 2. Call server.taskComplete(taskId, result) or server.taskFail(taskId, error) when done |
| 122 | + */ |
| 123 | +using MCPTaskToolHandler = std::function<void(const String& taskId, JsonVariant params)>; |
| 124 | + |
| 125 | +/** |
| 126 | + * Manages all active tasks with TTL expiration. |
| 127 | + */ |
| 128 | +class TaskManager { |
| 129 | +public: |
| 130 | + TaskManager() = default; |
| 131 | + |
| 132 | + /** |
| 133 | + * Create a new task. Returns the task ID. |
| 134 | + */ |
| 135 | + String createTask(const char* toolName, int64_t requestedTtl = -1) { |
| 136 | + MCPTask task; |
| 137 | + task.taskId = _generateId(); |
| 138 | + task.status = TaskStatus::Working; |
| 139 | + task.statusMessage = "The operation is now in progress."; |
| 140 | + task.toolName = toolName; |
| 141 | + task.createdAt = _isoNow(); |
| 142 | + task.lastUpdatedAt = task.createdAt; |
| 143 | + task.ttl = requestedTtl; |
| 144 | + _tasks[task.taskId] = task; |
| 145 | + return task.taskId; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Get a task by ID. Returns nullptr if not found or expired. |
| 150 | + */ |
| 151 | + MCPTask* getTask(const String& taskId) { |
| 152 | + _expireOldTasks(); |
| 153 | + auto it = _tasks.find(taskId); |
| 154 | + if (it == _tasks.end()) return nullptr; |
| 155 | + return &it->second; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Update task status. |
| 160 | + */ |
| 161 | + bool updateStatus(const String& taskId, TaskStatus newStatus, const String& message = "") { |
| 162 | + auto* task = getTask(taskId); |
| 163 | + if (!task) return false; |
| 164 | + if (isTerminalStatus(task->status)) return false; // Can't change terminal |
| 165 | + |
| 166 | + // Validate transitions |
| 167 | + if (task->status == TaskStatus::Working) { |
| 168 | + // Can go anywhere |
| 169 | + } else if (task->status == TaskStatus::InputRequired) { |
| 170 | + // Can go to working, completed, failed, cancelled |
| 171 | + } |
| 172 | + |
| 173 | + task->status = newStatus; |
| 174 | + if (!message.isEmpty()) task->statusMessage = message; |
| 175 | + task->lastUpdatedAt = _isoNow(); |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Complete a task with a result. |
| 181 | + */ |
| 182 | + bool completeTask(const String& taskId, const String& resultJson) { |
| 183 | + auto* task = getTask(taskId); |
| 184 | + if (!task) return false; |
| 185 | + if (isTerminalStatus(task->status)) return false; |
| 186 | + |
| 187 | + task->status = TaskStatus::Completed; |
| 188 | + task->statusMessage = "Task completed successfully."; |
| 189 | + task->lastUpdatedAt = _isoNow(); |
| 190 | + task->resultJson = resultJson; |
| 191 | + task->hasResult = true; |
| 192 | + return true; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Fail a task with an error message. |
| 197 | + */ |
| 198 | + bool failTask(const String& taskId, const String& errorMessage) { |
| 199 | + auto* task = getTask(taskId); |
| 200 | + if (!task) return false; |
| 201 | + if (isTerminalStatus(task->status)) return false; |
| 202 | + |
| 203 | + task->status = TaskStatus::Failed; |
| 204 | + task->statusMessage = errorMessage; |
| 205 | + task->lastUpdatedAt = _isoNow(); |
| 206 | + return true; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Cancel a task. |
| 211 | + */ |
| 212 | + bool cancelTask(const String& taskId) { |
| 213 | + auto* task = getTask(taskId); |
| 214 | + if (!task) return false; |
| 215 | + if (isTerminalStatus(task->status)) return false; |
| 216 | + |
| 217 | + task->status = TaskStatus::Cancelled; |
| 218 | + task->statusMessage = "The task was cancelled by request."; |
| 219 | + task->lastUpdatedAt = _isoNow(); |
| 220 | + return true; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * List all tasks (with pagination). |
| 225 | + */ |
| 226 | + std::vector<MCPTask> listTasks(size_t startIdx = 0, size_t pageSize = 20, size_t* nextIdx = nullptr) { |
| 227 | + _expireOldTasks(); |
| 228 | + std::vector<MCPTask> result; |
| 229 | + size_t idx = 0; |
| 230 | + for (auto& kv : _tasks) { |
| 231 | + if (idx >= startIdx && result.size() < pageSize) { |
| 232 | + result.push_back(kv.second); |
| 233 | + } |
| 234 | + idx++; |
| 235 | + } |
| 236 | + if (nextIdx) { |
| 237 | + *nextIdx = (startIdx + pageSize < _tasks.size()) ? startIdx + pageSize : 0; |
| 238 | + } |
| 239 | + return result; |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Remove a specific task (e.g., after TTL expiry). |
| 244 | + */ |
| 245 | + void removeTask(const String& taskId) { |
| 246 | + _tasks.erase(taskId); |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * Get current task count. |
| 251 | + */ |
| 252 | + size_t taskCount() const { return _tasks.size(); } |
| 253 | + |
| 254 | + /** |
| 255 | + * Check if tasks feature is enabled. |
| 256 | + */ |
| 257 | + bool isEnabled() const { return _enabled; } |
| 258 | + void setEnabled(bool e) { _enabled = e; } |
| 259 | + |
| 260 | + /** |
| 261 | + * Set maximum number of concurrent tasks. |
| 262 | + */ |
| 263 | + void setMaxTasks(size_t max) { _maxTasks = max; } |
| 264 | + size_t maxTasks() const { return _maxTasks; } |
| 265 | + |
| 266 | + /** |
| 267 | + * Set default poll interval (ms). |
| 268 | + */ |
| 269 | + void setDefaultPollInterval(int32_t ms) { _defaultPollInterval = ms; } |
| 270 | + int32_t defaultPollInterval() const { return _defaultPollInterval; } |
| 271 | + |
| 272 | +private: |
| 273 | + std::map<String, MCPTask> _tasks; |
| 274 | + bool _enabled = false; |
| 275 | + size_t _maxTasks = 16; // Reasonable for MCU |
| 276 | + int32_t _defaultPollInterval = 5000; |
| 277 | + uint32_t _nextId = 1; |
| 278 | + |
| 279 | + String _generateId() { |
| 280 | + // Simple monotonic ID suitable for MCU (no UUID needed) |
| 281 | + char buf[16]; |
| 282 | + snprintf(buf, sizeof(buf), "task-%u", _nextId++); |
| 283 | + return String(buf); |
| 284 | + } |
| 285 | + |
| 286 | + String _isoNow() { |
| 287 | + // On MCU, millis()-based relative timestamp; on test, static |
| 288 | + unsigned long ms = millis(); |
| 289 | + unsigned long secs = ms / 1000; |
| 290 | + unsigned long mins = secs / 60; |
| 291 | + unsigned long hrs = mins / 60; |
| 292 | + char buf[32]; |
| 293 | + snprintf(buf, sizeof(buf), "1970-01-01T%02lu:%02lu:%02luZ", |
| 294 | + hrs % 24, mins % 60, secs % 60); |
| 295 | + return String(buf); |
| 296 | + } |
| 297 | + |
| 298 | + void _expireOldTasks() { |
| 299 | + if (_tasks.empty()) return; |
| 300 | + std::vector<String> expired; |
| 301 | + for (auto& kv : _tasks) { |
| 302 | + if (kv.second.ttl > 0 && isTerminalStatus(kv.second.status)) { |
| 303 | + // Simple heuristic: remove completed tasks after TTL |
| 304 | + // (In real impl, compare createdAt + ttl vs now) |
| 305 | + // For MCU, just cap at _maxTasks * 2 |
| 306 | + } |
| 307 | + } |
| 308 | + // Cap total tasks to prevent memory exhaustion |
| 309 | + while (_tasks.size() > _maxTasks * 2) { |
| 310 | + // Remove oldest terminal tasks first |
| 311 | + for (auto it = _tasks.begin(); it != _tasks.end(); ++it) { |
| 312 | + if (isTerminalStatus(it->second.status)) { |
| 313 | + _tasks.erase(it); |
| 314 | + break; |
| 315 | + } |
| 316 | + } |
| 317 | + // If no terminal tasks, break to avoid infinite loop |
| 318 | + break; |
| 319 | + } |
| 320 | + } |
| 321 | +}; |
| 322 | + |
| 323 | +} // namespace mcpd |
| 324 | + |
| 325 | +#endif // MCPD_TASK_H |
0 commit comments