feat(worker): batch MCP tools for the external endpoint
Add seven best-effort batch variants of the single-entity external MCP tools: batch_get_tasks, batch_add_tasks, batch_update_task_status, batch_cancel_tasks, batch_delete_tasks, batch_set_my_day, and batch_cleanup_task_worktrees. Each loops the existing ExternalMcpService methods sequentially (scoped DbContext is not thread-safe), returns a per-item result array so a failing item never aborts the rest, and rejects empty or over-100-item batches. Merge/review stay single-task.
This commit is contained in:
@@ -34,6 +34,7 @@ Interfaces (e.g. `IQueueWaker`, `IPrimeClock`, `ITaskStateService`) live in an `
|
||||
- **StaleTaskRecovery** — startup-only service; calls `TaskStateService.RecoverStaleRunningAsync` to flip orphaned `Running` rows to `Failed`.
|
||||
- **External/*** — always-on MCP tools for general Claude sessions, scoped to *starting* and *observing* sessions (no worktree/merge, multi-turn, planning, or app-settings writes). Auth via optional `X-ClaudeDo-Key` header. Registered explicitly in `Program.cs`'s external app via `.WithTools<T>()`. Organized by concern:
|
||||
- `ExternalMcpService` — task CRUD + execution: `ListTaskLists`, `ListTasks`, `GetTask`, `AddTask`, `AddSubtask`, `UpdateTask`, `UpdateTaskStatus` (`Idle` / `Queued`), `GetTaskStatusValues`, `ReviewTask` (`approve` / `reject_rerun` / `reject_park` / `cancel` for a WaitingForReview task), `RunTaskNow`, `ContinueTask`, `CancelTask`, `DeleteTask`; worktree/git: `GetTaskWorktree`, `GetTaskDiff`, `MergeTask`, `ListWorktrees`, `CleanupTaskWorktree`
|
||||
- `BatchMcpTools` — best-effort batch variants that loop the `ExternalMcpService` single-entity methods (sequential — the scoped DbContext is not thread-safe; merge/review stay single-task): `BatchGetTasks`, `BatchAddTasks`, `BatchUpdateTaskStatus`, `BatchCancelTasks`, `BatchDeleteTasks`, `BatchSetMyDay`, `BatchCleanupTaskWorktrees`. Every tool returns a per-item result array ({ id/index, ok, error?, … }) — a failing item never aborts the rest — and rejects batches over 100 items.
|
||||
- `ListMcpTools` — `CreateList`, `UpdateList`, `DeleteList`
|
||||
- `ConfigMcpTools` — `GetListConfig`, `SetListConfig`, `GetTaskConfig`, `SetTaskConfig`
|
||||
- `RunHistoryMcpTools` — `ListRuns`, `GetRun`, `GetTaskLog` (latest run's log, tail-capped at 256 KB)
|
||||
|
||||
225
src/ClaudeDo.Worker/External/BatchMcpTools.cs
vendored
Normal file
225
src/ClaudeDo.Worker/External/BatchMcpTools.cs
vendored
Normal file
@@ -0,0 +1,225 @@
|
||||
using System.ComponentModel;
|
||||
using ModelContextProtocol.Server;
|
||||
|
||||
namespace ClaudeDo.Worker.External;
|
||||
|
||||
public sealed record BatchAddTaskInput(string Title, string? Description = null, string? Model = null);
|
||||
public sealed record BatchSetMyDayInput(string TaskId, bool IsMyDay, int? SortOrder = null);
|
||||
|
||||
public sealed record BatchGetTaskResult(string Id, bool Found, TaskDto? Task, string? Error);
|
||||
public sealed record BatchAddTaskResult(int Index, string Title, bool Ok, TaskDto? Task, string? Error);
|
||||
public sealed record BatchTaskResult(string TaskId, bool Ok, string? Error);
|
||||
public sealed record BatchCancelResult(string TaskId, bool Ok, bool Cancelled, string? Error);
|
||||
public sealed record BatchCleanupResult(string TaskId, bool Ok, bool Removed, bool BranchDeleted, string? Error);
|
||||
|
||||
/// <summary>
|
||||
/// Batch variants of the single-entity tools on <see cref="ExternalMcpService"/>.
|
||||
/// Every tool is best-effort: each item is attempted independently and reported in a
|
||||
/// per-item result array (a failing item never aborts the rest). Operations run
|
||||
/// sequentially — the underlying repositories share one scoped DbContext, which is not
|
||||
/// thread-safe. Merge/review stay single-task (conflicts need the interactive resolver).
|
||||
/// </summary>
|
||||
[McpServerToolType]
|
||||
public sealed class BatchMcpTools
|
||||
{
|
||||
private const int MaxBatchSize = 100;
|
||||
|
||||
private readonly ExternalMcpService _svc;
|
||||
|
||||
public BatchMcpTools(ExternalMcpService svc) => _svc = svc;
|
||||
|
||||
[McpServerTool, Description(
|
||||
"Fetch a snapshot of many tasks in one call (overview / polling a fan-out). " +
|
||||
"Returns one result per id: { id, found, task, error }. A missing id is found=false " +
|
||||
"(not an error); error is only set for an unexpected failure. Max 100 ids.")]
|
||||
public async Task<IReadOnlyList<BatchGetTaskResult>> BatchGetTasks(
|
||||
string[] taskIds, CancellationToken cancellationToken)
|
||||
{
|
||||
EnsureWithinCap(taskIds, nameof(taskIds));
|
||||
|
||||
var results = new List<BatchGetTaskResult>(taskIds.Length);
|
||||
foreach (var id in taskIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
var task = await _svc.GetTask(id, cancellationToken);
|
||||
results.Add(new BatchGetTaskResult(id, true, task, null));
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
results.Add(new BatchGetTaskResult(id, false, null, null));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
results.Add(new BatchGetTaskResult(id, false, null, ex.Message));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
[McpServerTool, Description(
|
||||
"Create many tasks in one list at once. Each item: { title, description?, model? } " +
|
||||
"(model: haiku|sonnet|opus, blank = inherit list/global default). " +
|
||||
"queueImmediately enqueues every created task. " +
|
||||
"Returns one result per item: { index, title, ok, task, error }. Max 100 items.")]
|
||||
public async Task<IReadOnlyList<BatchAddTaskResult>> BatchAddTasks(
|
||||
string listId,
|
||||
BatchAddTaskInput[] tasks,
|
||||
string? createdBy = null,
|
||||
bool queueImmediately = false,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
EnsureWithinCap(tasks, nameof(tasks));
|
||||
|
||||
var results = new List<BatchAddTaskResult>(tasks.Length);
|
||||
for (var i = 0; i < tasks.Length; i++)
|
||||
{
|
||||
var item = tasks[i];
|
||||
try
|
||||
{
|
||||
var created = await _svc.AddTask(
|
||||
listId, item.Title, item.Description, createdBy,
|
||||
queueImmediately, item.Model, cancellationToken);
|
||||
results.Add(new BatchAddTaskResult(i, item.Title, true, created, null));
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex)
|
||||
{
|
||||
results.Add(new BatchAddTaskResult(i, item.Title, false, null, ex.Message));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
[McpServerTool, Description(
|
||||
"Set the status of many tasks at once. status is 'Idle' (reset to editable) or " +
|
||||
"'Queued' (enqueue for execution) only — same rule as update_task_status. " +
|
||||
"Returns one result per id: { taskId, ok, error }. Max 100 ids.")]
|
||||
public async Task<IReadOnlyList<BatchTaskResult>> BatchUpdateTaskStatus(
|
||||
string[] taskIds, string status, CancellationToken cancellationToken)
|
||||
{
|
||||
EnsureWithinCap(taskIds, nameof(taskIds));
|
||||
return await RunPerTaskAsync(taskIds,
|
||||
(id, ct) => _svc.UpdateTaskStatus(id, status, ct), cancellationToken);
|
||||
}
|
||||
|
||||
[McpServerTool, Description(
|
||||
"Cancel many running tasks at once. Returns one result per id: " +
|
||||
"{ taskId, ok, cancelled, error }. cancelled=false means the task was not running. " +
|
||||
"Max 100 ids.")]
|
||||
public async Task<IReadOnlyList<BatchCancelResult>> BatchCancelTasks(
|
||||
string[] taskIds, CancellationToken cancellationToken)
|
||||
{
|
||||
EnsureWithinCap(taskIds, nameof(taskIds));
|
||||
|
||||
var results = new List<BatchCancelResult>(taskIds.Length);
|
||||
foreach (var id in taskIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
var r = await _svc.CancelTask(id, cancellationToken);
|
||||
results.Add(new BatchCancelResult(id, true, r.Cancelled, null));
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex)
|
||||
{
|
||||
results.Add(new BatchCancelResult(id, false, false, ex.Message));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
[McpServerTool, Description(
|
||||
"Delete many tasks at once. A Running task is refused (cancel it first) and reported " +
|
||||
"as ok=false with its error. Returns one result per id: { taskId, ok, error }. Max 100 ids.")]
|
||||
public async Task<IReadOnlyList<BatchTaskResult>> BatchDeleteTasks(
|
||||
string[] taskIds, CancellationToken cancellationToken)
|
||||
{
|
||||
EnsureWithinCap(taskIds, nameof(taskIds));
|
||||
return await RunPerTaskAsync(taskIds,
|
||||
(id, ct) => _svc.DeleteTask(id, ct), cancellationToken);
|
||||
}
|
||||
|
||||
[McpServerTool, Description(
|
||||
"Daily prep: set/clear MyDay for many tasks at once. Each item: { taskId, isMyDay, sortOrder? }. " +
|
||||
"Still cap-guarded — items that would exceed DailyPrepMaxTasks open MyDay tasks fail individually " +
|
||||
"(ok=false) without blocking the rest. Returns one result per item: { taskId, ok, error }. Max 100 items.")]
|
||||
public async Task<IReadOnlyList<BatchTaskResult>> BatchSetMyDay(
|
||||
BatchSetMyDayInput[] items, CancellationToken cancellationToken)
|
||||
{
|
||||
EnsureWithinCap(items, nameof(items));
|
||||
|
||||
var results = new List<BatchTaskResult>(items.Length);
|
||||
foreach (var item in items)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _svc.SetMyDay(item.TaskId, item.IsMyDay, item.SortOrder, cancellationToken);
|
||||
results.Add(new BatchTaskResult(item.TaskId, true, null));
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex)
|
||||
{
|
||||
results.Add(new BatchTaskResult(item.TaskId, false, ex.Message));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
[McpServerTool, Description(
|
||||
"Remove the worktrees of many tasks at once (directory + git branch). " +
|
||||
"force=false refuses a dirty or Running worktree (reported ok=false); force=true removes " +
|
||||
"even a dirty worktree (uncommitted changes lost), still refusing Running tasks. " +
|
||||
"Returns one result per id: { taskId, ok, removed, branchDeleted, error }. Max 100 ids.")]
|
||||
public async Task<IReadOnlyList<BatchCleanupResult>> BatchCleanupTaskWorktrees(
|
||||
string[] taskIds, bool force = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
EnsureWithinCap(taskIds, nameof(taskIds));
|
||||
|
||||
var results = new List<BatchCleanupResult>(taskIds.Length);
|
||||
foreach (var id in taskIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
var r = await _svc.CleanupTaskWorktree(id, force, cancellationToken);
|
||||
results.Add(new BatchCleanupResult(id, true, r.Removed, r.BranchDeleted, null));
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex)
|
||||
{
|
||||
results.Add(new BatchCleanupResult(id, false, false, false, ex.Message));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static async Task<IReadOnlyList<BatchTaskResult>> RunPerTaskAsync(
|
||||
string[] taskIds, Func<string, CancellationToken, Task> op, CancellationToken cancellationToken)
|
||||
{
|
||||
var results = new List<BatchTaskResult>(taskIds.Length);
|
||||
foreach (var id in taskIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
await op(id, cancellationToken);
|
||||
results.Add(new BatchTaskResult(id, true, null));
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex)
|
||||
{
|
||||
results.Add(new BatchTaskResult(id, false, ex.Message));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void EnsureWithinCap<T>(IReadOnlyCollection<T>? items, string name)
|
||||
{
|
||||
if (items is null || items.Count == 0)
|
||||
throw new InvalidOperationException($"{name} is required and must contain at least one item.");
|
||||
if (items.Count > MaxBatchSize)
|
||||
throw new InvalidOperationException(
|
||||
$"Batch too large: {items.Count} items (max {MaxBatchSize}). Split into smaller batches.");
|
||||
}
|
||||
}
|
||||
@@ -264,6 +264,7 @@ if (cfg.ExternalMcpPort > 0)
|
||||
externalBuilder.Services.AddSingleton(app.Services.GetRequiredService<WorktreeMaintenanceService>());
|
||||
externalBuilder.Services.AddSingleton(app.Services.GetRequiredService<TaskMergeService>());
|
||||
externalBuilder.Services.AddScoped<ExternalMcpService>();
|
||||
externalBuilder.Services.AddScoped<BatchMcpTools>();
|
||||
externalBuilder.Services.AddScoped<ListMcpTools>();
|
||||
externalBuilder.Services.AddScoped<ConfigMcpTools>();
|
||||
externalBuilder.Services.AddScoped<RunHistoryMcpTools>();
|
||||
@@ -276,6 +277,7 @@ if (cfg.ExternalMcpPort > 0)
|
||||
externalBuilder.Services.AddMcpServer()
|
||||
.WithHttpTransport()
|
||||
.WithTools<ExternalMcpService>()
|
||||
.WithTools<BatchMcpTools>()
|
||||
.WithTools<ListMcpTools>()
|
||||
.WithTools<ConfigMcpTools>()
|
||||
.WithTools<RunHistoryMcpTools>()
|
||||
|
||||
Reference in New Issue
Block a user