feat(mcp): add SuggestImprovement tool (server-stamped, one layer deep)

This commit is contained in:
mika kuns
2026-06-04 15:51:57 +02:00
parent 7542bc2058
commit 9d133e227b
2 changed files with 108 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
using System.ComponentModel;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Hub;
using ModelContextProtocol.Server;
namespace ClaudeDo.Worker.Runner;
public sealed record SuggestedImprovementDto(string ChildTaskId);
[McpServerToolType]
public sealed class TaskRunMcpService
{
private readonly TaskRepository _tasks;
private readonly TaskRunMcpContextAccessor _ctx;
private readonly HubBroadcaster _broadcaster;
public TaskRunMcpService(TaskRepository tasks, TaskRunMcpContextAccessor ctx, HubBroadcaster broadcaster)
{
_tasks = tasks;
_ctx = ctx;
_broadcaster = broadcaster;
}
[McpServerTool, Description(
"File an out-of-scope improvement as a child task of the current task. The child runs " +
"automatically after this task finishes and is surfaced for review alongside it. Use ONLY " +
"for work that is genuinely outside this task's scope (a refactor, follow-up, or tech debt) " +
"— never for work that belongs to the current task.")]
public async Task<SuggestedImprovementDto> SuggestImprovement(
string title,
string description,
CancellationToken cancellationToken)
{
var callerId = _ctx.Current.CallerTaskId;
var caller = await _tasks.GetByIdAsync(callerId, cancellationToken)
?? throw new InvalidOperationException("Calling task not found.");
if (caller.ParentTaskId is not null)
throw new InvalidOperationException(
"A child task cannot suggest further improvements (improvements are one layer deep).");
var child = await _tasks.CreateChildAsync(
callerId, title, description, commitType: null, createdBy: callerId, cancellationToken);
await _broadcaster.TaskUpdated(child.Id);
await _broadcaster.TaskUpdated(callerId);
return new SuggestedImprovementDto(child.Id);
}
}

View File

@@ -0,0 +1,61 @@
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Hub;
using ClaudeDo.Worker.Runner;
using ClaudeDo.Worker.Tests.Infrastructure;
using ClaudeDo.Worker.Tests.Services;
using Microsoft.AspNetCore.Http;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
using Xunit;
namespace ClaudeDo.Worker.Tests;
public sealed class SuggestImprovementTests : IDisposable
{
private readonly DbFixture _db = new();
public void Dispose() => _db.Dispose();
private static TaskRunMcpContextAccessor AccessorFor(string callerTaskId)
{
var http = new HttpContextAccessor { HttpContext = new DefaultHttpContext() };
http.HttpContext!.Items["TaskRunContext"] = new TaskRunMcpContext { CallerTaskId = callerTaskId };
return new TaskRunMcpContextAccessor(http);
}
private async Task SeedCallerAsync(string id, string? parentId)
{
using var ctx = _db.CreateContext();
if (!ctx.Lists.Any())
ctx.Lists.Add(new ListEntity { Id = "l1", Name = "L", CreatedAt = DateTime.UtcNow });
ctx.Tasks.Add(new TaskEntity { Id = id, ListId = "l1", Title = "Caller",
Status = TaskStatus.Running, ParentTaskId = parentId, CommitType = "feat", CreatedAt = DateTime.UtcNow });
await ctx.SaveChangesAsync();
}
[Fact]
public async Task SuggestImprovement_stamps_parent_createdBy_status_and_list()
{
await SeedCallerAsync("caller", parentId: null);
using var ctx = _db.CreateContext();
var svc = new TaskRunMcpService(new TaskRepository(ctx), AccessorFor("caller"),
new HubBroadcaster(new FakeHubContext()));
var dto = await svc.SuggestImprovement("Refactor X", "details", default);
var child = await new TaskRepository(ctx).GetByIdAsync(dto.ChildTaskId);
Assert.Equal("caller", child!.ParentTaskId);
Assert.Equal("caller", child.CreatedBy);
Assert.Equal(TaskStatus.Idle, child.Status);
Assert.Equal("l1", child.ListId);
}
[Fact]
public async Task SuggestImprovement_rejects_when_caller_is_a_child()
{
await SeedCallerAsync("parent", parentId: null);
await SeedCallerAsync("child", parentId: "parent");
using var ctx = _db.CreateContext();
var svc = new TaskRunMcpService(new TaskRepository(ctx), AccessorFor("child"),
new HubBroadcaster(new FakeHubContext()));
await Assert.ThrowsAsync<InvalidOperationException>(
() => svc.SuggestImprovement("nested", "x", default));
}
}