expose task control and deduplicated operations through MCP
This commit is contained in:
@@ -176,7 +176,7 @@ public class StudioApplicationConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
Shipper shipper(CompilerBuildGateway compiler, PackerWorkspaceService packer, ObjectMapper mapper,
|
||||
StudioShipperService shipper(CompilerBuildGateway compiler, PackerWorkspaceService packer, ObjectMapper mapper,
|
||||
OperationExecutor operations, p.studio.api.automation.ProjectAccess access) {
|
||||
return new StudioShipperService(compiler, packer, mapper, operations, access);
|
||||
}
|
||||
@@ -313,12 +313,20 @@ public class StudioApplicationConfiguration {
|
||||
return new p.studio.core.language.TaskLanguageQueries(projects, language);
|
||||
}
|
||||
|
||||
@Bean
|
||||
p.studio.api.automation.TaskTools taskTools(p.studio.core.project.ProjectAccessRegistry projects,
|
||||
ProjectCatalog catalog, p.studio.core.shipper.SavedProjectPreparation shipper,
|
||||
java.util.Optional<p.studio.core.runtime.HeadlessGateway> headless) {
|
||||
return new p.studio.core.session.DefaultTaskTools(projects, catalog, shipper, headless);
|
||||
}
|
||||
|
||||
@Bean
|
||||
p.studio.app.mcp.McpToolRegistry mcpToolRegistry(p.studio.app.mcp.McpToolCatalog catalog, ObjectMapper mapper,
|
||||
p.studio.api.automation.ProjectKnowledge knowledge, p.studio.api.automation.LanguageQueries language) {
|
||||
// Control/build/runtime/operation providers are connected in PLN-0139 step 20.
|
||||
p.studio.api.automation.ProjectKnowledge knowledge, p.studio.api.automation.LanguageQueries language,
|
||||
p.studio.api.automation.TaskTools tasks, java.util.Optional<p.studio.core.runtime.HeadlessGateway> headless) {
|
||||
var handlers = new java.util.LinkedHashMap<>(p.studio.app.mcp.McpProjectKnowledgeTools.handlers(knowledge, mapper));
|
||||
handlers.putAll(p.studio.app.mcp.McpLanguageTools.handlers(language, mapper));
|
||||
handlers.putAll(p.studio.app.mcp.McpTaskTools.handlers(tasks, mapper, headless.isPresent()));
|
||||
return new p.studio.app.mcp.McpToolRegistry(catalog, mapper, handlers);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package p.studio.app.mcp;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import p.studio.api.automation.*;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Wire projection only; task authority, work and deduplication remain in Core. */
|
||||
public final class McpTaskTools {
|
||||
private McpTaskTools() {}
|
||||
public static Map<String, Function<JsonNode, McpToolResult>> handlers(TaskTools tools, ObjectMapper mapper, boolean headlessAvailable) {
|
||||
var handlers = new LinkedHashMap<String, Function<JsonNode, McpToolResult>>();
|
||||
handlers.put("studio_control_acquire", args -> control(mapper, tools.acquire(root(args))));
|
||||
handlers.put("studio_control_release", args -> control(mapper, tools.release(root(args), task(args)).toCompletableFuture().join()));
|
||||
handlers.put("studio_build_start", args -> receipt(mapper, tools.build(root(args), task(args), args.path("clientRequestId").asText())));
|
||||
handlers.put("studio_operation_get", args -> operation(mapper, tools.operation(root(args), task(args),
|
||||
new OperationLookup(args.path("operationId").asText(null), args.path("clientRequestId").asText(null)))));
|
||||
handlers.put("studio_operation_cancel", args -> McpToolResult.success(mapper, mapper.createObjectNode()
|
||||
.put("operationId", args.path("operationId").asText())
|
||||
.put("cancellationRequested", tools.cancel(root(args), task(args), args.path("operationId").asText()))));
|
||||
if (headlessAvailable) {
|
||||
handlers.put("studio_headless_advance", args -> {
|
||||
if (!args.path("frames").canConvertToLong()) throw new IllegalArgumentException("frames exceeds integer range");
|
||||
return receipt(mapper, tools.advance(root(args), task(args), args.path("clientRequestId").asText(), args.path("frames").longValue()));
|
||||
});
|
||||
for (var action : TaskTools.HeadlessAction.values()) {
|
||||
handlers.put("studio_headless_" + action.name().toLowerCase(Locale.ROOT), args -> {
|
||||
Map<String, Object> input = action == TaskTools.HeadlessAction.SET_INPUT ? input(args.path("input")) : Map.of();
|
||||
var result = tools.headless(root(args), task(args), action, args.path("buildId").asText(null), input);
|
||||
return result.capture() != null ? McpToolResult.capture(mapper, result.capture(), false)
|
||||
: McpToolResult.success(mapper, mapper.valueToTree(result.values()));
|
||||
});
|
||||
}
|
||||
}
|
||||
return handlers;
|
||||
}
|
||||
private static Map<String, Object> input(JsonNode input) {
|
||||
var normalized = new LinkedHashMap<String, Object>();
|
||||
for (var name : List.of("up", "down", "left", "right", "a", "b", "x", "y", "l", "r", "start", "select", "touch"))
|
||||
normalized.put(name, input.path(name).asBoolean(false));
|
||||
normalized.put("touchX", input.path("touchX").asInt(0));
|
||||
normalized.put("touchY", input.path("touchY").asInt(0));
|
||||
return Map.copyOf(normalized);
|
||||
}
|
||||
private static McpToolResult control(ObjectMapper mapper, ControlResult result) {
|
||||
if (!result.success()) {
|
||||
var code = switch (result.status()) {
|
||||
case CLEANUP_FAILED -> AutomationFailure.Code.CLEANUP_FAILED;
|
||||
case CLOSED -> AutomationFailure.Code.CLOSED;
|
||||
case INVALID_TASK -> AutomationFailure.Code.INVALID_TASK;
|
||||
case NOT_GRANTED -> AutomationFailure.Code.NOT_GRANTED;
|
||||
default -> AutomationFailure.Code.BUSY;
|
||||
};
|
||||
return McpToolResult.failure(mapper, new AutomationFailure(code, result.diagnostic()));
|
||||
}
|
||||
var value = mapper.createObjectNode().put("state", result.task() == null ? "MANUAL" : "AGENT_WORKING");
|
||||
if (result.task() != null) value.put("taskId", result.task().value());
|
||||
return McpToolResult.success(mapper, value);
|
||||
}
|
||||
private static ObjectNode identity(ObjectMapper mapper, TrackedOperation operation) {
|
||||
return mapper.createObjectNode().put("operationId", operation.operation().id().value())
|
||||
.put("clientRequestId", operation.clientRequestId()).put("state", operation.operation().state().name());
|
||||
}
|
||||
private static McpToolResult receipt(ObjectMapper mapper, TrackedOperation operation) { return McpToolResult.success(mapper, identity(mapper, operation)); }
|
||||
private static McpToolResult operation(ObjectMapper mapper, TrackedOperation operation) {
|
||||
var value = identity(mapper, operation);
|
||||
value.put("workerTerminated", operation.workerTerminated());
|
||||
value.set("progress", mapper.valueToTree(operation.progress()));
|
||||
value.set("failure", mapper.valueToTree(operation.failure()));
|
||||
value.putNull("result");
|
||||
if (operation.buildResult() != null) {
|
||||
var build = operation.buildResult();
|
||||
var result = value.putObject("result").put("kind", "build").put("status", build.status().name());
|
||||
result.put("buildId", operation.buildId());
|
||||
result.put("manifestPath", build.manifestPath() == null ? null : build.manifestPath().toString());
|
||||
result.set("emittedArtifacts", mapper.valueToTree(build.emittedArtifacts().stream().map(Path::toString).toList()));
|
||||
result.set("diagnostics", mapper.valueToTree(build.diagnostics()));
|
||||
var logs = result.putArray("logs");
|
||||
build.logs().forEach(log -> logs.addObject().put("source", log.source().name()).put("severity", log.severity().name())
|
||||
.put("message", log.message()).put("timestamp", log.timestamp().toString()));
|
||||
}
|
||||
CanonicalCapture capture = null;
|
||||
if (operation.runtimeResult() != null) {
|
||||
var runtime = operation.runtimeResult();
|
||||
var result = value.putObject("result").put("kind", "runtime").put("status", runtime.kind().name())
|
||||
.put("requestedFrames", runtime.requestedFrames()).put("completedFrames", runtime.completedFrames());
|
||||
result.set("diagnostics", mapper.valueToTree(runtime.diagnostics()));
|
||||
capture = runtime.capture();
|
||||
}
|
||||
return McpToolResult.successWithCapture(mapper, value, capture, operation.captureStale());
|
||||
}
|
||||
private static Path root(JsonNode args) { return Path.of(args.path("projectRoot").asText()); }
|
||||
private static String task(JsonNode args) { return args.path("taskId").asText(); }
|
||||
}
|
||||
@@ -32,6 +32,14 @@ public final class McpToolResult {
|
||||
.put("data", Base64.getEncoder().encodeToString(capture.png()));
|
||||
return result;
|
||||
}
|
||||
public static McpToolResult successWithCapture(ObjectMapper mapper, ObjectNode value, CanonicalCapture capture, boolean stale) {
|
||||
if (capture == null) return success(mapper, value);
|
||||
var image = capture(mapper, capture, stale);
|
||||
value.set("capture", image.wire.path("structuredContent").path("value"));
|
||||
var result = success(mapper, value);
|
||||
result.wire.withArray("content").add(image.wire.withArray("content").get(1).deepCopy());
|
||||
return result;
|
||||
}
|
||||
private static McpToolResult envelope(ObjectMapper mapper, JsonNode data, boolean error) {
|
||||
var wire = mapper.createObjectNode().put("isError", error);
|
||||
wire.set("structuredContent", data);
|
||||
|
||||
Reference in New Issue
Block a user